From 98187b4c0b402348af8f432c54d22103491484a6 Mon Sep 17 00:00:00 2001 From: Aravindus1 Date: Tue, 22 Jul 2025 23:31:50 +0530 Subject: [PATCH] dashboard changes --- queries/dashboard.js | 27 +++++++++++++++++++++++++++ server.js | 20 ++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/queries/dashboard.js b/queries/dashboard.js index d42c30c..0efda44 100644 --- a/queries/dashboard.js +++ b/queries/dashboard.js @@ -242,3 +242,30 @@ export const accountsWithDateRange = (startDate, endDate) => ` GROUP BY line_item_usage_account_id ORDER BY totalCost DESC `; +//dashboard/usage-per-month?startDate=2024-01-01&endDate=2025-12-31 + +export const usageCostPerMonthWithDateRange = (startDate, endDate) => ` + SELECT + CONCAT(year, '-', LPAD(month, 2, '0')) AS month, + SUM(line_item_usage_amount) AS totalUsageAmount, + SUM(line_item_unblended_cost) AS totalUnblendedCost, + SUM(line_item_blended_cost) AS totalBlendedCost + FROM ${process.env.ATHENA_CU_TABLE} + WHERE line_item_usage_start_date >= date('${startDate}') + AND line_item_usage_start_date <= date('${endDate}') + AND line_item_blended_cost > 0 + GROUP BY year, month + ORDER BY month ASC; +`; +//dashboard/yearly-spend +export const yearlySpendSummary = ` + SELECT + year AS usageYear, + SUM(line_item_usage_amount) AS totalUsageAmount, + SUM(line_item_unblended_cost) AS totalUnblendedCost, + SUM(line_item_blended_cost) AS totalBlendedCost + FROM ${process.env.ATHENA_CU_TABLE} + WHERE line_item_blended_cost > 0 + GROUP BY year + ORDER BY usageYear ASC; +`; diff --git a/server.js b/server.js index 49f81df..a781ff7 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,7 @@ import Fastify from "fastify"; import cors from "@fastify/cors"; import sequelizePlugin from "./plugins/sequelize.js"; import dotenv from "dotenv"; -import cors from "@fastify/cors"; + import { executeQueryAsync, retrieveResultsAsync } from "./services/athena.js"; dotenv.config(); import * as dashboard from "./queries/dashboard.js"; @@ -10,9 +10,7 @@ import * as queries from "./queries/queries.js"; import * as dashboardQueries from "./queries/dashboard.js"; const server = Fastify({ logger: true }); -await server.register(cors, { - origin: "*", -}); + server.register(sequelizePlugin); await server.register(cors, { @@ -376,6 +374,20 @@ server.get("/dashboard/accounts/:accountId/trends", async (request, reply) => { }; }); +server.get("/dashboard/usage-per-month", async (request, reply) => { + const { startDate, endDate } = request.query; + const query = dashboardQueries.usageCostPerMonthWithDateRange(startDate, endDate); + const queryExecutionId = await executeQueryAsync(query); + const results = await retrieveResultsAsync(queryExecutionId); + return results; +}); +server.get("/dashboard/yearly-spend", async (request, reply) => { + const query = dashboardQueries.yearlySpendSummary; + const queryExecutionId = await executeQueryAsync(query); + const results = await retrieveResultsAsync(queryExecutionId); + return results; +}); + try { await server.listen({ port: 3000 })