#!/usr/bin/env node
const { spawn } = require('child_process');
async function getCloudWatchAmortized() {
console.log('π° CLOUDWATCH 30-DAY AMORTIZED COST');
console.log('=' .repeat(40));
const server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
UMBRELLA_API_BASE_URL: 'https://api-front.umbrellacost.io/api/v1'
}
});
const responses = [];
server.stdout.on('data', (data) => {
const lines = data.toString().split('\n').filter(line => line.trim());
for (const line of lines) {
try {
const response = JSON.parse(line);
responses.push(response);
} catch (e) {}
}
});
await new Promise(resolve => setTimeout(resolve, 2000));
// Auth
server.stdin.write(JSON.stringify({
jsonrpc: "2.0", id: 1, method: "tools/call",
params: {
name: 'authenticate_user',
arguments: { username: 'david+saola@umbrellacost.com', password: 'Dsamsung1!' }
}
}) + '\n');
console.log('π Authenticating...');
await new Promise(resolve => setTimeout(resolve, 5000));
// Get accounts with correct tool name
server.stdin.write(JSON.stringify({
jsonrpc: "2.0", id: 2, method: "tools/call",
params: { name: 'api___user_management_accounts', arguments: {} }
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 4000));
const accountsResponse = responses.find(r => r.id === 2);
let accountId = null;
if (accountsResponse?.result?.content?.[0]?.text) {
const text = accountsResponse.result.content[0].text;
console.log('π Accounts response received');
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
if (data.accounts && data.accounts.length > 0) {
accountId = data.accounts[0].linkedaccid;
console.log(`π’ Using account: ${accountId}`);
}
} catch (e) {
console.log('Error parsing accounts:', e.message);
}
}
} else {
console.log('β No accounts response');
}
if (!accountId) {
console.log('β Could not get account ID - using fallback approach');
// Try without specific account filtering
server.stdin.write(JSON.stringify({
jsonrpc: "2.0", id: 3, method: "tools/call",
params: {
name: 'api___invoices_caui',
arguments: {
startDate: "2025-07-27",
endDate: "2025-08-26",
groupBy: "service",
periodGranLevel: "day",
costType: ["cost"],
isAmortized: "true",
cloud_context: "aws",
filters: {
service: "Amazon CloudWatch"
}
}
}
}) + '\n');
} else {
// Use specific account
server.stdin.write(JSON.stringify({
jsonrpc: "2.0", id: 3, method: "tools/call",
params: {
name: 'api___invoices_caui',
arguments: {
startDate: "2025-07-27",
endDate: "2025-08-26",
groupBy: "service",
periodGranLevel: "day",
costType: ["cost"],
isAmortized: "true",
cloud_context: "aws",
accountId: accountId,
filters: {
service: "Amazon CloudWatch"
}
}
}
}) + '\n');
}
console.log('π Getting CloudWatch amortized costs (30 days)...');
let found = false;
for (let i = 0; i < 30 && !found; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
const response = responses.find(r => r.id === 3);
if (response?.result?.content?.[0]?.text) {
found = true;
const text = response.result.content[0].text;
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
console.log('\nπ° CLOUDWATCH AMORTIZED COST RESULTS');
console.log('=' .repeat(42));
if (data.results && data.results.length > 0) {
let totalCost = 0;
let daysWithCost = 0;
data.results.forEach(day => {
const cost = parseFloat(day.cost || 0);
if (cost > 0) {
totalCost += cost;
daysWithCost++;
console.log(`π
${day.date}: $${cost.toFixed(6)}`);
}
});
console.log('\nπ SUMMARY:');
console.log(`π° Total CloudWatch Cost (30 days): $${totalCost.toFixed(6)}`);
console.log(`π Cost Type: Amortized`);
console.log(`π§ Service: Amazon CloudWatch`);
console.log(`π
Period: July 27 - August 26, 2025`);
console.log(`π Daily Average: $${(totalCost / 30).toFixed(6)}`);
console.log(`π Days with cost: ${daysWithCost}/30`);
if (totalCost > 0) {
console.log('\nβ
CloudWatch amortized cost analysis complete');
}
} else {
console.log('β No cost data in response');
console.log('Available keys:', Object.keys(data));
}
} catch (e) {
console.log('β JSON parse error:', e.message);
console.log('Response preview:', text.substring(0, 300));
}
} else {
console.log('β No JSON block found');
console.log('Response:', text.substring(0, 200));
}
break;
}
if (i % 10 === 0 && i > 0) console.log(`β³ ${i}s...`);
}
server.kill();
}
getCloudWatchAmortized().catch(console.error);