fuchsia_triage/metrics/
metric_value.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::{unhandled_type, Lambda};
use diagnostics_hierarchy::{ArrayContent, Property as DiagnosticProperty};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

/// The calculated or selected value of a Metric.
///
/// Problem means that the value could not be calculated.
#[derive(Deserialize, Debug, Clone, Serialize)]
pub enum MetricValue {
    // Ensure every variant of MetricValue is tested in metric_value_traits().
    // TODO(cphoenix): Support u64.
    Int(i64),
    Float(f64),
    String(String),
    Bool(bool),
    Vector(Vec<MetricValue>),
    Bytes(Vec<u8>),
    Problem(Problem),
    Lambda(Box<Lambda>),
    Node,
}

/// Some kind of problematic non-value. In most cases, this should be treated as a thrown error.
#[derive(Deserialize, Clone, Serialize)]
pub enum Problem {
    Missing(String),
    /// An unknown (not supported yet) value that is present but cannot be used in computation.
    UnhandledType(String),
    /// An error in the grammar or semantics of a .triage file, usually detectable by inspection.
    SyntaxError(String),
    /// An incorrect type for an operation.
    ValueError(String),
    /// An internal bug; these should never be seen in the wild.
    InternalBug(String),
    /// One or more expected errors - don't bother humans with the result.
    Ignore(Vec<Problem>),
    /// An error which can occur in evaluation of an expression.
    EvaluationError(String),
}

impl Problem {
    // Ranks problems in order of severity; bigger numbers are worse.
    // This is NOT an API or contract. The ranking can change between program versions.
    // Multiple problems can share a rank.
    pub(crate) fn severity(&self) -> i32 {
        match self {
            Problem::Ignore(_) => 1,
            Problem::Missing(_) => 2,
            Problem::UnhandledType(_) => 3,
            Problem::ValueError(_) => 4,
            Problem::EvaluationError(_) => 5,
            Problem::SyntaxError(_) => 6,
            Problem::InternalBug(_) => 7,
        }
    }
}

impl PartialEq for MetricValue {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (MetricValue::Int(l), MetricValue::Int(r)) => l == r,
            (MetricValue::Float(l), MetricValue::Float(r)) => l == r,
            (MetricValue::Bytes(l), MetricValue::Bytes(r)) => l == r,
            (MetricValue::Int(l), MetricValue::Float(r)) => *l as f64 == *r,
            (MetricValue::Float(l), MetricValue::Int(r)) => *l == *r as f64,
            (MetricValue::String(l), MetricValue::String(r)) => l == r,
            (MetricValue::Bool(l), MetricValue::Bool(r)) => l == r,
            (MetricValue::Vector(l), MetricValue::Vector(r)) => l == r,
            _ => false,
        }
    }
}

impl Eq for MetricValue {}

impl std::fmt::Display for MetricValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MetricValue::Int(n) => write!(f, "Int({})", n),
            MetricValue::Float(n) => write!(f, "Float({})", n),
            MetricValue::Bool(n) => write!(f, "Bool({})", n),
            MetricValue::String(n) => write!(f, "String({})", n),
            MetricValue::Vector(n) => write!(f, "Vector({:?})", n),
            MetricValue::Bytes(n) => write!(f, "Bytes({:?})", n),
            MetricValue::Problem(p) => write!(f, "{:?}", p),
            MetricValue::Lambda(n) => write!(f, "Fn({:?})", n),
            MetricValue::Node => write!(f, "Node"),
        }
    }
}

impl From<f64> for MetricValue {
    fn from(val: f64) -> MetricValue {
        MetricValue::Float(val)
    }
}

impl From<i64> for MetricValue {
    fn from(val: i64) -> MetricValue {
        MetricValue::Int(val)
    }
}

