@@ -5,7 +5,6 @@ import cn from 'bem-cn-lite';
55import DataTable , { Column , Settings , SortOrder } from '@gravity-ui/react-data-table' ;
66import { Loader } from '@gravity-ui/uikit' ;
77
8- import { DateRange , DateRangeValues } from '../../../../components/DateRange' ;
98import { InternalLink } from '../../../../components/InternalLink' ;
109
1110import HistoryContext from '../../../../contexts/HistoryContext' ;
@@ -18,7 +17,7 @@ import {
1817 setShardsQueryFilters ,
1918} from '../../../../store/reducers/shardsWorkload' ;
2019import { setCurrentSchemaPath , getSchema } from '../../../../store/reducers/schema' ;
21- import type { IShardsWorkloadFilters } from '../../../../types/store/shardsWorkload' ;
20+ import { EShardsWorkloadMode , IShardsWorkloadFilters } from '../../../../types/store/shardsWorkload' ;
2221
2322import type { EPathType } from '../../../../types/api/schema' ;
2423
@@ -31,10 +30,12 @@ import {getDefaultNodePath} from '../../../Node/NodePages';
3130
3231import { isColumnEntityType } from '../../utils/schema' ;
3332
33+ import { Filters } from './Filters' ;
34+
3435import i18n from './i18n' ;
3536import './TopShards.scss' ;
3637
37- const b = cn ( 'top-shards' ) ;
38+ export const b = cn ( 'top-shards' ) ;
3839const bLink = cn ( 'yc-link' ) ;
3940
4041const TABLE_SETTINGS : Settings = {
@@ -83,6 +84,12 @@ function dataTableToStringSortOrder(value: SortOrder | SortOrder[] = []) {
8384 return sortOrders . map ( ( { columnId} ) => columnId ) . join ( ',' ) ;
8485}
8586
87+ function fillDateRangeFor ( value : IShardsWorkloadFilters ) {
88+ value . to = Date . now ( ) ;
89+ value . from = value . to - HOUR_IN_SECONDS * 1000 ;
90+ return value ;
91+ }
92+
8693interface TopShardsProps {
8794 tenantPath : string ;
8895 type ?: EPathType ;
@@ -101,17 +108,20 @@ export const TopShards = ({tenantPath, type}: TopShardsProps) => {
101108 wasLoaded,
102109 } = useTypedSelector ( ( state ) => state . shardsWorkload ) ;
103110
104- // default date range should be the last hour, but shouldn't propagate into URL until user interacts with the control
111+ // default filters shouldn't propagate into URL until user interacts with the control
105112 // redux initial value can't be used, as it synchronizes with URL
106113 const [ filters , setFilters ] = useState < IShardsWorkloadFilters > ( ( ) => {
107- if ( ! storeFilters ?. from && ! storeFilters ?. to ) {
108- return {
109- from : Date . now ( ) - HOUR_IN_SECONDS * 1000 ,
110- to : Date . now ( ) ,
111- } ;
114+ const defaultValue = { ...storeFilters } ;
115+
116+ if ( ! defaultValue . mode ) {
117+ defaultValue . mode = EShardsWorkloadMode . Immediate ;
118+ }
119+
120+ if ( ! defaultValue . from && ! defaultValue . to ) {
121+ fillDateRangeFor ( defaultValue ) ;
112122 }
113123
114- return storeFilters ;
124+ return defaultValue ;
115125 } ) ;
116126
117127 const [ sortOrder , setSortOrder ] = useState ( tableColumnsNames . CPUCores ) ;
@@ -150,12 +160,28 @@ export const TopShards = ({tenantPath, type}: TopShardsProps) => {
150160 setSortOrder ( dataTableToStringSortOrder ( newSortOrder ) ) ;
151161 } ;
152162
153- const handleDateRangeChange = ( value : DateRangeValues ) => {
163+ const handleFiltersChange = ( value : Partial < IShardsWorkloadFilters > ) => {
164+ const newStateValue = { ...value } ;
165+ const isDateRangePristine =
166+ ! storeFilters . from && ! storeFilters . to && ! value . from && ! value . to ;
167+
168+ if ( isDateRangePristine ) {
169+ switch ( value . mode ) {
170+ case EShardsWorkloadMode . Immediate :
171+ newStateValue . from = newStateValue . to = undefined ;
172+ break ;
173+ case EShardsWorkloadMode . History :
174+ // should default to the current datetime every time history mode activates
175+ fillDateRangeFor ( newStateValue ) ;
176+ break ;
177+ }
178+ }
179+
154180 dispatch ( setShardsQueryFilters ( value ) ) ;
155- setFilters ( value ) ;
181+ setFilters ( ( state ) => ( { ... state , ... newStateValue } ) ) ;
156182 } ;
157183
158- const tableColumns : Column < any > [ ] = useMemo ( ( ) => {
184+ const tableColumns = useMemo ( ( ) => {
159185 const onSchemaClick = ( schemaPath : string ) => {
160186 return ( ) => {
161187 dispatch ( setCurrentSchemaPath ( schemaPath ) ) ;
@@ -164,7 +190,7 @@ export const TopShards = ({tenantPath, type}: TopShardsProps) => {
164190 } ;
165191 } ;
166192
167- return [
193+ const columns : Column < any > [ ] = [
168194 {
169195 name : tableColumnsNames . Path ,
170196 render : ( { value : relativeNodePath } ) => {
@@ -217,23 +243,29 @@ export const TopShards = ({tenantPath, type}: TopShardsProps) => {
217243 align : DataTable . RIGHT ,
218244 sortable : false ,
219245 } ,
220- {
221- name : tableColumnsNames . PeakTime ,
222- render : ( { value} ) => formatDateTime ( new Date ( value as string ) . valueOf ( ) ) ,
223- sortable : false ,
224- } ,
225246 {
226247 name : tableColumnsNames . InFlightTxCount ,
227248 render : ( { value} ) => formatNumber ( value as number ) ,
228249 align : DataTable . RIGHT ,
229250 sortable : false ,
230251 } ,
231- {
252+ ] ;
253+
254+ if ( filters . mode === EShardsWorkloadMode . History ) {
255+ // after NodeId
256+ columns . splice ( 5 , 0 , {
257+ name : tableColumnsNames . PeakTime ,
258+ render : ( { value} ) => formatDateTime ( new Date ( value as string ) . valueOf ( ) ) ,
259+ sortable : false ,
260+ } ) ;
261+ columns . push ( {
232262 name : tableColumnsNames . IntervalEnd ,
233263 render : ( { value} ) => formatDateTime ( new Date ( value as string ) . getTime ( ) ) ,
234- }
235- ] ;
236- } , [ dispatch , history , tenantPath ] ) ;
264+ } ) ;
265+ }
266+
267+ return columns ;
268+ } , [ dispatch , filters . mode , history , tenantPath ] ) ;
237269
238270 const renderLoader = ( ) => {
239271 return (
@@ -272,10 +304,8 @@ export const TopShards = ({tenantPath, type}: TopShardsProps) => {
272304
273305 return (
274306 < div className = { b ( ) } >
275- < div className = { b ( 'controls' ) } >
276- { i18n ( 'description' ) }
277- < DateRange from = { filters . from } to = { filters . to } onChange = { handleDateRangeChange } />
278- </ div >
307+ < Filters value = { filters } onChange = { handleFiltersChange } />
308+ { filters . mode === EShardsWorkloadMode . History && < div > { i18n ( 'description' ) } </ div > }
279309 { renderContent ( ) }
280310 </ div >
281311 ) ;
0 commit comments