|
| 1 | +import {useDispatch} from 'react-redux'; |
| 2 | +import cn from 'bem-cn-lite'; |
| 3 | + |
| 4 | +import DataTable, {Column} from '@yandex-cloud/react-data-table'; |
| 5 | +import {Loader} from '@gravity-ui/uikit'; |
| 6 | + |
| 7 | +import TruncatedQuery from '../../../../components/TruncatedQuery/TruncatedQuery'; |
| 8 | + |
| 9 | +import {changeUserInput} from '../../../../store/reducers/executeQuery'; |
| 10 | +import {fetchTopQueries, setTopQueriesState} from '../../../../store/reducers/executeTopQueries'; |
| 11 | + |
| 12 | +import type {KeyValueRow} from '../../../../types/api/query'; |
| 13 | +import type {EPathType} from '../../../../types/api/schema'; |
| 14 | + |
| 15 | +import {DEFAULT_TABLE_SETTINGS} from '../../../../utils/constants'; |
| 16 | +import {useAutofetcher, useTypedSelector} from '../../../../utils/hooks'; |
| 17 | +import {prepareQueryError} from '../../../../utils/query'; |
| 18 | + |
| 19 | +import {isColumnEntityType} from '../../utils/schema'; |
| 20 | +import {TenantGeneralTabsIds} from '../../TenantPages'; |
| 21 | + |
| 22 | +import i18n from './i18n'; |
| 23 | +import './TopQueries.scss'; |
| 24 | +import {useCallback, useEffect} from 'react'; |
| 25 | + |
| 26 | +const b = cn('kv-top-queries'); |
| 27 | + |
| 28 | +const MAX_QUERY_HEIGHT = 10; |
| 29 | +const COLUMNS: Column<KeyValueRow>[] = [ |
| 30 | + { |
| 31 | + name: 'CPUTimeUs', |
| 32 | + width: 140, |
| 33 | + sortAccessor: (row) => Number(row['CPUTimeUs']), |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'QueryText', |
| 37 | + width: 500, |
| 38 | + sortable: false, |
| 39 | + render: ({value}) => <TruncatedQuery value={value} maxQueryHeight={MAX_QUERY_HEIGHT} />, |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +interface TopQueriesProps { |
| 44 | + path: string; |
| 45 | + changeSchemaTab: (tab: TenantGeneralTabsIds) => void; |
| 46 | + type?: EPathType; |
| 47 | +} |
| 48 | + |
| 49 | +export const TopQueries = ({path, type, changeSchemaTab}: TopQueriesProps) => { |
| 50 | + const dispatch = useDispatch(); |
| 51 | + |
| 52 | + const {autorefresh} = useTypedSelector((state) => state.schema); |
| 53 | + |
| 54 | + const { |
| 55 | + loading, |
| 56 | + wasLoaded, |
| 57 | + error, |
| 58 | + data: {result: data = undefined} = {}, |
| 59 | + } = useTypedSelector((state) => state.executeTopQueries); |
| 60 | + |
| 61 | + useAutofetcher( |
| 62 | + () => dispatch(fetchTopQueries({database: path})), |
| 63 | + [dispatch, path], |
| 64 | + autorefresh, |
| 65 | + ); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + dispatch( |
| 69 | + setTopQueriesState({ |
| 70 | + wasLoaded: false, |
| 71 | + data: undefined, |
| 72 | + }), |
| 73 | + ); |
| 74 | + }, [dispatch, path]); |
| 75 | + |
| 76 | + const handleRowClick = useCallback( |
| 77 | + (row) => { |
| 78 | + const {QueryText: input} = row; |
| 79 | + |
| 80 | + dispatch(changeUserInput({input})); |
| 81 | + changeSchemaTab(TenantGeneralTabsIds.query); |
| 82 | + }, |
| 83 | + [changeSchemaTab, dispatch], |
| 84 | + ); |
| 85 | + |
| 86 | + const renderLoader = () => { |
| 87 | + return ( |
| 88 | + <div className={b('loader')}> |
| 89 | + <Loader size="m" /> |
| 90 | + </div> |
| 91 | + ); |
| 92 | + }; |
| 93 | + |
| 94 | + const renderContent = () => { |
| 95 | + if (loading && !wasLoaded) { |
| 96 | + return renderLoader(); |
| 97 | + } |
| 98 | + |
| 99 | + if (error && !error.isCancelled) { |
| 100 | + return <div className="error">{prepareQueryError(error)}</div>; |
| 101 | + } |
| 102 | + |
| 103 | + if (!data || isColumnEntityType(type)) { |
| 104 | + return i18n('no-data'); |
| 105 | + } |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className={b('result')}> |
| 109 | + <div className={b('table-wrapper')}> |
| 110 | + <div className={b('table-content')}> |
| 111 | + <DataTable |
| 112 | + columns={COLUMNS} |
| 113 | + data={data} |
| 114 | + settings={DEFAULT_TABLE_SETTINGS} |
| 115 | + onRowClick={handleRowClick} |
| 116 | + theme="yandex-cloud" |
| 117 | + /> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + }; |
| 123 | + |
| 124 | + return <div className={b()}>{renderContent()}</div>; |
| 125 | +}; |
0 commit comments