impl From<DiagnosticProperty> for MetricValue {
    fn from(property: DiagnosticProperty) -> Self {
        match property {
            DiagnosticProperty::String(_name, value) => Self::String(value),
            DiagnosticProperty::Bytes(_name, value) => Self::Bytes(value),
            DiagnosticProperty::Int(_name, value) => Self::Int(value),
            DiagnosticProperty::Uint(_name, value) => Self::Int(value as i64),
            DiagnosticProperty::Double(_name, value) => Self::Float(value),
            DiagnosticProperty::Bool(_name, value) => Self::Bool(value),
            // TODO(cphoenix): Figure out what to do about histograms.
            DiagnosticProperty::DoubleArray(_name, ArrayContent::Values(values)) => {
                Self::Vector(values.iter().map(|value| Self::Float(*value)).collect())
            }
            DiagnosticProperty::IntArray(_name, ArrayContent::Values(values)) => {
                Self::Vector(values.iter().map(|value| Self::Int(*value)).collect())
            }
            DiagnosticProperty::UintArray(_name, ArrayContent::Values(values)) => {
                Self::Vector(values.iter().map(|value| Self::Int(*value as i64)).collect())
            }
            DiagnosticProperty::DoubleArray(_name, ArrayContent::LinearHistogram(_))
            | DiagnosticProperty::IntArray(_name, ArrayContent::LinearHistogram(_))
            | DiagnosticProperty::UintArray(_name, ArrayContent::LinearHistogram(_))
            | DiagnosticProperty::DoubleArray(_name, ArrayContent::ExponentialHistogram(_))
            | DiagnosticProperty::IntArray(_name, ArrayContent::ExponentialHistogram(_))
            | DiagnosticProperty::UintArray(_name, ArrayContent::ExponentialHistogram(_)) => {
                unhandled_type("Histogram is not supported")
            }
            DiagnosticProperty::StringList(_name, _list) => {
                unhandled_type("StringList is not supported")
            }
        }
    }
}

impl From<JsonValue> for MetricValue {
    fn from(value: JsonValue) -> Self {
        match value {
            JsonValue::String(value) => Self::String(value),
            JsonValue::Bool(value) => Self::Bool(value),
            JsonValue::Number(_) => Self::from(&value),
            JsonValue::Array(values) => Self::Vector(values.into_iter().map(Self::from).collect()),
            _ => unhandled_type("Unsupported JSON type"),
        }
    }
}

impl From<&JsonValue> for MetricValue {
    fn from(value: &JsonValue) -> Self {
        match value {
            JsonValue::String(value) => Self::String(value.clone()),
            JsonValue::Bool(value) => Self::Bool(*value),
            JsonValue::Number(value) => {
                if value.is_i64() {
                    Self::Int(value.as_i64().unwrap())
                } else if value.is_u64() {
                    Self::Int(value.as_u64().unwrap() as i64)
                } else if value.is_f64() {
                    Self::Float(value.as_f64().unwrap())
                } else {
                    unhandled_type("Unable to convert JSON number")
                }
            }
            JsonValue::Array(values) => Self::Vector(values.iter().map(Self::from).collect()),
            _ => unhandled_type("Unsupported JSON type"),
        }
    }
}

impl std::fmt::Debug for Problem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Problem::Missing(s) => write!(f, "Missing: {}", s),
            Problem::Ignore(problems) => {
                if problems.len() == 1 {
                    write!(f, "Ignore: {:?}", problems[0])
                } else {
                    write!(f, "Ignore: [")?;
                    for problem in problems.iter() {
                        write!(f, "{:?}; ", problem)?;
                    }
                    write!(f, "]")
                }
            }
            Problem::SyntaxError(s) => write!(f, "SyntaxError: {}", s),
            Problem::ValueError(s) => write!(f, "ValueError: {}", s),
            Problem::InternalBug(s) => write!(f, "InternalBug: {}", s),
            Problem::UnhandledType(s) => write!(f, "UnhandledType: {}", s),
            Problem::EvaluationError(s) => write!(f, "EvaluationError: {}", s),
        }
    }
}

#[cfg(test)]
pub(crate) mod test {
    use super::*;
    use crate::assert_problem;
    use diagnostics_hierarchy::ArrayFormat;
    use serde_json::{json, Number as JsonNumber};

