Skip to content

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

fix(posthog-telemetry): read from respective strategy for DRI count (#8639)

marliessophiedf3a4c761e74
diff --git a/packages/shared/src/server/repositories/dataset-run-items.ts b/packages/shared/src/server/repositories/dataset-run-items.ts
index 5ccf22d663..b2314a4ae6 100644
--- a/packages/shared/src/server/repositories/dataset-run-items.ts
+++ b/packages/shared/src/server/repositories/dataset-run-items.ts
@@ -18,6 +18,7 @@ import { env } from "../../env";
import { commandClickhouse } from "./clickhouse";
import Decimal from "decimal.js";
import { ClickHouseClientConfigOptions } from "@clickhouse/client";
+import { convertDateToClickhouseDateTime } from "../clickhouse/client";
type DatasetItemIdsByTraceIdQuery = {
projectId: string;
@@ -562,3 +563,40 @@ export const deleteDatasetRunItemsByDatasetRunIds = async ({
},
});
};
+
+export const getDatasetRunItemCountsByProjectInCreationInterval = async ({
+ start,
+ end,
+}: {
+ start: Date;
+ end: Date;
+}) => {
+ const query = `
+ SELECT
+ project_id,
+ count(*) as count
+ FROM dataset_run_items_rmt
+ WHERE created_at >= {start: DateTime64(3)}
+ AND created_at < {end: DateTime64(3)}
+ GROUP BY project_id
+`;
+
+ const rows = await queryClickhouse<{ project_id: string; count: string }>({
+ query,
+ params: {
+ start: convertDateToClickhouseDateTime(start),
+ end: convertDateToClickhouseDateTime(end),
+ },
+ tags: {
+ feature: "datasets",
+ type: "dataset-run-items",
+ kind: "analytic",
+ operation_name: "getDatasetRunItemCountsByProjectInCreationInterval",
+ },
+ });
+
+ return rows.map((row) => ({
+ projectId: row.project_id,
+ count: Number(row.count),
+ }));
+};
diff --git a/web/src/features/telemetry/index.ts b/web/src/features/telemetry/index.ts
index 0cdb1f1713..b9458d0e56 100644
--- a/web/src/features/telemetry/index.ts
+++ b/web/src/features/telemetry/index.ts
@@ -3,6 +3,9 @@ import { ServerPosthog } from "@/src/features/posthog-analytics/ServerPosthog";
import { Prisma, prisma } from "@langfuse/shared/src/db";
import { v4 as uuidv4 } from "uuid";
import {
+ DatasetRunItemsOperationType,
+ executeWithDatasetRunItemsStrategy,
+ getDatasetRunItemCountsByProjectInCreationInterval,
getObservationCountsByProjectInCreationInterval,
getScoreCountsByProjectInCreationInterval,
getTraceCountsByProjectInCreationInterval,
@@ -226,13 +229,30 @@ async function posthogTelemetry({
},
});
- // Count dataset run items
- const countDatasetRunItems = await prisma.datasetRunItems.count({
- where: {
- createdAt: {
- gte: startTimeframe?.toISOString(),
- lt: endTimeframe.toISOString(),
- },
+ const countDatasetRunItems = await executeWithDatasetRunItemsStrategy({
+ input: {},
+ operationType: DatasetRunItemsOperationType.READ,
+ postgresExecution: async () => {
+ // Count dataset run items
+ return await prisma.datasetRunItems.count({
+ where: {
+ createdAt: {
+ gte: startTimeframe?.toISOString(),
+ lt: endTimeframe.toISOString(),
+ },
+ },
+ });
+ },
+ clickhouseExecution: async () => {
+ const countDatasetRunItemsClickhouse =
+ await getDatasetRunItemCountsByProjectInCreationInterval({
+ start: startTimeframe ?? new Date(0),
+ end: endTimeframe,
+ });
+ return countDatasetRunItemsClickhouse.reduce(
+ (acc, curr) => acc + curr.count,
+ 0,
+ );
},
});