Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(monitors): add in-progress leading window to live preview (#14251)

Mark Salpeterf482f3c7fef1
diff --git a/packages/shared/src/features/monitors/processor/processor.ts b/packages/shared/src/features/monitors/processor/processor.ts
index 0a3c4d3be9..ee3f8d9256 100644
--- a/packages/shared/src/features/monitors/processor/processor.ts
+++ b/packages/shared/src/features/monitors/processor/processor.ts
@@ -24,6 +24,7 @@ import {
} from "../scheduler/types";
import { monitorFromPrisma, windowToMs } from "../service/helpers";
import {
+ monitorEvaluationOffsetMs,
MonitorSeveritySchema,
MonitorStatusSchema,
type MonitorAlert,
@@ -34,8 +35,7 @@ import { applyStateMachine, type MonitorCompletion } from "./applyStateMachine";
import { computeSeverity } from "./computeSeverity";
import { renderAlertMessage } from "./renderAlertMessage";
-/** monitorEvaluationOffsetMs shifts the query window back so ClickHouse reads data settled past the events-table write lag. */
-export const monitorEvaluationOffsetMs = 30 * 1000;
+export { monitorEvaluationOffsetMs } from "../types";
/** ErrorBadQuery sentinels a metric whose shape doesn't resolve against the v2 data model or whose batch query failed to execute. */
export const ErrorBadQuery = Symbol("ErrorBadQuery");
diff --git a/packages/shared/src/features/monitors/types.ts b/packages/shared/src/features/monitors/types.ts
index f88b460f41..0f90a17c50 100644
--- a/packages/shared/src/features/monitors/types.ts
+++ b/packages/shared/src/features/monitors/types.ts
@@ -14,6 +14,9 @@ import { granularities, metric as MetricSchema, viewsV2 } from "../query/types";
import { isValidQuery } from "./isValidQuery";
import { isValidThresholdOrder } from "./isValidThresholdOrder";
+/** monitorEvaluationOffsetMs shifts the query window back so ClickHouse reads settled data past the events-table write lag. */
+export const monitorEvaluationOffsetMs = 30 * 1000;
+
/** ErrorNameRequired is the message emitted when the Monitor name is missing or empty. */
export const ErrorNameRequired = "Name is a required field";
diff --git a/web/src/features/monitors/components/MonitorChartPreview.clienttest.ts b/web/src/features/monitors/components/MonitorChartPreview.clienttest.ts
new file mode 100644
index 0000000000..64c612da42
--- /dev/null
+++ b/web/src/features/monitors/components/MonitorChartPreview.clienttest.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ monitorEvaluationOffsetMs,
+ windowToMs,
+} from "@langfuse/shared/monitors";
+
+import { __test } from "./MonitorChartPreview";
+
+const { leadingWindowRange, toLeadingPoint } = __test;
+
+describe("leadingWindowRange", () => {
+ it("returns [now−offset−windowMs, now−offset] for a 5m window", () => {
+ const now = 1_700_000_000_000;
+ const windowMs = Number(windowToMs("5m"));
+ const result = leadingWindowRange("5m", now);
+ expect(result.toTimestamp).toBe(
+ new Date(now - monitorEvaluationOffsetMs).toISOString(),
+ );
+ expect(result.fromTimestamp).toBe(
+ new Date(now - monitorEvaluationOffsetMs - windowMs).toISOString(),
+ );
+ });
+
+ it("returns [now−offset−windowMs, now−offset] for a 1h window", () => {
+ const now = 1_700_000_000_000;
+ const windowMs = Number(windowToMs("1h"));
+ const result = leadingWindowRange("1h", now);
+ expect(result.toTimestamp).toBe(
+ new Date(now - monitorEvaluationOffsetMs).toISOString(),
+ );
+ expect(result.fromTimestamp).toBe(
+ new Date(now - monitorEvaluationOffsetMs - windowMs).toISOString(),
+ );
+ });
+});
+
+describe("toLeadingPoint", () => {
+ it("pins the scalar value to the provided toTimestamp", () => {
+ const ts = "2024-01-15T12:00:00.000Z";
+ const row = { count_count: "42" };
+ const pt = toLeadingPoint(row, ts, "count", "count");
+ expect(pt.time_dimension).toBe(ts);
+ expect(pt.dimension).toBe("metric");
+ expect(pt.metric).toBe(42);
+ });
+
+ it("coerces missing metric to 0", () => {
+ const ts = "2024-01-15T12:00:00.000Z";
+ const pt = toLeadingPoint({}, ts, "count", "count");
+ expect(pt.metric).toBe(0);
+ });
+});
diff --git a/web/src/features/monitors/components/MonitorChartPreview.tsx b/web/src/features/monitors/components/MonitorChartPreview.tsx
index d7a535280c..19c40008cb 100644
--- a/web/src/features/monitors/components/MonitorChartPreview.tsx
+++ b/web/src/features/monitors/components/MonitorChartPreview.tsx
@@ -13,6 +13,7 @@ import {
RESOURCE_LIMIT_ERROR_MESSAGE,
} from "@langfuse/shared";
import {
+ monitorEvaluationOffsetMs,
type MonitorThresholdOperator,
type MonitorView,
type MonitorWindow,
@@ -46,7 +47,7 @@ export const MonitorChartPreview = ({
alertThreshold: number | null | undefined;
warningThreshold: number | null | undefined;
}) => {
- /** fromTimestamp and toTimestamp span 20 complete window buckets ending at the last floored boundary. */
+ /** bucketRange spans 20 complete window buckets ending at the last floored boundary. */
const { fromTimestamp, toTimestamp } = useMemo(() => {
const ms = Number(windowToMs(window));
const to = Math.floor(Date.now() / ms) * ms;
@@ -57,7 +58,13 @@ export const MonitorChartPreview = ({
};
}, [window]);
- /** queryResult runs the preview query for the draft monitor. */
+ /** leadingRange is the [now−offset−windowMs, now−offset] scalar query range matching the processor's evaluationWindow. */
+ const leadingRange = useMemo(
+ () => leadingWindowRange(window, Date.now()),
+ [window],
+ );
+
+ /** queryResult runs the 20-bucket time-series preview query. */
const queryResult = api.dashboard.executeQuery.useQuery(
{
projectId,
@@ -81,12 +88,40 @@ export const MonitorChartPreview = ({
},
);
- /** data reshapes the query rows into chart points. */
- const data: DataPoint[] = useMemo(
- () => toChartPoints(queryResult.data ?? [], measure, aggregation),
- [queryResult.data, measure, aggregation],
+ /** scalarResult runs the full-window scalar query for the leading point. */
+ const scalarResult = api.dashboard.executeQuery.useQuery(
+ {
+ projectId,
+ version: "v2",
+ query: {
+ view,
+ dimensions: [],
+ metrics: [{ measure, aggregation }],
+ filters,
+ timeDimension: null,
+ fromTimestamp: leadingRange.fromTimestamp,
+ toTimestamp: leadingRange.toTimestamp,
+ orderBy: null,
+ chartConfig: { type: "LINE_TIME_SERIES" },
+ },
+ },
+ {
+ trpc: { context: { skipBatch: true } },
+ meta: { silentHttpCodes: [422] },
+ refetchOnWindowFocus: false,
+ },
);
+ /** data merges the 20-bucket series with an optional leading point. */
+ const data: DataPoint[] = useMemo(() => {
+ const points = toChartPoints(queryResult.data ?? [], measure, aggregation);
+ const scalarRow = scalarResult.data?.[0];
+ if (scalarRow !== undefined) {
+ points.push(toLeadingPoint(scalarRow, toTimestamp, measure, aggregation));
+ }
+ return points;
+ }, [queryResult.data, scalarResult.data, toTimestamp, measure, aggregation]);
+
/** thresholds lists the warning and alert bands to draw on the chart. */
const thresholds = useMemo(() => {
const ordered = [];
@@ -177,3 +212,35 @@ const toChartPoints = (
} satisfies DataPoint;
});
};
+
+/** leadingWindowRange returns the [now−offset−windowMs, now−offset] range for the scalar leading-point query. */
+const leadingWindowRange = (
+ window: MonitorWindow,
+ now: number,
+): { fromTimestamp: string; toTimestamp: string } => {
+ const windowMs = Number(windowToMs(window));
+ const to = now - monitorEvaluationOffsetMs;
+ const from = to - windowMs;
+ return {
+ toTimestamp: new Date(to).toISOString(),
+ fromTimestamp: new Date(from).toISOString(),
+ };
+};
+
+/** toLeadingPoint converts a scalar executeQuery row into a DataPoint pinned to toTimestamp. */
+const toLeadingPoint = (
+ row: Record<string, unknown>,
+ toTimestamp: string,
+ measure: string,
+ aggregation: z.infer<typeof metricAggregations>,
+): DataPoint => {
+ const metricField = `${aggregation}_${measure}`;
+ const metric = row[metricField];
+ return {
+ time_dimension: toTimestamp,
+ dimension: "metric",
+ metric: Array.isArray(metric) ? metric : Number(metric || 0),
+ } satisfies DataPoint;
+};
+
+export const __test = { leadingWindowRange, toLeadingPoint };