Setting the file. One moment. Calculate · Amazon Keyspaces · aws/agent-toolkit-for-aws · Skills Docs10
Setup DevOps Agent
33
AWS Deployment
scripts/calculate.ts
scripts/calculate.ts
TypeScript·151 lines·6 KB
16
calculateReadUnitsPerOperation,
17 calculateTtlUnitsPerOperation,
18 type DatacenterRef,
19 type EstimateResults,
20 type PricingEstimateResult,
21} from './calculator/PricingFormulas';
22
23const regionsMap: Record<string, string> = require('../assets/data/regions.json');
24
25// ─── Main ─────────────────────────────────────────────────────────────────────
26
27function main() {
28 const args = process.argv.slice(2);
29 if (args.length < 5) {
30 console.error('Usage: calculate.ts <region> <reads/s> <writes/s> <rowSizeBytes> <storageGB> [ttl/s] [pitr]');
31 process.exit(1);
32 }
33
34 const [regionArg, readsArg, writesArg, rowSizeArg, storageArg, ttlArg = '0', pitrArg = 'false'] = args;
35
36 const longRegion = regionsMap[regionArg] ?? regionArg;
37 const reads_per_second = Number(readsArg);
38 const writes_per_second = Number(writesArg);
39 const avg_row_size_bytes = Number(rowSizeArg);
40 const storage_gb = Number(storageArg);
41 const ttls_per_second = Number(ttlArg);
42 const pitr_enabled = pitrArg === 'true';
43
44 // Input validation
45 if (isNaN(reads_per_second) || reads_per_second < 0) { console.error('reads_per_second must be a non-negative number'); process.exit(1); }
46 if (isNaN(writes_per_second) || writes_per_second < 0) { console.error('writes_per_second must be a non-negative number'); process.exit(1); }
47 if (isNaN(avg_row_size_bytes) || avg_row_size_bytes <= 0) { console.error('avg_row_size_bytes must be a positive number'); process.exit(1); }
48 if (isNaN(storage_gb) || storage_gb < 0) { console.error('storage_gb must be a non-negative number'); process.exit(1); }
49 if (isNaN(ttls_per_second) || ttls_per_second < 0) { console.error('ttls_per_second must be a non-negative number'); process.exit(1); }
50
51 // Build inputs for calculatePricingEstimate
52 const datacenters: DatacenterRef[] = [{ name: regionArg, nodeCount: 0 }];
53 const regions: Record<string, string> = { [regionArg]: longRegion };
54 const estimateResults: EstimateResults = {
55 [regionArg]: {
56 default: {
57 keyspace_name: 'default',
58 keyspace_type: 'user',
59 replication_factor: 3,
60 total_live_space_gb: storage_gb,
61 uncompressed_single_replica_gb: storage_gb,
62 avg_read_row_size_bytes: avg_row_size_bytes,
63 avg_write_row_size_bytes: avg_row_size_bytes,
64 reads_per_second,
65 writes_per_second,
66 ttls_per_second,
67 use_backup: pitr_enabled,
68 },
69 },
70 };
71
72 const pricing: PricingEstimateResult | null = calculatePricingEstimate(datacenters, regions, estimateResults);
73 if (!pricing) {
74 console.error(`Region not found: ${longRegion}`);
75 process.exit(1);
76 }
77
78 // Extract per-keyspace costs from the result
79 const dcCost = pricing.total_datacenter_cost[regionArg];
80 const kc = dcCost.keyspaceCosts['default'];
81
82 const odTotal = pricing.total_monthly_on_demand_cost;
83 const provTotal = pricing.total_monthly_provisioned_cost;
84 const odTotalSP = pricing.total_monthly_on_demand_cost_savings;
85 const provTotalSP = pricing.total_monthly_provisioned_cost_savings;
86 const savingsPlanAvailable = odTotalSP !== odTotal || provTotalSP !== provTotal;
87
88 const result = {
89 region: { short: regionArg, long: longRegion },
90 inputs: {
91 reads_per_second,
92 writes_per_second,
93 avg_row_size_bytes,
94 storage_gb,
95 ttls_per_second,
96 pitr_enabled,
97 },
98 units_per_operation: {
99 write: calculateWriteUnitsPerOperation(avg_row_size_bytes),
100 read: calculateReadUnitsPerOperation(avg_row_size_bytes),
101 ttl: calculateTtlUnitsPerOperation(avg_row_size_bytes),
102 },
103 on_demand: {
104 reads_strong: kc.reads_on_demand,
105 reads_eventual: kc.reads_on_demand / 2,
106 writes: kc.writes_on_demand,
107 ttl_deletes: kc.ttlDeletes,
108 storage: kc.storage,
109 backup: kc.backup,
110 total: odTotal,
111 },
112 provisioned: {
113 reads_strong: kc.reads_provisioned,
114 reads_eventual: kc.reads_provisioned / 2,
115 writes: kc.writes_provisioned,
116 ttl_deletes: kc.ttlDeletes,
117 storage: kc.storage,
118 backup: kc.backup,
119 total: provTotal,
120 },
121 savings_plan_available: savingsPlanAvailable,
122 on_demand_savings_plan: savingsPlanAvailable ? {
123 reads_strong: kc.reads_on_demand_savings,
124 reads_eventual: kc.reads_on_demand_savings / 2,
125 writes: kc.writes_on_demand_savings,
126 ttl_deletes: kc.ttlDeletes,
127 storage: kc.storage,
128 backup: kc.backup,
129 total: odTotalSP,
130 } : null,
131 provisioned_savings_plan: savingsPlanAvailable ? {
132 reads_strong: kc.reads_provisioned_savings,
133 reads_eventual: kc.reads_provisioned_savings / 2,
134 writes: kc.writes_provisioned_savings,
135 ttl_deletes: kc.ttlDeletes,
136 storage: kc.storage,
137 backup: kc.backup,
138 total: provTotalSP,
139 } : null,
140 report_data: {
141 datacenters,
142 regions,
143 estimateResults,
144 pricing,
145 },
146 };
147
148 console.log(JSON.stringify(result, null, 2));
149}
150
151main();