| 1 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
use std::sync::Arc; |
| 6 |
|
| 7 |
use crate::common_metric_data::CommonMetricDataInternal; |
| 8 |
use crate::error_recording::{test_get_num_recorded_errors, ErrorType}; |
| 9 |
use crate::metrics::Metric; |
| 10 |
use crate::metrics::MetricType; |
| 11 |
use crate::storage::StorageManager; |
| 12 |
use crate::CommonMetricData; |
| 13 |
use crate::Glean; |
| 14 |
|
| 15 |
/// A boolean metric. |
| 16 |
/// |
| 17 |
/// Records a simple flag. |
| 18 |
#[derive(Clone, Debug)] |
| 19 |
pub struct BooleanMetric { |
| 20 |
meta: Arc<CommonMetricDataInternal>, |
| 21 |
} |
| 22 |
|
| 23 |
impl MetricType for BooleanMetric { |
| 24 |
fn meta(&self) -> &CommonMetricDataInternal { |
| 25 |
&self.meta |
| 26 |
} |
| 27 |
|
| 28 |
fn with_name(&self, name: String) -> Self { |
| 29 |
let mut meta = (*self.meta).clone(); |
| 30 |
meta.inner.name = name; |
| 31 |
Self { |
| 32 |
meta: Arc::new(meta), |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
fn with_dynamic_label(&self, label: String) -> Self { |
| 37 |
let mut meta = (*self.meta).clone(); |
| 38 |
meta.inner.dynamic_label = Some(label); |
| 39 |
Self { |
| 40 |
meta: Arc::new(meta), |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// IMPORTANT: |
| 46 |
// |
| 47 |
// When changing this implementation, make sure all the operations are |
| 48 |
// also declared in the related trait in `../traits/`. |
| 49 |
impl BooleanMetric { |
| 50 |
/// Creates a new boolean metric. |
| 51 |
pub fn new(meta: CommonMetricData) -> Self { |
| 52 |
Self { |
| 53 |
meta: Arc::new(meta.into()), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/// Sets to the specified boolean value. |
| 58 |
/// |
| 59 |
/// # Arguments |
| 60 |
/// |
| 61 |
/// * `glean` - the Glean instance this metric belongs to. |
| 62 |
/// * `value` - the value to set. |
| 63 |
#[doc(hidden)] |
| 64 |
pub fn set_sync(&self, glean: &Glean, value: bool) { |
| 65 |
if !self.should_record(glean) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
let value = Metric::Boolean(value); |
| 70 |
glean.storage().record(glean, &self.meta, &value) |
| 71 |
} |
| 72 |
|
| 73 |
/// Sets to the specified boolean value. |
| 74 |
/// |
| 75 |
/// # Arguments |
| 76 |
/// |
| 77 |
/// * `value` - the value to set. |
| 78 |
pub fn set(&self, value: bool) { |
| 79 |
let metric = self.clone(); |
| 80 |
crate::launch_with_glean(move |glean| metric.set_sync(glean, value)) |
| 81 |
} |
| 82 |
|
| 83 |
/// **Test-only API (exported for FFI purposes).** |
| 84 |
/// |
| 85 |
/// Gets the currently stored value as a boolean. |
| 86 |
/// |
| 87 |
/// This doesn't clear the stored value. |
| 88 |
#[doc(hidden)] |
| 89 |
pub fn get_value(&self, glean: &Glean, ping_name: Option<&str>) -> Option<bool> { |
| 90 |
let queried_ping_name = ping_name.unwrap_or_else(|| &self.meta().inner.send_in_pings[0]); |
| 91 |
|
| 92 |
match StorageManager.snapshot_metric_for_test( |
| 93 |
glean.storage(), |
| 94 |
queried_ping_name, |
| 95 |
&self.meta.identifier(glean), |
| 96 |
self.meta.inner.lifetime, |
| 97 |
) { |
| 98 |
Some(Metric::Boolean(b)) => Some(b), |
| 99 |
_ => None, |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// **Test-only API (exported for FFI purposes).** |
| 104 |
/// |
| 105 |
/// Gets the currently stored value as an integer. |
| 106 |
/// |
| 107 |
/// This doesn't clear the stored value. |
| 108 |
/// |
| 109 |
/// # Arguments |
| 110 |
/// |
| 111 |
/// * `ping_name` - the optional name of the ping to retrieve the metric |
| 112 |
/// for. Defaults to the first value in `send_in_pings`. |
| 113 |
/// |
| 114 |
/// # Returns |
| 115 |
/// |
| 116 |
/// The stored value or `None` if nothing stored. |
| 117 |
pub fn test_get_value(&self, ping_name: Option<String>) -> Option<bool> { |
| 118 |
crate::block_on_dispatcher(); |
| 119 |
crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref())) |
| 120 |
} |
| 121 |
|
| 122 |
/// **Exported for test purposes.** |
| 123 |
/// |
| 124 |
/// Gets the number of recorded errors for the given metric and error type. |
| 125 |
/// |
| 126 |
/// # Arguments |
| 127 |
/// |
| 128 |
/// * `error` - The type of error |
| 129 |
/// |
| 130 |
/// # Returns |
| 131 |
/// |
| 132 |
/// The number of errors reported. |
| 133 |
pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 { |
| 134 |
crate::block_on_dispatcher(); |
| 135 |
|
| 136 |
crate::core::with_glean(|glean| { |
| 137 |
test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0) |
| 138 |
}) |
| 139 |
} |
| 140 |
} |