🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(web): improve chart loading and failure hints (#12180)
Max Deichmannbd4cbf470b8a
diff --git a/packages/shared/src/errors/errorMessages.ts b/packages/shared/src/errors/errorMessages.tsindex 392eb58c59..34ee67f7ea 100644--- a/packages/shared/src/errors/errorMessages.ts+++ b/packages/shared/src/errors/errorMessages.ts@@ -7,6 +7,12 @@* Used when queries require too many resources to complete*/export const RESOURCE_LIMIT_ERROR_MESSAGE = [- "Your query could not be completed because it required too many resources.",+ "Your query could not be completed.","Please narrow your request by adding more specific filters (e.g., a shorter date range).",].join(" ");++/**+ * Hint shown for slow-running chart queries after a short delay+ */+export const SLOW_QUERY_HINT_TEXT =+ "This query is taking longer than usual. Try reducing the time frame or adding more filters.";diff --git a/web/src/__tests__/chart-loading-state-utils.clienttest.ts b/web/src/__tests__/chart-loading-state-utils.clienttest.tsnew file mode 100644index 0000000000..d8f3da07f7--- /dev/null+++ b/web/src/__tests__/chart-loading-state-utils.clienttest.ts@@ -0,0 +1,46 @@+import { RESOURCE_LIMIT_ERROR_MESSAGE } from "@langfuse/shared";+import { getChartLoadingStateProps } from "@/src/features/widgets/chart-library/chartLoadingStateUtils";++describe("getChartLoadingStateProps", () => {+ test("returns spinner-only loading props while query is pending", () => {+ expect(+ getChartLoadingStateProps({+ isPending: true,+ isError: false,+ }),+ ).toEqual({+ isLoading: true,+ showSpinner: true,+ showHintImmediately: false,+ hintText: undefined,+ });+ });++ test("returns immediate resource-limit hint when query failed", () => {+ expect(+ getChartLoadingStateProps({+ isPending: false,+ isError: true,+ }),+ ).toEqual({+ isLoading: true,+ showSpinner: false,+ showHintImmediately: true,+ hintText: RESOURCE_LIMIT_ERROR_MESSAGE,+ });+ });++ test("returns hidden state once query has no pending/error state", () => {+ expect(+ getChartLoadingStateProps({+ isPending: false,+ isError: false,+ }),+ ).toEqual({+ isLoading: false,+ showSpinner: false,+ showHintImmediately: false,+ hintText: undefined,+ });+ });+});diff --git a/web/src/__tests__/chart-loading-state.clienttest.tsx b/web/src/__tests__/chart-loading-state.clienttest.tsxnew file mode 100644index 0000000000..cab65237c6--- /dev/null+++ b/web/src/__tests__/chart-loading-state.clienttest.tsx@@ -0,0 +1,90 @@+import React from "react";+import { act, render, screen } from "@testing-library/react";+import "@testing-library/jest-dom";+import { SLOW_QUERY_HINT_TEXT } from "@langfuse/shared";+import { ChartLoadingState } from "@/src/features/widgets/chart-library/ChartLoadingState";++describe("ChartLoadingState", () => {+ beforeEach(() => {+ jest.useFakeTimers();+ });++ afterEach(() => {+ jest.useRealTimers();+ });++ test("renders spinner immediately and delayed hint after configured delay", () => {+ const hintDelayMs = 1375;++ render(<ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />);++ expect(+ screen.getByRole("status", { name: "Loading chart data" }),+ ).toBeInTheDocument();+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();++ act(() => {+ jest.advanceTimersByTime(hintDelayMs - 1);+ });+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();++ act(() => {+ jest.advanceTimersByTime(1);+ });+ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();+ });++ test("resets delayed hint when loading toggles off and on again", () => {+ const hintDelayMs = 825;++ const { rerender } = render(+ <ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />,+ );++ act(() => {+ jest.advanceTimersByTime(hintDelayMs);+ });+ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();++ rerender(<ChartLoadingState isLoading={false} hintDelayMs={hintDelayMs} />);+ expect(screen.queryByRole("status")).not.toBeInTheDocument();+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();++ rerender(<ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />);+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();++ act(() => {+ jest.advanceTimersByTime(hintDelayMs);+ });+ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();+ });++ test("shows hint immediately without spinner for overload state", () => {+ const { container } = render(+ <ChartLoadingState+ isLoading={true}+ showSpinner={false}+ showHintImmediately={true}+ />,+ );++ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();+ expect(container.querySelector("svg")).not.toBeInTheDocument();+ });++ test("renders a provided hint text for error states", () => {+ const customHint = "Custom resource limit hint";++ render(+ <ChartLoadingState+ isLoading={true}+ showHintImmediately={true}+ showSpinner={false}+ hintText={customHint}+ />,+ );++ expect(screen.getByText(customHint)).toBeInTheDocument();+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();+ });+});diff --git a/web/src/features/widgets/chart-library/ChartLoadingState.tsx b/web/src/features/widgets/chart-library/ChartLoadingState.tsxnew file mode 100644index 0000000000..af41e462ef--- /dev/null+++ b/web/src/features/widgets/chart-library/ChartLoadingState.tsx@@ -0,0 +1,83 @@+import { useEffect, useState } from "react";+import { Loader2 } from "lucide-react";+import { cn } from "@/src/utils/tailwind";+import { SLOW_QUERY_HINT_TEXT } from "@langfuse/shared";++const DEFAULT_HINT_DELAY_MS = 2000;++type ChartLoadingStateProps = {+ isLoading: boolean;+ className?: string;+ spinnerClassName?: string;+ hintClassName?: string;+ spinnerLabel?: string;+ hintText?: string;+ hintDelayMs?: number;+ showSpinner?: boolean;+ showHintImmediately?: boolean;+};++export function ChartLoadingState({+ isLoading,+ className,+ spinnerClassName,+ hintClassName,+ spinnerLabel = "Loading chart data",+ hintText = SLOW_QUERY_HINT_TEXT,+ hintDelayMs = DEFAULT_HINT_DELAY_MS,+ showSpinner = true,+ showHintImmediately = false,+}: ChartLoadingStateProps) {+ const [showHint, setShowHint] = useState(false);++ useEffect(() => {+ if (!isLoading) {+ setShowHint(false);+ return;+ }++ const timeoutId = window.setTimeout(() => {+ setShowHint(true);+ }, hintDelayMs);++ return () => {+ window.clearTimeout(timeoutId);+ };+ }, [hintDelayMs, isLoading]);++ if (!isLoading) {+ return null;+ }++ const shouldShowHint = showHintImmediately || showHint;++ return (+ <div+ role="status"+ aria-live="polite"+ aria-label={spinnerLabel}+ className={cn(+ "flex flex-col items-center justify-center gap-2 text-muted-foreground",+ className,+ )}+ >+ <div className="flex h-4 w-4 items-center justify-center">+ {showSpinner ? (+ <Loader2 className={cn("h-4 w-4 animate-spin", spinnerClassName)} />+ ) : (+ <span className="h-4 w-4" aria-hidden="true" />+ )}+ </div>+ {shouldShowHint ? (+ <p+ className={cn(+ "max-w-xs text-center text-xs duration-300 animate-in fade-in-0",+ hintClassName,+ )}+ >+ {hintText}+ </p>+ ) : null}+ </div>+ );+}diff --git a/web/src/features/widgets/chart-library/PivotTable.tsx b/web/src/features/widgets/chart-library/PivotTable.tsxindex 535960553a..987c3002c5 100644--- a/web/src/features/widgets/chart-library/PivotTable.tsx+++ b/web/src/features/widgets/chart-library/PivotTable.tsx@@ -44,7 +44,7 @@ import { type ChartProps } from "@/src/features/widgets/chart-library/chart-propimport { numberFormatter } from "@/src/utils/numbers";import { formatMetricName } from "@/src/features/widgets/utils";import { type OrderByState } from "@langfuse/shared";-import { Loader2 } from "lucide-react";+import { ChartLoadingState } from "@/src/features/widgets/chart-library/ChartLoadingState";/*** Props interface for the PivotTable component@@ -381,12 +381,11 @@ export const PivotTable: React.FC<PivotTableProps> = ({return (<div className="relative h-full overflow-auto px-5 pb-2">{isLoading && (- <div className="absolute inset-0 z-10 flex items-center justify-center bg-background/80 backdrop-blur-sm">- <div className="flex items-center gap-2 text-sm text-muted-foreground">- <Loader2 className="h-4 w-4 animate-spin" />- <span>Refreshing data...</span>- </div>- </div>+ <ChartLoadingState+ isLoading={isLoading}+ className="absolute inset-0 z-10 bg-background/80 backdrop-blur-sm"+ hintClassName="max-w-sm px-4"+ />)}<Table><TableHeader className="sticky top-0 z-10">diff --git a/web/src/features/widgets/chart-library/chartLoadingStateUtils.ts b/web/src/features/widgets/chart-library/chartLoadingStateUtils.tsnew file mode 100644index 0000000000..53dc4042b7--- /dev/null+++ b/web/src/features/widgets/chart-library/chartLoadingStateUtils.ts@@ -0,0 +1,25 @@+import { RESOURCE_LIMIT_ERROR_MESSAGE } from "@langfuse/shared";++type ChartQueryState = {+ isPending: boolean;+ isError: boolean;+};++type ChartLoadingProps = {+ isLoading: boolean;+ showSpinner: boolean;+ showHintImmediately: boolean;+ hintText?: string;+};++export function getChartLoadingStateProps({+ isPending,+ isError,+}: ChartQueryState): ChartLoadingProps {+ return {+ isLoading: isPending || isError,+ showSpinner: isPending,+ showHintImmediately: isError,+ hintText: isError ? RESOURCE_LIMIT_ERROR_MESSAGE : undefined,+ };+}diff --git a/web/src/features/widgets/components/DashboardWidget.tsx b/web/src/features/widgets/components/DashboardWidget.tsxindex 9e54ff278c..d6e3ce5895 100644--- a/web/src/features/widgets/components/DashboardWidget.tsx+++ b/web/src/features/widgets/components/DashboardWidget.tsx@@ -14,13 +14,14 @@ import {TrashIcon,CopyIcon,GripVerticalIcon,- Loader2,} from "lucide-react";import { useRouter } from "next/router";import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";import { showErrorToast } from "@/src/features/notifications/showErrorToast";import { DownloadButton } from "@/src/features/widgets/chart-library/DownloadButton";import { formatMetricName } from "@/src/features/widgets/utils";+import { ChartLoadingState } from "@/src/features/widgets/chart-library/ChartLoadingState";+import { getChartLoadingStateProps } from "@/src/features/widgets/chart-library/chartLoadingStateUtils";import { useV4Beta } from "@/src/features/events/hooks/useV4Beta";import { type ViewVersion } from "@/src/features/query";@@ -139,10 +140,18 @@ export function DashboardWidget({skipBatch: true,},},+ meta: {+ silentHttpCodes: [422],+ },enabled: !widget.isPending && Boolean(widget.data),},);+ const chartLoadingState = getChartLoadingStateProps({+ isPending: queryResult.isPending,+ isError: queryResult.isError,+ });+const transformedData = useMemo(() => {if (!widget.data || !queryResult.data) {return [];@@ -292,22 +301,14 @@ export function DashboardWidget({</button></>)}- {/* Download button or loading indicator - always available */}- {queryResult.isPending ? (- <div- className="text-muted-foreground"- aria-label="Loading chart data"- title="Loading..."- >- <Loader2 size={16} className="animate-spin" />- </div>- ) : (+ {/* Download button is available once chart data has loaded */}+ {!queryResult.isPending ? (<DownloadButtondata={transformedData}fileName={widget.data.name}className="hidden group-hover:block"/>- )}+ ) : null}</div></div><div@@ -316,7 +317,7 @@ export function DashboardWidget({>{widget.data.description}</div>- <div className="min-h-0 flex-1">+ <div className="relative min-h-0 flex-1"><ChartchartType={widget.data.chartType}data={transformedData}@@ -345,6 +346,14 @@ export function DashboardWidget({}isLoading={queryResult.isPending}/>+ <ChartLoadingState+ isLoading={chartLoadingState.isLoading}+ showSpinner={chartLoadingState.showSpinner}+ showHintImmediately={chartLoadingState.showHintImmediately}+ hintText={chartLoadingState.hintText}+ className="absolute inset-0 z-20 bg-background/80 backdrop-blur-sm"+ hintClassName="max-w-sm px-4"+ /></div></div>);diff --git a/web/src/features/widgets/components/WidgetForm.tsx b/web/src/features/widgets/components/WidgetForm.tsxindex c22674ba68..3bc8e2f172 100644--- a/web/src/features/widgets/components/WidgetForm.tsx+++ b/web/src/features/widgets/components/WidgetForm.tsx@@ -64,6 +64,8 @@ import {MAX_PIVOT_TABLE_DIMENSIONS,MAX_PIVOT_TABLE_METRICS,} from "@/src/features/widgets/utils/pivot-table-utils";+import { ChartLoadingState } from "@/src/features/widgets/chart-library/ChartLoadingState";+import { getChartLoadingStateProps } from "@/src/features/widgets/chart-library/chartLoadingStateUtils";type ChartType = {group: "time-series" | "total-value";@@ -881,9 +883,17 @@ export function WidgetForm({skipBatch: true,},},+ meta: {+ silentHttpCodes: [422],+ },},);+ const chartLoadingState = getChartLoadingStateProps({+ isPending: queryResult.isPending,+ isError: queryResult.isError,+ });+// Transform the query results to a consistent format for chartsconst transformedData: DataPoint[] = useMemo(() =>@@ -1743,7 +1753,7 @@ export function WidgetForm({</div>{/* Right column - Chart */}<div className="w-2/3">- <Card className={"aspect-video"}>+ <Card className="flex aspect-video flex-col"><CardHeader><CardTitle className="truncate" title={widgetName}>{widgetName}@@ -1753,48 +1763,69 @@ export function WidgetForm({</CardDescription></CardHeader>{queryResult.data ? (- <Chart- chartType={selectedChartType as DashboardWidgetChartType}- data={transformedData}- rowLimit={rowLimit}- chartConfig={- selectedChartType === "PIVOT_TABLE"- ? {- type: selectedChartType as DashboardWidgetChartType,- dimensions: pivotDimensions,- row_limit: rowLimit,- metrics: selectedMetrics.map((metric) => metric.id), // Pass metric field names- defaultSort:- defaultSortColumn && defaultSortColumn !== "none"- ? {- column: defaultSortColumn,- order: defaultSortOrder,- }- : undefined,- }- : selectedChartType === "HISTOGRAM"+ <div className="relative min-h-0 flex-1">+ <Chart+ chartType={selectedChartType as DashboardWidgetChartType}+ data={transformedData}+ rowLimit={rowLimit}+ chartConfig={+ selectedChartType === "PIVOT_TABLE"? {type: selectedChartType as DashboardWidgetChartType,- bins: histogramBins,- }- : {- type: selectedChartType as DashboardWidgetChartType,+ dimensions: pivotDimensions,row_limit: rowLimit,+ metrics: selectedMetrics.map((metric) => metric.id), // Pass metric field names+ defaultSort:+ defaultSortColumn && defaultSortColumn !== "none"+ ? {+ column: defaultSortColumn,+ order: defaultSortOrder,+ }+ : undefined,}- }- sortState={- selectedChartType === "PIVOT_TABLE"- ? previewSortState- : undefined- }- onSortChange={undefined}- />+ : selectedChartType === "HISTOGRAM"+ ? {+ type: selectedChartType as DashboardWidgetChartType,+ bins: histogramBins,+ }+ : {+ type: selectedChartType as DashboardWidgetChartType,+ row_limit: rowLimit,+ }+ }+ sortState={+ selectedChartType === "PIVOT_TABLE"+ ? previewSortState+ : undefined+ }+ onSortChange={undefined}+ isLoading={queryResult.isPending}+ />+ <ChartLoadingState+ isLoading={chartLoadingState.isLoading}+ showSpinner={chartLoadingState.showSpinner}+ showHintImmediately={chartLoadingState.showHintImmediately}+ hintText={chartLoadingState.hintText}+ className="absolute inset-0 z-20 bg-background/80 backdrop-blur-sm"+ hintClassName="max-w-sm px-4"+ />+ </div>) : (<CardContent><div className="flex h-[300px] items-center justify-center">- <p className="text-muted-foreground">- Waiting for Input / Loading...- </p>+ {chartLoadingState.isLoading ? (+ <ChartLoadingState+ isLoading={chartLoadingState.isLoading}+ showSpinner={chartLoadingState.showSpinner}+ showHintImmediately={chartLoadingState.showHintImmediately}+ hintText={chartLoadingState.hintText}+ hintClassName="max-w-sm px-4"+ />+ ) : (+ <p className="text-muted-foreground">+ Waiting for Input / Loading...+ </p>+ )}</div></CardContent>)}