1use candid::{CandidType, Principal};
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
7pub enum MetricValue {
8 Counter(i64),
10 Gauge(f64),
12 Histogram { x: f64, y: f64 },
14 Log(String),
16}
17
18#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
20pub struct Metric {
21 pub name: String,
22 pub key: String,
23 pub value: MetricValue,
24}
25
26#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
28pub enum StoredMetric {
29 Counter { name: String, value: i64 },
30 Gauge { name: String, value: f64 },
31 Histogram { name: String, total: u64 },
32 Log { name: String, total: u64 },
33}
34
35#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
37pub struct LogPage {
38 pub name: String,
39 pub entries: Vec<(u64, String)>,
40 pub total: u64,
41 pub offset: u64,
42}
43
44#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
46pub struct HistogramPage {
47 pub name: String,
48 pub points: Vec<(f64, f64)>,
49 pub total: u64,
50}
51
52#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
54pub enum TransformerOutput {
55 Scalar(f64),
57 Series(Vec<(f64, f64)>),
59 Text(String),
61}
62
63#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
65pub struct TransformerInfo {
66 pub name: String,
67 pub key: String,
68 pub script: String,
69 pub created_at: u64,
70}
71
72#[derive(Clone, Debug)]
73pub struct AnalyticsClient {
74 pub backend_canister_id: Principal,
75}
76
77impl AnalyticsClient {
78 pub fn new(backend_canister_id: Principal) -> Self {
79 Self {
80 backend_canister_id,
81 }
82 }
83
84 pub fn record_metric(&self, metric: Metric) -> Result<(), String> {
86 ic_cdk::call::Call::unbounded_wait(self.backend_canister_id, "record_metric").with_args(&(metric,)).oneway().map_err(|e| e.to_string())?;
87 Ok(())
88 }
89}