1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use fuchsia_inspect::{self as inspect, Node, NumericProperty};
6use fuchsia_inspect_derive::Inspect;
7use settings_inspect_utils::managed_inspect_map::ManagedInspectMap;
89const STASH_INSPECT_NODE_NAME: &str = "stash_failures";
1011pub struct StashInspectLogger {
12/// Map from a setting's device storage key to its inspect data.
13flush_failure_counts: ManagedInspectMap<StashInspectInfo>,
14}
1516/// Contains the node and property used to record the number of stash failures for a given
17/// setting.
18///
19/// Inspect nodes are not used, but need to be held as they're deleted from inspect once they go
20/// out of scope.
21#[derive(Default, Inspect)]
22struct StashInspectInfo {
23/// Node of this info.
24inspect_node: inspect::Node,
2526/// Number of write failures.
27count: inspect::UintProperty,
28}
2930impl StashInspectLogger {
31pub fn new(node: &Node) -> Self {
32let inspect_node = node.create_child(STASH_INSPECT_NODE_NAME);
33Self {
34 flush_failure_counts: ManagedInspectMap::<StashInspectInfo>::with_node(inspect_node),
35 }
36 }
3738/// Records a write failure for the given setting.
39pub fn record_flush_failure(&mut self, key: String) {
40let stash_inspect_info =
41self.flush_failure_counts.get_or_insert_with(key, StashInspectInfo::default);
42let _ = stash_inspect_info.count.add(1u64);
43 }
44}
4546#[cfg(test)]
47mod tests {
48use super::*;
49use diagnostics_assertions::assert_data_tree;
50use fuchsia_inspect::component;
5152// Verify that the StashInspectLogger accumulates failure counts to inspect.
53#[fuchsia::test]
54fn test_stash_logger() {
55let inspector = component::inspector();
56let mut logger = StashInspectLogger::new(inspector.root());
5758 logger.record_flush_failure("test_key".to_string());
59 logger.record_flush_failure("test_key2".to_string());
60 logger.record_flush_failure("test_key2".to_string());
6162assert_data_tree!(inspector, root: {
63 stash_failures: {
64"test_key": {
65"count": 1u64,
66 },
67"test_key2": {
68"count": 2u64,
69 }
70 }
71 });
72 }
73}