|
| 1 | +import type {TIndexDescription} from '../../../types/api/schema'; |
| 2 | +import type {TVectorIndexKmeansTreeDescriptionSettings} from '../../../types/api/schema/tableIndex'; |
| 3 | +import {formatNumber} from '../../../utils/dataFormatters/dataFormatters'; |
| 4 | +import type {InfoViewerItem} from '../InfoViewer'; |
| 5 | +import {formatTableIndexItem} from '../formatters'; |
| 6 | +import {createInfoFormatter} from '../utils'; |
| 7 | + |
| 8 | +import i18n from './i18n'; |
| 9 | + |
| 10 | +const DISPLAYED_FIELDS_KEYS = [ |
| 11 | + 'Type', |
| 12 | + 'State', |
| 13 | + 'DataSize', |
| 14 | + 'KeyColumnNames', |
| 15 | + 'DataColumnNames', |
| 16 | +] as const; |
| 17 | +type DisplayedIndexField = (typeof DISPLAYED_FIELDS_KEYS)[number]; |
| 18 | + |
| 19 | +export function getDisplayedIndexFields(): ReadonlySet<DisplayedIndexField> { |
| 20 | + return new Set<DisplayedIndexField>(DISPLAYED_FIELDS_KEYS); |
| 21 | +} |
| 22 | + |
| 23 | +export function buildIndexInfo(tableIndex?: TIndexDescription): InfoViewerItem[] { |
| 24 | + const info: InfoViewerItem[] = []; |
| 25 | + if (!tableIndex) { |
| 26 | + return info; |
| 27 | + } |
| 28 | + |
| 29 | + getDisplayedIndexFields().forEach((key) => { |
| 30 | + const value = tableIndex[key]; |
| 31 | + if (value !== undefined) { |
| 32 | + info.push(formatTableIndexItem(key, value)); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + return info; |
| 37 | +} |
| 38 | + |
| 39 | +type VectorSettings = TVectorIndexKmeansTreeDescriptionSettings; |
| 40 | + |
| 41 | +export function buildVectorIndexInfo(vectorSettings?: VectorSettings): InfoViewerItem[] { |
| 42 | + const info: InfoViewerItem[] = []; |
| 43 | + if (!vectorSettings) { |
| 44 | + return info; |
| 45 | + } |
| 46 | + |
| 47 | + const vectorFormatter = createInfoFormatter<{clusters?: number; levels?: number}>({ |
| 48 | + values: { |
| 49 | + clusters: (v) => (typeof v === 'number' ? formatNumber(v) : v), |
| 50 | + levels: (v) => (typeof v === 'number' ? formatNumber(v) : v), |
| 51 | + }, |
| 52 | + labels: { |
| 53 | + clusters: i18n('field_clusters'), |
| 54 | + levels: i18n('field_levels'), |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const settingsFormatter = createInfoFormatter<{ |
| 59 | + vector_dimension?: number; |
| 60 | + vector_type?: string; |
| 61 | + metric?: string; |
| 62 | + }>({ |
| 63 | + values: { |
| 64 | + vector_dimension: (v) => (typeof v === 'number' ? formatNumber(v) : v), |
| 65 | + }, |
| 66 | + labels: { |
| 67 | + vector_dimension: i18n('field_vector-dimension'), |
| 68 | + vector_type: i18n('field_vector-type'), |
| 69 | + metric: i18n('field_metric'), |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + const {clusters, levels, settings} = vectorSettings ?? {}; |
| 74 | + if (clusters !== undefined) { |
| 75 | + info.push(vectorFormatter('clusters', clusters)); |
| 76 | + } |
| 77 | + if (levels !== undefined) { |
| 78 | + info.push(vectorFormatter('levels', levels)); |
| 79 | + } |
| 80 | + |
| 81 | + const {vector_dimension: vectorDimension, vector_type: vectorType, metric} = settings ?? {}; |
| 82 | + |
| 83 | + if (vectorDimension !== undefined) { |
| 84 | + info.push(settingsFormatter('vector_dimension', vectorDimension)); |
| 85 | + } |
| 86 | + if (vectorType !== undefined) { |
| 87 | + info.push(settingsFormatter('vector_type', vectorType)); |
| 88 | + } |
| 89 | + if (metric !== undefined) { |
| 90 | + info.push(settingsFormatter('metric', metric)); |
| 91 | + } |
| 92 | + |
| 93 | + return info; |
| 94 | +} |
0 commit comments