11import React from 'react' ;
22
3+ import type { RadioButtonOption } from '@gravity-ui/uikit' ;
4+ import { RadioButton } from '@gravity-ui/uikit' ;
35import { useHistory , useLocation } from 'react-router-dom' ;
6+ import { StringParam , useQueryParam } from 'use-query-params' ;
7+ import { z } from 'zod' ;
48
59import type { DateRangeValues } from '../../../../components/DateRange' ;
610import { DateRange } from '../../../../components/DateRange' ;
7- import { ResponseError } from '../../../../components/Errors/ResponseError' ;
8- import { ResizeableDataTable } from '../../../../components/ResizeableDataTable/ResizeableDataTable' ;
911import { Search } from '../../../../components/Search' ;
1012import { TableWithControlsLayout } from '../../../../components/TableWithControlsLayout/TableWithControlsLayout' ;
1113import { parseQuery } from '../../../../routes' ;
1214import { changeUserInput } from '../../../../store/reducers/executeQuery' ;
13- import {
14- setTopQueriesFilters ,
15- topQueriesApi ,
16- } from '../../../../store/reducers/executeTopQueries/executeTopQueries' ;
15+ import { setTopQueriesFilters } from '../../../../store/reducers/executeTopQueries/executeTopQueries' ;
1716import {
1817 TENANT_PAGE ,
1918 TENANT_PAGES_IDS ,
2019 TENANT_QUERY_TABS_ID ,
2120} from '../../../../store/reducers/tenant/constants' ;
22- import type { EPathType } from '../../../../types/api/schema' ;
2321import { cn } from '../../../../utils/cn' ;
24- import { isSortableTopQueriesProperty } from '../../../../utils/diagnostics' ;
25- import { useAutoRefreshInterval , useTypedDispatch , useTypedSelector } from '../../../../utils/hooks' ;
26- import { parseQueryErrorToString } from '../../../../utils/query' ;
22+ import { useTypedDispatch , useTypedSelector } from '../../../../utils/hooks' ;
2723import { TenantTabsGroups , getTenantPath } from '../../TenantPages' ;
28- import { QUERY_TABLE_SETTINGS } from '../../utils/constants' ;
29- import { isColumnEntityType } from '../../utils/schema' ;
3024
31- import { TOP_QUERIES_COLUMNS , TOP_QUERIES_COLUMNS_WIDTH_LS_KEY } from './getTopQueriesColumns' ;
25+ import { RunningQueriesData } from './RunningQueriesData' ;
26+ import { TopQueriesData } from './TopQueriesData' ;
3227import i18n from './i18n' ;
3328
3429import './TopQueries.scss' ;
3530
3631const b = cn ( 'kv-top-queries' ) ;
3732
33+ const QueryModeIds = {
34+ top : 'top' ,
35+ running : 'running' ,
36+ } as const ;
37+
38+ const QUERY_MODE_OPTIONS : RadioButtonOption [ ] = [
39+ {
40+ value : QueryModeIds . top ,
41+ get content ( ) {
42+ return i18n ( 'mode_top' ) ;
43+ } ,
44+ } ,
45+ {
46+ value : QueryModeIds . running ,
47+ get content ( ) {
48+ return i18n ( 'mode_running' ) ;
49+ } ,
50+ } ,
51+ ] ;
52+
53+ const queryModeSchema = z . nativeEnum ( QueryModeIds ) . catch ( QueryModeIds . top ) ;
54+
3855interface TopQueriesProps {
3956 tenantName : string ;
40- type ?: EPathType ;
4157}
4258
43- export const TopQueries = ( { tenantName, type } : TopQueriesProps ) => {
59+ export const TopQueries = ( { tenantName} : TopQueriesProps ) => {
4460 const dispatch = useTypedDispatch ( ) ;
4561 const location = useLocation ( ) ;
4662 const history = useHistory ( ) ;
63+ const [ _queryMode = QueryModeIds . top , setQueryMode ] = useQueryParam ( 'queryMode' , StringParam ) ;
4764
48- const [ autoRefreshInterval ] = useAutoRefreshInterval ( ) ;
49-
50- const filters = useTypedSelector ( ( state ) => state . executeTopQueries ) ;
51- const { currentData, isFetching, error} = topQueriesApi . useGetTopQueriesQuery (
52- {
53- database : tenantName ,
54- filters,
55- } ,
56- { pollingInterval : autoRefreshInterval } ,
57- ) ;
58- const loading = isFetching && currentData === undefined ;
59- const { result : data } = currentData || { } ;
65+ const queryMode = queryModeSchema . parse ( _queryMode ) ;
6066
61- const rawColumns = TOP_QUERIES_COLUMNS ;
62- const columns = rawColumns . map ( ( column ) => ( {
63- ...column ,
64- sortable : isSortableTopQueriesProperty ( column . name ) ,
65- } ) ) ;
67+ const isTopQueries = queryMode === QueryModeIds . top ;
6668
67- const handleRowClick = React . useCallback (
68- ( row : any ) => {
69- const { QueryText : input } = row ;
69+ const filters = useTypedSelector ( ( state ) => state . executeTopQueries ) ;
7070
71+ const onRowClick = React . useCallback (
72+ ( input : string ) => {
7173 dispatch ( changeUserInput ( { input} ) ) ;
7274
7375 const queryParams = parseQuery ( location ) ;
@@ -91,48 +93,33 @@ export const TopQueries = ({tenantName, type}: TopQueriesProps) => {
9193 dispatch ( setTopQueriesFilters ( value ) ) ;
9294 } ;
9395
94- const renderContent = ( ) => {
95- if ( error && ! data ) {
96- return null ;
97- }
98-
99- if ( ! data || isColumnEntityType ( type ) ) {
100- return i18n ( 'no-data' ) ;
101- }
102-
103- return (
104- < ResizeableDataTable
105- columnsWidthLSKey = { TOP_QUERIES_COLUMNS_WIDTH_LS_KEY }
106- columns = { columns }
107- data = { data }
108- settings = { QUERY_TABLE_SETTINGS }
109- onRowClick = { handleRowClick }
110- rowClassName = { ( ) => b ( 'row' ) }
111- />
112- ) ;
113- } ;
114-
115- const renderControls = ( ) => {
116- return (
117- < React . Fragment >
96+ return (
97+ < TableWithControlsLayout >
98+ < TableWithControlsLayout . Controls >
99+ < RadioButton
100+ options = { QUERY_MODE_OPTIONS }
101+ value = { queryMode }
102+ onUpdate = { setQueryMode }
103+ />
118104 < Search
119105 value = { filters . text }
120106 onChange = { handleTextSearchUpdate }
121107 placeholder = { i18n ( 'filter.text.placeholder' ) }
122108 className = { b ( 'search' ) }
123109 />
124- < DateRange from = { filters . from } to = { filters . to } onChange = { handleDateRangeChange } />
125- </ React . Fragment >
126- ) ;
127- } ;
128-
129- return (
130- < TableWithControlsLayout >
131- < TableWithControlsLayout . Controls > { renderControls ( ) } </ TableWithControlsLayout . Controls >
132- { error ? < ResponseError error = { parseQueryErrorToString ( error ) } /> : null }
133- < TableWithControlsLayout . Table loading = { loading } >
134- { renderContent ( ) }
135- </ TableWithControlsLayout . Table >
110+ { isTopQueries ? (
111+ < DateRange
112+ from = { filters . from }
113+ to = { filters . to }
114+ onChange = { handleDateRangeChange }
115+ />
116+ ) : null }
117+ </ TableWithControlsLayout . Controls >
118+ { isTopQueries ? (
119+ < TopQueriesData database = { tenantName } onRowClick = { onRowClick } />
120+ ) : (
121+ < RunningQueriesData database = { tenantName } />
122+ ) }
136123 </ TableWithControlsLayout >
137124 ) ;
138125} ;
0 commit comments