@lssm/lib.cost-tracking 0.0.0-canary-20251206160926
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/README.md +30 -0
- package/dist/budget-alert-manager.d.ts +22 -0
- package/dist/budget-alert-manager.js +1 -0
- package/dist/cost-model.d.ts +14 -0
- package/dist/cost-model.js +1 -0
- package/dist/cost-tracker.d.ts +21 -0
- package/dist/cost-tracker.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1 -0
- package/dist/optimization-recommender.d.ts +8 -0
- package/dist/optimization-recommender.js +1 -0
- package/dist/types.d.ts +62 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @lssm/lib.cost-tracking
|
|
2
|
+
|
|
3
|
+
Cost attribution, budgeting, and optimization helpers for ContractSpec operations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Normalize DB/API/compute metrics into unified cost samples
|
|
8
|
+
- Attribute costs per operation, tenant, and feature flag
|
|
9
|
+
- Detect budget overruns with configurable alerts
|
|
10
|
+
- Generate optimization suggestions (N+1, caching, batching)
|
|
11
|
+
- Integrates with telemetry produced via `@lssm/lib.observability`
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { CostTracker, defaultCostModel } from '@lssm/lib.cost-tracking';
|
|
17
|
+
|
|
18
|
+
const tracker = new CostTracker({ costModel: defaultCostModel });
|
|
19
|
+
|
|
20
|
+
tracker.recordSample({
|
|
21
|
+
operation: 'orders.list',
|
|
22
|
+
tenantId: 'acme',
|
|
23
|
+
dbReads: 12,
|
|
24
|
+
dbWrites: 2,
|
|
25
|
+
externalCalls: [{ provider: 'stripe', cost: 0.02 }],
|
|
26
|
+
computeMs: 150,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const summary = tracker.getTotals();
|
|
30
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { OperationCostSummary, TenantBudget } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/budget-alert-manager.d.ts
|
|
4
|
+
interface BudgetAlertManagerOptions {
|
|
5
|
+
budgets: TenantBudget[];
|
|
6
|
+
onAlert?: (payload: {
|
|
7
|
+
tenantId: string;
|
|
8
|
+
limit: number;
|
|
9
|
+
total: number;
|
|
10
|
+
summary: OperationCostSummary;
|
|
11
|
+
}) => void;
|
|
12
|
+
}
|
|
13
|
+
declare class BudgetAlertManager {
|
|
14
|
+
private readonly options;
|
|
15
|
+
private readonly limits;
|
|
16
|
+
private readonly spend;
|
|
17
|
+
constructor(options: BudgetAlertManagerOptions);
|
|
18
|
+
track(summary: OperationCostSummary): void;
|
|
19
|
+
getSpend(tenantId: string): number;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { BudgetAlertManager, BudgetAlertManagerOptions };
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CostModel, CostSample } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/cost-model.d.ts
|
|
4
|
+
declare const defaultCostModel: CostModel;
|
|
5
|
+
declare function calculateSampleCost(sample: CostSample, model: CostModel): {
|
|
6
|
+
dbReads: number;
|
|
7
|
+
dbWrites: number;
|
|
8
|
+
compute: number;
|
|
9
|
+
memory: number;
|
|
10
|
+
external: number;
|
|
11
|
+
custom: number;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { calculateSampleCost, defaultCostModel };
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CostModel, CostSample, OperationCostSummary } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/cost-tracker.d.ts
|
|
4
|
+
interface CostTrackerOptions {
|
|
5
|
+
costModel?: CostModel;
|
|
6
|
+
onSampleRecorded?: (sample: CostSample, total: number) => void;
|
|
7
|
+
}
|
|
8
|
+
declare class CostTracker {
|
|
9
|
+
private readonly options;
|
|
10
|
+
private readonly totals;
|
|
11
|
+
private readonly costModel;
|
|
12
|
+
constructor(options?: CostTrackerOptions);
|
|
13
|
+
recordSample(sample: CostSample): OperationCostSummary;
|
|
14
|
+
getTotals(filter?: {
|
|
15
|
+
tenantId?: string;
|
|
16
|
+
}): OperationCostSummary[];
|
|
17
|
+
reset(): void;
|
|
18
|
+
private buildKey;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { CostTracker, CostTrackerOptions };
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BudgetAlert, CostModel, CostSample, ExternalCallCost, OperationCostSummary, OptimizationSuggestion, TenantBudget } from "./types.js";
|
|
2
|
+
import { calculateSampleCost, defaultCostModel } from "./cost-model.js";
|
|
3
|
+
import { CostTracker, CostTrackerOptions } from "./cost-tracker.js";
|
|
4
|
+
import { BudgetAlertManager, BudgetAlertManagerOptions } from "./budget-alert-manager.js";
|
|
5
|
+
import { OptimizationRecommender } from "./optimization-recommender.js";
|
|
6
|
+
export { BudgetAlert, BudgetAlertManager, BudgetAlertManagerOptions, CostModel, CostSample, CostTracker, CostTrackerOptions, ExternalCallCost, OperationCostSummary, OptimizationRecommender, OptimizationSuggestion, TenantBudget, calculateSampleCost, defaultCostModel };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
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};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { OperationCostSummary, OptimizationSuggestion } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/optimization-recommender.d.ts
|
|
4
|
+
declare class OptimizationRecommender {
|
|
5
|
+
generate(summary: OperationCostSummary): OptimizationSuggestion[];
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { OptimizationRecommender };
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface ExternalCallCost {
|
|
3
|
+
provider: string;
|
|
4
|
+
cost?: number;
|
|
5
|
+
durationMs?: number;
|
|
6
|
+
requestCount?: number;
|
|
7
|
+
}
|
|
8
|
+
interface CostSample {
|
|
9
|
+
operation: string;
|
|
10
|
+
tenantId?: string;
|
|
11
|
+
environment?: 'dev' | 'staging' | 'prod';
|
|
12
|
+
dbReads?: number;
|
|
13
|
+
dbWrites?: number;
|
|
14
|
+
computeMs?: number;
|
|
15
|
+
memoryMbMs?: number;
|
|
16
|
+
externalCalls?: ExternalCallCost[];
|
|
17
|
+
customCost?: number;
|
|
18
|
+
timestamp?: Date;
|
|
19
|
+
metadata?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
interface CostModel {
|
|
22
|
+
dbReadCost: number;
|
|
23
|
+
dbWriteCost: number;
|
|
24
|
+
computeMsCost: number;
|
|
25
|
+
memoryMbMsCost: number;
|
|
26
|
+
}
|
|
27
|
+
interface OperationCostSummary {
|
|
28
|
+
operation: string;
|
|
29
|
+
tenantId?: string;
|
|
30
|
+
total: number;
|
|
31
|
+
breakdown: {
|
|
32
|
+
dbReads: number;
|
|
33
|
+
dbWrites: number;
|
|
34
|
+
compute: number;
|
|
35
|
+
memory: number;
|
|
36
|
+
external: number;
|
|
37
|
+
custom: number;
|
|
38
|
+
};
|
|
39
|
+
samples: number;
|
|
40
|
+
}
|
|
41
|
+
interface TenantBudget {
|
|
42
|
+
tenantId: string;
|
|
43
|
+
monthlyLimit: number;
|
|
44
|
+
currency?: string;
|
|
45
|
+
alertThreshold?: number;
|
|
46
|
+
currentSpend?: number;
|
|
47
|
+
}
|
|
48
|
+
interface OptimizationSuggestion {
|
|
49
|
+
operation: string;
|
|
50
|
+
tenantId?: string;
|
|
51
|
+
category: 'n_plus_one' | 'caching' | 'batching' | 'external';
|
|
52
|
+
message: string;
|
|
53
|
+
evidence: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
interface BudgetAlert {
|
|
56
|
+
tenantId: string;
|
|
57
|
+
limit: number;
|
|
58
|
+
actual: number;
|
|
59
|
+
triggeredAt: Date;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { BudgetAlert, CostModel, CostSample, ExternalCallCost, OperationCostSummary, OptimizationSuggestion, TenantBudget };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lssm/lib.cost-tracking",
|
|
3
|
+
"version": "0.0.0-canary-20251206160926",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
14
|
+
"build": "bun build:bundle && bun build:types",
|
|
15
|
+
"build:bundle": "tsdown",
|
|
16
|
+
"build:types": "tsc --noEmit",
|
|
17
|
+
"dev": "bun build:bundle --watch",
|
|
18
|
+
"clean": "rimraf dist .turbo",
|
|
19
|
+
"lint": "bun lint:fix",
|
|
20
|
+
"lint:fix": "eslint src --fix",
|
|
21
|
+
"lint:check": "eslint src",
|
|
22
|
+
"test": "bun run"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@lssm/lib.observability": "workspace:*"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@lssm/tool.tsdown": "workspace:*",
|
|
29
|
+
"@lssm/tool.typescript": "workspace:*",
|
|
30
|
+
"tsdown": "^0.17.0",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./dist/index.js",
|
|
35
|
+
"./*": "./*"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|