From 088dae2a0b2af519e97605f470cc07d045a10e75 Mon Sep 17 00:00:00 2001 From: caoxing Date: Thu, 16 Jan 2025 19:08:30 +0800 Subject: [PATCH] feat: add create or repair search index tip --- .../aggregation/aggregation.service.ts | 2 +- .../auth/local-auth/local-auth.service.ts | 1 - .../src/features/table/table-index.service.ts | 2 +- .../test/record-search-query.e2e-spec.ts | 2 +- .../app/blocks/view/search/SearchCommand.tsx | 81 ++++++++++++++++++- .../common-i18n/src/locales/en/table.json | 3 +- .../common-i18n/src/locales/fr/table.json | 6 ++ .../common-i18n/src/locales/ja/table.json | 6 ++ .../common-i18n/src/locales/ru/table.json | 6 ++ .../common-i18n/src/locales/zh/table.json | 3 +- packages/sdk/src/config/local-storage-keys.ts | 1 + 11 files changed, 103 insertions(+), 10 deletions(-) diff --git a/apps/nestjs-backend/src/features/aggregation/aggregation.service.ts b/apps/nestjs-backend/src/features/aggregation/aggregation.service.ts index 1d4068429..454f8a427 100644 --- a/apps/nestjs-backend/src/features/aggregation/aggregation.service.ts +++ b/apps/nestjs-backend/src/features/aggregation/aggregation.service.ts @@ -243,7 +243,7 @@ export class AggregationService { const { statisticsData, fieldInstanceMap } = await this.fetchStatisticsParams({ tableId, withView: { - viewId: queryRo.viewId, + viewId: ignoreViewQuery ? undefined : viewId, customFilter: queryRo.filter, }, }); diff --git a/apps/nestjs-backend/src/features/auth/local-auth/local-auth.service.ts b/apps/nestjs-backend/src/features/auth/local-auth/local-auth.service.ts index ddbd29648..768b52965 100644 --- a/apps/nestjs-backend/src/features/auth/local-auth/local-auth.service.ts +++ b/apps/nestjs-backend/src/features/auth/local-auth/local-auth.service.ts @@ -42,7 +42,6 @@ export class LocalAuthService { hashPassword: string | null, salt: string | null ) { - return true; const _hashPassword = await bcrypt.hash(password || '', salt || ''); return _hashPassword === hashPassword; } diff --git a/apps/nestjs-backend/src/features/table/table-index.service.ts b/apps/nestjs-backend/src/features/table/table-index.service.ts index 6b11d56f6..46768fca1 100644 --- a/apps/nestjs-backend/src/features/table/table-index.service.ts +++ b/apps/nestjs-backend/src/features/table/table-index.service.ts @@ -238,7 +238,7 @@ export class TableIndexService { isStructuredCellValue: field.isStructuredCellValue, })) as IFieldInstance[]; const createSqls = this.dbProvider.searchIndex().getCreateIndexSql(dbTableName, fieldInstances); - this.prismaService.$tx(async (prisma) => { + await this.prismaService.$tx(async (prisma) => { await prisma.$executeRawUnsafe(dropSql); for (let i = 0; i < createSqls.length; i++) { await prisma.$executeRawUnsafe(createSqls[i]); diff --git a/apps/nestjs-backend/test/record-search-query.e2e-spec.ts b/apps/nestjs-backend/test/record-search-query.e2e-spec.ts index 3cd8027b8..8840daff6 100644 --- a/apps/nestjs-backend/test/record-search-query.e2e-spec.ts +++ b/apps/nestjs-backend/test/record-search-query.e2e-spec.ts @@ -331,7 +331,7 @@ describe('OpenAPI Record-Search-Query (e2e)', async () => { expect(result2.data.length).toBe(0); }); - it.only('should convert field index automatically when field be convert with table index', async () => { + it('should convert field index automatically when field be convert with table index', async () => { const textfield = table.fields.find( (f) => f.cellValueType === CellValueType.String && !f.isPrimary )!; diff --git a/apps/nextjs-app/src/features/app/blocks/view/search/SearchCommand.tsx b/apps/nextjs-app/src/features/app/blocks/view/search/SearchCommand.tsx index 77d811e0a..a6bd7b00c 100644 --- a/apps/nextjs-app/src/features/app/blocks/view/search/SearchCommand.tsx +++ b/apps/nextjs-app/src/features/app/blocks/view/search/SearchCommand.tsx @@ -8,6 +8,7 @@ import { getTableAbnormalIndex, repairTableIndex, } from '@teable/openapi'; +import { LocalStorageKeys } from '@teable/sdk/config'; import { useBaseId, useFields, useFieldStaticGetter, useTableId, useView } from '@teable/sdk/hooks'; import { Command, @@ -27,9 +28,19 @@ import { HoverCardTrigger, HoverCardContent, Button, + AlertDialog, + AlertDialogContent, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogDescription, + Checkbox, + AlertDialogFooter, + AlertDialogCancel, + AlertDialogAction, } from '@teable/ui-lib'; import { useTranslation } from 'next-i18next'; import { useCallback, useMemo, useState } from 'react'; +import { useLocalStorage } from 'react-use'; interface ISearchCommand { value: string; @@ -37,6 +48,12 @@ interface ISearchCommand { onHideSwitchChange: (hideNotMatchRow?: boolean) => void; onChange: (fieldIds: string[] | null) => void; } + +enum ActionType { + repair = 'repair', + create = 'create', +} + export const SearchCommand = (props: ISearchCommand) => { const { onChange, value, hideNotMatchRow, onHideSwitchChange } = props; const { t } = useTranslation(['common', 'table']); @@ -52,16 +69,23 @@ export const SearchCommand = (props: ISearchCommand) => { const queryClient = useQueryClient(); + const [alertVisible, setAlertVisible] = useState(false); + const [shouldAlert, setShouldAlert] = useLocalStorage(LocalStorageKeys.SearchIndexAlert, true); + const [shouldTips, setShouldTips] = useState(false); + const [actionType, setActionType] = useState(ActionType.create); + const { data: tableActivatedIndex } = useQuery({ queryKey: ['table-index', tableId], queryFn: () => getTableActivatedIndex(baseId!, tableId!).then(({ data }) => data), }); + const enabledSearchIndex = tableActivatedIndex?.includes(TableIndex.search); + const { data: searchAbnormalIndex, isLoading: getAbnormalLoading } = useQuery({ queryKey: ['table-abnormal-index', baseId, tableId, TableIndex.search], queryFn: () => getTableAbnormalIndex(baseId!, tableId!, TableIndex.search).then(({ data }) => data), - enabled: !!tableActivatedIndex?.includes(TableIndex.search), + enabled: enabledSearchIndex, }); const { mutateAsync: toggleIndexFn, isLoading } = useMutation({ @@ -244,7 +268,7 @@ export const SearchCommand = (props: ISearchCommand) => { {t('table:table.index.description')} - {!!searchAbnormalIndex?.length && ( + {enabledSearchIndex && !!searchAbnormalIndex?.length && (
@@ -253,6 +277,11 @@ export const SearchCommand = (props: ISearchCommand) => { variant={'destructive'} className="flex h-6 items-center gap-0.5" onClick={async () => { + if (shouldAlert) { + setAlertVisible(true); + setActionType(ActionType.repair); + return; + } await repairIndexFn(TableIndex.search); }} > @@ -279,8 +308,13 @@ export const SearchCommand = (props: ISearchCommand) => { { + checked={enabledSearchIndex} + onCheckedChange={async (val) => { + if (val && shouldAlert) { + setAlertVisible(true); + setActionType(ActionType.create); + return; + } baseId && tableId && (await toggleIndexFn(TableIndex.search)); }} /> @@ -289,6 +323,45 @@ export const SearchCommand = (props: ISearchCommand) => {
+ + + + + {t('table:import.title.tipsTitle')} + {t('table:table.index.enableIndexTip')} + +
+ { + setShouldTips(should); + }} + /> + +
+ + {t('table:import.menu.cancel')} + { + if (actionType === ActionType.create) { + toggleIndexFn(TableIndex.search); + } else { + repairIndexFn(TableIndex.search); + } + setShouldAlert(!shouldTips); + }} + > + {t('table:import.title.confirm')} + + +
+
); }; diff --git a/packages/common-i18n/src/locales/en/table.json b/packages/common-i18n/src/locales/en/table.json index ab21251e7..fdc3050a1 100644 --- a/packages/common-i18n/src/locales/en/table.json +++ b/packages/common-i18n/src/locales/en/table.json @@ -328,7 +328,8 @@ "index": { "description": "Indexes can significantly improve search performance, especially when dealing with large amounts of data. The downside is that they take up additional storage space and may slightly slow down write operations. If you frequently perform search operations or have a large amount of data, it is recommended to enable indexes.", "repair": "repair", - "repairTip": "Index anomalies detected, which may cause search performance degradation. It is recommended to click the repair button to fix the index" + "repairTip": "Index anomalies detected, which may cause search performance degradation. It is recommended to click the repair button to fix the index", + "enableIndexTip": "The time required to create an index depends on the table size. During creation, the table's read and write performance may be affected. Please wait patiently." } }, "import": { diff --git a/packages/common-i18n/src/locales/fr/table.json b/packages/common-i18n/src/locales/fr/table.json index 2e0db893c..2d67adf76 100644 --- a/packages/common-i18n/src/locales/fr/table.json +++ b/packages/common-i18n/src/locales/fr/table.json @@ -312,6 +312,12 @@ "loading": "Vérification de l'intégrité...", "allGood": "Tout va bien !", "fixIssues": "Corriger les problèmes" + }, + "index": { + "description": "Les index peuvent améliorer considérablement les performances de recherche, en particulier lors du traitement de grandes quantités de données. L'inconvénient est qu'ils occupent de l'espace de stockage supplémentaire et peuvent légèrement ralentir les opérations d'écriture. Si vous effectuez fréquemment des opérations de recherche ou si vous avez une grande quantité de données, il est recommandé d'activer les index.", + "repair": "réparer", + "repairTip": "Des anomalies d'index ont été détectées, ce qui peut entraîner une dégradation des performances de recherche. Il est recommandé de cliquer sur le bouton réparer pour corriger l'index", + "enableIndexTip": "Le temps nécessaire pour créer un index dépend de la taille de la table. Pendant la création, les performances de lecture et d'écriture de la table peuvent être affectées. Veuillez patienter." } }, "import": { diff --git a/packages/common-i18n/src/locales/ja/table.json b/packages/common-i18n/src/locales/ja/table.json index 54fc0b32c..03cc96ff0 100644 --- a/packages/common-i18n/src/locales/ja/table.json +++ b/packages/common-i18n/src/locales/ja/table.json @@ -312,6 +312,12 @@ "loading": "整合性チェック中...", "allGood": "すべて正常です!", "fixIssues": "問題を修正" + }, + "index": { + "description": "インデックスは、特に大量のデータを扱う場合、検索パフォーマンスを大幅に向上させることができます。ただし、追加のストレージ容量を消費し、書き込み操作が若干遅くなる可能性があります。頻繁に検索操作を実行する場合や大量のデータがある場合は、インデックスを有効にすることをお勧めします。", + "repair": "修復", + "repairTip": "インデックスの異常が検出されました。検索パフォーマンスの低下を引き起こす可能性があります。修復ボタンをクリックしてインデックスを修正することをお勧めします。", + "enableIndexTip": "インデックスの作成に必要な時間は、テーブルのサイズによって異なります。作成中は、テーブルの読み取りと書き込みのパフォーマンスに影響が出る可能性があります。しばらくお待ちください。" } }, "import": { diff --git a/packages/common-i18n/src/locales/ru/table.json b/packages/common-i18n/src/locales/ru/table.json index f0e7d4852..08555bf33 100644 --- a/packages/common-i18n/src/locales/ru/table.json +++ b/packages/common-i18n/src/locales/ru/table.json @@ -312,6 +312,12 @@ "loading": "Проверка целостности...", "allGood": "Все хорошо!", "fixIssues": "Исправить проблемы" + }, + "index": { + "description": "Индексы могут значительно улучшить производительность поиска, особенно при работе с большими объемами данных. Недостатком является то, что они занимают дополнительное пространство для хранения и могут немного замедлить операции записи. Если вы часто выполняете операции поиска или имеете большой объем данных, рекомендуется включить индексы.", + "repair": "Исправить", + "repairTip": "Обнаружены аномалии индекса, которые могут привести к снижению производительности поиска. Рекомендуется нажать кнопку исправить для устранения проблем с индексом", + "enableIndexTip": "Время, необходимое для создания индекса, зависит от размера таблицы. Во время создания производительность чтения и записи таблицы может быть затронута. Пожалуйста, подождите." } }, "import": { diff --git a/packages/common-i18n/src/locales/zh/table.json b/packages/common-i18n/src/locales/zh/table.json index 89eabba97..12a1b7eda 100644 --- a/packages/common-i18n/src/locales/zh/table.json +++ b/packages/common-i18n/src/locales/zh/table.json @@ -327,7 +327,8 @@ "index": { "description": "索引可以显著提升搜索性能,特别是在处理大量数据时, 缺点是占用额外存储空间,写入操作可能略微变慢,如果经常进行搜索操作或数据量较大,建议开启索引。", "repair": "修复", - "repairTip": "检测到索引异常,可能导致搜索性能下降,建议点击修复按钮修复索引" + "repairTip": "检测到索引异常,可能导致搜索性能下降,建议点击修复按钮修复索引", + "enableIndexTip": "创建索引的时间取决于表的大小,创建过程中,表的读写性能可能会受到一定影响,请耐心等待。" } }, "import": { diff --git a/packages/sdk/src/config/local-storage-keys.ts b/packages/sdk/src/config/local-storage-keys.ts index 12399c419..8e2a4d6bf 100644 --- a/packages/sdk/src/config/local-storage-keys.ts +++ b/packages/sdk/src/config/local-storage-keys.ts @@ -10,6 +10,7 @@ export enum LocalStorageKeys { ViewKanbanCollapsedStack = 'ls_view_kanban_collapsed_stack', CompletedGuideMap = 'ls_completed_guide_map', ImportAlert = 'ls_import_alert', + SearchIndexAlert = 'ls_search_index_alert', ExpandRecordHiddenFieldsVisible = 'ls_expand_record_hidden_fields_visible', EnableGlobalSearch = 'ls_enable_globalSearch', TableSearchFieldsCache = 'ls_table_search_fields_cache',