Skip to content

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

5.0 KB
Raw Blame
/* eslint-disable @repo/no-style-props */
import { useMemo } from "react";
import { DashboardCard } from "@/src/features/dashboard/components/cards/DashboardCard";
import { type ScoreDataTypeType, type FilterState } from "@langfuse/shared";
import {
  extractTimeSeriesData,
  fillMissingValuesAndTransform,
  isEmptyTimeSeries,
} from "@/src/features/dashboard/components/hooks";
import {
  type DashboardDateRangeAggregationOption,
  dashboardDateRangeAggregationSettings,
} from "@/src/utils/date-range-utils";
import { getScoreDataTypeIcon } from "@/src/features/scores/lib/scoreColumns";
import { NoDataOrLoading } from "@/src/components/NoDataOrLoading";
import { type QueryType, type ViewVersion } from "@langfuse/shared/query";
import { mapLegacyUiTableFilterToView } from "@/src/features/dashboard/lib/dashboardUiTableToViewMapping";
import { type DatabaseRow } from "@/src/server/api/services/sqlInterface";
import { Chart } from "@/src/features/widgets/chart-library/Chart";
import { timeSeriesToDataPoints } from "@/src/features/dashboard/lib/chart-data-adapters";
import { useScheduledDashboardExecuteQuery } from "@/src/features/dashboard/hooks/DashboardQuerySchedulerProvider";

// Static — hoisted so its reference is stable across re-renders (keeps the
// memoized <Chart> from reconciling on dashboard scheduler re-renders).
const SCORES_CHART_CONFIG = {
  type: "LINE_TIME_SERIES",
  show_data_point_dots: false,
} as const;

export function ChartScores(props: {
  className?: string;
  agg: DashboardDateRangeAggregationOption;
  globalFilterState: FilterState;
  fromTimestamp: Date;
  toTimestamp: Date;
  projectId: string;
  isLoading?: boolean;
  metricsVersion: ViewVersion;
  schedulerId?: string;
  syncId?: string;
  sync?: {
    activeKey: string | undefined;
    onActiveKeyChange: (key: string | undefined) => void;
  };
}) {
  const scoresQuery: QueryType = {
    view: "scores-numeric",
    dimensions: [{ field: "name" }, { field: "dataType" }, { field: "source" }],
    metrics: [{ measure: "value", aggregation: "avg" }],
    filters: mapLegacyUiTableFilterToView(
      "scores-numeric",
      props.globalFilterState,
    ),
    timeDimension: {
      granularity:
        dashboardDateRangeAggregationSettings[props.agg].dateTrunc ?? "day",
    },
    fromTimestamp: props.fromTimestamp.toISOString(),
    toTimestamp: props.toTimestamp.toISOString(),
    orderBy: null,
  };

  const scores = useScheduledDashboardExecuteQuery(
    {
      projectId: props.projectId,
      query: scoresQuery,
      version: props.metricsVersion,
    },
    {
      trpc: {
        context: {
          skipBatch: true,
        },
      },
      queryId: `${props.schedulerId ?? "home:chart-scores"}:scores`,
      enabled: !props.isLoading,
    },
  );

  // Memoize the transform on the (scheduler-stable) query result so the chart's
  // data prop keeps a stable reference across dashboard re-renders. (LFE-10549)
  const extractedScores = useMemo(
    () =>
      scores.data
        ? fillMissingValuesAndTransform(
            extractTimeSeriesData(
              scores.data as DatabaseRow[],
              "time_dimension",
              [
                {
                  uniqueIdentifierColumns: [
                    {
                      accessor: "data_type",
                      formatFct: (value) =>
                        getScoreDataTypeIcon(value as ScoreDataTypeType),
                    },
                    { accessor: "name" },
                    {
                      accessor: "source",
                      formatFct: (value) => `(${value.toLowerCase()})`,
                    },
                  ],
                  valueColumn: "avg_value",
                },
              ],
            ),
          )
        : [],
    [scores.data],
  );

  const chartData = useMemo(
    () => timeSeriesToDataPoints(extractedScores),
    [extractedScores],
  );

  return (
    <DashboardCard
      className={props.className}
      title="Scores"
      description="Moving average per score"
      isLoading={props.isLoading || scores.isPending}
    >
      {!isEmptyTimeSeries({ data: extractedScores }) ? (
        // The height is the flex basis (floor); grow lets the chart absorb
        // extra tile height. On grid (lg) screens the floor is smaller so
        // tiles fit narrow viewports — grow recovers the height above the
        // grid's rowHeight floor. (LFE-10813)
        <div className="h-80 w-full shrink-0 grow lg:h-56">
          <Chart
            chartType="LINE_TIME_SERIES"
            data={chartData}
            rowLimit={100}
            chartConfig={SCORES_CHART_CONFIG}
            syncId={props.syncId}
            sync={props.sync}
          />
        </div>
      ) : (
        <NoDataOrLoading
          isLoading={props.isLoading || scores.isPending}
          description="Scores evaluate LLM quality and can be created manually or using the SDK."
          href="https://langfuse.com/docs/evaluation/overview"
          className="h-auto grow"
        />
      )}
    </DashboardCard>
  );
}