1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub mod counter;
pub mod dict;
pub mod gauge;
pub mod histogram;
pub mod logger;
pub mod pulse;
pub mod table;
use crate::encoding;
use crate::io::provider::{PackedDelta, PackedMetric, PackedState, StreamType, Timestamp};
use anyhow::Error;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt;
pub trait DataFraction:
DeserializeOwned + Serialize + Clone + fmt::Debug + Sync + Send + 'static
{
}
impl<T> DataFraction for T where
T: DeserializeOwned + Serialize + Clone + fmt::Debug + Sync + Send + 'static
{
}
pub trait Metric: DataFraction {
type State: DataFraction;
type Event: DataFraction;
fn stream_type() -> StreamType;
fn apply(&self, state: &mut Self::State, event: TimedEvent<Self::Event>);
fn pack_metric(&self) -> Result<PackedMetric, Error> {
encoding::to_vec(self)
.map_err(Error::from)
.map(PackedMetric::from)
}
fn unpack_metric(data: &PackedMetric) -> Result<Self, Error> {
encoding::from_slice(&data.0).map_err(Error::from)
}
fn pack_delta(delta: &[TimedEvent<Self::Event>]) -> Result<PackedDelta, Error> {
encoding::to_vec(delta)
.map_err(Error::from)
.map(PackedDelta::from)
}
fn unpack_delta(data: &PackedDelta) -> Result<Delta<Self::Event>, Error> {
encoding::from_slice(&data.0).map_err(Error::from)
}
fn pack_state(state: &Self::State) -> Result<PackedState, Error> {
encoding::to_vec(state)
.map_err(Error::from)
.map(PackedState::from)
}
fn unpack_state(data: &PackedState) -> Result<Self::State, Error> {
encoding::from_slice(&data.0).map_err(Error::from)
}
}
pub type Delta<T> = Vec<TimedEvent<T>>;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TimedEvent<T> {
pub timestamp: Timestamp,
pub event: T,
}