1- import type { Models } from '@appwrite.io/console' ;
1+ import {
2+ Query ,
3+ UsageEventDimension ,
4+ UsageEventMetric ,
5+ UsageOrderBy ,
6+ UsageOrderDirection ,
7+ type Models
8+ } from '@appwrite.io/console' ;
9+ import { sdk } from '$lib/stores/sdk' ;
210
311export function accumulateUsage ( usage : Models . Metric [ ] , base : number ) : Models . Metric [ ] {
412 const accumulation = usage . reduce (
@@ -18,6 +26,64 @@ export function accumulateUsage(usage: Models.Metric[], base: number): Models.Me
1826 return accumulation . metrics ;
1927}
2028
29+ export type ExecutionsBreakdown = {
30+ resourceId : string ;
31+ name ?: string ;
32+ value : number ;
33+ } ;
34+
35+ const executionsBreakdownLimit = 25 ;
36+
37+ /**
38+ * `UsageProject.executionsBreakdown` was dropped in SDK 16; the per-resource split now comes from
39+ * the dimensional usage API, which returns bare resource IDs, so names are resolved separately.
40+ *
41+ * Scoped to `functions.executions` rather than the umbrella `executions` metric, which also counts
42+ * site executions — those resolve to no name here and their rows link to a function that does not exist.
43+ * Omitting `interval` makes each point a whole-window aggregate per resource, so the limit is a
44+ * top-N-by-total rather than a truncation of the underlying data.
45+ */
46+ export async function listExecutionsBreakdown (
47+ region : string ,
48+ projectId : string ,
49+ startAt : string ,
50+ endAt : string
51+ ) : Promise < ExecutionsBreakdown [ ] > {
52+ const project = sdk . forProject ( region , projectId ) ;
53+
54+ const events = await project . usage . listEvents ( {
55+ metrics : [ UsageEventMetric . FunctionsExecutions ] ,
56+ dimensions : [ UsageEventDimension . ResourceId ] ,
57+ startAt,
58+ endAt,
59+ orderBy : UsageOrderBy . Value ,
60+ orderDir : UsageOrderDirection . Desc ,
61+ limit : executionsBreakdownLimit
62+ } ) ;
63+
64+ const totals = new Map < string , number > ( ) ;
65+ for ( const metric of events . metrics ) {
66+ for ( const point of metric . points ) {
67+ if ( ! point . resourceId ) continue ;
68+ totals . set ( point . resourceId , ( totals . get ( point . resourceId ) ?? 0 ) + point . value ) ;
69+ }
70+ }
71+
72+ if ( totals . size === 0 ) return [ ] ;
73+
74+ const resourceIds = [ ...totals . keys ( ) ] ;
75+ const functions = await project . functions . list ( {
76+ queries : [ Query . equal ( '$id' , resourceIds ) , Query . limit ( resourceIds . length ) ]
77+ } ) ;
78+ const names = new Map ( functions . functions . map ( ( func ) => [ func . $id , func . name ] ) ) ;
79+
80+ return resourceIds . map ( ( resourceId ) => ( {
81+ resourceId,
82+ name : names . get ( resourceId ) ,
83+ value : totals . get ( resourceId )
84+ } ) ) ;
85+ }
86+
2187export type Metric = {
2288 /**
2389 * The value of this metric at the timestamp.
0 commit comments