Skip to content

Commit e4a3baa

Browse files
committed
refactor(TopQueries): migrate to ts
1 parent b20983f commit e4a3baa

File tree

10 files changed

+263
-256
lines changed

10 files changed

+263
-256
lines changed

src/containers/Tenant/Diagnostics/Diagnostics.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {Switch, Tabs} from '@gravity-ui/uikit';
99

1010
import {Loader} from '../../../components/Loader';
1111

12-
//@ts-ignore
13-
import TopQueries from './TopQueries/TopQueries';
12+
import {TopQueries} from './TopQueries';
1413
//@ts-ignore
1514
import DetailedOverview from './DetailedOverview/DetailedOverview';
1615
import {OverloadedShards} from './OverloadedShards';

src/containers/Tenant/Diagnostics/TopQueries/TopQueries.js

Lines changed: 0 additions & 188 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"no-data": "No data"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {i18n, Lang} from '../../../../../utils/i18n';
2+
3+
import en from './en.json';
4+
import ru from './ru.json';
5+
6+
const COMPONENT = 'ydb-diagnostics-top-queries';
7+
8+
i18n.registerKeyset(Lang.En, COMPONENT, en);
9+
i18n.registerKeyset(Lang.Ru, COMPONENT, ru);
10+
11+
export default i18n.keyset(COMPONENT);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"no-data": "Нет данных"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './TopQueries';

0 commit comments

Comments
 (0)