@burdenoff/be-sdk 2026.1031.2 → 2026.1101.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-5ZP576CV.js → chunk-3MEZRD4Z.js} +2 -2
- package/dist/{chunk-5ZP576CV.js.map → chunk-3MEZRD4Z.js.map} +1 -1
- package/dist/chunk-CKL6U4XE.js +883 -0
- package/dist/chunk-CKL6U4XE.js.map +1 -0
- package/dist/{chunk-57GT6PJK.js → chunk-X3RYQ74H.js} +5 -471
- package/dist/chunk-X3RYQ74H.js.map +1 -0
- package/dist/client-DSI_760t.d.ts +36 -0
- package/dist/gateway/index.d.ts +2 -1
- package/dist/{index-DJCb-l2J.d.ts → index-DXPFlxpZ.d.ts} +2 -127
- package/dist/{index-szTxGaO6.d.ts → index-wGuKj5rr.d.ts} +3 -35
- package/dist/index.d.ts +6 -3
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/init/index.js +1 -1
- package/dist/tracker/index.d.ts +4 -1
- package/dist/tracker/index.js +11 -1
- package/dist/usage/index.d.ts +179 -0
- package/dist/usage/index.js +16 -0
- package/dist/usage/index.js.map +1 -0
- package/package.json +2 -2
- package/dist/chunk-57GT6PJK.js.map +0 -1
|
@@ -0,0 +1,883 @@
|
|
|
1
|
+
import { GatewayClient } from './chunk-ZWKJKL6Y.js';
|
|
2
|
+
import { ValidationError, QuotaExceededError } from './chunk-QGBXVNRM.js';
|
|
3
|
+
import { logger } from './chunk-RU266375.js';
|
|
4
|
+
import { __name } from './chunk-XE4OXN2W.js';
|
|
5
|
+
|
|
6
|
+
// src/usage/billing.ts
|
|
7
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 5e3;
|
|
8
|
+
var DEFAULT_QUOTA_LIMIT_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
9
|
+
var QUOTA_LIMIT_CACHE_MAX_ENTRIES = 1e3;
|
|
10
|
+
var GET_WORKSPACE_ORG_QUERY = `
|
|
11
|
+
query GetWorkspaceOrg($workspaceId: ID!) {
|
|
12
|
+
workspace(id: $workspaceId) {
|
|
13
|
+
orgId
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
`;
|
|
17
|
+
var GET_DEFAULT_BILLING_ACCOUNT_QUERY = `
|
|
18
|
+
query GetDefaultBillingAccount($orgId: ID!) {
|
|
19
|
+
getDefaultBillingAccountByOrgId(orgId: $orgId) {
|
|
20
|
+
id
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
var CHECK_QUOTA_QUERY = `
|
|
25
|
+
query CheckQuotaExhausted($billingAccountId: ID!, $quotaName: String!, $productId: String) {
|
|
26
|
+
checkQuotaExhausted(
|
|
27
|
+
billingAccountId: $billingAccountId
|
|
28
|
+
quotaName: $quotaName
|
|
29
|
+
productId: $productId
|
|
30
|
+
) {
|
|
31
|
+
limitLeft
|
|
32
|
+
noLimit
|
|
33
|
+
quotaAssignmentId
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
37
|
+
var ADD_USAGE_MUTATION = `
|
|
38
|
+
mutation AddUsage(
|
|
39
|
+
$billingAccountId: ID!
|
|
40
|
+
$quotaName: String!
|
|
41
|
+
$value: Int!
|
|
42
|
+
$description: String!
|
|
43
|
+
$tags: JSON
|
|
44
|
+
$hash: String
|
|
45
|
+
$userId: ID
|
|
46
|
+
$productId: String
|
|
47
|
+
) {
|
|
48
|
+
addUsage(
|
|
49
|
+
billingAccountId: $billingAccountId
|
|
50
|
+
quotaName: $quotaName
|
|
51
|
+
value: $value
|
|
52
|
+
description: $description
|
|
53
|
+
tags: $tags
|
|
54
|
+
hash: $hash
|
|
55
|
+
userId: $userId
|
|
56
|
+
productId: $productId
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
`;
|
|
60
|
+
var GET_QUOTA_LIMIT_QUERY = `
|
|
61
|
+
query GetQuotaLimit($billingAccountId: ID!, $quotaName: String!, $productId: String) {
|
|
62
|
+
getQuotaUsageAndLimit(
|
|
63
|
+
billingAccountId: $billingAccountId
|
|
64
|
+
quotaName: $quotaName
|
|
65
|
+
productId: $productId
|
|
66
|
+
) {
|
|
67
|
+
limit
|
|
68
|
+
noLimit
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
var FREE_UP_USAGE_MUTATION = `
|
|
73
|
+
mutation FreeUpUsage(
|
|
74
|
+
$billingAccountId: ID!
|
|
75
|
+
$quotaName: String!
|
|
76
|
+
$value: Int!
|
|
77
|
+
$description: String
|
|
78
|
+
$tags: JSON
|
|
79
|
+
$hash: String
|
|
80
|
+
$userId: ID
|
|
81
|
+
$productId: String
|
|
82
|
+
) {
|
|
83
|
+
freeUpUsage(
|
|
84
|
+
billingAccountId: $billingAccountId
|
|
85
|
+
quotaName: $quotaName
|
|
86
|
+
value: $value
|
|
87
|
+
description: $description
|
|
88
|
+
tags: $tags
|
|
89
|
+
hash: $hash
|
|
90
|
+
userId: $userId
|
|
91
|
+
productId: $productId
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
function unwrap(data) {
|
|
96
|
+
if (!data) {
|
|
97
|
+
throw new Error("GraphQL response did not include data");
|
|
98
|
+
}
|
|
99
|
+
return data;
|
|
100
|
+
}
|
|
101
|
+
__name(unwrap, "unwrap");
|
|
102
|
+
var BillingUsageClient = class {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "BillingUsageClient");
|
|
105
|
+
}
|
|
106
|
+
gatewayClient;
|
|
107
|
+
quotaLimitCacheTtlMs;
|
|
108
|
+
logPrefix;
|
|
109
|
+
// In-process cache for plan limits (changes at most when a workspace
|
|
110
|
+
// upgrades their plan). Lazily pruned on access — no timers in a library.
|
|
111
|
+
quotaLimitCache = /* @__PURE__ */ new Map();
|
|
112
|
+
constructor(config) {
|
|
113
|
+
if (!config.serviceName) {
|
|
114
|
+
throw new TypeError("BillingUsageClient requires a serviceName");
|
|
115
|
+
}
|
|
116
|
+
this.gatewayClient = config.gatewayClient ?? new GatewayClient({
|
|
117
|
+
layer: "wspace",
|
|
118
|
+
serviceName: config.serviceName,
|
|
119
|
+
timeout: config.timeout ?? DEFAULT_REQUEST_TIMEOUT_MS
|
|
120
|
+
});
|
|
121
|
+
this.quotaLimitCacheTtlMs = config.quotaLimitCacheTtlMs ?? DEFAULT_QUOTA_LIMIT_CACHE_TTL_MS;
|
|
122
|
+
this.logPrefix = `[billing-usage:${config.serviceName}]`;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Same-layer (wspace -> wspace) internal-gateway call.
|
|
126
|
+
*/
|
|
127
|
+
async queryWorkspaceLayer(query, variables, workspaceId) {
|
|
128
|
+
const response = await this.gatewayClient.internalQuery({
|
|
129
|
+
query,
|
|
130
|
+
variables,
|
|
131
|
+
headers: {
|
|
132
|
+
"x-workspace-id": workspaceId
|
|
133
|
+
},
|
|
134
|
+
// Blocking on the create path — keep it single-shot & snappy; consumers fail open.
|
|
135
|
+
skipRetry: true
|
|
136
|
+
});
|
|
137
|
+
return unwrap(response.data);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Cross-layer (wspace -> global) public-gateway call. No actor headers — the
|
|
141
|
+
* signed service JWT carries identity; adding `x-actor-id` would 403.
|
|
142
|
+
*/
|
|
143
|
+
async queryGlobalLayer(query, variables) {
|
|
144
|
+
const response = await this.gatewayClient.crossLayerQuery({
|
|
145
|
+
query,
|
|
146
|
+
variables
|
|
147
|
+
});
|
|
148
|
+
return unwrap(response.data);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Resolve the organization that owns a workspace. Fails open (`null`).
|
|
152
|
+
*/
|
|
153
|
+
async getOrgIdFromWorkspace(workspaceId) {
|
|
154
|
+
try {
|
|
155
|
+
const data = await this.queryWorkspaceLayer(GET_WORKSPACE_ORG_QUERY, {
|
|
156
|
+
workspaceId
|
|
157
|
+
}, workspaceId);
|
|
158
|
+
return data.workspace?.orgId ?? null;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
logger.warn(`${this.logPrefix} Failed to resolve workspace org`, {
|
|
161
|
+
workspaceId,
|
|
162
|
+
error: error instanceof Error ? error.message : String(error)
|
|
163
|
+
});
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Resolve the default billing account for an organization via the global
|
|
169
|
+
* layer. Fails open (`null`).
|
|
170
|
+
*/
|
|
171
|
+
async getDefaultBillingAccountId(orgId) {
|
|
172
|
+
try {
|
|
173
|
+
const data = await this.queryGlobalLayer(GET_DEFAULT_BILLING_ACCOUNT_QUERY, {
|
|
174
|
+
orgId
|
|
175
|
+
});
|
|
176
|
+
return data.getDefaultBillingAccountByOrgId?.id ?? null;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
logger.warn(`${this.logPrefix} Failed to resolve default billing account`, {
|
|
179
|
+
orgId,
|
|
180
|
+
error: error instanceof Error ? error.message : String(error)
|
|
181
|
+
});
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Check that an organization still has quota headroom for `value` units of
|
|
187
|
+
* `quotaName`. Returns `false` when no billing account exists or the quota
|
|
188
|
+
* is exhausted; rethrows nothing — transport errors propagate to the caller
|
|
189
|
+
* of this low-level variant (the workspace variant fails open instead).
|
|
190
|
+
*/
|
|
191
|
+
async ensureQuotaAvailableForOrg(input) {
|
|
192
|
+
const billingAccountId = await this.getDefaultBillingAccountId(input.orgId);
|
|
193
|
+
if (!billingAccountId) {
|
|
194
|
+
logger.warn(`${this.logPrefix} No billing account available for org quota check`, {
|
|
195
|
+
orgId: input.orgId,
|
|
196
|
+
quotaName: input.quotaName
|
|
197
|
+
});
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
const amount = input.value ?? 1;
|
|
201
|
+
const data = await this.queryGlobalLayer(CHECK_QUOTA_QUERY, {
|
|
202
|
+
billingAccountId,
|
|
203
|
+
quotaName: input.quotaName,
|
|
204
|
+
productId: input.productId
|
|
205
|
+
});
|
|
206
|
+
const result = data.checkQuotaExhausted;
|
|
207
|
+
if (!result) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
if (result.noLimit) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
return result.limitLeft >= amount;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Workspace-addressed quota gate: resolves the org, then applies
|
|
217
|
+
* {@link ensureQuotaAvailableForOrg}. Fails OPEN (`true`) when org
|
|
218
|
+
* resolution fails (unbilled / dev / internal workspaces) so local-stack
|
|
219
|
+
* test workspaces and workspaces with a transiently broken tenant lookup
|
|
220
|
+
* are never locked out; real limits only kick in when the workspace has a
|
|
221
|
+
* billable org.
|
|
222
|
+
*/
|
|
223
|
+
async ensureQuotaAvailableForWorkspace(input) {
|
|
224
|
+
const orgId = await this.getOrgIdFromWorkspace(input.workspaceId);
|
|
225
|
+
if (!orgId) {
|
|
226
|
+
logger.warn(`${this.logPrefix} No organization available; allowing quota op (fail-open)`, {
|
|
227
|
+
workspaceId: input.workspaceId,
|
|
228
|
+
quotaName: input.quotaName
|
|
229
|
+
});
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
return this.ensureQuotaAvailableForOrg({
|
|
233
|
+
...input,
|
|
234
|
+
orgId
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Record usage against an organization's billing account. No-ops (`false`)
|
|
239
|
+
* when there is no billing account.
|
|
240
|
+
*/
|
|
241
|
+
async addUsageForOrg(input) {
|
|
242
|
+
const billingAccountId = await this.getDefaultBillingAccountId(input.orgId);
|
|
243
|
+
if (!billingAccountId) {
|
|
244
|
+
logger.warn(`${this.logPrefix} No billing account available for addUsage`, {
|
|
245
|
+
orgId: input.orgId,
|
|
246
|
+
quotaName: input.quotaName
|
|
247
|
+
});
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
const data = await this.queryGlobalLayer(ADD_USAGE_MUTATION, {
|
|
251
|
+
billingAccountId,
|
|
252
|
+
quotaName: input.quotaName,
|
|
253
|
+
value: input.value ?? 1,
|
|
254
|
+
description: input.description,
|
|
255
|
+
tags: input.tags,
|
|
256
|
+
hash: input.hash,
|
|
257
|
+
userId: input.userId,
|
|
258
|
+
productId: input.productId
|
|
259
|
+
});
|
|
260
|
+
return data.addUsage ?? false;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Workspace-addressed {@link addUsageForOrg}. No-ops (`false`) when the
|
|
264
|
+
* workspace has no org.
|
|
265
|
+
*/
|
|
266
|
+
async addUsageForWorkspace(input) {
|
|
267
|
+
const orgId = await this.getOrgIdFromWorkspace(input.workspaceId);
|
|
268
|
+
if (!orgId) {
|
|
269
|
+
logger.warn(`${this.logPrefix} No organization available for workspace addUsage`, {
|
|
270
|
+
workspaceId: input.workspaceId,
|
|
271
|
+
quotaName: input.quotaName
|
|
272
|
+
});
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
return this.addUsageForOrg({
|
|
276
|
+
...input,
|
|
277
|
+
orgId
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Credit usage back to an organization's billing account. No-ops (`false`)
|
|
282
|
+
* when there is no billing account.
|
|
283
|
+
*/
|
|
284
|
+
async freeUpUsageForOrg(input) {
|
|
285
|
+
const billingAccountId = await this.getDefaultBillingAccountId(input.orgId);
|
|
286
|
+
if (!billingAccountId) {
|
|
287
|
+
logger.warn(`${this.logPrefix} No billing account available for freeUpUsage`, {
|
|
288
|
+
orgId: input.orgId,
|
|
289
|
+
quotaName: input.quotaName
|
|
290
|
+
});
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
const data = await this.queryGlobalLayer(FREE_UP_USAGE_MUTATION, {
|
|
294
|
+
billingAccountId,
|
|
295
|
+
quotaName: input.quotaName,
|
|
296
|
+
value: input.value ?? 1,
|
|
297
|
+
description: input.description,
|
|
298
|
+
tags: input.tags,
|
|
299
|
+
hash: input.hash,
|
|
300
|
+
userId: input.userId,
|
|
301
|
+
productId: input.productId
|
|
302
|
+
});
|
|
303
|
+
return data.freeUpUsage ?? false;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Workspace-addressed {@link freeUpUsageForOrg}. No-ops (`false`) when the
|
|
307
|
+
* workspace has no org.
|
|
308
|
+
*/
|
|
309
|
+
async freeUpUsageForWorkspace(input) {
|
|
310
|
+
const orgId = await this.getOrgIdFromWorkspace(input.workspaceId);
|
|
311
|
+
if (!orgId) {
|
|
312
|
+
logger.warn(`${this.logPrefix} No organization available for workspace freeUpUsage`, {
|
|
313
|
+
workspaceId: input.workspaceId,
|
|
314
|
+
quotaName: input.quotaName
|
|
315
|
+
});
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
return this.freeUpUsageForOrg({
|
|
319
|
+
...input,
|
|
320
|
+
orgId
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Fetch the plan limit for a quota from billing-svc, bypassing billing's
|
|
325
|
+
* internal usage counter (the caller's own DB is the source of truth for
|
|
326
|
+
* current usage; billing only stores the plan maximum). Results are cached
|
|
327
|
+
* in-process per workspace/quota/product. Fails open (`null`) when billing
|
|
328
|
+
* is unreachable so dev/test workspaces without billing accounts are never
|
|
329
|
+
* blocked.
|
|
330
|
+
*/
|
|
331
|
+
async getQuotaLimitForWorkspace(workspaceId, quotaName, productId) {
|
|
332
|
+
const cacheKey = `${workspaceId}:${quotaName}:${productId}`;
|
|
333
|
+
const cached = this.quotaLimitCache.get(cacheKey);
|
|
334
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
335
|
+
return {
|
|
336
|
+
limit: cached.limit,
|
|
337
|
+
noLimit: cached.noLimit
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
const orgId = await this.getOrgIdFromWorkspace(workspaceId);
|
|
341
|
+
if (!orgId) {
|
|
342
|
+
logger.warn(`${this.logPrefix} No org found for quota limit lookup; failing open`, {
|
|
343
|
+
workspaceId,
|
|
344
|
+
quotaName
|
|
345
|
+
});
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
const billingAccountId = await this.getDefaultBillingAccountId(orgId);
|
|
349
|
+
if (!billingAccountId) {
|
|
350
|
+
logger.warn(`${this.logPrefix} No billing account found for quota limit lookup; failing open`, {
|
|
351
|
+
workspaceId,
|
|
352
|
+
quotaName
|
|
353
|
+
});
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const data = await this.queryGlobalLayer(GET_QUOTA_LIMIT_QUERY, {
|
|
358
|
+
billingAccountId,
|
|
359
|
+
quotaName,
|
|
360
|
+
productId
|
|
361
|
+
});
|
|
362
|
+
const result = data.getQuotaUsageAndLimit;
|
|
363
|
+
if (!result) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
this.rememberQuotaLimit(cacheKey, {
|
|
367
|
+
limit: result.limit,
|
|
368
|
+
noLimit: result.noLimit
|
|
369
|
+
});
|
|
370
|
+
return {
|
|
371
|
+
limit: result.limit,
|
|
372
|
+
noLimit: result.noLimit
|
|
373
|
+
};
|
|
374
|
+
} catch (error) {
|
|
375
|
+
logger.warn(`${this.logPrefix} Failed to fetch quota limit; failing open`, {
|
|
376
|
+
workspaceId,
|
|
377
|
+
quotaName,
|
|
378
|
+
error: error instanceof Error ? error.message : String(error)
|
|
379
|
+
});
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
rememberQuotaLimit(cacheKey, value) {
|
|
384
|
+
if (this.quotaLimitCache.size >= QUOTA_LIMIT_CACHE_MAX_ENTRIES) {
|
|
385
|
+
const now = Date.now();
|
|
386
|
+
for (const [key, entry] of this.quotaLimitCache) {
|
|
387
|
+
if (entry.expiresAt <= now) this.quotaLimitCache.delete(key);
|
|
388
|
+
}
|
|
389
|
+
if (this.quotaLimitCache.size >= QUOTA_LIMIT_CACHE_MAX_ENTRIES) {
|
|
390
|
+
const overflow = this.quotaLimitCache.size - QUOTA_LIMIT_CACHE_MAX_ENTRIES + 1;
|
|
391
|
+
let removed = 0;
|
|
392
|
+
for (const key of this.quotaLimitCache.keys()) {
|
|
393
|
+
this.quotaLimitCache.delete(key);
|
|
394
|
+
removed += 1;
|
|
395
|
+
if (removed >= overflow) break;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
this.quotaLimitCache.set(cacheKey, {
|
|
400
|
+
...value,
|
|
401
|
+
expiresAt: Date.now() + this.quotaLimitCacheTtlMs
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// src/usage/index.ts
|
|
407
|
+
var UsageMetricType = /* @__PURE__ */ (function(UsageMetricType2) {
|
|
408
|
+
UsageMetricType2["API_CALLS"] = "api_calls";
|
|
409
|
+
UsageMetricType2["STORAGE"] = "storage";
|
|
410
|
+
UsageMetricType2["BANDWIDTH"] = "bandwidth";
|
|
411
|
+
UsageMetricType2["USERS"] = "users";
|
|
412
|
+
UsageMetricType2["PROJECTS"] = "projects";
|
|
413
|
+
UsageMetricType2["WORKSPACES"] = "workspaces";
|
|
414
|
+
UsageMetricType2["RECORDS"] = "records";
|
|
415
|
+
UsageMetricType2["COMPUTE_TIME"] = "compute_time";
|
|
416
|
+
UsageMetricType2["TRANSACTIONS"] = "transactions";
|
|
417
|
+
UsageMetricType2["CUSTOM"] = "custom";
|
|
418
|
+
return UsageMetricType2;
|
|
419
|
+
})({});
|
|
420
|
+
var QuotaPeriod = /* @__PURE__ */ (function(QuotaPeriod2) {
|
|
421
|
+
QuotaPeriod2["DAILY"] = "daily";
|
|
422
|
+
QuotaPeriod2["WEEKLY"] = "weekly";
|
|
423
|
+
QuotaPeriod2["MONTHLY"] = "monthly";
|
|
424
|
+
QuotaPeriod2["YEARLY"] = "yearly";
|
|
425
|
+
QuotaPeriod2["LIFETIME"] = "lifetime";
|
|
426
|
+
return QuotaPeriod2;
|
|
427
|
+
})({});
|
|
428
|
+
var UsageModule = class {
|
|
429
|
+
static {
|
|
430
|
+
__name(this, "UsageModule");
|
|
431
|
+
}
|
|
432
|
+
client;
|
|
433
|
+
context;
|
|
434
|
+
constructor(client, context) {
|
|
435
|
+
this.client = client;
|
|
436
|
+
this.context = context;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Track usage
|
|
440
|
+
*/
|
|
441
|
+
async track(options) {
|
|
442
|
+
if (!options.entityId) {
|
|
443
|
+
throw new ValidationError("Entity ID is required");
|
|
444
|
+
}
|
|
445
|
+
if (!options.entityType) {
|
|
446
|
+
throw new ValidationError("Entity type is required");
|
|
447
|
+
}
|
|
448
|
+
if (!options.metricType) {
|
|
449
|
+
throw new ValidationError("Metric type is required");
|
|
450
|
+
}
|
|
451
|
+
if (typeof options.amount !== "number" || options.amount <= 0) {
|
|
452
|
+
throw new ValidationError("Amount must be a positive number");
|
|
453
|
+
}
|
|
454
|
+
logger.debug("[Usage] Tracking usage", {
|
|
455
|
+
entityId: options.entityId,
|
|
456
|
+
metricType: options.metricType,
|
|
457
|
+
amount: options.amount
|
|
458
|
+
});
|
|
459
|
+
const mutation = `
|
|
460
|
+
mutation TrackUsage($input: TrackUsageInput!) {
|
|
461
|
+
trackUsage(input: $input) {
|
|
462
|
+
id
|
|
463
|
+
entityId
|
|
464
|
+
entityType
|
|
465
|
+
metricType
|
|
466
|
+
amount
|
|
467
|
+
resourceId
|
|
468
|
+
metadata
|
|
469
|
+
workspaceId
|
|
470
|
+
organizationId
|
|
471
|
+
recordedAt
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
`;
|
|
475
|
+
const variables = {
|
|
476
|
+
input: {
|
|
477
|
+
entityId: options.entityId,
|
|
478
|
+
entityType: options.entityType,
|
|
479
|
+
metricType: options.metricType,
|
|
480
|
+
amount: options.amount,
|
|
481
|
+
resourceId: options.resourceId,
|
|
482
|
+
metadata: options.metadata,
|
|
483
|
+
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
484
|
+
organizationId: options.organizationId
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
const response = await this.client.query(mutation, variables, this.context);
|
|
488
|
+
logger.info("[Usage] Usage tracked", {
|
|
489
|
+
recordId: response.trackUsage.id
|
|
490
|
+
});
|
|
491
|
+
return response.trackUsage;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Check quota availability
|
|
495
|
+
*/
|
|
496
|
+
async checkQuota(options) {
|
|
497
|
+
if (!options.entityId) {
|
|
498
|
+
throw new ValidationError("Entity ID is required");
|
|
499
|
+
}
|
|
500
|
+
if (!options.metricType) {
|
|
501
|
+
throw new ValidationError("Metric type is required");
|
|
502
|
+
}
|
|
503
|
+
if (typeof options.requestedAmount !== "number" || options.requestedAmount <= 0) {
|
|
504
|
+
throw new ValidationError("Requested amount must be a positive number");
|
|
505
|
+
}
|
|
506
|
+
logger.debug("[Usage] Checking quota", {
|
|
507
|
+
entityId: options.entityId,
|
|
508
|
+
metricType: options.metricType,
|
|
509
|
+
requestedAmount: options.requestedAmount
|
|
510
|
+
});
|
|
511
|
+
const query = `
|
|
512
|
+
query CheckQuota($input: CheckQuotaInput!) {
|
|
513
|
+
checkQuota(input: $input) {
|
|
514
|
+
allowed
|
|
515
|
+
currentUsage
|
|
516
|
+
quotaLimit
|
|
517
|
+
remainingQuota
|
|
518
|
+
resetAt
|
|
519
|
+
period
|
|
520
|
+
reason
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
`;
|
|
524
|
+
const variables = {
|
|
525
|
+
input: {
|
|
526
|
+
entityId: options.entityId,
|
|
527
|
+
entityType: options.entityType,
|
|
528
|
+
metricType: options.metricType,
|
|
529
|
+
requestedAmount: options.requestedAmount,
|
|
530
|
+
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
531
|
+
organizationId: options.organizationId
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const response = await this.client.query(query, variables, this.context);
|
|
535
|
+
logger.debug("[Usage] Quota check result", {
|
|
536
|
+
allowed: response.checkQuota.allowed
|
|
537
|
+
});
|
|
538
|
+
return response.checkQuota;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Require quota (throws if exceeded)
|
|
542
|
+
*/
|
|
543
|
+
async requireQuota(options) {
|
|
544
|
+
const result = await this.checkQuota(options);
|
|
545
|
+
if (!result.allowed) {
|
|
546
|
+
throw new QuotaExceededError(result.reason || "Quota exceeded", {
|
|
547
|
+
entityId: options.entityId,
|
|
548
|
+
metricType: options.metricType,
|
|
549
|
+
currentUsage: result.currentUsage,
|
|
550
|
+
quotaLimit: result.quotaLimit,
|
|
551
|
+
requestedAmount: options.requestedAmount,
|
|
552
|
+
resetAt: result.resetAt
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Track usage and check quota atomically
|
|
558
|
+
*/
|
|
559
|
+
async trackAndCheck(options) {
|
|
560
|
+
const quotaCheck = await this.checkQuota({
|
|
561
|
+
entityId: options.entityId,
|
|
562
|
+
entityType: options.entityType,
|
|
563
|
+
metricType: options.metricType,
|
|
564
|
+
requestedAmount: options.amount,
|
|
565
|
+
workspaceId: options.workspaceId,
|
|
566
|
+
organizationId: options.organizationId
|
|
567
|
+
});
|
|
568
|
+
if (!quotaCheck.allowed) {
|
|
569
|
+
throw new QuotaExceededError(quotaCheck.reason || "Quota exceeded", {
|
|
570
|
+
entityId: options.entityId,
|
|
571
|
+
metricType: options.metricType,
|
|
572
|
+
currentUsage: quotaCheck.currentUsage,
|
|
573
|
+
quotaLimit: quotaCheck.quotaLimit
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
const usage = await this.track(options);
|
|
577
|
+
return {
|
|
578
|
+
usage,
|
|
579
|
+
quotaCheck
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Get usage records
|
|
584
|
+
*/
|
|
585
|
+
async getUsage(options) {
|
|
586
|
+
logger.debug("[Usage] Getting usage records", {
|
|
587
|
+
entityId: options.entityId
|
|
588
|
+
});
|
|
589
|
+
const query = `
|
|
590
|
+
query GetUsage(
|
|
591
|
+
$entityId: ID!
|
|
592
|
+
$entityType: String!
|
|
593
|
+
$metricType: UsageMetricType
|
|
594
|
+
$startDate: DateTime
|
|
595
|
+
$endDate: DateTime
|
|
596
|
+
$workspaceId: ID
|
|
597
|
+
$organizationId: ID
|
|
598
|
+
$limit: Int
|
|
599
|
+
$offset: Int
|
|
600
|
+
) {
|
|
601
|
+
getUsage(
|
|
602
|
+
entityId: $entityId
|
|
603
|
+
entityType: $entityType
|
|
604
|
+
metricType: $metricType
|
|
605
|
+
startDate: $startDate
|
|
606
|
+
endDate: $endDate
|
|
607
|
+
workspaceId: $workspaceId
|
|
608
|
+
organizationId: $organizationId
|
|
609
|
+
limit: $limit
|
|
610
|
+
offset: $offset
|
|
611
|
+
) {
|
|
612
|
+
records {
|
|
613
|
+
id
|
|
614
|
+
entityId
|
|
615
|
+
entityType
|
|
616
|
+
metricType
|
|
617
|
+
amount
|
|
618
|
+
resourceId
|
|
619
|
+
metadata
|
|
620
|
+
workspaceId
|
|
621
|
+
organizationId
|
|
622
|
+
recordedAt
|
|
623
|
+
}
|
|
624
|
+
total
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
`;
|
|
628
|
+
const variables = {
|
|
629
|
+
entityId: options.entityId,
|
|
630
|
+
entityType: options.entityType,
|
|
631
|
+
metricType: options.metricType,
|
|
632
|
+
startDate: options.startDate,
|
|
633
|
+
endDate: options.endDate,
|
|
634
|
+
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
635
|
+
organizationId: options.organizationId,
|
|
636
|
+
limit: options.limit || 100,
|
|
637
|
+
offset: options.offset || 0
|
|
638
|
+
};
|
|
639
|
+
const response = await this.client.query(query, variables, this.context);
|
|
640
|
+
return response.getUsage;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Get usage summary
|
|
644
|
+
*/
|
|
645
|
+
async getSummary(entityId, entityType, metricType, period) {
|
|
646
|
+
logger.debug("[Usage] Getting usage summary", {
|
|
647
|
+
entityId,
|
|
648
|
+
metricType,
|
|
649
|
+
period
|
|
650
|
+
});
|
|
651
|
+
const query = `
|
|
652
|
+
query GetUsageSummary(
|
|
653
|
+
$entityId: ID!
|
|
654
|
+
$entityType: String!
|
|
655
|
+
$metricType: UsageMetricType!
|
|
656
|
+
$period: QuotaPeriod!
|
|
657
|
+
) {
|
|
658
|
+
getUsageSummary(
|
|
659
|
+
entityId: $entityId
|
|
660
|
+
entityType: $entityType
|
|
661
|
+
metricType: $metricType
|
|
662
|
+
period: $period
|
|
663
|
+
) {
|
|
664
|
+
entityId
|
|
665
|
+
entityType
|
|
666
|
+
metricType
|
|
667
|
+
totalUsage
|
|
668
|
+
period
|
|
669
|
+
periodStart
|
|
670
|
+
periodEnd
|
|
671
|
+
quotaLimit
|
|
672
|
+
utilizationPercentage
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
`;
|
|
676
|
+
const response = await this.client.query(query, {
|
|
677
|
+
entityId,
|
|
678
|
+
entityType,
|
|
679
|
+
metricType,
|
|
680
|
+
period
|
|
681
|
+
}, this.context);
|
|
682
|
+
return response.getUsageSummary;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Set quota limit
|
|
686
|
+
*/
|
|
687
|
+
async setQuota(options) {
|
|
688
|
+
if (!options.entityId) {
|
|
689
|
+
throw new ValidationError("Entity ID is required");
|
|
690
|
+
}
|
|
691
|
+
if (!options.metricType) {
|
|
692
|
+
throw new ValidationError("Metric type is required");
|
|
693
|
+
}
|
|
694
|
+
if (typeof options.limit !== "number" || options.limit < 0) {
|
|
695
|
+
throw new ValidationError("Limit must be a non-negative number");
|
|
696
|
+
}
|
|
697
|
+
logger.debug("[Usage] Setting quota", {
|
|
698
|
+
entityId: options.entityId,
|
|
699
|
+
metricType: options.metricType,
|
|
700
|
+
limit: options.limit
|
|
701
|
+
});
|
|
702
|
+
const mutation = `
|
|
703
|
+
mutation SetQuota($input: SetQuotaInput!) {
|
|
704
|
+
setQuota(input: $input) {
|
|
705
|
+
id
|
|
706
|
+
entityId
|
|
707
|
+
entityType
|
|
708
|
+
metricType
|
|
709
|
+
limit
|
|
710
|
+
period
|
|
711
|
+
currentUsage
|
|
712
|
+
resetAt
|
|
713
|
+
createdAt
|
|
714
|
+
updatedAt
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
`;
|
|
718
|
+
const variables = {
|
|
719
|
+
input: {
|
|
720
|
+
entityId: options.entityId,
|
|
721
|
+
entityType: options.entityType,
|
|
722
|
+
metricType: options.metricType,
|
|
723
|
+
limit: options.limit,
|
|
724
|
+
period: options.period,
|
|
725
|
+
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
726
|
+
organizationId: options.organizationId
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
const response = await this.client.query(mutation, variables, this.context);
|
|
730
|
+
logger.info("[Usage] Quota set", {
|
|
731
|
+
quotaId: response.setQuota.id
|
|
732
|
+
});
|
|
733
|
+
return response.setQuota;
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Get quota
|
|
737
|
+
*/
|
|
738
|
+
async getQuota(entityId, entityType, metricType) {
|
|
739
|
+
logger.debug("[Usage] Getting quota", {
|
|
740
|
+
entityId,
|
|
741
|
+
metricType
|
|
742
|
+
});
|
|
743
|
+
const query = `
|
|
744
|
+
query GetQuota($entityId: ID!, $entityType: String!, $metricType: UsageMetricType!) {
|
|
745
|
+
getQuota(entityId: $entityId, entityType: $entityType, metricType: $metricType) {
|
|
746
|
+
id
|
|
747
|
+
entityId
|
|
748
|
+
entityType
|
|
749
|
+
metricType
|
|
750
|
+
limit
|
|
751
|
+
period
|
|
752
|
+
currentUsage
|
|
753
|
+
resetAt
|
|
754
|
+
createdAt
|
|
755
|
+
updatedAt
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
`;
|
|
759
|
+
const response = await this.client.query(query, {
|
|
760
|
+
entityId,
|
|
761
|
+
entityType,
|
|
762
|
+
metricType
|
|
763
|
+
}, this.context);
|
|
764
|
+
return response.getQuota;
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Get all quotas for entity
|
|
768
|
+
*/
|
|
769
|
+
async getQuotas(entityId, entityType) {
|
|
770
|
+
logger.debug("[Usage] Getting all quotas", {
|
|
771
|
+
entityId,
|
|
772
|
+
entityType
|
|
773
|
+
});
|
|
774
|
+
const query = `
|
|
775
|
+
query GetQuotas($entityId: ID!, $entityType: String!) {
|
|
776
|
+
getQuotas(entityId: $entityId, entityType: $entityType) {
|
|
777
|
+
id
|
|
778
|
+
entityId
|
|
779
|
+
entityType
|
|
780
|
+
metricType
|
|
781
|
+
limit
|
|
782
|
+
period
|
|
783
|
+
currentUsage
|
|
784
|
+
resetAt
|
|
785
|
+
createdAt
|
|
786
|
+
updatedAt
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
`;
|
|
790
|
+
const response = await this.client.query(query, {
|
|
791
|
+
entityId,
|
|
792
|
+
entityType
|
|
793
|
+
}, this.context);
|
|
794
|
+
return response.getQuotas;
|
|
795
|
+
}
|
|
796
|
+
/**
|
|
797
|
+
* Reset quota usage
|
|
798
|
+
*/
|
|
799
|
+
async resetQuota(entityId, entityType, metricType) {
|
|
800
|
+
logger.debug("[Usage] Resetting quota", {
|
|
801
|
+
entityId,
|
|
802
|
+
metricType
|
|
803
|
+
});
|
|
804
|
+
const mutation = `
|
|
805
|
+
mutation ResetQuota($entityId: ID!, $entityType: String!, $metricType: UsageMetricType!) {
|
|
806
|
+
resetQuota(entityId: $entityId, entityType: $entityType, metricType: $metricType) {
|
|
807
|
+
success
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
`;
|
|
811
|
+
await this.client.query(mutation, {
|
|
812
|
+
entityId,
|
|
813
|
+
entityType,
|
|
814
|
+
metricType
|
|
815
|
+
}, this.context);
|
|
816
|
+
logger.info("[Usage] Quota reset", {
|
|
817
|
+
entityId,
|
|
818
|
+
metricType
|
|
819
|
+
});
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Delete quota
|
|
824
|
+
*/
|
|
825
|
+
async deleteQuota(entityId, entityType, metricType) {
|
|
826
|
+
logger.debug("[Usage] Deleting quota", {
|
|
827
|
+
entityId,
|
|
828
|
+
metricType
|
|
829
|
+
});
|
|
830
|
+
const mutation = `
|
|
831
|
+
mutation DeleteQuota($entityId: ID!, $entityType: String!, $metricType: UsageMetricType!) {
|
|
832
|
+
deleteQuota(entityId: $entityId, entityType: $entityType, metricType: $metricType) {
|
|
833
|
+
success
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
`;
|
|
837
|
+
await this.client.query(mutation, {
|
|
838
|
+
entityId,
|
|
839
|
+
entityType,
|
|
840
|
+
metricType
|
|
841
|
+
}, this.context);
|
|
842
|
+
logger.info("[Usage] Quota deleted", {
|
|
843
|
+
entityId,
|
|
844
|
+
metricType
|
|
845
|
+
});
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* Bulk track usage
|
|
850
|
+
*/
|
|
851
|
+
async trackBulk(records) {
|
|
852
|
+
logger.debug("[Usage] Bulk tracking usage", {
|
|
853
|
+
count: records.length
|
|
854
|
+
});
|
|
855
|
+
const mutation = `
|
|
856
|
+
mutation TrackUsageBulk($records: [TrackUsageInput!]!) {
|
|
857
|
+
trackUsageBulk(records: $records) {
|
|
858
|
+
id
|
|
859
|
+
entityId
|
|
860
|
+
entityType
|
|
861
|
+
metricType
|
|
862
|
+
amount
|
|
863
|
+
resourceId
|
|
864
|
+
metadata
|
|
865
|
+
workspaceId
|
|
866
|
+
organizationId
|
|
867
|
+
recordedAt
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
`;
|
|
871
|
+
const response = await this.client.query(mutation, {
|
|
872
|
+
records
|
|
873
|
+
}, this.context);
|
|
874
|
+
logger.info("[Usage] Bulk usage tracked", {
|
|
875
|
+
count: response.trackUsageBulk.length
|
|
876
|
+
});
|
|
877
|
+
return response.trackUsageBulk;
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
export { BillingUsageClient, QuotaPeriod, UsageMetricType, UsageModule };
|
|
882
|
+
//# sourceMappingURL=chunk-CKL6U4XE.js.map
|
|
883
|
+
//# sourceMappingURL=chunk-CKL6U4XE.js.map
|