    #[fuchsia::test]
    fn test_equality() {
        // Equal Value, Equal Type
        assert_eq!(MetricValue::Int(1), MetricValue::Int(1));
        assert_eq!(MetricValue::Float(1.0), MetricValue::Float(1.0));
        assert_eq!(MetricValue::String("A".to_string()), MetricValue::String("A".to_string()));
        assert_eq!(MetricValue::Bool(true), MetricValue::Bool(true));
        assert_eq!(MetricValue::Bool(false), MetricValue::Bool(false));
        assert_eq!(
            MetricValue::Vector(vec![
                MetricValue::Int(1),
                MetricValue::Float(1.0),
                MetricValue::String("A".to_string()),
                MetricValue::Bool(true),
            ]),
            MetricValue::Vector(vec![
                MetricValue::Int(1),
                MetricValue::Float(1.0),
                MetricValue::String("A".to_string()),
                MetricValue::Bool(true),
            ])
        );
        assert_eq!(MetricValue::Bytes(vec![1, 2, 3]), MetricValue::Bytes(vec![1, 2, 3]));

        // Floats and ints interconvert. Test both ways for full code coverage.
        assert_eq!(MetricValue::Int(1), MetricValue::Float(1.0));
        assert_eq!(MetricValue::Float(1.0), MetricValue::Int(1));

        // Numbers, vectors, and byte arrays do not interconvert when compared with Rust ==.
        // Note, though, that the expression "1 == [1]" will evaluate to true.
        assert!(MetricValue::Int(1) != MetricValue::Vector(vec![MetricValue::Int(1)]));
        assert!(MetricValue::Bytes(vec![1]) != MetricValue::Vector(vec![MetricValue::Int(1)]));
        assert!(MetricValue::Int(1) != MetricValue::Bytes(vec![1]));

        // Nested array
        assert_eq!(
            MetricValue::Vector(vec![
                MetricValue::Int(1),
                MetricValue::Float(1.0),
                MetricValue::String("A".to_string()),
                MetricValue::Bool(true),
            ]),
            MetricValue::Vector(vec![
                MetricValue::Int(1),
                MetricValue::Float(1.0),
                MetricValue::String("A".to_string()),
                MetricValue::Bool(true),
            ])
        );

        // Problem should never be equal
        assert!(
            MetricValue::Problem(Problem::Missing("err".to_string()))
                != MetricValue::Problem(Problem::Missing("err".to_string()))
        );
        // Use assert_problem() macro to test error messages.
        assert_problem!(MetricValue::Problem(Problem::Missing("err".to_string())), "Missing: err");

        // We don't have a contract for Lambda equality. We probably don't need one.
    }

    #[fuchsia::test]
    fn test_inequality() {
        // Different Value, Equal Type
        assert_ne!(MetricValue::Int(1), MetricValue::Int(2));
        assert_ne!(MetricValue::Float(1.0), MetricValue::Float(2.0));
        assert_ne!(MetricValue::String("A".to_string()), MetricValue::String("B".to_string()));
        assert_ne!(MetricValue::Bool(true), MetricValue::Bool(false));
        assert_ne!(
            MetricValue::Vector(vec![
                MetricValue::Int(1),
                MetricValue::Float(1.0),
                MetricValue::String("A".to_string()),
                MetricValue::Bool(true),
            ]),
            MetricValue::Vector(vec![
                MetricValue::Int(2),
                MetricValue::Float(2.0),
                MetricValue::String("B".to_string()),
                MetricValue::Bool(false),
            ])
        );

        // Different Type
        assert_ne!(MetricValue::Int(2), MetricValue::Float(1.0));
        assert_ne!(MetricValue::Int(1), MetricValue::String("A".to_string()));
        assert_ne!(MetricValue::Int(1), MetricValue::Bool(true));
        assert_ne!(MetricValue::Float(1.0), MetricValue::String("A".to_string()));
        assert_ne!(MetricValue::Float(1.0), MetricValue::Bool(true));
        assert_ne!(MetricValue::String("A".to_string()), MetricValue::Bool(true));
    }

