forked from kamranahmedse/aws-cost-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost.ts
188 lines (159 loc) · 5.09 KB
/
cost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import AWS from 'aws-sdk';
import dayjs from 'dayjs';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
dayjs.extend(isSameOrAfter);
dayjs.extend(isSameOrBefore);
import { AWSConfig } from './config';
import { showSpinner } from './logger';
export type RawCostByService = {
[key: string]: {
[date: string]: number;
};
};
export async function getRawCostByService(
awsConfig: AWSConfig,
): Promise<RawCostByService> {
showSpinner('Getting pricing data');
const costExplorer = new AWS.CostExplorer(awsConfig);
const endDate = dayjs(); // `endDate` is set to 'today' but its cost be omitted because of API spec (see: https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html#API_GetCostAndUsage_RequestSyntax)
const startDate = endDate.subtract(65, 'day');
const groupByConfig = [
{
Type: 'DIMENSION',
Key: 'SERVICE',
},
];
let filterConfig = {
Not: {
Dimensions: {
Key: 'RECORD_TYPE',
Values: ['Credit', 'Refund', 'Upfront', 'Support'],
},
},
};
if (awsConfig.targetAccount) {
groupByConfig.push({
Type: 'DIMENSION',
Key: 'LINKED_ACCOUNT',
});
filterConfig = {
And: [
{
Dimensions: {
Key: 'LINKED_ACCOUNT',
Values: [awsConfig.targetAccount],
},
},
filterConfig,
],
};
}
// Get the cost and usage data for the specified account
const pricingData = await costExplorer
.getCostAndUsage({
TimePeriod: {
Start: startDate.format('YYYY-MM-DD'),
End: endDate.format('YYYY-MM-DD'),
},
Granularity: 'DAILY',
Filter: filterConfig,
Metrics: ['UnblendedCost'],
GroupBy: groupByConfig,
})
.promise();
const costByService = {};
for (const day of pricingData.ResultsByTime) {
for (const group of day.Groups) {
const filterKeys = group.Keys;
const serviceName = filterKeys.find((key) => !/^\d{12}$/.test(key)); // AWS service name is non-12-digits string
const cost = group.Metrics.UnblendedCost.Amount;
const costDate = day.TimePeriod.Start; // must be set to `Start` not `End` because the end of `Period` parameter will be omitted (see: https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html#API_GetCostAndUsage_RequestSyntax)
costByService[serviceName] = costByService[serviceName] || {};
costByService[serviceName][costDate] = parseFloat(cost);
}
}
return costByService;
}
export type TotalCosts = {
totals: {
lastMonth: number;
thisMonth: number;
last7Days: number;
yesterday: number;
};
totalsByService: {
lastMonth: { [key: string]: number };
thisMonth: { [key: string]: number };
last7Days: { [key: string]: number };
yesterday: { [key: string]: number };
};
};
function calculateServiceTotals(
rawCostByService: RawCostByService,
): TotalCosts {
const totals = {
lastMonth: 0,
thisMonth: 0,
last7Days: 0,
yesterday: 0,
};
const totalsByService = {
lastMonth: {},
thisMonth: {},
last7Days: {},
yesterday: {},
};
const startOfLastMonth = dayjs().subtract(1, 'month').startOf('month');
const startOfThisMonth = dayjs().startOf('month');
const startOfLast7Days = dayjs().subtract(7, 'day').startOf('day');
const startOfYesterday = dayjs().subtract(1, 'day').startOf('day');
for (const service of Object.keys(rawCostByService)) {
const servicePrices = rawCostByService[service];
let lastMonthServiceTotal = 0;
let thisMonthServiceTotal = 0;
let last7DaysServiceTotal = 0;
let yesterdayServiceTotal = 0;
for (const date of Object.keys(servicePrices)) {
const price = servicePrices[date];
const dateObj = dayjs(date);
if (dateObj.isSame(startOfLastMonth, 'month')) {
lastMonthServiceTotal += price;
}
if (dateObj.isSame(startOfThisMonth, 'month')) {
thisMonthServiceTotal += price;
}
if (
dateObj.isSameOrAfter(startOfLast7Days) &&
dateObj.isSameOrBefore(dayjs().startOf('day'))
) {
last7DaysServiceTotal += price;
}
if (dateObj.isSame(startOfYesterday, 'day')) {
yesterdayServiceTotal += price;
}
}
totalsByService.lastMonth[service] = lastMonthServiceTotal;
totalsByService.thisMonth[service] = thisMonthServiceTotal;
totalsByService.last7Days[service] = last7DaysServiceTotal;
totalsByService.yesterday[service] = yesterdayServiceTotal;
totals.lastMonth += lastMonthServiceTotal;
totals.thisMonth += thisMonthServiceTotal;
totals.last7Days += last7DaysServiceTotal;
totals.yesterday += yesterdayServiceTotal;
}
return {
totals,
totalsByService,
};
}
export async function getTotalCosts(awsConfig: AWSConfig): Promise<TotalCosts> {
const rawCosts = await getRawCostByService(awsConfig);
const totals = calculateServiceTotals(rawCosts);
return totals;
}
if (process.env.NODE_ENV === 'test') {
Object.assign(module.exports, {
calculateServiceTotals,
});
}