Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e35a3b5

Browse files
committed
List latency by user
1 parent 6679ae3 commit e35a3b5

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

site/src/api/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ export const getInsightsUserLatency = async (filters: {
13731373
start_time: string
13741374
end_time: string
13751375
template_ids: string
1376-
}): Promise<TypesGen.DAUsResponse> => {
1376+
}): Promise<TypesGen.UserLatencyInsightsResponse> => {
13771377
const params = new URLSearchParams(filters)
13781378
const response = await axios.get(`/api/v2/insights/user-latency?${params}`)
13791379
return response.data

site/src/pages/TemplatePage/TemplateInsightsPage/TemplateInsightsPage.tsx

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Box from "@mui/material/Box"
2-
import { styled } from "@mui/material/styles"
2+
import { styled, useTheme } from "@mui/material/styles"
33
import { BoxProps } from "@mui/system"
44
import { useQuery } from "@tanstack/react-query"
5-
import { getTemplateDAUs } from "api/api"
5+
import { getInsightsUserLatency, getTemplateDAUs } from "api/api"
66
import { DAUChart } from "components/DAUChart/DAUChart"
77
import { useTemplateLayoutContext } from "components/TemplateLayout/TemplateLayout"
88
import {
99
HelpTooltip,
1010
HelpTooltipTitle,
1111
HelpTooltipText,
1212
} from "components/Tooltips/HelpTooltip"
13+
import { getLatencyColor } from "utils/latency"
1314

14-
const TemplateInsightsPage = () => {
15+
export default function TemplateInsightsPage() {
1516
return (
1617
<Box
1718
sx={{
@@ -22,10 +23,7 @@ const TemplateInsightsPage = () => {
2223
}}
2324
>
2425
<DailyUsersPanel sx={{ gridColumn: "span 2" }} />
25-
26-
<Panel>
27-
<PanelHeader>Latency by user</PanelHeader>
28-
</Panel>
26+
<UserLatencyPanel />
2927

3028
<Panel sx={{ gridColumn: "span 3" }}>
3129
<PanelHeader>App&lsquo;s & IDE usage</PanelHeader>
@@ -34,8 +32,6 @@ const TemplateInsightsPage = () => {
3432
)
3533
}
3634

37-
export default TemplateInsightsPage
38-
3935
const DailyUsersPanel = (props: BoxProps) => {
4036
const { template } = useTemplateLayoutContext()
4137
const { data } = useQuery({
@@ -58,6 +54,48 @@ const DailyUsersPanel = (props: BoxProps) => {
5854
)
5955
}
6056

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+
6199
const Panel = styled(Box)(({ theme }) => ({
62100
borderRadius: theme.shape.borderRadius,
63101
border: `1px solid ${theme.palette.divider}`,
@@ -77,3 +115,18 @@ const PanelContent = styled(Box)(({ theme }) => ({
77115
padding: theme.spacing(0, 3, 3),
78116
flex: 1,
79117
}))
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

Comments
 (0)