@@ -5,6 +5,7 @@ import { z } from "zod/v4";
55import { deleteProject } from "@/lib/actions/project" ;
66import { checkUserWorkspaceRole } from "@/lib/actions/workspace/utils" ;
77import { completeMonthsElapsed } from "@/lib/actions/workspaces/utils" ;
8+ import { cache , WORKSPACE_BYTES_USAGE_CACHE_KEY } from "@/lib/cache" ;
89import { clickhouseClient } from "@/lib/clickhouse/client" ;
910import { db } from "@/lib/db/drizzle" ;
1011import { membersOfWorkspaces , projects , subscriptionTiers , users , workspaces } from "@/lib/db/migrations/schema" ;
@@ -129,13 +130,6 @@ export const getWorkspaceInfo = async (workspaceId: string): Promise<Workspace>
129130} ;
130131
131132export const getWorkspaceUsage = async ( workspaceId : string ) : Promise < WorkspaceUsage > => {
132- const projectIds = await db . query . projects . findMany ( {
133- where : eq ( projects . workspaceId , workspaceId ) ,
134- columns : {
135- id : true ,
136- } ,
137- } ) ;
138-
139133 const resetTime = await db . query . workspaces . findFirst ( {
140134 where : eq ( workspaces . id , workspaceId ) ,
141135 columns : {
@@ -147,18 +141,38 @@ export const getWorkspaceUsage = async (workspaceId: string): Promise<WorkspaceU
147141 throw new Error ( "Workspace not found" ) ;
148142 }
149143
144+ const resetTimeDate = new Date ( resetTime . resetTime ) ;
145+ const latestResetTime = addMonths ( resetTimeDate , completeMonthsElapsed ( resetTimeDate , new Date ( ) ) ) ;
146+
147+ const cacheKey = `${ WORKSPACE_BYTES_USAGE_CACHE_KEY } :${ workspaceId } ` ;
148+ try {
149+ const cachedUsage = await cache . get < number > ( cacheKey ) ;
150+ if ( cachedUsage !== null ) {
151+ return {
152+ totalBytesIngested : Number ( cachedUsage ) ,
153+ resetTime : latestResetTime ,
154+ } ;
155+ }
156+ } catch ( error ) {
157+ // If cache fails, continue to ClickHouse query
158+ console . error ( "Error reading from cache:" , error ) ;
159+ }
160+
161+ // Cache miss - query ClickHouse for the actual breakdown
162+ const projectIds = await db . query . projects . findMany ( {
163+ where : eq ( projects . workspaceId , workspaceId ) ,
164+ columns : {
165+ id : true ,
166+ } ,
167+ } ) ;
168+
150169 if ( projectIds . length === 0 ) {
151170 return {
152- spansBytesIngested : 0 ,
153- browserSessionEventsBytesIngested : 0 ,
154- eventsBytesIngested : 0 ,
155- resetTime : new Date ( resetTime . resetTime ) ,
171+ totalBytesIngested : 0 ,
172+ resetTime : latestResetTime ,
156173 } ;
157174 }
158175
159- const resetTimeDate = new Date ( resetTime . resetTime ) ;
160-
161- const latestResetTime = addMonths ( resetTimeDate , completeMonthsElapsed ( resetTimeDate , new Date ( ) ) ) ;
162176 const query = `WITH spans_bytes_ingested AS (
163177 SELECT
164178 SUM(spans.size_bytes) as spans_bytes_ingested
@@ -205,10 +219,13 @@ export const getWorkspaceUsage = async (workspaceId: string): Promise<WorkspaceU
205219 throw new Error ( "Error getting workspace usage" ) ;
206220 }
207221
222+ const totalBytesIngested =
223+ Number ( result [ 0 ] . spans_bytes_ingested ) +
224+ Number ( result [ 0 ] . browser_session_events_bytes_ingested ) +
225+ Number ( result [ 0 ] . events_bytes_ingested ) ;
226+
208227 return {
209- spansBytesIngested : Number ( result [ 0 ] . spans_bytes_ingested ) ,
210- browserSessionEventsBytesIngested : Number ( result [ 0 ] . browser_session_events_bytes_ingested ) ,
211- eventsBytesIngested : Number ( result [ 0 ] . events_bytes_ingested ) ,
228+ totalBytesIngested,
212229 resetTime : latestResetTime ,
213230 } ;
214231} ;
0 commit comments