|
| 1 | +import {useEffect, useMemo, useRef} from 'react'; |
| 2 | +import {useDispatch} from 'react-redux'; |
| 3 | +import DataTable, {type Column} from '@gravity-ui/react-data-table'; |
| 4 | + |
| 5 | +import type {HotKey} from '../../../../types/api/hotkeys'; |
| 6 | +import type {IResponseError} from '../../../../types/api/error'; |
| 7 | +import {Icon} from '../../../../components/Icon'; |
| 8 | +import {ResponseError} from '../../../../components/Errors/ResponseError'; |
| 9 | +import {useTypedSelector} from '../../../../utils/hooks'; |
| 10 | +import {cn} from '../../../../utils/cn'; |
| 11 | +import {DEFAULT_TABLE_SETTINGS} from '../../../../utils/constants'; |
| 12 | +import { |
| 13 | + setHotKeysData, |
| 14 | + setHotKeysDataWasNotLoaded, |
| 15 | + setHotKeysError, |
| 16 | + setHotKeysLoading, |
| 17 | +} from '../../../../store/reducers/hotKeys/hotKeys'; |
| 18 | + |
| 19 | +import './HotKeys.scss'; |
| 20 | +import i18n from './i18n'; |
| 21 | + |
| 22 | +const b = cn('ydb-hot-keys'); |
| 23 | + |
| 24 | +const tableColumnsIds = { |
| 25 | + accessSample: 'accessSample', |
| 26 | + keyValues: 'keyValues', |
| 27 | +} as const; |
| 28 | + |
| 29 | +const getHotKeysColumns = (keyColumnsIds: string[] = []): Column<HotKey>[] => { |
| 30 | + const keysColumns: Column<HotKey>[] = keyColumnsIds.map((col, index) => ({ |
| 31 | + name: col, |
| 32 | + header: ( |
| 33 | + <div className={b('primary-key-column')}> |
| 34 | + <Icon name="key" viewBox="0 0 12 7" width={12} height={7} /> |
| 35 | + {col} |
| 36 | + </div> |
| 37 | + ), |
| 38 | + render: ({row}) => row.keyValues[index], |
| 39 | + align: DataTable.RIGHT, |
| 40 | + sortable: false, |
| 41 | + })); |
| 42 | + |
| 43 | + return [ |
| 44 | + { |
| 45 | + name: tableColumnsIds.accessSample, |
| 46 | + header: 'Samples', |
| 47 | + render: ({row}) => row.accessSample, |
| 48 | + align: DataTable.RIGHT, |
| 49 | + sortable: false, |
| 50 | + }, |
| 51 | + ...keysColumns, |
| 52 | + ]; |
| 53 | +}; |
| 54 | + |
| 55 | +interface HotKeysProps { |
| 56 | + path: string; |
| 57 | +} |
| 58 | + |
| 59 | +export function HotKeys({path}: HotKeysProps) { |
| 60 | + const dispatch = useDispatch(); |
| 61 | + |
| 62 | + const collectSamplesTimerRef = useRef<ReturnType<typeof setTimeout>>(); |
| 63 | + |
| 64 | + const {loading, wasLoaded, data, error} = useTypedSelector((state) => state.hotKeys); |
| 65 | + const {loading: schemaLoading, data: schemaData} = useTypedSelector((state) => state.schema); |
| 66 | + |
| 67 | + const keyColumnsIds = schemaData[path]?.PathDescription?.Table?.KeyColumnNames; |
| 68 | + |
| 69 | + const tableColumns = useMemo(() => { |
| 70 | + return getHotKeysColumns(keyColumnsIds); |
| 71 | + }, [keyColumnsIds]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + const fetchHotkeys = async (enableSampling: boolean) => { |
| 75 | + // Set hotkeys error, but not data, since data is set conditionally |
| 76 | + try { |
| 77 | + const response = await window.api.getHotKeys(path, enableSampling); |
| 78 | + return response; |
| 79 | + } catch (err) { |
| 80 | + dispatch(setHotKeysError(err as IResponseError)); |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const fetchData = async () => { |
| 86 | + // If there is previous pending request for samples, cancel it |
| 87 | + if (collectSamplesTimerRef.current !== undefined) { |
| 88 | + window.clearInterval(collectSamplesTimerRef.current); |
| 89 | + } |
| 90 | + |
| 91 | + dispatch(setHotKeysDataWasNotLoaded()); |
| 92 | + dispatch(setHotKeysLoading()); |
| 93 | + |
| 94 | + // Send request that will trigger hot keys sampling (enable_sampling = true) |
| 95 | + const initialResponse = await fetchHotkeys(true); |
| 96 | + |
| 97 | + // If there are hotkeys in the initial request (hotkeys was collected before) |
| 98 | + // we could just use colleted samples (collected hotkeys are stored only for 30 seconds) |
| 99 | + if (initialResponse && initialResponse.hotkeys) { |
| 100 | + dispatch(setHotKeysData(initialResponse)); |
| 101 | + } else if (initialResponse) { |
| 102 | + // Else wait for 5 seconds, while hot keys are being collected |
| 103 | + // And request these samples (enable_sampling = false) |
| 104 | + const timer = setTimeout(async () => { |
| 105 | + const responseWithSamples = await fetchHotkeys(false); |
| 106 | + if (responseWithSamples) { |
| 107 | + dispatch(setHotKeysData(responseWithSamples)); |
| 108 | + } |
| 109 | + }, 5000); |
| 110 | + collectSamplesTimerRef.current = timer; |
| 111 | + } |
| 112 | + }; |
| 113 | + fetchData(); |
| 114 | + }, [dispatch, path]); |
| 115 | + |
| 116 | + // It takes a while to collect hot keys. Display explicit status message, while collecting |
| 117 | + if ((loading && !wasLoaded) || schemaLoading) { |
| 118 | + return <div>{i18n('hot-keys-collecting')}</div>; |
| 119 | + } |
| 120 | + |
| 121 | + if (error) { |
| 122 | + return <ResponseError error={error} />; |
| 123 | + } |
| 124 | + |
| 125 | + if (!data) { |
| 126 | + return <div>{i18n('no-data')}</div>; |
| 127 | + } |
| 128 | + |
| 129 | + return ( |
| 130 | + <div className={b('table-content')}> |
| 131 | + <DataTable |
| 132 | + columns={tableColumns} |
| 133 | + data={data} |
| 134 | + settings={DEFAULT_TABLE_SETTINGS} |
| 135 | + theme="yandex-cloud" |
| 136 | + initialSortOrder={{ |
| 137 | + columnId: tableColumnsIds.accessSample, |
| 138 | + order: DataTable.DESCENDING, |
| 139 | + }} |
| 140 | + /> |
| 141 | + </div> |
| 142 | + ); |
| 143 | +} |
0 commit comments