@lssm/lib.cost-tracking 0.0.0-canary-20251217063201 → 0.0.0-canary-20251217072406

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.
@@ -1 +1,32 @@
1
- var e=class{limits=new Map;spend=new Map;constructor(e){this.options=e;for(let t of e.budgets)this.limits.set(t.tenantId,t),this.spend.set(t.tenantId,0)}track(e){if(!e.tenantId)return;let t=(this.spend.get(e.tenantId)??0)+e.total;this.spend.set(e.tenantId,t);let n=this.limits.get(e.tenantId);if(!n)return;let r=n.alertThreshold??.8;t>=n.monthlyLimit*r&&this.options.onAlert?.({tenantId:e.tenantId,limit:n.monthlyLimit,total:t,summary:e})}getSpend(e){return this.spend.get(e)??0}};export{e as BudgetAlertManager};
1
+ //#region src/budget-alert-manager.ts
2
+ var BudgetAlertManager = class {
3
+ limits = /* @__PURE__ */ new Map();
4
+ spend = /* @__PURE__ */ new Map();
5
+ constructor(options) {
6
+ this.options = options;
7
+ for (const budget of options.budgets) {
8
+ this.limits.set(budget.tenantId, budget);
9
+ this.spend.set(budget.tenantId, 0);
10
+ }
11
+ }
12
+ track(summary) {
13
+ if (!summary.tenantId) return;
14
+ const current = (this.spend.get(summary.tenantId) ?? 0) + summary.total;
15
+ this.spend.set(summary.tenantId, current);
16
+ const budget = this.limits.get(summary.tenantId);
17
+ if (!budget) return;
18
+ const threshold = budget.alertThreshold ?? .8;
19
+ if (current >= budget.monthlyLimit * threshold) this.options.onAlert?.({
20
+ tenantId: summary.tenantId,
21
+ limit: budget.monthlyLimit,
22
+ total: current,
23
+ summary
24
+ });
25
+ }
26
+ getSpend(tenantId) {
27
+ return this.spend.get(tenantId) ?? 0;
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ export { BudgetAlertManager };
@@ -1 +1,21 @@
1
- const e={dbReadCost:2e-6,dbWriteCost:1e-5,computeMsCost:15e-8,memoryMbMsCost:2e-8};function t(e,t){let n=(e.externalCalls??[]).reduce((e,t)=>e+(t.cost??0),0);return{dbReads:(e.dbReads??0)*t.dbReadCost,dbWrites:(e.dbWrites??0)*t.dbWriteCost,compute:(e.computeMs??0)*t.computeMsCost,memory:(e.memoryMbMs??0)*t.memoryMbMsCost,external:n,custom:e.customCost??0}}export{t as calculateSampleCost,e as defaultCostModel};
1
+ //#region src/cost-model.ts
2
+ const defaultCostModel = {
3
+ dbReadCost: 2e-6,
4
+ dbWriteCost: 1e-5,
5
+ computeMsCost: 15e-8,
6
+ memoryMbMsCost: 2e-8
7
+ };
8
+ function calculateSampleCost(sample, model) {
9
+ const external = (sample.externalCalls ?? []).reduce((sum, call) => sum + (call.cost ?? 0), 0);
10
+ return {
11
+ dbReads: (sample.dbReads ?? 0) * model.dbReadCost,
12
+ dbWrites: (sample.dbWrites ?? 0) * model.dbWriteCost,
13
+ compute: (sample.computeMs ?? 0) * model.computeMsCost,
14
+ memory: (sample.memoryMbMs ?? 0) * model.memoryMbMsCost,
15
+ external,
16
+ custom: sample.customCost ?? 0
17
+ };
18
+ }
19
+
20
+ //#endregion
21
+ export { calculateSampleCost, defaultCostModel };
@@ -1 +1,60 @@
1
- import{calculateSampleCost as e,defaultCostModel as t}from"./cost-model.js";var n=class{totals=new Map;costModel;constructor(e={}){this.options=e,this.costModel=e.costModel??t}recordSample(t){let n=e(t,this.costModel),r=n.dbReads+n.dbWrites+n.compute+n.memory+n.external+n.custom,i=this.buildKey(t.operation,t.tenantId),a=this.totals.get(i),o=a?{...a,total:a.total+r,breakdown:{dbReads:a.breakdown.dbReads+n.dbReads,dbWrites:a.breakdown.dbWrites+n.dbWrites,compute:a.breakdown.compute+n.compute,memory:a.breakdown.memory+n.memory,external:a.breakdown.external+n.external,custom:a.breakdown.custom+n.custom},samples:a.samples+1}:{operation:t.operation,tenantId:t.tenantId,total:r,breakdown:{dbReads:n.dbReads,dbWrites:n.dbWrites,compute:n.compute,memory:n.memory,external:n.external,custom:n.custom},samples:1};return this.totals.set(i,o),this.options.onSampleRecorded?.(t,r),o}getTotals(e){let t=Array.from(this.totals.values());return e?.tenantId?t.filter(t=>t.tenantId===e.tenantId):t}reset(){this.totals.clear()}buildKey(e,t){return t?`${t}:${e}`:e}};export{n as CostTracker};
1
+ import { calculateSampleCost, defaultCostModel } from "./cost-model.js";
2
+
3
+ //#region src/cost-tracker.ts
4
+ var CostTracker = class {
5
+ totals = /* @__PURE__ */ new Map();
6
+ costModel;
7
+ constructor(options = {}) {
8
+ this.options = options;
9
+ this.costModel = options.costModel ?? defaultCostModel;
10
+ }
11
+ recordSample(sample) {
12
+ const breakdown = calculateSampleCost(sample, this.costModel);
13
+ const total = breakdown.dbReads + breakdown.dbWrites + breakdown.compute + breakdown.memory + breakdown.external + breakdown.custom;
14
+ const key = this.buildKey(sample.operation, sample.tenantId);
15
+ const existing = this.totals.get(key);
16
+ const summary = existing ? {
17
+ ...existing,
18
+ total: existing.total + total,
19
+ breakdown: {
20
+ dbReads: existing.breakdown.dbReads + breakdown.dbReads,
21
+ dbWrites: existing.breakdown.dbWrites + breakdown.dbWrites,
22
+ compute: existing.breakdown.compute + breakdown.compute,
23
+ memory: existing.breakdown.memory + breakdown.memory,
24
+ external: existing.breakdown.external + breakdown.external,
25
+ custom: existing.breakdown.custom + breakdown.custom
26
+ },
27
+ samples: existing.samples + 1
28
+ } : {
29
+ operation: sample.operation,
30
+ tenantId: sample.tenantId,
31
+ total,
32
+ breakdown: {
33
+ dbReads: breakdown.dbReads,
34
+ dbWrites: breakdown.dbWrites,
35
+ compute: breakdown.compute,
36
+ memory: breakdown.memory,
37
+ external: breakdown.external,
38
+ custom: breakdown.custom
39
+ },
40
+ samples: 1
41
+ };
42
+ this.totals.set(key, summary);
43
+ this.options.onSampleRecorded?.(sample, total);
44
+ return summary;
45
+ }
46
+ getTotals(filter) {
47
+ const items = Array.from(this.totals.values());
48
+ if (!filter?.tenantId) return items;
49
+ return items.filter((item) => item.tenantId === filter.tenantId);
50
+ }
51
+ reset() {
52
+ this.totals.clear();
53
+ }
54
+ buildKey(operation, tenantId) {
55
+ return tenantId ? `${tenantId}:${operation}` : operation;
56
+ }
57
+ };
58
+
59
+ //#endregion
60
+ export { CostTracker };
package/dist/index.js CHANGED
@@ -1 +1,6 @@
1
- import{calculateSampleCost as e,defaultCostModel as t}from"./cost-model.js";import{CostTracker as n}from"./cost-tracker.js";import{BudgetAlertManager as r}from"./budget-alert-manager.js";import{OptimizationRecommender as i}from"./optimization-recommender.js";export{r as BudgetAlertManager,n as CostTracker,i as OptimizationRecommender,e as calculateSampleCost,t as defaultCostModel};
1
+ import { calculateSampleCost, defaultCostModel } from "./cost-model.js";
2
+ import { CostTracker } from "./cost-tracker.js";
3
+ import { BudgetAlertManager } from "./budget-alert-manager.js";
4
+ import { OptimizationRecommender } from "./optimization-recommender.js";
5
+
6
+ export { BudgetAlertManager, CostTracker, OptimizationRecommender, calculateSampleCost, defaultCostModel };
@@ -1 +1,39 @@
1
- var e=class{generate(e){let t=[],n=e.total/e.samples;return e.breakdown.dbReads/e.samples>1e3&&t.push({operation:e.operation,tenantId:e.tenantId,category:`n_plus_one`,message:`High average DB read count detected. Consider batching queries or adding pagination.`,evidence:{avgReads:e.breakdown.dbReads/e.samples}}),e.breakdown.compute/e.total>.6&&t.push({operation:e.operation,tenantId:e.tenantId,category:`batching`,message:`Compute dominates cost. Investigate hot loops or move heavy logic to background jobs.`,evidence:{computeShare:e.breakdown.compute/e.total}}),e.breakdown.external>n*.5&&t.push({operation:e.operation,tenantId:e.tenantId,category:`external`,message:`External provider spend is high. Reuse results or enable caching.`,evidence:{externalCost:e.breakdown.external}}),e.breakdown.memory>e.breakdown.compute*1.2&&t.push({operation:e.operation,tenantId:e.tenantId,category:`caching`,message:`Memory utilization suggests cached payloads linger. Tune TTL or stream responses.`,evidence:{memoryCost:e.breakdown.memory}}),t}};export{e as OptimizationRecommender};
1
+ //#region src/optimization-recommender.ts
2
+ var OptimizationRecommender = class {
3
+ generate(summary) {
4
+ const suggestions = [];
5
+ const avgCost = summary.total / summary.samples;
6
+ if (summary.breakdown.dbReads / summary.samples > 1e3) suggestions.push({
7
+ operation: summary.operation,
8
+ tenantId: summary.tenantId,
9
+ category: "n_plus_one",
10
+ message: "High average DB read count detected. Consider batching queries or adding pagination.",
11
+ evidence: { avgReads: summary.breakdown.dbReads / summary.samples }
12
+ });
13
+ if (summary.breakdown.compute / summary.total > .6) suggestions.push({
14
+ operation: summary.operation,
15
+ tenantId: summary.tenantId,
16
+ category: "batching",
17
+ message: "Compute dominates cost. Investigate hot loops or move heavy logic to background jobs.",
18
+ evidence: { computeShare: summary.breakdown.compute / summary.total }
19
+ });
20
+ if (summary.breakdown.external > avgCost * .5) suggestions.push({
21
+ operation: summary.operation,
22
+ tenantId: summary.tenantId,
23
+ category: "external",
24
+ message: "External provider spend is high. Reuse results or enable caching.",
25
+ evidence: { externalCost: summary.breakdown.external }
26
+ });
27
+ if (summary.breakdown.memory > summary.breakdown.compute * 1.2) suggestions.push({
28
+ operation: summary.operation,
29
+ tenantId: summary.tenantId,
30
+ category: "caching",
31
+ message: "Memory utilization suggests cached payloads linger. Tune TTL or stream responses.",
32
+ evidence: { memoryCost: summary.breakdown.memory }
33
+ });
34
+ return suggestions;
35
+ }
36
+ };
37
+
38
+ //#endregion
39
+ export { OptimizationRecommender };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lssm/lib.cost-tracking",
3
- "version": "0.0.0-canary-20251217063201",
3
+ "version": "0.0.0-canary-20251217072406",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -23,8 +23,8 @@
23
23
  "test": "bun run"
24
24
  },
25
25
  "devDependencies": {
26
- "@lssm/tool.tsdown": "0.0.0-canary-20251217063201",
27
- "@lssm/tool.typescript": "0.0.0-canary-20251217063201",
26
+ "@lssm/tool.tsdown": "0.0.0-canary-20251217072406",
27
+ "@lssm/tool.typescript": "0.0.0-canary-20251217072406",
28
28
  "tsdown": "^0.17.4",
29
29
  "typescript": "^5.9.3"
30
30
  },