Line data Source code
1 : // Licensed under the Apache License, Version 2.0
2 : // Copyright 2025, Michael Bushe, All rights reserved.
3 :
4 : import '../../resource/resource.dart';
5 : import 'metric.dart';
6 :
7 : /// MetricData represents a collection of metrics to be exported.
8 : class MetricData {
9 : /// The resource associated with the metrics.
10 : final Resource? resource;
11 :
12 : /// The collection of metrics.
13 : final List<Metric> metrics;
14 :
15 : /// Creates a new MetricData instance.
16 68 : MetricData({
17 : this.resource,
18 : required this.metrics,
19 : });
20 :
21 : /// Creates an empty MetricData instance.
22 1 : factory MetricData.empty() {
23 2 : return MetricData(metrics: []);
24 : }
25 :
26 : /// Returns a new MetricData instance with the metrics filtered by the given predicate.
27 0 : MetricData filter(bool Function(Metric metric) predicate) {
28 0 : return MetricData(
29 0 : resource: resource,
30 0 : metrics: metrics.where(predicate).toList(),
31 : );
32 : }
33 :
34 : /// Returns a new MetricData instance with the given metrics added.
35 0 : MetricData merge(MetricData other) {
36 0 : return MetricData(
37 0 : resource: resource ?? other.resource,
38 0 : metrics: [...metrics, ...other.metrics],
39 : );
40 : }
41 : }
|