MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / eval_aggregation

Function eval_aggregation

nodedb/src/control/promql/evaluator/aggregate.rs:12–69  ·  view source on GitHub ↗
(
    op: AggOp,
    val: Value,
    param: Option<f64>,
    grouping: &Grouping,
    ts: i64,
)

Source from the content-addressed store, hash-verified

10use super::helpers::{group_key, group_labels};
11
12pub fn eval_aggregation(
13 op: AggOp,
14 val: Value,
15 param: Option<f64>,
16 grouping: &Grouping,
17 ts: i64,
18) -> Result<Value, PromqlError> {
19 let Value::Vector(samples) = val else {
20 return Err(PromqlError::TypeError {
21 context: "aggregation".to_string(),
22 detail: "requires instant vector".to_string(),
23 });
24 };
25
26 let mut groups: BTreeMap<String, Vec<&InstantSample>> = BTreeMap::new();
27 for s in &samples {
28 let key = group_key(&s.labels, grouping);
29 groups.entry(key).or_default().push(s);
30 }
31
32 let mut result = Vec::new();
33 for group in groups.values() {
34 let vals: Vec<f64> = group.iter().map(|s| s.value).collect();
35
36 // Topk/bottomk return individual series, not a single aggregate.
37 if matches!(op, AggOp::Topk | AggOp::Bottomk) {
38 let k = param.unwrap_or(1.0) as usize;
39 let mut sorted_group: Vec<&InstantSample> = group.clone();
40 sorted_group.sort_by(|a, b| {
41 b.value
42 .partial_cmp(&a.value)
43 .unwrap_or(std::cmp::Ordering::Equal)
44 });
45 if matches!(op, AggOp::Bottomk) {
46 sorted_group.reverse();
47 }
48 for s in sorted_group.into_iter().take(k) {
49 result.push(InstantSample {
50 labels: group_labels(&s.labels, grouping),
51 value: s.value,
52 timestamp_ms: ts,
53 });
54 }
55 continue;
56 }
57
58 let agg_val = compute_agg(op, &vals, param);
59
60 let labels = group_labels(&group[0].labels, grouping);
61 result.push(InstantSample {
62 labels,
63 value: agg_val,
64 timestamp_ms: ts,
65 });
66 }
67
68 Ok(Value::Vector(result))
69}

Callers 1

evalFunction · 0.85

Calls 11

group_labelsFunction · 0.85
compute_aggFunction · 0.85
to_stringMethod · 0.80
entryMethod · 0.80
collectMethod · 0.80
group_keyFunction · 0.70
pushMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45
partial_cmpMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected