bybit/models/
system_status_websocket.rs1use crate::prelude::*;
2
3#[derive(Serialize, Deserialize, Clone, Debug)]
5#[serde(rename_all = "camelCase")]
6pub struct SystemStatusWebsocketItem {
7 pub id: String,
9
10 pub title: String,
12
13 pub state: String,
15
16 #[serde(with = "string_to_u64")]
18 pub begin: u64,
19
20 #[serde(with = "string_to_u64")]
24 pub end: u64,
25
26 pub href: String,
28
29 #[serde(rename = "serviceTypes")]
31 pub service_types: Vec<u32>,
32
33 pub product: Vec<u32>,
35
36 #[serde(rename = "uidSuffix")]
38 pub uid_suffix: Vec<u32>,
39
40 #[serde(rename = "maintainType")]
42 #[serde(with = "string_to_u32")]
43 pub maintain_type: u32,
44
45 #[serde(with = "string_to_u32")]
47 pub env: u32,
48}
49
50impl SystemStatusWebsocketItem {
51 pub fn new(
53 id: &str,
54 title: &str,
55 state: &str,
56 begin: u64,
57 end: u64,
58 href: &str,
59 service_types: Vec<u32>,
60 product: Vec<u32>,
61 uid_suffix: Vec<u32>,
62 maintain_type: u32,
63 env: u32,
64 ) -> Self {
65 Self {
66 id: id.to_string(),
67 title: title.to_string(),
68 state: state.to_string(),
69 begin,
70 end,
71 href: href.to_string(),
72 service_types,
73 product,
74 uid_suffix,
75 maintain_type,
76 env,
77 }
78 }
79
80 pub fn is_in_progress(&self) -> bool {
82 self.state.to_lowercase() == "in_progress"
83 }
84
85 pub fn is_scheduled(&self) -> bool {
87 self.state.to_lowercase() == "scheduled"
88 }
89
90 pub fn is_completed(&self) -> bool {
92 self.state.to_lowercase() == "completed"
93 }
94
95 pub fn affects_service_type(&self, service_type: u32) -> bool {
97 self.service_types.contains(&service_type)
98 }
99
100 pub fn affects_product(&self, product_id: u32) -> bool {
102 self.product.contains(&product_id)
103 }
104
105 pub fn affects_uid_suffix(&self, uid_suffix: u32) -> bool {
107 self.uid_suffix.contains(&uid_suffix)
108 }
109
110 pub fn is_active(&self, current_time: u64) -> bool {
112 self.is_in_progress() && current_time >= self.begin && current_time <= self.end
113 }
114
115 pub fn duration(&self) -> u64 {
117 if self.end > self.begin {
118 self.end - self.begin
119 } else {
120 0
121 }
122 }
123}
124
125#[derive(Serialize, Deserialize, Clone, Debug)]
127pub struct SystemStatusUpdate {
128 #[serde(rename = "topic")]
132 pub topic: String,
133
134 #[serde(rename = "ts")]
136 #[serde(with = "string_to_u64")]
137 pub timestamp: u64,
138
139 #[serde(rename = "data")]
141 pub data: Vec<SystemStatusWebsocketItem>,
142}
143
144impl SystemStatusUpdate {
145 pub fn new(topic: &str, timestamp: u64, data: Vec<SystemStatusWebsocketItem>) -> Self {
147 Self {
148 topic: topic.to_string(),
149 timestamp,
150 data,
151 }
152 }
153
154 pub fn first_in_progress(&self) -> Option<&SystemStatusWebsocketItem> {
156 self.data.iter().find(|item| item.is_in_progress())
157 }
158
159 pub fn scheduled_items(&self) -> Vec<&SystemStatusWebsocketItem> {
161 self.data
162 .iter()
163 .filter(|item| item.is_scheduled())
164 .collect()
165 }
166
167 pub fn completed_items(&self) -> Vec<&SystemStatusWebsocketItem> {
169 self.data
170 .iter()
171 .filter(|item| item.is_completed())
172 .collect()
173 }
174
175 pub fn in_progress_items(&self) -> Vec<&SystemStatusWebsocketItem> {
177 self.data
178 .iter()
179 .filter(|item| item.is_in_progress())
180 .collect()
181 }
182
183 pub fn has_active_maintenance(&self, current_time: u64) -> bool {
185 self.data.iter().any(|item| item.is_active(current_time))
186 }
187
188 pub fn items_by_service_type(&self, service_type: u32) -> Vec<&SystemStatusWebsocketItem> {
190 self.data
191 .iter()
192 .filter(|item| item.affects_service_type(service_type))
193 .collect()
194 }
195
196 pub fn items_by_product(&self, product_id: u32) -> Vec<&SystemStatusWebsocketItem> {
198 self.data
199 .iter()
200 .filter(|item| item.affects_product(product_id))
201 .collect()
202 }
203
204 pub fn find_by_id(&self, id: &str) -> Option<&SystemStatusWebsocketItem> {
206 self.data.iter().find(|item| item.id == id)
207 }
208}