From 9fd31aaf6c251fbb90c87f4a71e57ab709ecaa73 Mon Sep 17 00:00:00 2001 From: John Richmond <5629+jr@users.noreply.github.com> Date: Fri, 6 Jun 2025 11:20:44 -0700 Subject: [PATCH] Add the timestamp of the last event to the Developers tab (#81) --- src/actions/analytics/events.ts | 4 ++- src/app/(authenticated)/usage/Developers.tsx | 26 +++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/actions/analytics/events.ts b/src/actions/analytics/events.ts index 05e556e66f..336ab0ab2d 100644 --- a/src/actions/analytics/events.ts +++ b/src/actions/analytics/events.ts @@ -191,6 +191,7 @@ const developerUsageSchema = z.object({ tasksCompleted: z.coerce.number(), tokens: z.coerce.number(), cost: z.coerce.number(), + lastEventTimestamp: z.coerce.number(), }); export type DeveloperUsage = z.infer & { @@ -237,7 +238,8 @@ export const getDeveloperUsage = async ({ SUM(CASE WHEN type = '${TelemetryEventName.TASK_CREATED}' THEN 1 ELSE 0 END) AS tasksStarted, SUM(CASE WHEN type = '${TelemetryEventName.TASK_COMPLETED}' THEN 1 ELSE 0 END) AS tasksCompleted, SUM(CASE WHEN type = '${TelemetryEventName.LLM_COMPLETION}' THEN COALESCE(inputTokens, 0) + COALESCE(outputTokens, 0) ELSE 0 END) AS tokens, - SUM(CASE WHEN type = '${TelemetryEventName.LLM_COMPLETION}' THEN COALESCE(cost, 0) ELSE 0 END) AS cost + SUM(CASE WHEN type = '${TelemetryEventName.LLM_COMPLETION}' THEN COALESCE(cost, 0) ELSE 0 END) AS cost, + MAX(timestamp) AS lastEventTimestamp FROM events WHERE orgId = {orgId: String} AND timestamp >= toUnixTimestamp(now() - INTERVAL {timePeriod: Int32} DAY) diff --git a/src/app/(authenticated)/usage/Developers.tsx b/src/app/(authenticated)/usage/Developers.tsx index fa280125f9..7e801de0c6 100644 --- a/src/app/(authenticated)/usage/Developers.tsx +++ b/src/app/(authenticated)/usage/Developers.tsx @@ -2,9 +2,14 @@ import { useMemo } from 'react'; import { useQuery } from '@tanstack/react-query'; import type { ColumnDef } from '@tanstack/react-table'; import { useAuth } from '@clerk/nextjs'; +import { formatDistance } from 'date-fns'; import { type DeveloperUsage, getDeveloperUsage } from '@/actions/analytics'; -import { formatCurrency, formatNumber } from '@/lib/formatters'; +import { + formatCurrency, + formatNumber, + formatTimestamp, +} from '@/lib/formatters'; import { Button, Skeleton } from '@/components/ui'; import { DataTable } from '@/components/layout'; @@ -63,6 +68,25 @@ export const Developers = ({ header: 'Cost (USD)', cell: ({ row }) => formatCurrency(row.original.cost), }, + { + header: 'Last Event', + cell: ({ row }) => { + const timestamp = row.original.lastEventTimestamp; + if (!timestamp) return 'No activity'; + + const date = new Date(timestamp * 1000); + const relativeTime = formatDistance(date, new Date(), { + addSuffix: true, + }); + const absoluteTime = formatTimestamp(timestamp); + + return ( + + {relativeTime} + + ); + }, + }, ], [onFilter], );