-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.dart
59 lines (51 loc) · 2.04 KB
/
example.dart
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
import 'dart:convert';
import 'dart:math';
import 'package:eagleyeix/metric.dart';
/// A simple example of metrics.
class AppMetrics {
static final create = ExMetricKey(name: [
'eagleyeix',
'example',
'create'
], dimensions: {
ExMetricDimStatus.key: ExMetricDimStatus.found,
ExMetricDimUnit.key: ExMetricDimUnit.count
});
static final random = ExMetricKey(
name: ['eagleyeix', 'example', 'random'],
dimensions: {ExMetricDimUnit.key: ExMetricDimUnit.bytes});
}
void main() {
// Create random metrics just for exxamples.
var random = Random();
// Create a metric store and add some metrics to it
final store = ExMetricStoreHolder().store;
// Add twice one hundred metrics to the store
for (int _ in Iterable.generate(100)) {
store.addMetric(AppMetrics.create, 1);
store.addMetric(AppMetrics.random, random.nextInt(50).toDouble());
}
// Create a condition that checks if the unit is count
final ifCount = ExPostConditions.and([
ExPostConditions.valueMoreThan(0),
ExPostConditions.filterByAnyDimension(
{ExMetricDimUnit.key: ExMetricDimUnit.count})
]);
// Create two aggregations, once that does a count and the other that does a median
final aggregations = ExMetricAggregations.list([
// Count the metrics that are count unit and shrink it by logarithm
ExMetricAggregations.count(
postCondition: ifCount, shrinker: ExMetricDoubleShrinkers.log(base: 2)),
// Median the metrics that are not count unit and round them to the ceiling
ExMetricAggregations.median(
postCondition: ExPostConditions.not(ifCount),
shrinker: ExMetricDoubleShrinkers.ceil())
]);
// Aggregate all metrics in the store and print them
final stats = store.aggregateAll(aggregations);
print(jsonEncode(stats));
// [
// {"key":{"name":["eagleyeix","example","create"],"dimensions":{"status":"found","unit":"count","aggregation":"count"}},"value":6.0},
// {"key":{"name":["eagleyeix","example","random"],"dimensions":{"unit":"bytes","aggregation":"median"}},"value":24.0}
//]
}