1
1
import Box from "@mui/material/Box"
2
- import { styled } from "@mui/material/styles"
2
+ import { styled , useTheme } from "@mui/material/styles"
3
3
import { BoxProps } from "@mui/system"
4
4
import { useQuery } from "@tanstack/react-query"
5
- import { getTemplateDAUs } from "api/api"
5
+ import { getInsightsUserLatency , getTemplateDAUs } from "api/api"
6
6
import { DAUChart } from "components/DAUChart/DAUChart"
7
7
import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout"
8
8
import {
9
9
HelpTooltip ,
10
10
HelpTooltipTitle ,
11
11
HelpTooltipText ,
12
12
} from "components/Tooltips/HelpTooltip"
13
+ import { getLatencyColor } from "utils/latency"
13
14
14
- const TemplateInsightsPage = ( ) => {
15
+ export default function TemplateInsightsPage ( ) {
15
16
return (
16
17
< Box
17
18
sx = { {
@@ -22,10 +23,7 @@ const TemplateInsightsPage = () => {
22
23
} }
23
24
>
24
25
< DailyUsersPanel sx = { { gridColumn : "span 2" } } />
25
-
26
- < Panel >
27
- < PanelHeader > Latency by user</ PanelHeader >
28
- </ Panel >
26
+ < UserLatencyPanel />
29
27
30
28
< Panel sx = { { gridColumn : "span 3" } } >
31
29
< PanelHeader > App‘s & IDE usage </ PanelHeader >
@@ -34,8 +32,6 @@ const TemplateInsightsPage = () => {
34
32
)
35
33
}
36
34
37
- export default TemplateInsightsPage
38
-
39
35
const DailyUsersPanel = ( props : BoxProps ) => {
40
36
const { template } = useTemplateLayoutContext ( )
41
37
const { data } = useQuery ( {
@@ -58,6 +54,48 @@ const DailyUsersPanel = (props: BoxProps) => {
58
54
)
59
55
}
60
56
57
+ const UserLatencyPanel = ( props : BoxProps ) => {
58
+ const { template } = useTemplateLayoutContext ( )
59
+ const { data } = useQuery ( {
60
+ queryKey : [ "templates" , template . id , "user-latency" ] ,
61
+ queryFn : ( ) =>
62
+ getInsightsUserLatency ( {
63
+ template_ids : template . id ,
64
+ start_time : toTimeFilter ( getTimeFor7DaysAgo ( ) ) ,
65
+ end_time : toTimeFilter ( new Date ( ) ) ,
66
+ } ) ,
67
+ } )
68
+ const theme = useTheme ( )
69
+
70
+ return (
71
+ < Panel { ...props } sx = { { overflowY : "auto" , ...props . sx } } >
72
+ < PanelHeader sx = { { display : "flex" , alignItems : "center" , gap : 1 } } >
73
+ Latency by user
74
+ </ PanelHeader >
75
+ < PanelContent >
76
+ { data &&
77
+ data . report . users
78
+ . sort ( ( a , b ) => b . latency_ms . p95 - a . latency_ms . p95 )
79
+ . map ( ( row ) => (
80
+ < Box
81
+ key = { row . user_id }
82
+ sx = { {
83
+ display : "flex" ,
84
+ justifyContent : "space-between" ,
85
+ fontSize : 14 ,
86
+ } }
87
+ >
88
+ < Box sx = { { fontWeight : 500 } } > { row . username } </ Box >
89
+ < Box sx = { { color : getLatencyColor ( theme , row . latency_ms . p95 ) } } >
90
+ { row . latency_ms . p95 . toFixed ( 0 ) } ms
91
+ </ Box >
92
+ </ Box >
93
+ ) ) }
94
+ </ PanelContent >
95
+ </ Panel >
96
+ )
97
+ }
98
+
61
99
const Panel = styled ( Box ) ( ( { theme } ) => ( {
62
100
borderRadius : theme . shape . borderRadius ,
63
101
border : `1px solid ${ theme . palette . divider } ` ,
@@ -77,3 +115,18 @@ const PanelContent = styled(Box)(({ theme }) => ({
77
115
padding : theme . spacing ( 0 , 3 , 3 ) ,
78
116
flex : 1 ,
79
117
} ) )
118
+
119
+ function getTimeFor7DaysAgo ( ) : Date {
120
+ const sevenDaysAgo = new Date ( )
121
+ sevenDaysAgo . setDate ( sevenDaysAgo . getDate ( ) - 7 )
122
+ return sevenDaysAgo
123
+ }
124
+
125
+ function toTimeFilter ( date : Date ) {
126
+ date . setHours ( 0 , 0 , 0 , 0 )
127
+ const year = date . getUTCFullYear ( )
128
+ const month = String ( date . getUTCMonth ( ) + 1 ) . padStart ( 2 , "0" )
129
+ const day = String ( date . getUTCDate ( ) ) . padStart ( 2 , "0" )
130
+
131
+ return `${ year } -${ month } -${ day } T00:00:00Z`
132
+ }
0 commit comments