Bug 1632159 - Add FOG Datetime metric r=TravisLong

Differential Revision: https://phabricator.services.mozilla.com/D74260
This commit is contained in:
Chris H-C 2020-05-07 18:45:23 +00:00
Родитель 5e6f06d6f5
Коммит cf008d3a85
6 изменённых файлов: 99 добавлений и 0 удалений

1
Cargo.lock сгенерированный
Просмотреть файл

@ -1904,6 +1904,7 @@ dependencies = [
name = "glean"
version = "0.1.0"
dependencies = [
"chrono",
"glean-core",
"log",
"once_cell",

Просмотреть файл

@ -6,6 +6,7 @@ edition = "2018"
publish = false
[dependencies]
chrono = "0.4.10"
glean-core = "25.1.0"
log = "0.4"
once_cell = "1.2.0"

Просмотреть файл

@ -11,6 +11,7 @@
//! ```
// Re-exporting for later use in generated code.
pub extern crate chrono;
pub extern crate once_cell;
pub extern crate uuid;

Просмотреть файл

@ -0,0 +1,53 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use super::CommonMetricData;
use super::TimeUnit;
use chrono::{DateTime, FixedOffset};
/// A datetime metric of a certain resolution.
///
/// Datetimes are used to make record when something happened according to the
/// client's clock.
#[derive(Debug)]
pub struct DatetimeMetric(glean_core::metrics::DatetimeMetric);
impl DatetimeMetric {
/// Create a new datetime metric.
pub fn new(meta: CommonMetricData, time_unit: TimeUnit) -> Self {
Self(glean_core::metrics::DatetimeMetric::new(meta, time_unit))
}
/// Set the datetime to the provided value, or the local now.
///
/// ## Arguments
///
/// - `value` - The date and time and timezone value to set.
/// If None we use the current local time.
pub fn set(&self, value: Option<DateTime<FixedOffset>>) {
crate::with_glean(move |glean| self.0.set(glean, value))
}
/// **Test-only API.**
///
/// Get the currently stored value.
/// This doesn't clear the stored value.
///
/// ## Note
///
/// This currently returns the value as an ISO 8601 time string.
/// See [bug 1636176](https://bugzilla.mozilla.org/show_bug.cgi?id=1636176).
///
/// ## Arguments
///
/// * `storage_name` - the storage name to look into.
///
/// ## Return value
///
/// Returns the stored value or `None` if nothing stored.
pub fn test_get_value(&self, storage_name: &str) -> Option<String> {
crate::with_glean(move |glean| self.0.test_get_value_as_string(glean, storage_name))
}
}

Просмотреть файл

@ -14,6 +14,7 @@ pub use glean_core::{metrics::TimeUnit, CommonMetricData, ErrorType, Lifetime};
mod boolean;
mod counter;
mod datetime;
mod event;
mod labeled;
mod ping;
@ -25,6 +26,7 @@ mod uuid;
pub use self::boolean::BooleanMetric;
pub use self::counter::CounterMetric;
pub use self::datetime::DatetimeMetric;
pub use self::event::{EventMetric, ExtraKeys, NoExtraKeys};
pub use self::labeled::LabeledMetric;
pub use self::ping::Ping;

Просмотреть файл

@ -0,0 +1,41 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
mod common;
use common::*;
use glean::metrics::{CommonMetricData, DatetimeMetric, Lifetime, TimeUnit};
use chrono::{FixedOffset, TimeZone};
#[test]
fn sets_datetime_value() {
let _lock = lock_test();
let _t = setup_glean(None);
let store_names: Vec<String> = vec!["store1".into()];
let metric = DatetimeMetric::new(
CommonMetricData {
name: "datetime_metric".into(),
category: "telemetry".into(),
send_in_pings: store_names.clone(),
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
},
TimeUnit::Second,
);
assert_eq!(None, metric.test_get_value("store1"));
let a_datetime = FixedOffset::east(5 * 3600)
.ymd(2020, 05, 07)
.and_hms(11, 58, 00);
metric.set(Some(a_datetime));
assert_eq!(
"2020-05-07T11:58:00+05:00",
metric.test_get_value("store1").unwrap()
);
}