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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::borrow::Cow;
use std::fmt;
use std::net::IpAddr;
use std::str;
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
use crate::utils::{ts_rfc3339, ts_rfc3339_opt};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SessionStatus {
Ok,
Exited,
Crashed,
Abnormal,
}
impl Default for SessionStatus {
fn default() -> Self {
Self::Ok
}
}
#[derive(Debug, Error)]
#[error("invalid session status")]
pub struct ParseSessionStatusError;
impl str::FromStr for SessionStatus {
type Err = ParseSessionStatusError;
fn from_str(string: &str) -> Result<Self, Self::Err> {
Ok(match string {
"ok" => SessionStatus::Ok,
"crashed" => SessionStatus::Crashed,
"abnormal" => SessionStatus::Abnormal,
"exited" => SessionStatus::Exited,
_ => return Err(ParseSessionStatusError),
})
}
}
impl fmt::Display for SessionStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SessionStatus::Ok => write!(f, "ok"),
SessionStatus::Crashed => write!(f, "crashed"),
SessionStatus::Abnormal => write!(f, "abnormal"),
SessionStatus::Exited => write!(f, "exited"),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SessionAttributes<'a> {
pub release: Cow<'a, str>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub environment: Option<Cow<'a, str>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ip_address: Option<IpAddr>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub user_agent: Option<String>,
}
fn is_false(val: &bool) -> bool {
!val
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SessionUpdate<'a> {
#[serde(rename = "sid", default = "Uuid::new_v4")]
pub session_id: Uuid,
#[serde(rename = "did", default)]
pub distinct_id: Option<String>,
#[serde(rename = "seq", default, skip_serializing_if = "Option::is_none")]
pub sequence: Option<u64>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "ts_rfc3339_opt"
)]
pub timestamp: Option<SystemTime>,
#[serde(default = "SystemTime::now", with = "ts_rfc3339")]
pub started: SystemTime,
#[serde(default, skip_serializing_if = "is_false")]
pub init: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub duration: Option<f64>,
#[serde(default)]
pub status: SessionStatus,
#[serde(default)]
pub errors: u64,
#[serde(rename = "attrs")]
pub attributes: SessionAttributes<'a>,
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_zero(val: &u32) -> bool {
*val == 0
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SessionAggregateItem {
#[serde(with = "ts_rfc3339")]
pub started: SystemTime,
#[serde(rename = "did", default, skip_serializing_if = "Option::is_none")]
pub distinct_id: Option<String>,
#[serde(default, skip_serializing_if = "is_zero")]
pub exited: u32,
#[serde(default, skip_serializing_if = "is_zero")]
pub errored: u32,
#[serde(default, skip_serializing_if = "is_zero")]
pub abnormal: u32,
#[serde(default, skip_serializing_if = "is_zero")]
pub crashed: u32,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SessionAggregates<'a> {
#[serde(default)]
pub aggregates: Vec<SessionAggregateItem>,
#[serde(rename = "attrs")]
pub attributes: SessionAttributes<'a>,
}