|
| 1 | +import { |
| 2 | + Box, |
| 3 | + FormControl, |
| 4 | + InputLabel, |
| 5 | + Link, |
| 6 | + MenuItem, |
| 7 | + Select, |
| 8 | + SelectChangeEvent, |
| 9 | + Skeleton, |
| 10 | + Stack, |
| 11 | + Typography, |
| 12 | +} from "@mui/material"; |
| 13 | +import { |
| 14 | + seriesWithInterpolatedTimes, |
| 15 | + TimeSeriesPanelWithData, |
| 16 | +} from "components/metrics/panels/TimeSeriesPanel"; |
| 17 | +import dayjs from "dayjs"; |
| 18 | +import utc from "dayjs/plugin/utc"; |
| 19 | +import { fetcher } from "lib/GeneralUtils"; |
| 20 | +import Head from "next/head"; |
| 21 | +import NextLink from "next/link"; |
| 22 | +import { useMemo, useState } from "react"; |
| 23 | +import useSWR from "swr"; |
| 24 | +dayjs.extend(utc); |
| 25 | + |
| 26 | +interface SuccessRateRow { |
| 27 | + day: string; |
| 28 | + repo: string; |
| 29 | + successes: number; |
| 30 | + failures: number; |
| 31 | + timed_out: number; |
| 32 | + total: number; |
| 33 | + pass_rate: number; |
| 34 | +} |
| 35 | + |
| 36 | +function PassRateChart({ |
| 37 | + data, |
| 38 | + days, |
| 39 | +}: { |
| 40 | + data: SuccessRateRow[]; |
| 41 | + days: number; |
| 42 | +}) { |
| 43 | + const startTime = dayjs.utc().subtract(days, "day").startOf("day"); |
| 44 | + const stopTime = dayjs.utc().endOf("day"); |
| 45 | + |
| 46 | + const series = seriesWithInterpolatedTimes( |
| 47 | + data, |
| 48 | + startTime, |
| 49 | + stopTime, |
| 50 | + "day", |
| 51 | + "repo", |
| 52 | + "day", |
| 53 | + "pass_rate", |
| 54 | + true, |
| 55 | + true, |
| 56 | + "name", |
| 57 | + "line" |
| 58 | + ); |
| 59 | + |
| 60 | + return ( |
| 61 | + <Box sx={{ height: 420 }}> |
| 62 | + <TimeSeriesPanelWithData |
| 63 | + data={data} |
| 64 | + series={series} |
| 65 | + title="Pass Rate Over Time" |
| 66 | + groupByFieldName="repo" |
| 67 | + yAxisRenderer={(v: number) => `${(v * 100).toFixed(0)}%`} |
| 68 | + yAxisLabel="Pass Rate" |
| 69 | + additionalOptions={{ |
| 70 | + yAxis: { min: 0, max: 1 }, |
| 71 | + }} |
| 72 | + useUTC |
| 73 | + /> |
| 74 | + </Box> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +function FailuresChart({ |
| 79 | + data, |
| 80 | + days, |
| 81 | +}: { |
| 82 | + data: SuccessRateRow[]; |
| 83 | + days: number; |
| 84 | +}) { |
| 85 | + const startTime = dayjs.utc().subtract(days, "day").startOf("day"); |
| 86 | + const stopTime = dayjs.utc().endOf("day"); |
| 87 | + |
| 88 | + const series = seriesWithInterpolatedTimes( |
| 89 | + data, |
| 90 | + startTime, |
| 91 | + stopTime, |
| 92 | + "day", |
| 93 | + "repo", |
| 94 | + "day", |
| 95 | + "failures", |
| 96 | + true, |
| 97 | + false, |
| 98 | + "name", |
| 99 | + "stacked_bar" |
| 100 | + ); |
| 101 | + |
| 102 | + return ( |
| 103 | + <Box sx={{ height: 420 }}> |
| 104 | + <TimeSeriesPanelWithData |
| 105 | + data={data} |
| 106 | + series={series} |
| 107 | + title="Failures Over Time" |
| 108 | + groupByFieldName="repo" |
| 109 | + yAxisRenderer={(v: number) => String(Math.round(v))} |
| 110 | + yAxisLabel="Failures" |
| 111 | + useUTC |
| 112 | + /> |
| 113 | + </Box> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +function TotalRunsChart({ |
| 118 | + data, |
| 119 | + days, |
| 120 | +}: { |
| 121 | + data: SuccessRateRow[]; |
| 122 | + days: number; |
| 123 | +}) { |
| 124 | + const startTime = dayjs.utc().subtract(days, "day").startOf("day"); |
| 125 | + const stopTime = dayjs.utc().endOf("day"); |
| 126 | + |
| 127 | + const series = seriesWithInterpolatedTimes( |
| 128 | + data, |
| 129 | + startTime, |
| 130 | + stopTime, |
| 131 | + "day", |
| 132 | + "repo", |
| 133 | + "day", |
| 134 | + "total", |
| 135 | + true, |
| 136 | + false, |
| 137 | + "name", |
| 138 | + "stacked_bar" |
| 139 | + ); |
| 140 | + |
| 141 | + return ( |
| 142 | + <Box sx={{ height: 420 }}> |
| 143 | + <TimeSeriesPanelWithData |
| 144 | + data={data} |
| 145 | + series={series} |
| 146 | + title="Total Runs Over Time" |
| 147 | + groupByFieldName="repo" |
| 148 | + yAxisRenderer={(v: number) => String(Math.round(v))} |
| 149 | + yAxisLabel="Total Runs" |
| 150 | + useUTC |
| 151 | + /> |
| 152 | + </Box> |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +export default function CrcrMetricsPage() { |
| 157 | + const [days, setDays] = useState(30); |
| 158 | + |
| 159 | + const url = `/api/clickhouse/crcr_success_rate?parameters=${encodeURIComponent( |
| 160 | + JSON.stringify({ days: String(days) }) |
| 161 | + )}`; |
| 162 | + const { data, error } = useSWR<SuccessRateRow[]>(url, fetcher, { |
| 163 | + refreshInterval: 5 * 60_000, |
| 164 | + }); |
| 165 | + |
| 166 | + const repos = useMemo(() => { |
| 167 | + if (!data) return []; |
| 168 | + return [...new Set(data.map((r) => r.repo))].sort(); |
| 169 | + }, [data]); |
| 170 | + |
| 171 | + const isLoading = !data && !error; |
| 172 | + |
| 173 | + return ( |
| 174 | + <> |
| 175 | + <Head> |
| 176 | + <title>CRCR Metrics | PyTorch HUD</title> |
| 177 | + </Head> |
| 178 | + <Stack spacing={3} sx={{ p: 3, maxWidth: 1400, mx: "auto" }}> |
| 179 | + <Box display="flex" justifyContent="space-between" alignItems="center"> |
| 180 | + <Typography variant="h4">CRCR Metrics</Typography> |
| 181 | + <FormControl size="small" sx={{ minWidth: 140 }}> |
| 182 | + <InputLabel>Time Range</InputLabel> |
| 183 | + <Select |
| 184 | + value={days} |
| 185 | + label="Time Range" |
| 186 | + onChange={(e: SelectChangeEvent<number>) => |
| 187 | + setDays(Number(e.target.value)) |
| 188 | + } |
| 189 | + > |
| 190 | + <MenuItem value={7}>Last 7 days</MenuItem> |
| 191 | + <MenuItem value={14}>Last 14 days</MenuItem> |
| 192 | + <MenuItem value={30}>Last 30 days</MenuItem> |
| 193 | + <MenuItem value={90}>Last 90 days</MenuItem> |
| 194 | + </Select> |
| 195 | + </FormControl> |
| 196 | + </Box> |
| 197 | + |
| 198 | + <Typography variant="body2" color="text.secondary"> |
| 199 | + Success rate and failure trends for all CRCR-registered downstream |
| 200 | + repos.{" "} |
| 201 | + <NextLink href="/crcr" passHref legacyBehavior> |
| 202 | + <Link underline="hover">Back to CRCR Summary</Link> |
| 203 | + </NextLink> |
| 204 | + </Typography> |
| 205 | + |
| 206 | + {error && ( |
| 207 | + <Typography color="error"> |
| 208 | + {error.message || "Failed to load metrics data"} |
| 209 | + </Typography> |
| 210 | + )} |
| 211 | + |
| 212 | + {isLoading && ( |
| 213 | + <Stack spacing={2}> |
| 214 | + <Skeleton variant="rectangular" height={420} /> |
| 215 | + <Skeleton variant="rectangular" height={420} /> |
| 216 | + </Stack> |
| 217 | + )} |
| 218 | + |
| 219 | + {data && ( |
| 220 | + <Stack spacing={3}> |
| 221 | + <PassRateChart data={data} days={days} /> |
| 222 | + <FailuresChart data={data} days={days} /> |
| 223 | + <TotalRunsChart data={data} days={days} /> |
| 224 | + </Stack> |
| 225 | + )} |
| 226 | + |
| 227 | + {data && repos.length > 0 && ( |
| 228 | + <Box> |
| 229 | + <Typography variant="subtitle2" color="text.secondary" gutterBottom> |
| 230 | + Repos in view ({repos.length}): |
| 231 | + </Typography> |
| 232 | + <Box sx={{ display: "flex", gap: 1, flexWrap: "wrap" }}> |
| 233 | + {repos.map((repo) => ( |
| 234 | + <NextLink |
| 235 | + key={repo} |
| 236 | + href={`/crcr/${repo}`} |
| 237 | + passHref |
| 238 | + legacyBehavior |
| 239 | + > |
| 240 | + <Link |
| 241 | + underline="hover" |
| 242 | + sx={{ |
| 243 | + fontSize: "0.85rem", |
| 244 | + px: 1, |
| 245 | + py: 0.25, |
| 246 | + bgcolor: "action.hover", |
| 247 | + borderRadius: 1, |
| 248 | + }} |
| 249 | + > |
| 250 | + {repo} |
| 251 | + </Link> |
| 252 | + </NextLink> |
| 253 | + ))} |
| 254 | + </Box> |
| 255 | + </Box> |
| 256 | + )} |
| 257 | + </Stack> |
| 258 | + </> |
| 259 | + ); |
| 260 | +} |
0 commit comments