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::time::Duration;
6 
7 use ffi_support::FfiStr;
8 
9 use crate::{define_metric, handlemap_ext::HandleMapExtension, with_glean_value, Lifetime};
10 
11 define_metric!(TimespanMetric => TIMESPAN_METRICS {
12     new           -> glean_new_timespan_metric(time_unit: i32),
13     test_get_num_recorded_errors -> glean_timespan_test_get_num_recorded_errors,
14     destroy       -> glean_destroy_timespan_metric,
15 });
16 
17 #[no_mangle]
glean_timespan_set_start(metric_id: u64, start_time: u64)18 pub extern "C" fn glean_timespan_set_start(metric_id: u64, start_time: u64) {
19     with_glean_value(|glean| {
20         TIMESPAN_METRICS.call_infallible_mut(metric_id, |metric| {
21             metric.set_start(glean, start_time);
22         })
23     })
24 }
25 
26 #[no_mangle]
glean_timespan_set_stop(metric_id: u64, stop_time: u64)27 pub extern "C" fn glean_timespan_set_stop(metric_id: u64, stop_time: u64) {
28     with_glean_value(|glean| {
29         TIMESPAN_METRICS.call_infallible_mut(metric_id, |metric| {
30             metric.set_stop(glean, stop_time);
31         })
32     })
33 }
34 
35 #[no_mangle]
glean_timespan_cancel(metric_id: u64)36 pub extern "C" fn glean_timespan_cancel(metric_id: u64) {
37     TIMESPAN_METRICS.call_infallible_mut(metric_id, |metric| {
38         metric.cancel();
39     })
40 }
41 
42 #[no_mangle]
glean_timespan_set_raw_nanos(metric_id: u64, elapsed_nanos: u64)43 pub extern "C" fn glean_timespan_set_raw_nanos(metric_id: u64, elapsed_nanos: u64) {
44     let elapsed = Duration::from_nanos(elapsed_nanos);
45     with_glean_value(|glean| {
46         TIMESPAN_METRICS.call_infallible(metric_id, |metric| {
47             metric.set_raw(glean, elapsed);
48         })
49     })
50 }
51 
52 #[no_mangle]
glean_timespan_test_has_value(metric_id: u64, storage_name: FfiStr) -> u853 pub extern "C" fn glean_timespan_test_has_value(metric_id: u64, storage_name: FfiStr) -> u8 {
54     with_glean_value(|glean| {
55         TIMESPAN_METRICS.call_infallible(metric_id, |metric| {
56             metric
57                 .test_get_value(glean, storage_name.as_str())
58                 .is_some()
59         })
60     })
61 }
62 
63 #[no_mangle]
glean_timespan_test_get_value(metric_id: u64, storage_name: FfiStr) -> u6464 pub extern "C" fn glean_timespan_test_get_value(metric_id: u64, storage_name: FfiStr) -> u64 {
65     with_glean_value(|glean| {
66         TIMESPAN_METRICS.call_infallible(metric_id, |metric| {
67             metric.test_get_value(glean, storage_name.as_str()).unwrap()
68         })
69     })
70 }
71