#!/usr/bin/env node
const { spawn } = require('child_process');
async function getCloudWatchWeekly() {
console.log('๐
CLOUDWATCH COSTS - WEEKLY CHUNKS');
console.log('Getting real daily costs in smaller batches');
console.log('=' .repeat(60));
const server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: process.cwd(),
env: {
...process.env,
UMBRELLA_API_BASE_URL: 'https://api-front.umbrellacost.io/api/v1'
}
});
const responses = [];
let requestId = 1;
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) {}
}
});
server.stderr.on('data', () => {}); // Suppress logs
await new Promise(resolve => setTimeout(resolve, 3000));
// Auth
const authRequest = {
jsonrpc: "2.0",
id: requestId++,
method: "tools/call",
params: {
name: 'authenticate_user',
arguments: {
username: 'david+saola@umbrellacost.com',
password: 'Dsamsung1!'
}
}
};
server.stdin.write(JSON.stringify(authRequest) + '\n');
await new Promise(resolve => setTimeout(resolve, 5000));
if (!responses.find(r => r.id === authRequest.id && r.result?.content?.[0]?.text?.includes('Successfully authenticated'))) {
console.log('โ Auth failed');
server.kill();
return;
}
console.log('โ
Authenticated');
// Try just last week first (Aug 19-26)
console.log('\n๐ Getting last week (Aug 19-26)...');
const weekRequest = {
jsonrpc: "2.0",
id: requestId++,
method: "tools/call",
params: {
name: 'api___invoices_caui',
arguments: {
startDate: "2025-08-19",
endDate: "2025-08-26",
costType: ["cost", "discount"],
periodGranLevel: "day",
groupBy: "service",
isAmortized: true,
accountId: "932213950603"
}
}
};
server.stdin.write(JSON.stringify(weekRequest) + '\n');
// Wait up to 15 seconds
let found = false;
for (let i = 0; i < 15 && !found; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
const response = responses.find(r => r.id === weekRequest.id);
if (response) {
found = true;
if (response.result?.content?.[0]?.text) {
const text = response.result.content[0].text;
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
const data = JSON.parse(jsonMatch[1]);
const cloudwatchData = data.filter(item =>
(item.service_name || '').toLowerCase() === 'amazoncloudwatch'
);
if (cloudwatchData.length > 0) {
console.log(`โ
Found ${cloudwatchData.length} CloudWatch records for last week\n`);
const dailyCosts = {};
cloudwatchData.forEach(item => {
const date = item.usage_date.split('T')[0];
const cost = parseFloat(item.total_cost) || 0;
dailyCosts[date] = cost;
});
console.log('๐๏ธ LAST WEEK ACTUAL CLOUDWATCH COSTS:');
console.log('='.repeat(45));
const sortedDates = Object.keys(dailyCosts).sort();
let weekTotal = 0;
sortedDates.forEach((date, index) => {
const cost = dailyCosts[date];
weekTotal += cost;
const dateObj = new Date(date + 'T12:00:00Z');
const dayName = dateObj.toLocaleDateString('en-US', { weekday: 'short' });
const monthDay = dateObj.toLocaleDateString('en-US', { month: 'short', day: '2-digit' });
console.log(`${monthDay} (${dayName}): $${cost.toFixed(2)}`);
});
console.log('='.repeat(45));
console.log(`๐ฐ Week Total: $${weekTotal.toFixed(2)}`);
console.log(`๐ Daily Average: $${(weekTotal / sortedDates.length).toFixed(2)}`);
// Show the variation
const costs = Object.values(dailyCosts);
const maxCost = Math.max(...costs);
const minCost = Math.min(...costs);
console.log(`๐ Highest: $${maxCost.toFixed(2)}`);
console.log(`๐ Lowest: $${minCost.toFixed(2)}`);
console.log(`๐ฏ Daily Variation: $${(maxCost - minCost).toFixed(2)}`);
// Extrapolate to 30 days
const avgDaily = weekTotal / sortedDates.length;
console.log(`\n๐ฎ 30-Day Projection: $${(avgDaily * 30).toFixed(2)}`);
} else {
console.log('โ No CloudWatch data in response');
}
}
} else {
console.log('โ No data in response');
}
}
}
if (!found) {
console.log('โ No response received');
}
server.kill();
}
getCloudWatchWeekly().catch(console.error);