detect/
triage_shim.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
// 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.

// Interface to the Triage library.

use crate::diagnostics::Selectors;
use anyhow::Error;
use fuchsia_triage::{ActionTagDirective, ParseResult, SnapshotTrigger};
use std::collections::HashMap;

pub fn evaluate_int_math(expression: &str) -> Result<i64, Error> {
    fuchsia_triage::evaluate_int_math(expression)
}

type ConfigFiles = HashMap<String, String>;

type DiagnosticData = fuchsia_triage::DiagnosticData;

pub struct TriageLib {
    triage_config: fuchsia_triage::ParseResult,
}

impl TriageLib {
    pub fn new(configs: ConfigFiles) -> Result<TriageLib, Error> {
        let triage_config = ParseResult::new(&configs, &ActionTagDirective::AllowAll)?;
        Ok(TriageLib { triage_config })
    }

    pub fn selectors(&self) -> Selectors {
        Selectors::new().with_inspect_selectors(fuchsia_triage::all_selectors(&self.triage_config))
    }

    pub fn evaluate(
        &self,
        data: Vec<DiagnosticData>,
    ) -> (Vec<SnapshotTrigger>, fuchsia_triage::WarningVec) {
        fuchsia_triage::snapshots(&data, &self.triage_config)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use maplit::hashmap;

    const CONFIG: &str = r#"{
            select: {
                foo: "INSPECT:foo.cm:path/to:leaf",
            },
            act: {
                yes: {
                    type: "Snapshot",
                    trigger: "foo==8",
                    repeat: "Micros(42)",
                    signature: "got-it",
                },
            },
        }"#;

    const INSPECT: &str = r#"[
        {
            "moniker": "foo.cm",
            "metadata": {},
            "payload": {"path": {"to": {"leaf": 8}}}
        }
    ]"#;

    #[fuchsia::test]
    fn library_calls_work() -> Result<(), Error> {
        let configs = hashmap! { "foo.triage".to_string() => CONFIG.to_string() };
        let lib = TriageLib::new(configs)?;
        let data = vec![DiagnosticData::new(
            "inspect.json".to_string(),
            fuchsia_triage::Source::Inspect,
            INSPECT.to_string(),
        )?];
        let expected_trigger =
            vec![SnapshotTrigger { signature: "got-it".to_string(), interval: 42_000 }];

        assert_eq!(
            lib.selectors().inspect_selectors,
            vec!["INSPECT:foo.cm:path/to:leaf".to_string()]
        );
        assert_eq!(lib.evaluate(data), (expected_trigger, vec![]));
        Ok(())
    }
}