/
event.rs
82 lines (76 loc) · 2.61 KB
/
event.rs
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
80
81
82
use metric::{LogLine, Telemetry};
use uuid::Uuid;
/// Supported event encodings.
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum Encoding {
/// Raw bytes, no encoding.
Raw,
/// Avro
Avro,
/// JSON
JSON,
}
/// Event: the central cernan datastructure
///
/// Event is the heart of cernan, the enumeration that cernan works on in all
/// cases. The enumeration fields drive sink / source / filter operations
/// depending on their implementation.
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub enum Event {
/// A wrapper for `metric::Telemetry`. See its documentation for more
/// detail.
Telemetry(Telemetry),
/// A wrapper for `metric::LogLine`. See its documentation for more
/// detail.
Log(LogLine),
/// A flush pulse signal. The `TimerFlush` keeps a counter of the total
/// flushes made in this cernan's run. See `source::Flush` for the origin of
/// these pulses in cernan operation.
TimerFlush(u64),
/// Shutdown event which marks the location in the queue after which no
/// more events will appear. It is expected that after receiving this
/// marker the given source will exit cleanly.
Shutdown,
/// Raw, encoded bytes.
Raw {
/// Ordering value used by some sinks accepting Raw events.
order_by: u64,
/// Encoding for the included bytes.
encoding: Encoding,
/// Encoded payload.
bytes: Vec<u8>,
/// Connection ID of the source on which this raw event was received
connection_id: Option<Uuid>
},
}
impl Event {
/// Determine if an event is a `TimerFlush`.
pub fn is_timer_flush(&self) -> bool {
match *self {
Event::TimerFlush(_) => true,
_ => false,
}
}
/// Retrieve the timestamp from an `Event` if such exists. `TimerFlush` has
/// no sensible timestamp -- being itself a mechanism _of_ time, not inside
/// time -- and these `Event`s will always return None.
pub fn timestamp(&self) -> Option<i64> {
match *self {
Event::Telemetry(ref telem) => Some(telem.timestamp),
Event::Log(ref log) => Some(log.time),
Event::TimerFlush(_) | Event::Shutdown | Event::Raw { .. } => None,
}
}
}
impl Event {
/// Create a new `Event::Telemetry` from an existing `metric::Telemetry`.
#[inline]
pub fn new_telemetry(metric: Telemetry) -> Event {
Event::Telemetry(metric)
}
/// Create a new `Event::Log` from an existing `metric::LogLine`.
#[inline]
pub fn new_log(log: LogLine) -> Event {
Event::Log(log)
}
}