Skip to main content

ratio_reactor/
status.rs

1//! # Queue status implementations
2//!
3//! ## License
4//!
5//! This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
6//! If a copy of the MPL was not distributed with this file,
7//! You can obtain one at <https://mozilla.org/MPL/2.0/>.
8//!
9//! **Code examples both in the docstrings and rendered documentation are free to use.**
10
11use std::collections::{BTreeMap, VecDeque};
12
13use serde::{Deserialize, Serialize};
14
15use crate::key::KeyedData;
16use crate::reactor::QueueStatus;
17
18/// Waiting status for a single spot "queue".
19#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
20pub struct SpotWaitingStatus {
21    /// Whether there is an active job.
22    pub active: bool,
23    /// Whether there is a waiting job.
24    pub waiting: bool,
25}
26impl<I> QueueStatus<I> for SpotWaitingStatus {
27    fn report_enqueue(&mut self, _input: &I) {
28        self.waiting = true;
29    }
30    fn report_start(&mut self, _input: &I) {
31        self.waiting = false;
32        self.active = true;
33    }
34    fn report_idle(&mut self) {
35        self.active = false;
36    }
37}
38
39/// Generic status for a single spot waiting "queue".
40#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
41pub struct SpotGenericStatus<S> {
42    /// Active job status if there is any.
43    pub active: Option<S>,
44    /// Waiting job status if there is any.
45    pub waiting: Option<S>,
46}
47impl<S> Default for SpotGenericStatus<S> {
48    fn default() -> Self {
49        Self {
50            active: None,
51            waiting: None,
52        }
53    }
54}
55impl<I, S> QueueStatus<I> for SpotGenericStatus<S>
56where
57    S: Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + for<'a> From<&'a I>,
58{
59    fn report_enqueue(&mut self, input: &I) {
60        self.waiting = Some(input.into());
61    }
62    fn report_start(&mut self, input: &I) {
63        self.active = self.waiting.take().or_else(|| Some(input.into()));
64    }
65    fn report_idle(&mut self) {
66        self.active = None;
67    }
68}
69
70/// Display status for a single spot waiting "queue".
71/// Relies on std::fmt::Display to represent job info.
72#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
73pub struct SpotDisplayStatus {
74    /// Active job info if there is any.
75    pub active: Option<String>,
76    /// Waiting job info if there is any.
77    pub waiting: Option<String>,
78}
79impl<I: std::fmt::Display> QueueStatus<I> for SpotDisplayStatus {
80    fn report_enqueue(&mut self, input: &I) {
81        self.waiting = Some(input.to_string());
82    }
83    fn report_start(&mut self, input: &I) {
84        self.active = self.waiting.take().or_else(|| Some(input.to_string()));
85    }
86    fn report_idle(&mut self) {
87        self.active = None;
88    }
89}
90
91/// Queue length status for a single file queue.
92#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
93pub struct QueueLengthStatus {
94    /// Whether there is an active job.
95    pub active: bool,
96    /// Number of waiting jobs.
97    pub waiting: usize,
98}
99impl<I> QueueStatus<I> for QueueLengthStatus {
100    fn report_enqueue(&mut self, _input: &I) {
101        self.waiting += 1;
102    }
103    fn report_start(&mut self, _input: &I) {
104        self.waiting -= 1;
105        self.active = true;
106    }
107    fn report_idle(&mut self) {
108        self.active = false;
109    }
110}
111
112/// Generic queue status for a single file queue.
113#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
114pub struct QueueGenericStatus<S> {
115    /// Active job status if there is any.
116    pub active: Option<S>,
117    /// Waiting jobs status.
118    pub waiting: VecDeque<S>,
119}
120impl<S> Default for QueueGenericStatus<S> {
121    fn default() -> Self {
122        Self {
123            active: None,
124            waiting: VecDeque::new(),
125        }
126    }
127}
128impl<I, S> QueueStatus<I> for QueueGenericStatus<S>
129where
130    S: Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + for<'a> From<&'a I>,
131{
132    fn report_enqueue(&mut self, input: &I) {
133        self.waiting.push_back(input.into());
134    }
135    fn report_start(&mut self, input: &I) {
136        self.active = self.waiting.pop_front().or_else(|| Some(input.into()));
137    }
138    fn report_idle(&mut self) {
139        self.active = None;
140    }
141}
142
143/// Queue display status for a single file queue.
144/// Relies on std::fmt::Display to represent job info.
145#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
146pub struct QueueDisplayStatus {
147    /// Active job info if there is any.
148    pub active: Option<String>,
149    /// Waiting jobs info.
150    pub waiting: VecDeque<String>,
151}
152impl<I: std::fmt::Display> QueueStatus<I> for QueueDisplayStatus {
153    fn report_enqueue(&mut self, input: &I) {
154        self.waiting.push_back(input.to_string());
155    }
156    fn report_start(&mut self, input: &I) {
157        self.active = self.waiting.pop_front().or_else(|| Some(input.to_string()));
158    }
159    fn report_idle(&mut self) {
160        self.active = None;
161    }
162}
163
164/// Waiting status for single spot "queues" per key.
165#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
166pub struct KeySpotWaitingStatus<K>
167where
168    K: Ord,
169{
170    /// Whether there is an active job.
171    pub active: bool,
172    /// Whether there is a waiting job per key.
173    pub waiting: BTreeMap<K, bool>,
174}
175impl<K> Default for KeySpotWaitingStatus<K>
176where
177    K: Ord,
178{
179    fn default() -> Self {
180        Self {
181            active: false,
182            waiting: Default::default(),
183        }
184    }
185}
186impl<K, D> QueueStatus<KeyedData<K, D>> for KeySpotWaitingStatus<K>
187where
188    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
189{
190    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
191        if self.waiting.contains_key(&input.key) {
192            if let Some(w) = self.waiting.get_mut(&input.key) {
193                *w = true;
194            }
195        } else {
196            self.waiting.insert(input.key.clone(), true);
197        }
198    }
199    fn report_start(&mut self, input: &KeyedData<K, D>) {
200        self.waiting.remove(&input.key);
201        self.active = true;
202    }
203    fn report_idle(&mut self) {
204        self.active = false;
205    }
206}
207
208/// Generic status for single spot "queues" per key.
209#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
210pub struct KeySpotGenericStatus<K, S>
211where
212    K: Ord,
213{
214    /// Whether there is an active job.
215    pub active: Option<S>,
216    /// Whether there is a waiting job per key.
217    pub waiting: BTreeMap<K, S>,
218}
219impl<K, S> Default for KeySpotGenericStatus<K, S>
220where
221    K: Ord,
222{
223    fn default() -> Self {
224        Self {
225            active: None,
226            waiting: Default::default(),
227        }
228    }
229}
230impl<K, D, S> QueueStatus<KeyedData<K, D>> for KeySpotGenericStatus<K, S>
231where
232    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
233    S: Clone
234        + PartialEq
235        + for<'a> From<&'a KeyedData<K, D>>
236        + Serialize
237        + for<'de> Deserialize<'de>,
238{
239    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
240        if self.waiting.contains_key(&input.key) {
241            if let Some(w) = self.waiting.get_mut(&input.key) {
242                *w = input.into();
243            }
244        } else {
245            self.waiting.insert(input.key.clone(), input.into());
246        }
247    }
248    fn report_start(&mut self, input: &KeyedData<K, D>) {
249        self.active = self
250            .waiting
251            .remove(&input.key)
252            .or_else(|| Some(input.into()));
253    }
254    fn report_idle(&mut self) {
255        self.active = None;
256    }
257}
258
259/// Display status for single spot "queues" per key.
260#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
261pub struct KeySpotDisplayStatus<K>
262where
263    K: Ord,
264{
265    /// Whether there is an active job.
266    pub active: Option<String>,
267    /// Whether there is a waiting job per key.
268    pub waiting: BTreeMap<K, String>,
269}
270impl<K> Default for KeySpotDisplayStatus<K>
271where
272    K: Ord,
273{
274    fn default() -> Self {
275        Self {
276            active: None,
277            waiting: Default::default(),
278        }
279    }
280}
281impl<K, D> QueueStatus<KeyedData<K, D>> for KeySpotDisplayStatus<K>
282where
283    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
284    KeyedData<K, D>: std::fmt::Display,
285{
286    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
287        if self.waiting.contains_key(&input.key) {
288            if let Some(w) = self.waiting.get_mut(&input.key) {
289                *w = input.to_string();
290            }
291        } else {
292            self.waiting.insert(input.key.clone(), input.to_string());
293        }
294    }
295    fn report_start(&mut self, input: &KeyedData<K, D>) {
296        self.active = self
297            .waiting
298            .remove(&input.key)
299            .or_else(|| Some(input.to_string()));
300    }
301    fn report_idle(&mut self) {
302        self.active = None;
303    }
304}
305
306/// Queue length status for keyed queues.
307#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
308pub struct KeyQueueLengthStatus<K>
309where
310    K: Ord,
311{
312    /// Whether there is an active job.
313    pub active: bool,
314    /// Waiting jobs info per key.
315    pub waiting: BTreeMap<K, usize>,
316}
317impl<K> Default for KeyQueueLengthStatus<K>
318where
319    K: Ord,
320{
321    fn default() -> Self {
322        Self {
323            active: false,
324            waiting: Default::default(),
325        }
326    }
327}
328impl<K, D> QueueStatus<KeyedData<K, D>> for KeyQueueLengthStatus<K>
329where
330    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
331{
332    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
333        if self.waiting.contains_key(&input.key) {
334            if let Some(ws) = self.waiting.get_mut(&input.key) {
335                *ws += 1;
336            }
337        } else {
338            self.waiting.insert(input.key.clone(), 1);
339        }
340    }
341    fn report_start(&mut self, input: &KeyedData<K, D>) {
342        if self.waiting.get(&input.key) == Some(&1) {
343            self.waiting.remove(&input.key);
344        } else if let Some(ws) = self.waiting.get_mut(&input.key) {
345            *ws -= 1;
346        }
347        self.active = true;
348    }
349    fn report_idle(&mut self) {
350        self.active = false;
351    }
352}
353
354/// Generic queue status for keyed queues.
355#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
356pub struct KeyQueueGenericStatus<K, S>
357where
358    K: Ord,
359{
360    /// Whether there is an active job.
361    pub active: Option<S>,
362    /// Waiting jobs info per key.
363    pub waiting: BTreeMap<K, VecDeque<S>>,
364}
365impl<K, S> Default for KeyQueueGenericStatus<K, S>
366where
367    K: Ord,
368{
369    fn default() -> Self {
370        Self {
371            active: None,
372            waiting: Default::default(),
373        }
374    }
375}
376impl<K, D, S> QueueStatus<KeyedData<K, D>> for KeyQueueGenericStatus<K, S>
377where
378    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
379    S: Clone
380        + PartialEq
381        + for<'a> From<&'a KeyedData<K, D>>
382        + Serialize
383        + for<'de> Deserialize<'de>,
384{
385    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
386        if self.waiting.contains_key(&input.key) {
387            if let Some(ws) = self.waiting.get_mut(&input.key) {
388                ws.push_back(input.into())
389            }
390        } else {
391            self.waiting
392                .insert(input.key.clone(), VecDeque::from([input.into()]));
393        }
394    }
395    fn report_start(&mut self, input: &KeyedData<K, D>) {
396        let info = if self.waiting.get(&input.key).map(|ws| ws.len()) == Some(1) {
397            self.waiting
398                .remove(&input.key)
399                .and_then(|mut ws| ws.pop_front())
400        } else {
401            self.waiting
402                .get_mut(&input.key)
403                .and_then(|ws| ws.pop_front())
404        };
405        self.active = info.or_else(|| Some(input.into()));
406    }
407    fn report_idle(&mut self) {
408        self.active = None;
409    }
410}
411
412/// Queue display status for keyed queues.
413#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
414pub struct KeyQueueDisplayStatus<K>
415where
416    K: Ord,
417{
418    /// Active job info if there is any.
419    pub active: Option<String>,
420    /// Waiting jobs info per key.
421    pub waiting: BTreeMap<K, VecDeque<String>>,
422}
423
424impl<K> Default for KeyQueueDisplayStatus<K>
425where
426    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
427{
428    fn default() -> Self {
429        Self {
430            active: None,
431            waiting: Default::default(),
432        }
433    }
434}
435impl<K, D> QueueStatus<KeyedData<K, D>> for KeyQueueDisplayStatus<K>
436where
437    K: Clone + Ord + Serialize + for<'de> Deserialize<'de>,
438    KeyedData<K, D>: std::fmt::Display,
439{
440    fn report_enqueue(&mut self, input: &KeyedData<K, D>) {
441        if self.waiting.contains_key(&input.key) {
442            if let Some(ws) = self.waiting.get_mut(&input.key) {
443                ws.push_back(input.to_string())
444            }
445        } else {
446            self.waiting
447                .insert(input.key.clone(), VecDeque::from([input.to_string()]));
448        }
449    }
450    fn report_start(&mut self, input: &KeyedData<K, D>) {
451        let info = if self.waiting.get(&input.key).map(|ws| ws.len()) == Some(1) {
452            self.waiting
453                .remove(&input.key)
454                .and_then(|mut ws| ws.pop_front())
455        } else {
456            self.waiting
457                .get_mut(&input.key)
458                .and_then(|ws| ws.pop_front())
459        };
460        self.active = info.or_else(|| Some(input.to_string()));
461    }
462    fn report_idle(&mut self) {
463        self.active = None;
464    }
465}