console_subscriber/
attribute.rs

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
use console_api as proto;
use std::collections::HashMap;
use tracing::span::Id;

#[derive(Debug, Default)]
pub(crate) struct Attributes {
    attributes: HashMap<FieldKey, proto::Attribute>,
}

#[derive(Debug, Clone)]
pub(crate) struct Update {
    pub(crate) field: proto::Field,
    pub(crate) op: Option<UpdateOp>,
    pub(crate) unit: Option<String>,
}

#[derive(Debug, Clone)]
pub(crate) enum UpdateOp {
    Add,
    Override,
    Sub,
}

/// Represents a key for a `proto::field::Name`. Because the
/// proto::field::Name might not be unique we also include the
/// resource id in this key
#[derive(Debug, Hash, PartialEq, Eq)]
struct FieldKey {
    update_id: Id,
    field_name: proto::field::Name,
}

// === impl Attributes ===

impl Attributes {
    pub(crate) fn values(&self) -> impl Iterator<Item = &proto::Attribute> {
        self.attributes.values()
    }

    pub(crate) fn update(&mut self, id: &Id, update: &Update) {
        let field_name = match update.field.name.as_ref() {
            Some(name) => name.clone(),
            None => {
                tracing::warn!(?update.field, "field missing name, skipping...");
                return;
            }
        };
        let update_id = id.clone();
        let key = FieldKey {
            update_id,
            field_name,
        };

        self.attributes
            .entry(key)
            .and_modify(|attr| update_attribute(attr, update))
            .or_insert_with(|| update.clone().into());
    }
}

fn update_attribute(attribute: &mut proto::Attribute, update: &Update) {
    use proto::field::Value::*;
    let attribute_val = attribute.field.as_mut().and_then(|a| a.value.as_mut());
    let update_val = update.field.value.clone();
    let update_name = update.field.name.clone();
    match (attribute_val, update_val) {
        (Some(BoolVal(v)), Some(BoolVal(upd))) => *v = upd,

        (Some(StrVal(v)), Some(StrVal(upd))) => *v = upd,

        (Some(DebugVal(v)), Some(DebugVal(upd))) => *v = upd,

        (Some(U64Val(v)), Some(U64Val(upd))) => match update.op {
            Some(UpdateOp::Add) => *v = v.saturating_add(upd),

            Some(UpdateOp::Sub) => *v = v.saturating_sub(upd),

            Some(UpdateOp::Override) => *v = upd,

            None => tracing::warn!(
                "numeric attribute update {:?} needs to have an op field",
                update_name
            ),
        },

        (Some(I64Val(v)), Some(I64Val(upd))) => match update.op {
            Some(UpdateOp::Add) => *v = v.saturating_add(upd),

            Some(UpdateOp::Sub) => *v = v.saturating_sub(upd),

            Some(UpdateOp::Override) => *v = upd,

            None => tracing::warn!(
                "numeric attribute update {:?} needs to have an op field",
                update_name
            ),
        },

        (val, update) => {
            tracing::warn!(
                "attribute {:?} cannot be updated by update {:?}",
                val,
                update
            );
        }
    }
}

impl From<Update> for proto::Attribute {
    fn from(upd: Update) -> Self {
        proto::Attribute {
            field: Some(upd.field),
            unit: upd.unit,
        }
    }
}