    #[fuchsia::test]
    fn test_fmt() {
        assert_eq!(format!("{}", MetricValue::Int(3)), "Int(3)");
        assert_eq!(format!("{}", MetricValue::Float(3.5)), "Float(3.5)");
        assert_eq!(format!("{}", MetricValue::Bool(true)), "Bool(true)");
        assert_eq!(format!("{}", MetricValue::Bool(false)), "Bool(false)");
        assert_eq!(format!("{}", MetricValue::String("cat".to_string())), "String(cat)");
        assert_eq!(
            format!("{}", MetricValue::Vector(vec![MetricValue::Int(1), MetricValue::Float(2.5)])),
            "Vector([Int(1), Float(2.5)])"
        );
        assert_eq!(format!("{}", MetricValue::Bytes(vec![1u8, 2u8])), "Bytes([1, 2])");
        assert_eq!(
            format!("{}", MetricValue::Problem(Problem::Missing("Where is Foo?".to_string()))),
            "Missing: Where is Foo?"
        );
        assert_eq!(
            format!("{}", MetricValue::Problem(Problem::ValueError("Where is Foo?".to_string()))),
            "ValueError: Where is Foo?"
        );
        assert_eq!(
            format!("{}", MetricValue::Problem(Problem::SyntaxError("Where is Foo?".to_string()))),
            "SyntaxError: Where is Foo?"
        );
        assert_eq!(
            format!("{}", MetricValue::Problem(Problem::InternalBug("Where is Foo?".to_string()))),
            "InternalBug: Where is Foo?"
        );
        assert_eq!(
            format!(
                "{}",
                MetricValue::Problem(Problem::UnhandledType("Where is Foo?".to_string()))
            ),
            "UnhandledType: Where is Foo?"
        );
        assert_eq!(
            format!(
                "{}",
                MetricValue::Problem(Problem::Ignore(vec![Problem::SyntaxError(
                    "Where is Foo?".to_string()
                ),]))
            ),
            "Ignore: SyntaxError: Where is Foo?"
        );
        assert_eq!(
            format!(
                "{}",
                MetricValue::Problem(Problem::Ignore(vec![
                    Problem::SyntaxError("Where is Foo?".to_string()),
                    Problem::ValueError("Where is Bar?".to_string()),
                ]))
            ),
            "Ignore: [SyntaxError: Where is Foo?; ValueError: Where is Bar?; ]"
        );
    }

    #[fuchsia::test]
    fn metric_value_from_json() {
        /*
            JSON subtypes:
                Bool(bool),
                Number(Number),
                String(String),
                Array(Vec<Value>),
                Object(Map<String, Value>),
        */
        macro_rules! test_from {
            ($json:path, $metric:path, $value:expr) => {
                test_from_to!($json, $metric, $value, $value);
            };
        }
        macro_rules! test_from_int {
            ($json:path, $metric:path, $value:expr) => {
                test_from_to!($json, $metric, JsonNumber::from($value), $value);
            };
        }
        macro_rules! test_from_float {
            ($json:path, $metric:path, $value:expr) => {
                test_from_to!($json, $metric, JsonNumber::from_f64($value).unwrap(), $value);
            };
        }
        macro_rules! test_from_to {
            ($json:path, $metric:path, $json_value:expr, $metric_value:expr) => {
                let metric_value = $metric($metric_value);
                let json_value = $json($json_value);
                assert_eq!(metric_value, MetricValue::from(json_value));
            };
        }
        test_from!(JsonValue::String, MetricValue::String, "Hi World".to_string());
        test_from_int!(JsonValue::Number, MetricValue::Int, 3);
        test_from_int!(JsonValue::Number, MetricValue::Int, i64::MAX);
        test_from_int!(JsonValue::Number, MetricValue::Int, i64::MIN);
        test_from_to!(JsonValue::Number, MetricValue::Int, JsonNumber::from(u64::MAX), -1);
        test_from_float!(JsonValue::Number, MetricValue::Float, std::f64::consts::PI);
        test_from_float!(JsonValue::Number, MetricValue::Float, f64::MAX);
        test_from_float!(JsonValue::Number, MetricValue::Float, f64::MIN);
        test_from!(JsonValue::Bool, MetricValue::Bool, true);
        test_from!(JsonValue::Bool, MetricValue::Bool, false);
        let json_vec = vec![json!(1), json!(2), json!(3)];
        let metric_vec = vec![MetricValue::Int(1), MetricValue::Int(2), MetricValue::Int(3)];
        test_from_to!(JsonValue::Array, MetricValue::Vector, json_vec, metric_vec);
        assert_problem!(
            MetricValue::from(JsonValue::Object(serde_json::Map::new())),
            "UnhandledType: Unsupported JSON type"
        );
    }

