-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SimpleMetric.ts
118 lines (109 loc) · 3.73 KB
/
SimpleMetric.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
import { AbstractMetric } from './AbstractMetric';
import { MetricValue } from './BasicInsightsMetricData';
/**
* Class to represent a simple metric.
*
* @author Tiago Grosso <tiagogrosso99@gmail.com>
* @since 0.1.0
*/
export class SimpleMetric extends AbstractMetric<MetricValue<number>[]> {
/**
* Gets the metric values that match the provided expression.
*
* @param expression expression to match the values to.
*
* @returns the metric values that match the provided expression.
*/
public getByExpression(expression: (elem: MetricValue<number>) => boolean): MetricValue<number>[] {
return this.metricData.values.filter(expression);
}
/**
* Gets the metric values greater than the provided limit.
*
* @param limit the limit.
*
* @returns the metric values greater than the provided limit.
*/
public getGreaterThan(limit: number): MetricValue<number>[] {
return this.getByExpression((elem) => elem.value > limit);
}
/**
* Gets the metric values greater than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the metric values greater than or equal to the provided limit.
*/
public getGreaterThanOrEqual(limit: number): MetricValue<number>[] {
return this.getByExpression((elem) => elem.value >= limit);
}
/**
* Gets the metric values smaller than the provided limit.
*
* @param limit the limit.
*
* @returns the metric values smaller than the provided limit.
*/
public getLessThan(limit: number): MetricValue<number>[] {
return this.getByExpression((elem) => elem.value < limit);
}
/**
* Gets the metric values smaller than or equal to the provided limit.
*
* @param limit the limit.
*
* @returns the metric values smaller than or equal to the provided limit.
*/
public getLessThanOrEqual(limit: number): MetricValue<number>[] {
return this.getByExpression((elem) => elem.value <= limit);
}
/**
* Gets the metric values equal to the provided value.
*
* @param value the limit.
*
* @returns the metric values equal to the provided value.
*/
public getEqual(value: number): MetricValue<number>[] {
return this.getByExpression((elem) => elem.value === value);
}
/**
* Gets the metric values between the provided bounds (non-inclusive).
*
* @param lower the lower bound.
* @param upper the upper bound.
*
* @returns the metric values between the provided bounds (non-inclusive).
*/
public getBetween(lower: number, upper: number): MetricValue<number>[] {
return this.getByExpression((elem) => upper > elem.value && elem.value > lower);
}
/**
* Returns the highest value of the metric. In case of a tie, the first value found is returned.
*
* @returns the highest value of the metric.
*/
public getHighest(): MetricValue<number> | undefined {
let highest: MetricValue<number> | undefined;
this.metricData.values.forEach((metric) => {
if (highest == undefined || metric.value > highest.value) {
highest = metric;
}
});
return highest;
}
/**
* Returns the lowest value of the metric. In case of a tie, the first value found is returned.
*
* @returns the lowest value of the metric.
*/
public getLowest(): MetricValue<number> | undefined {
let lowest: MetricValue<number> | undefined;
this.metricData.values.forEach((metric) => {
if (lowest == undefined || metric.value < lowest.value) {
lowest = metric;
}
});
return lowest;
}
}