rill_view/flow/data/
pulse.rs

1use rill_protocol::flow::core::{Flow, TimedEvent};
2// TODO: Join `Frame` and `Range` into a single module.
3use rill_protocol::frame::Frame;
4use rill_protocol::io::provider::StreamType;
5use rill_protocol::range::Range;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct PulsePoint {
10    pub value: f64,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct PulseState {
15    // IMMUTABLE:
16    pub range: Option<Range>,
17
18    // MUTABLE:
19    pub frame: Frame<TimedEvent<PulsePoint>>,
20    /// Intermediate counter value. Not available for changing!!!
21    value: f64,
22}
23
24impl PulseState {
25    pub fn new(range: Option<Range>, depth: Option<u32>) -> Self {
26        let depth = depth.unwrap_or(128);
27        Self {
28            range,
29            // TODO: Use duration for removing obsolete values instead
30            frame: Frame::new(depth),
31            value: 0.0,
32        }
33    }
34
35    /*
36    pub fn range(&self) -> Cow<Range> {
37        self.range.as_ref().map(Cow::Borrowed).unwrap_or_else(|| {
38            // TODO: Calc min and max from Frame
39            Cow::Owned(Range::new(0.0, 100.0))
40        })
41    }
42
43    pub fn pct(&self) -> Pct {
44        Pct::from_range(self.value, self.range().as_ref())
45    }
46    */
47}
48
49impl Flow for PulseState {
50    type Action = ();
51    type Event = PulseEvent;
52
53    fn stream_type() -> StreamType {
54        StreamType::from("rillrate.data.pulse.v0")
55    }
56
57    fn apply(&mut self, event: TimedEvent<Self::Event>) {
58        match event.event {
59            PulseEvent::Inc(delta) => {
60                self.value += delta;
61            }
62            PulseEvent::Dec(delta) => {
63                self.value -= delta;
64            }
65            PulseEvent::Set(value) => {
66                self.value = value;
67            }
68        }
69        // Use the clamped value if a range set, but don't affect the state.
70        let mut value = self.value;
71        if let Some(range) = self.range.as_ref() {
72            range.clamp(&mut value);
73        }
74        let point = PulsePoint { value };
75        let timed_event = TimedEvent {
76            timestamp: event.timestamp,
77            event: point,
78        };
79        self.frame.insert_pop(timed_event);
80    }
81}
82
83pub type PulseDelta = Vec<TimedEvent<PulseEvent>>;
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub enum PulseEvent {
87    Inc(f64),
88    Dec(f64),
89    Set(f64),
90}