    #[fuchsia::test]
    fn metric_value_from_diagnostic_property() {
        /*
            DiagnosticProperty subtypes:
                String(Key, String),
                Bytes(Key, Vec<u8>),
                Int(Key, i64),
                Uint(Key, u64),
                Double(Key, f64),
                Bool(Key, bool),
                DoubleArray(Key, ArrayContent<f64>),
                IntArray(Key, ArrayContent<i64>),
                UintArray(Key, ArrayContent<u64>),
                DoubleArray(Key, LinearHistogram<f64>),
                IntArray(Key, LinearHistogram<i64>),
                UintArray(Key, LinearHistogram<u64>),
                DoubleArray(Key, ExponentialHistogram<f64>),
                IntArray(Key, ExponentialHistogram<i64>),
                UintArray(Key, ExponentialHistogram<u64>),
        */
        macro_rules! test_from {
            ($diagnostic:path, $metric:path, $value:expr) => {
                test_from_to!($diagnostic, $metric, $value, $value);
            };
        }
        macro_rules! test_from_to {
            ($diagnostic:path, $metric:path, $diagnostic_value:expr, $metric_value:expr) => {
                assert_eq!(
                    $metric($metric_value),
                    MetricValue::from($diagnostic("foo".to_string(), $diagnostic_value))
                );
            };
        }
        test_from!(DiagnosticProperty::String, MetricValue::String, "Hi World".to_string());
        test_from!(DiagnosticProperty::Bytes, MetricValue::Bytes, vec![1, 2, 3]);
        test_from!(DiagnosticProperty::Int, MetricValue::Int, 3);
        test_from!(DiagnosticProperty::Int, MetricValue::Int, i64::MAX);
        test_from!(DiagnosticProperty::Int, MetricValue::Int, i64::MIN);
        test_from!(DiagnosticProperty::Uint, MetricValue::Int, 3);
        test_from_to!(DiagnosticProperty::Uint, MetricValue::Int, u64::MAX, -1);
        test_from!(DiagnosticProperty::Double, MetricValue::Float, std::f64::consts::PI);
        test_from!(DiagnosticProperty::Double, MetricValue::Float, f64::MAX);
        test_from!(DiagnosticProperty::Double, MetricValue::Float, f64::MIN);
        test_from!(DiagnosticProperty::Bool, MetricValue::Bool, true);
        test_from!(DiagnosticProperty::Bool, MetricValue::Bool, false);
        let diagnostic_array = ArrayContent::Values(vec![1.5, 2.5, 3.5]);
        test_from_to!(
            DiagnosticProperty::DoubleArray,
            MetricValue::Vector,
            diagnostic_array,
            vec![MetricValue::Float(1.5), MetricValue::Float(2.5), MetricValue::Float(3.5)]
        );
        let diagnostic_array = ArrayContent::Values(vec![1, 2, 3]);
        test_from_to!(
            DiagnosticProperty::IntArray,
            MetricValue::Vector,
            diagnostic_array,
            vec![MetricValue::Int(1), MetricValue::Int(2), MetricValue::Int(3)]
        );
        let diagnostic_array = ArrayContent::Values(vec![1, 2, 3]);
        test_from_to!(
            DiagnosticProperty::UintArray,
            MetricValue::Vector,
            diagnostic_array,
            vec![MetricValue::Int(1), MetricValue::Int(2), MetricValue::Int(3)]
        );

        let diagnostic_array = ArrayContent::new(vec![0, 1, 0, 0, 0], ArrayFormat::LinearHistogram)
            .expect("create histogram");
        assert_problem!(
            DiagnosticProperty::UintArray("foo".to_string(), diagnostic_array).into(),
            "UnhandledType: Histogram is not supported"
        );

        let diagnostic_array =
            ArrayContent::new(vec![-10, 1, 0, 0, 0], ArrayFormat::LinearHistogram)
                .expect("create histogram");
        assert_problem!(
            DiagnosticProperty::IntArray("foo".to_string(), diagnostic_array).into(),
            "UnhandledType: Histogram is not supported"
        );

        let diagnostic_array =
            ArrayContent::new(vec![0., 0.1, 0., 0., 0.], ArrayFormat::LinearHistogram)
                .expect("create histogram");
        assert_problem!(
            DiagnosticProperty::DoubleArray("foo".to_string(), diagnostic_array).into(),
            "UnhandledType: Histogram is not supported"
        );
    }
}