import { FC, useEffect, useId } from "react"; import { Table, Alert, Skeleton, Box, LoadingOverlay } from "@mantine/core"; import { IconAlertTriangle, IconInfoCircle } from "@tabler/icons-react"; import { InstantQueryResult, InstantSample, RangeSamples, } from "../../api/responseTypes/query"; import SeriesName from "./SeriesName"; import { useAPIQuery } from "../../api/api"; import classes from "./DataTable.module.css"; import { GraphDisplayMode } from "../../state/queryPageSlice"; const maxFormattableSeries = 1000; const maxDisplayableSeries = 10000; const limitSeries = ( series: S[] ): S[] => { if (series.length > maxDisplayableSeries) { return series.slice(0, maxDisplayableSeries); } return series; }; export interface GraphProps { expr: string; endTime: number | null; range: number; resolution: number | null; showExemplars: boolean; displayMode: GraphDisplayMode; retriggerIdx: number; } const Graph: FC = ({ expr, endTime, range, resolution, retriggerIdx, }) => { const realEndTime = (endTime !== null ? endTime : Date.now()) / 1000; const { data, error, isFetching, isLoading, refetch } = useAPIQuery({ key: useId(), path: "/query_range", params: { query: expr, step: ( resolution || Math.max(Math.floor(range / 250000), 1) ).toString(), start: (realEndTime - range / 1000).toString(), end: realEndTime.toString(), }, enabled: expr !== "", }); useEffect(() => { expr !== "" && refetch(); }, [retriggerIdx, refetch, expr, endTime, range, resolution]); // TODO: Share all the loading/error/empty data notices with the DataTable. // Show a skeleton only on the first load, not on subsequent ones. if (isLoading) { return ( {Array.from(Array(5), (_, i) => ( ))} ); } if (error) { return ( } > {error.message} ); } if (data === undefined) { return No data queried yet; } const { result, resultType } = data.data; if (result.length === 0) { return ( }> This query returned no data. ); } const doFormat = result.length <= maxFormattableSeries; return ( , }} styles={{ loader: { width: "100%", height: "100%" } }} /> {resultType === "vector" ? ( limitSeries(result).map((s, idx) => ( {s.value && s.value[1]} {s.histogram && "TODO HISTOGRAM DISPLAY"} )) ) : resultType === "matrix" ? ( limitSeries(result).map((s, idx) => ( {s.values && s.values.map((v, idx) => (
{v[1]} @ {v[0]}
))}
)) ) : resultType === "scalar" ? ( Scalar value {result[1]} ) : resultType === "string" ? ( String value {result[1]} ) : ( } > Invalid result value type )}
); }; export default Graph;