Skip to main content

bybit/models/
system_status_websocket.rs

1use crate::prelude::*;
2
3/// Represents a single system status/maintenance record in WebSocket stream
4#[derive(Serialize, Deserialize, Clone, Debug)]
5#[serde(rename_all = "camelCase")]
6pub struct SystemStatusWebsocketItem {
7    /// Unique identifier for the system status record
8    pub id: String,
9
10    /// Title of the system maintenance
11    pub title: String,
12
13    /// System state (e.g., "completed", "in_progress", "scheduled")
14    pub state: String,
15
16    /// Start time of system maintenance, timestamp in milliseconds
17    #[serde(with = "string_to_u64")]
18    pub begin: u64,
19
20    /// End time of system maintenance, timestamp in milliseconds.
21    /// Before maintenance is completed, it is the expected end time;
22    /// After maintenance is completed, it will be changed to the actual end time.
23    #[serde(with = "string_to_u64")]
24    pub end: u64,
25
26    /// Hyperlink to system maintenance details. Default value is empty string
27    pub href: String,
28
29    /// Service types affected by the maintenance
30    #[serde(rename = "serviceTypes")]
31    pub service_types: Vec<u32>,
32
33    /// Products affected by the maintenance
34    pub product: Vec<u32>,
35
36    /// Affected UID tail numbers
37    #[serde(rename = "uidSuffix")]
38    pub uid_suffix: Vec<u32>,
39
40    /// Maintenance type
41    #[serde(rename = "maintainType")]
42    #[serde(with = "string_to_u32")]
43    pub maintain_type: u32,
44
45    /// Environment
46    #[serde(with = "string_to_u32")]
47    pub env: u32,
48}
49
50impl SystemStatusWebsocketItem {
51    /// Constructs a new SystemStatusWebsocketItem with specified parameters
52    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    /// Returns true if the maintenance is currently in progress
81    pub fn is_in_progress(&self) -> bool {
82        self.state.to_lowercase() == "in_progress"
83    }
84
85    /// Returns true if the maintenance is scheduled for the future
86    pub fn is_scheduled(&self) -> bool {
87        self.state.to_lowercase() == "scheduled"
88    }
89
90    /// Returns true if the maintenance has been completed
91    pub fn is_completed(&self) -> bool {
92        self.state.to_lowercase() == "completed"
93    }
94
95    /// Returns true if the maintenance affects the given service type
96    pub fn affects_service_type(&self, service_type: u32) -> bool {
97        self.service_types.contains(&service_type)
98    }
99
100    /// Returns true if the maintenance affects the given product
101    pub fn affects_product(&self, product_id: u32) -> bool {
102        self.product.contains(&product_id)
103    }
104
105    /// Returns true if the maintenance affects the given UID suffix
106    pub fn affects_uid_suffix(&self, uid_suffix: u32) -> bool {
107        self.uid_suffix.contains(&uid_suffix)
108    }
109
110    /// Returns true if the maintenance is currently active (in progress)
111    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    /// Returns the duration of the maintenance in milliseconds
116    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/// Represents a WebSocket system status update event
126#[derive(Serialize, Deserialize, Clone, Debug)]
127pub struct SystemStatusUpdate {
128    /// The WebSocket topic for the event (e.g., "system.status")
129    ///
130    /// Specifies the data stream for system status updates.
131    #[serde(rename = "topic")]
132    pub topic: String,
133
134    /// The timestamp when the system generated the data (ms)
135    #[serde(rename = "ts")]
136    #[serde(with = "string_to_u64")]
137    pub timestamp: u64,
138
139    /// The event data containing system status records
140    #[serde(rename = "data")]
141    pub data: Vec<SystemStatusWebsocketItem>,
142}
143
144impl SystemStatusUpdate {
145    /// Constructs a new SystemStatusUpdate
146    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    /// Returns the first in-progress maintenance record, if any
155    pub fn first_in_progress(&self) -> Option<&SystemStatusWebsocketItem> {
156        self.data.iter().find(|item| item.is_in_progress())
157    }
158
159    /// Returns all scheduled maintenance records
160    pub fn scheduled_items(&self) -> Vec<&SystemStatusWebsocketItem> {
161        self.data
162            .iter()
163            .filter(|item| item.is_scheduled())
164            .collect()
165    }
166
167    /// Returns all completed maintenance records
168    pub fn completed_items(&self) -> Vec<&SystemStatusWebsocketItem> {
169        self.data
170            .iter()
171            .filter(|item| item.is_completed())
172            .collect()
173    }
174
175    /// Returns all in-progress maintenance records
176    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    /// Returns true if there are any active maintenance events
184    pub fn has_active_maintenance(&self, current_time: u64) -> bool {
185        self.data.iter().any(|item| item.is_active(current_time))
186    }
187
188    /// Returns maintenance items affecting a specific service type
189    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    /// Returns maintenance items affecting a specific product
197    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    /// Returns the maintenance item with the given ID, if it exists
205    pub fn find_by_id(&self, id: &str) -> Option<&SystemStatusWebsocketItem> {
206        self.data.iter().find(|item| item.id == id)
207    }
208}