#!/usr/bin/env node
const { spawn } = require('child_process');
async function getCloudWatch30DayFinal() {
console.log('π° CLOUDWATCH 30-DAY AMORTIZED COST - FINAL VERSION');
console.log('π’ AWS Account: 932213950603');
console.log('π
Period: July 27 - August 26, 2025 (30 days)');
console.log('π§ Using fixed server-side filtering');
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 {
responses.push(JSON.parse(line));
} catch (e) {}
}
});
// Show server logs for filter debugging
server.stderr.on('data', (data) => {
const msg = data.toString();
if (msg.includes('[CLOUDWATCH-FILTER]') || msg.includes('filters[service]')) {
console.log('π§ SERVER:', msg.trim());
}
});
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, 6000));
// First, let's test with a single day to see what service name works
console.log('π§ͺ Testing Aug 4th to find correct service name...');
server.stdin.write(JSON.stringify({
jsonrpc: "2.0", id: 2, method: "tools/call",
params: {
name: 'api___invoices_caui',
arguments: {
startDate: "2025-08-04",
endDate: "2025-08-04",
groupBy: "service",
periodGranLevel: "day",
costType: ["cost"],
isAmortized: true,
cloud_context: "aws",
accountId: "932213950603"
// NO FILTER - get all services to find CloudWatch
}
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 8000));
const allServicesResponse = responses.find(r => r.id === 2);
let cloudwatchServiceName = null;
if (allServicesResponse?.result?.content?.[0]?.text) {
const text = allServicesResponse.result.content[0].text;
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
if (data.results && data.results.length > 0) {
// Look for CloudWatch services
data.results.forEach(result => {
const service = result.service || result.service_name || '';
const cost = parseFloat(result.cost || result.total_cost || 0);
if (service.toLowerCase().includes('cloudwatch') ||
service.toLowerCase().includes('amazon cloudwatch')) {
console.log(`π― FOUND CloudWatch: "${service}" - $${cost.toFixed(2)}`);
if (!cloudwatchServiceName && cost > 0) {
cloudwatchServiceName = service;
}
}
});
if (!cloudwatchServiceName) {
console.log('β No CloudWatch service found in results');
console.log('Available services (first 10):');
data.results.slice(0, 10).forEach(r => {
console.log(` "${r.service}" - $${parseFloat(r.cost || 0).toFixed(2)}`);
});
}
}
} catch (e) {
console.log('β Parse error:', e.message);
}
}
}
if (cloudwatchServiceName) {
console.log(`\nβ
Using CloudWatch service: "${cloudwatchServiceName}"`);
console.log('π Now getting 30-day costs with proper filtering...');
// Now get 30-day costs with the correct service name
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: "none",
periodGranLevel: "day",
costType: ["cost"],
isAmortized: true,
cloud_context: "aws",
accountId: "932213950603",
filters: {
service: cloudwatchServiceName
}
}
}
}) + '\n');
await new Promise(resolve => setTimeout(resolve, 12000));
const costResponse = responses.find(r => r.id === 3);
if (costResponse?.result?.content?.[0]?.text) {
const text = costResponse.result.content[0].text;
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
if (Array.isArray(data) && data.length > 0) {
// Handle array format
let totalCost = 0;
let dayCount = 0;
console.log('\nπ° CloudWatch Daily Costs (30 days):');
data.forEach((day, i) => {
if (day && day.total_cost !== undefined) {
const cost = parseFloat(day.total_cost || 0);
const date = day.usage_date || `Day ${i+1}`;
if (cost > 0) {
totalCost += cost;
dayCount++;
console.log(`π
${date}: $${cost.toFixed(8)}`);
}
}
});
console.log(`\nπ CLOUDWATCH 30-DAY AMORTIZED COST: $${totalCost.toFixed(8)}`);
console.log(`π Days with costs: ${dayCount}/31`);
console.log(`π Daily average: $${(totalCost/30).toFixed(8)}`);
if (totalCost < 10000) {
console.log('β
Realistic CloudWatch costs (not total account costs)');
} else {
console.log('β Still looks like total account costs');
}
} else {
console.log('β Unexpected response format');
console.log('Response keys:', Object.keys(data));
}
} catch (e) {
console.log('β 30-day parse error:', e.message);
}
}
} else {
console.log('β No 30-day response received');
}
} else {
console.log('\nβ Could not find CloudWatch service - cannot proceed with 30-day calculation');
}
server.kill();
}
getCloudWatch30DayFinal().catch(console.error);