-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
198 lines (183 loc) · 5.26 KB
/
index.ts
File metadata and controls
198 lines (183 loc) · 5.26 KB
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
189
190
191
192
193
194
195
196
197
198
import * as AWS from 'aws-sdk'
import {
Context,
APIGatewayProxyResultV2,
APIGatewayProxyEventV2
} from 'aws-lambda'
import * as schema from './docs/schema.json'
import { calculate } from 'cloud-spend-forecaster'
import validate from 'jsonschema'
const dynamoDb = new AWS.DynamoDB.DocumentClient()
const TableName = process.env.SCORE_TABLE
export const units = 60000 // 1 minute in milliseconds
export const pullers = [
{ time: 1637798400000, requests: 50 }, // desc: 'start thursday 00:00' },
{ time: 1637884800000, requests: 100 }, // desc: 'black friday 00:00' },
{ time: 1637931600000, requests: 40000 }, // desc: 'black friday 13:00' },
{ time: 1637971200000, requests: 150 }, // desc: 'saturday 00:00' },
{ time: 1638144000000, requests: 150 }, // desc: 'cyber monday 00:00' },
{ time: 1638190800000, requests: 25000 }, // desc: 'cyber monday 13:00' },
{ time: 1638230400000, requests: 1500 }, // desc: 'Tuesday 00:00' },
{ time: 1638316799000, requests: 50 } // desc: 'end tuesday 23:59' }
]
export const failedRequestPenalty = 0.01
export const baseLineCost = 19907
export const nodes = {
't2.medium': {
maxPods: 17,
availableCpu: 3820,
availableMemory: 3358,
cost: 0.0464 / 60,
scalingIntervals: 5
},
'm5.4xlarge': {
maxPods: 234,
availableCpu: 15790,
availableMemory: 60971,
cost: 0.768 / 60,
scalingIntervals: 5
},
'm5.8xlarge': {
maxPods: 234,
availableCpu: 31750,
availableMemory: 124971,
cost: 1.536 / 60,
scalingIntervals: 5
},
'm5.16xlarge': {
maxPods: 737,
availableCpu: 47710,
availableMemory: 247438,
cost: 3.072 / 60,
scalingIntervals: 5
}
}
export async function play (
event: APIGatewayProxyEventV2,
context: Context
): Promise<APIGatewayProxyResultV2> {
const payload = JSON.parse(event.body)
const res = validate.validate(payload, schema)
if (!res.valid) throw new Error('INVALID PAYLOAD')
const node = {
...nodes[payload.Nodes['Node Type']],
minNodes: payload.Nodes['Node minimum count'],
maxNodes: payload.Nodes['Node maximum count']
}
const components = [
{
name: 'backend',
requestToCpu: 15,
requestToMemory: 4,
baselineCpu: 250,
baselineMemory: 512,
limitMemory: payload.Backend['Memory Limit'],
limitCpu: payload.Backend['CPU Limit'],
minReplica: payload.Backend['Minimum Replica'],
maxReplica: payload.Backend['Maximum Replica'],
scalingThresholdCpu: payload.Backend['CPU Scaling Threshold'],
scalingIntervals: 2
},
{
name: 'frontend',
requestToCpu: 10,
requestToMemory: 0.5,
baselineCpu: 50,
baselineMemory: 32,
limitMemory: payload.Frontend['Memory Limit'],
limitCpu: payload.Frontend['CPU Limit'],
minReplica: payload.Frontend['Minimum Replica'],
maxReplica: payload.Frontend['Maximum Replica'],
scalingThresholdCpu: payload.Frontend['CPU Scaling Threshold'],
scalingIntervals: 2
},
{
name: 'database',
requestToCpu: 20,
requestToMemory: 2,
baselineCpu: 500,
baselineMemory: 1024,
limitMemory: payload.Database['Memory Limit'],
limitCpu: payload.Database['CPU Limit'],
minReplica: payload.Database['Minimum Replica'],
maxReplica: payload.Database['Maximum Replica'],
scalingThresholdCpu: payload.Database['CPU Scaling Threshold'],
scalingIntervals: 2
}
]
console.log(components)
const data = calculate(
pullers,
units,
components,
node,
failedRequestPenalty
).map(interval => {
return {
time: interval.time,
requests: interval.requests,
failedRequests: interval.failedRequests,
cost: interval.cost
}
})
const totalRequests = Math.ceil(
data.reduce((accumulator, interval) => accumulator + interval.requests, 0)
)
const failedRequests = Math.ceil(
data.reduce(
(accumulator, interval) => accumulator + interval.failedRequests,
0
)
)
let spend = data.reduce(
(accumulator, interval) => accumulator + interval.cost,
0
)
const penalties = failedRequests * failedRequestPenalty
const savings = baseLineCost - spend
const score = savings - penalties
const output = {
id: event.requestContext.requestId,
data: data
.filter((interval, i) => i % 60 === 0 || interval.failedRequests > 1) // reduce how much data we send the browser
.map(interval => {
return {
time: interval.time,
requests: interval.requests,
failedRequests: interval.failedRequests
}
}),
totalRequests,
failedRequests,
spend,
penalties,
savings,
score
}
await dynamoDb
.put({
TableName,
Item: {
id: event.requestContext.requestId,
handle: payload.player.Handle || 'anonymous',
realname: payload.player['Real Name'] || 'anonymous',
email: payload.player['Email Address'] || 'anonymous',
companyname: payload.player.Company || 'anonymous',
totalRequests,
failedRequests,
penalties,
spend,
savings,
score,
configuration: payload
}
})
.promise()
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(output)
}
}