Skip to content

Commit

Permalink
feat: add create or repair search index tip
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxing9 committed Jan 16, 2025
1 parent a95ac48 commit 088dae2
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion apps/nestjs-backend/test/record-search-query.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,16 +28,32 @@ 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;
hideNotMatchRow?: boolean;
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']);
Expand All @@ -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({
Expand Down Expand Up @@ -244,7 +268,7 @@ export const SearchCommand = (props: ISearchCommand) => {
<span className="text-sm leading-3">{t('table:table.index.description')}</span>
</HoverCardContent>
</HoverCard>
{!!searchAbnormalIndex?.length && (
{enabledSearchIndex && !!searchAbnormalIndex?.length && (
<div className="flex items-center gap-0.5">
<HoverCard>
<HoverCardTrigger>
Expand All @@ -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);
}}
>
Expand All @@ -279,8 +308,13 @@ export const SearchCommand = (props: ISearchCommand) => {
<Switch
id={'search-index'}
className="scale-75"
checked={tableActivatedIndex?.includes(TableIndex.search)}
onCheckedChange={async () => {
checked={enabledSearchIndex}
onCheckedChange={async (val) => {
if (val && shouldAlert) {
setAlertVisible(true);
setActionType(ActionType.create);
return;
}
baseId && tableId && (await toggleIndexFn(TableIndex.search));
}}
/>
Expand All @@ -289,6 +323,45 @@ export const SearchCommand = (props: ISearchCommand) => {
</Label>
</div>
</div>

<AlertDialog open={alertVisible} onOpenChange={setAlertVisible}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t('table:import.title.tipsTitle')}</AlertDialogTitle>
<AlertDialogDescription>{t('table:table.index.enableIndexTip')}</AlertDialogDescription>
</AlertDialogHeader>
<div className="flex items-center">
<Checkbox
id="noTips"
checked={shouldTips}
onCheckedChange={(should: boolean) => {
setShouldTips(should);
}}
/>
<label
htmlFor="noTips"
className="pl-2 text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{t('table:import.tips.noTips')}
</label>
</div>
<AlertDialogFooter>
<AlertDialogCancel>{t('table:import.menu.cancel')}</AlertDialogCancel>
<AlertDialogAction
onClick={() => {
if (actionType === ActionType.create) {
toggleIndexFn(TableIndex.search);
} else {
repairIndexFn(TableIndex.search);
}
setShouldAlert(!shouldTips);
}}
>
{t('table:import.title.confirm')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</Command>
);
};
3 changes: 2 additions & 1 deletion packages/common-i18n/src/locales/en/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions packages/common-i18n/src/locales/fr/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 6 additions & 0 deletions packages/common-i18n/src/locales/ja/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@
"loading": "整合性チェック中...",
"allGood": "すべて正常です!",
"fixIssues": "問題を修正"
},
"index": {
"description": "インデックスは、特に大量のデータを扱う場合、検索パフォーマンスを大幅に向上させることができます。ただし、追加のストレージ容量を消費し、書き込み操作が若干遅くなる可能性があります。頻繁に検索操作を実行する場合や大量のデータがある場合は、インデックスを有効にすることをお勧めします。",
"repair": "修復",
"repairTip": "インデックスの異常が検出されました。検索パフォーマンスの低下を引き起こす可能性があります。修復ボタンをクリックしてインデックスを修正することをお勧めします。",
"enableIndexTip": "インデックスの作成に必要な時間は、テーブルのサイズによって異なります。作成中は、テーブルの読み取りと書き込みのパフォーマンスに影響が出る可能性があります。しばらくお待ちください。"
}
},
"import": {
Expand Down
6 changes: 6 additions & 0 deletions packages/common-i18n/src/locales/ru/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@
"loading": "Проверка целостности...",
"allGood": "Все хорошо!",
"fixIssues": "Исправить проблемы"
},
"index": {
"description": "Индексы могут значительно улучшить производительность поиска, особенно при работе с большими объемами данных. Недостатком является то, что они занимают дополнительное пространство для хранения и могут немного замедлить операции записи. Если вы часто выполняете операции поиска или имеете большой объем данных, рекомендуется включить индексы.",
"repair": "Исправить",
"repairTip": "Обнаружены аномалии индекса, которые могут привести к снижению производительности поиска. Рекомендуется нажать кнопку исправить для устранения проблем с индексом",
"enableIndexTip": "Время, необходимое для создания индекса, зависит от размера таблицы. Во время создания производительность чтения и записи таблицы может быть затронута. Пожалуйста, подождите."
}
},
"import": {
Expand Down
3 changes: 2 additions & 1 deletion packages/common-i18n/src/locales/zh/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@
"index": {
"description": "索引可以显著提升搜索性能,特别是在处理大量数据时, 缺点是占用额外存储空间,写入操作可能略微变慢,如果经常进行搜索操作或数据量较大,建议开启索引。",
"repair": "修复",
"repairTip": "检测到索引异常,可能导致搜索性能下降,建议点击修复按钮修复索引"
"repairTip": "检测到索引异常,可能导致搜索性能下降,建议点击修复按钮修复索引",
"enableIndexTip": "创建索引的时间取决于表的大小,创建过程中,表的读写性能可能会受到一定影响,请耐心等待。"
}
},
"import": {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/config/local-storage-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 088dae2

Please sign in to comment.