forked from aws/aws-cdk
/
statistic.ts
102 lines (92 loc) · 2.61 KB
/
statistic.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
export interface SimpleStatistic {
type: 'simple';
statistic: Statistic;
}
export interface PercentileStatistic {
type: 'percentile';
percentile: number;
}
export interface GenericStatistic {
type: 'generic';
statistic: string;
}
/**
* Parse a statistic, returning the type of metric that was used (simple or percentile)
*/
export function parseStatistic(stat: string): SimpleStatistic | PercentileStatistic | GenericStatistic {
const lowerStat = stat.toLowerCase();
// Simple statistics
const statMap: {[k: string]: Statistic} = {
average: Statistic.AVERAGE,
avg: Statistic.AVERAGE,
minimum: Statistic.MINIMUM,
min: Statistic.MINIMUM,
maximum: Statistic.MAXIMUM,
max: Statistic.MAXIMUM,
samplecount: Statistic.SAMPLE_COUNT,
n: Statistic.SAMPLE_COUNT,
sum: Statistic.SUM,
};
if (lowerStat in statMap) {
return {
type: 'simple',
statistic: statMap[lowerStat],
};
}
// Percentile statistics
const re = /^p([\d.]+)$/;
const m = re.exec(lowerStat);
if (m) {
return {
type: 'percentile',
percentile: parseFloat(m[1]),
};
}
return {
type: 'generic',
statistic: stat,
};
}
export function normalizeStatistic(stat: string): string {
const parsed = parseStatistic(stat);
if (parsed.type === 'simple' || parsed.type === 'generic') {
return parsed.statistic;
} else {
// Already percentile. Avoid parsing because we might get into
// floating point rounding issues, return as-is but lowercase the p.
return stat.toLowerCase();
}
}
/**
* Enum for simple statistics
*
* (This is a private copy of the type in `metric-types.ts`; this type should always
* been private, the public one has been deprecated and isn't used anywhere).
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Statistics-definitions.html
*/
export enum Statistic {
/**
* The count (number) of data points used for the statistical calculation.
*/
SAMPLE_COUNT = 'SampleCount',
/**
* The value of Sum / SampleCount during the specified period.
*/
AVERAGE = 'Average',
/**
* All values submitted for the matching metric added together.
* This statistic can be useful for determining the total volume of a metric.
*/
SUM = 'Sum',
/**
* The lowest value observed during the specified period.
* You can use this value to determine low volumes of activity for your application.
*/
MINIMUM = 'Minimum',
/**
* The highest value observed during the specified period.
* You can use this value to determine high volumes of activity for your application.
*/
MAXIMUM = 'Maximum',
}