@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.
- package/dist/budget-alert-manager.js +32 -1
- package/dist/cost-model.js +21 -1
- package/dist/cost-tracker.js +60 -1
- package/dist/index.js +6 -1
- package/dist/optimization-recommender.js +39 -1
- package/package.json +3 -3
|
@@ -1 +1,32 @@
|
|
|
1
|
-
|
|
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 };
|
package/dist/cost-model.js
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
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 };
|
package/dist/cost-tracker.js
CHANGED
|
@@ -1 +1,60 @@
|
|
|
1
|
-
import{calculateSampleCost
|
|
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
|
|
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
|
-
|
|
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-
|
|
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-
|
|
27
|
-
"@lssm/tool.typescript": "0.0.0-canary-
|
|
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
|
},
|