1- import React , { useEffect , useState } from 'react'
1+ import React from 'react'
22import { useParams } from 'react-router-dom'
3- import { ITableColumn } from 'uiSrc/components/virtual-table/interfaces'
4- import VirtualTable from 'uiSrc/components/virtual-table/VirtualTable'
5- import {
6- DURATION_UNITS ,
7- DurationUnits ,
8- SortOrder ,
9- TableCellAlignment ,
10- TableCellTextAlignment ,
11- } from 'uiSrc/constants'
3+ import { DURATION_UNITS , DurationUnits , SortOrder } from 'uiSrc/constants'
124import { convertNumberByUnits } from 'uiSrc/pages/slow-log/utils'
135import { sendEventTelemetry , TelemetryEvent } from 'uiSrc/telemetry'
146import { numberWithSpaces } from 'uiSrc/utils/numbers'
157import { Text } from 'uiSrc/components/base/text'
8+ import {
9+ Table ,
10+ ColumnDef ,
11+ SortingState ,
12+ } from 'uiSrc/components/base/layout/table'
1613
1714import { FormatedDate , RiTooltip } from 'uiSrc/components'
1815import styles from '../styles.module.scss'
1916
17+ import { SlowLog } from 'apiSrc/modules/slow-log/models'
18+ import { StyledTableWrapper } from './SlowLogTable.styles'
19+
2020export const DATE_FORMAT = 'HH:mm:ss d LLL yyyy'
2121
2222export interface Props {
23- items : any
23+ items : SlowLog [ ]
2424 loading : boolean
2525 durationUnit : DurationUnits
2626}
2727
28- const sortByTimeStamp = ( items = [ ] , order : SortOrder ) =>
29- [ ...items ] . sort ( ( a : any , b : any ) =>
30- order === SortOrder . DESC ? b . time - a . time : a . time - b . time ,
31- )
32-
3328const SlowLogTable = ( props : Props ) => {
34- const { items = [ ] , loading = false , durationUnit } = props
35- const [ table , setTable ] = useState < any > ( [ ] )
36- const [ sortedColumnName , setSortedColumnName ] = useState ( 'time' )
37- const [ sortedColumnOrder , setSortedColumnOrder ] = useState ( SortOrder . DESC )
29+ const { items = [ ] , durationUnit } = props
3830
3931 const { instanceId } = useParams < { instanceId : string } > ( )
40- const sortedColumn = {
41- column : sortedColumnName ,
42- order : sortedColumnOrder ,
43- }
44-
45- useEffect ( ( ) => {
46- setTable ( sortByTimeStamp ( items , sortedColumnOrder ) )
47- } , [ items , sortedColumnOrder ] )
4832
49- const columns : ITableColumn [ ] = [
33+ const columns : ColumnDef < SlowLog > [ ] = [
5034 {
5135 id : 'time' ,
52- label : 'Timestamp' ,
53- absoluteWidth : 190 ,
54- minWidth : 190 ,
55- isSortable : true ,
56- render : ( timestamp ) => (
57- < Text
58- size = "s"
59- color = "subdued"
60- data-testid = "timestamp-value"
61- className = { styles . timestampCell }
62- >
63- < FormatedDate date = { timestamp * 1000 } />
64- </ Text >
65- ) ,
36+ header : 'Timestamp' ,
37+ accessorKey : 'time' ,
38+ size : 15 ,
39+ cell : ( { getValue } ) => {
40+ const date = ( getValue ( ) as number ) * 1000
41+
42+ return < FormatedDate date = { date } />
43+ } ,
6644 } ,
6745 {
6846 id : 'durationUs' ,
69- label : `Duration, ${ DURATION_UNITS . find ( ( { value } ) => value === durationUnit ) ?. inputDisplay } ` ,
70- minWidth : 110 ,
71- absoluteWidth : 'auto' ,
72- textAlignment : TableCellTextAlignment . Right ,
73- alignment : TableCellAlignment . Right ,
74- render : ( duration ) => (
75- < Text size = "s" color = "subdued" data-testid = "duration-value" >
76- { numberWithSpaces ( convertNumberByUnits ( duration , durationUnit ) ) }
77- </ Text >
78- ) ,
47+ header : `Duration, ${ DURATION_UNITS . find ( ( { value } ) => value === durationUnit ) ?. inputDisplay } ` ,
48+ accessorKey : 'durationUs' ,
49+ size : 15 ,
50+ cell : ( { getValue } ) => {
51+ const duration = getValue ( ) as number
52+
53+ return (
54+ < Text size = "s" data-testid = "duration-value" >
55+ { numberWithSpaces ( convertNumberByUnits ( duration , durationUnit ) ) }
56+ </ Text >
57+ )
58+ } ,
7959 } ,
8060 {
8161 id : 'args' ,
82- label : 'Command' ,
83- render : ( command ) => (
84- < RiTooltip
85- position = "bottom"
86- content = { command }
87- anchorClassName = { styles . commandTooltip }
88- >
89- < span className = { styles . commandText } data-testid = "command-value" >
90- { command }
91- </ span >
92- </ RiTooltip >
93- ) ,
62+ header : 'Command' ,
63+ accessorKey : 'args' ,
64+ cell : ( { getValue } ) => {
65+ const command = getValue ( ) as string
66+
67+ return (
68+ < RiTooltip
69+ position = "bottom"
70+ content = { command }
71+ anchorClassName = { styles . commandTooltip }
72+ >
73+ < span className = { styles . commandText } data-testid = "command-value" >
74+ { command }
75+ </ span >
76+ </ RiTooltip >
77+ )
78+ } ,
9479 } ,
9580 ]
9681
97- const onChangeSorting = ( column : any , order : SortOrder ) => {
98- setSortedColumnName ( column )
99- setSortedColumnOrder ( order )
82+ const handleSortingChange = ( state : SortingState ) => {
83+ const { desc } = state [ 0 ] || { desc : true }
84+ const order = desc ? SortOrder . DESC : SortOrder . ASC
85+
10086 sendEventTelemetry ( {
10187 event : TelemetryEvent . SLOWLOG_SORTED ,
10288 eventData : {
@@ -107,17 +93,15 @@ const SlowLogTable = (props: Props) => {
10793 }
10894
10995 return (
110- < div className = { styles . tableWrapper } data-testid = "slowlog-table" >
111- < VirtualTable
112- selectable = { false }
113- loading = { loading }
114- items = { table }
96+ < StyledTableWrapper data-testid = "slowlog-table" >
97+ < Table
11598 columns = { columns }
116- sortedColumn = { sortedColumn }
117- onChangeSorting = { onChangeSorting }
118- hideFooter
99+ data = { items }
100+ onSortingChange = { handleSortingChange }
101+ maxHeight = "60vh"
102+ stripedRows
119103 />
120- </ div >
104+ </ StyledTableWrapper >
121105 )
122106}
123107
0 commit comments