-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
helper.ts
99 lines (90 loc) · 2.69 KB
/
helper.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { isFiniteNumber } from '../../../../common/utils/is_finite_number';
import {
AwsLambdaArchitecture,
AWSLambdaPriceFactor,
} from './get_serverless_summary';
export function calcMemoryUsedRate({
memoryFree,
memoryTotal,
}: {
memoryFree?: number | null;
memoryTotal?: number | null;
}) {
if (!isFiniteNumber(memoryFree) || !isFiniteNumber(memoryTotal)) {
return undefined;
}
return (memoryTotal - memoryFree) / memoryTotal;
}
export function calcMemoryUsed({
memoryFree,
memoryTotal,
}: {
memoryFree?: number | null;
memoryTotal?: number | null;
}) {
if (!isFiniteNumber(memoryFree) || !isFiniteNumber(memoryTotal)) {
return undefined;
}
return memoryTotal - memoryFree;
}
const GB = 1024 ** 3;
/**
* To calculate the compute usage we need to multiply the "system.memory.total" by "faas.billed_duration".
* But the result of this calculation is in Bytes-milliseconds, as the "system.memory.total" is stored in bytes and the "faas.billed_duration" is stored in milliseconds.
* But to calculate the overall cost AWS uses GB-second, so we need to convert the result to this unit.
*/
export function convertComputeUsageToGbSec({
computeUsageBytesMs,
countInvocations,
}: {
computeUsageBytesMs?: number | null;
countInvocations?: number | null;
}) {
if (
!isFiniteNumber(computeUsageBytesMs) ||
!isFiniteNumber(countInvocations)
) {
return undefined;
}
const computeUsageGbSec = computeUsageBytesMs / GB / 1000;
return computeUsageGbSec * countInvocations;
}
export function calcEstimatedCost({
awsLambdaPriceFactor,
architecture,
transactionThroughput,
awsLambdaRequestCostPerMillion,
computeUsageGbSec,
}: {
awsLambdaPriceFactor?: AWSLambdaPriceFactor;
architecture?: AwsLambdaArchitecture;
transactionThroughput: number;
awsLambdaRequestCostPerMillion?: number;
computeUsageGbSec?: number;
}) {
try {
if (
!awsLambdaPriceFactor ||
!architecture ||
!isFiniteNumber(awsLambdaRequestCostPerMillion) ||
!isFiniteNumber(awsLambdaPriceFactor?.[architecture]) ||
!isFiniteNumber(computeUsageGbSec)
) {
return undefined;
}
const priceFactor = awsLambdaPriceFactor?.[architecture];
const estimatedCost =
computeUsageGbSec * priceFactor +
transactionThroughput * (awsLambdaRequestCostPerMillion / 1000000);
// Rounds up the decimals
return Math.ceil(estimatedCost * 100) / 100;
} catch (e) {
return undefined;
}
}