Skip to content

Commit

Permalink
Scan result
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-buddy committed Jan 24, 2024
1 parent 9f40845 commit b22f8ba
Showing 1 changed file with 2 additions and 92 deletions.
94 changes: 2 additions & 92 deletions packages/spacecat-shared-aws-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,6 @@

import { CostExplorerClient, GetCostAndUsageCommand } from "@aws-sdk/client-cost-explorer";

function calculatePreviousMonthDate() {
const date = new Date();
let month = 1 + date.getMonth(); // as month starts from 0
let year = date.getFullYear();
const endDate = `${String(year).padStart(4, '0')}/${String(month).padStart(2, '0')}/01`;
if (month === 1) { // if month is january then previous month will be december of previous year
month = 12;
year -= 1;
}
else {
month -= 1;
}
const startDate = `${String(year).padStart(4, '0')}/${String(month).padStart(2, '0')}/01`;
return { startDate, endDate };
}
const { startDate, endDate } = calculatePreviousMonthDate();
const input = {
"TimePeriod": {
"End": endDate,
"Start": startDate
},
"Granularity": "MONTHLY",
"Filter": {
"Tags": {
"Key": 'Adobe.ArchPath',
"Values": ['EC.SpaceCat.Services'],
"MatchOptions": ['EQUALS']
}
},
"Metrics": [
"UnblendedCost"
],
"GroupBy": [
{
"Key": "SERVICE",
"Type": "DIMENSION"
},
{
"Key": "Environment",
"Type": "TAG"
}
]
};

const parseYearDate = (str) => {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const date = new Date(str);
return `${months[date.getMonth()]}-${date.getFullYear().toString().substring(2)}`;
};

const serviceMap = (service) => {
switch (service) {
case 'AWS Lambda':
return 'LAMBDA';
case 'AWS Secrets Manager':
return 'SECRETS MANAGER';
case 'Amazon DynamoDB':
return 'DYNAMODB';
case 'Amazon Simple Storage Service':
return 'S3';
case 'Amazon Simple Queue Service':
return 'SQS';
case 'AmazonCloudWatch':
return 'CLOUDWATCH';
default:
return service;
}
};

export default class AWSCostApiClient {
static createFrom(context) {
Expand All @@ -92,30 +24,8 @@ export default class AWSCostApiClient {

const client = new CostExplorerClient({ region });

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable client.
}
async getUsageCost() {
async getUsageCost(input) {
const command = new GetCostAndUsageCommand(input);
let response;
try {
response = await client.send(command);
} catch (err) {
throw new Error(`Error in getting COGs usage cost: ${err}`);
}
if (response && response.ResultsByTime && response.ResultsByTime.length > 0) {
let result;
response.ResultsByTime.forEach((result) => {
if (result.Groups && result.Groups.length > 0) {
let granularity;
result.Groups.forEach((group) => {
const key = group.Keys[0];
if (group.Metrics && group.Metrics.UnblendedCost) {
granularity[serviceMap(key)] = parseFloat(group.Metrics.UnblendedCost.Amount).toFixed(2);
}
});
result[parseYearDate(result.TimePeriod.Start)] = granularity;
}
});
return result;
}
return {};
return await client.send(command);
}
}

0 comments on commit b22f8ba

Please sign in to comment.