tof_dataclasses/
series.rs

1//! Compact data structures for analysis. Won't get serialized. 
2//! Summarize data of all boards and provide a more consice
3//! table-like structure for analysis.
4//! Currently under a lot of development. The goal is to get
5//! something which easily translates to a polars data frame.
6
7use std::fmt;
8
9use std::collections::{
10    HashMap,
11    VecDeque,
12};
13
14use crate::monitoring::{
15    MoniData,
16    RBMoniData,
17    LTBMoniData,
18    PAMoniData,
19    PBMoniData,
20    MtbMoniData, 
21    CPUMoniData,
22};
23
24#[cfg(feature = "polars")]
25use polars::prelude::*;
26
27/// A MoniSeries is a collection of (primarily) monitoring
28/// data, which comes from multiple senders.
29/// E.g. a MoniSeries could hold RBMoniData from all 
30/// 40 ReadoutBoards.
31pub trait MoniSeries<T>
32  where T : Copy + MoniData {
33
34  fn get_data(&self) -> &HashMap<u8,VecDeque<T>>;
35
36  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<T>>;
37 
38  fn get_max_size(&self) -> usize;
39
40  /// A HashMap of -> rbid, Vec\<var\> 
41  fn get_var(&self, varname : &str) -> HashMap<u8, Vec<f32>> {
42    let mut values = HashMap::<u8, Vec<f32>>::new();
43    for k in self.get_data().keys() {
44      match self.get_var_for_board(varname, k) {
45        None => (),
46        Some(vals) => {
47          values.insert(*k, vals);
48        }
49      }
50      //values.insert(*k, Vec::<f32>::new());
51      //match self.get_data().get(k) {
52      //  None => (),
53      //  Some(vec_moni) => {
54      //    for moni in vec_moni {
55      //      match moni.get(varname) {
56      //        None => (),
57      //        Some(val) => {
58      //          values.get_mut(k).unwrap().push(val);
59      //        }
60      //      }
61      //    }
62      //  }
63      //}
64    }
65    values
66  }
67
68  /// Get a certain variable, but only for a single board
69  fn get_var_for_board(&self, varname : &str, rb_id : &u8) -> Option<Vec<f32>> {
70    let mut values = Vec::<f32>::new();
71    match self.get_data().get(&rb_id) {
72      None => (),
73      Some(vec_moni) => {
74        for moni in vec_moni {
75          match moni.get(varname) {
76            None => {
77              return None;
78            },
79            Some(val) => {
80              values.push(val);
81            }
82          }
83        }
84      }
85    }
86    // FIXME This needs to be returning a reference,
87    // not cloning
88    Some(values)
89  }
90
91  #[cfg(feature = "polars")]
92  fn get_dataframe(&self) -> PolarsResult<DataFrame> {
93    let mut series = Vec::<Column>::new();
94    for k in Self::keys() {
95      match self.get_series(k) {
96        None => {
97          error!("Unable to get series for {}", k);
98        }
99        Some(ser) => {
100          series.push(ser.into());
101        }
102      }
103    }
104    let df = DataFrame::new(series)?;
105    Ok(df)
106  }
107
108  #[cfg(feature = "polars")]
109  /// Get the variable for all boards. This keeps the order of the 
110  /// underlying VecDeque. Values of all boards intermixed.
111  /// To get a more useful version, use the Dataframe instead.
112  ///
113  /// # Arguments
114  ///
115  /// * varname : The name of the attribute of the underlying
116  ///             moni structure
117  fn get_series(&self, varname : &str) -> Option<Series> {
118    let mut data = Vec::<f32>::with_capacity(self.get_data().len());
119    for rbid in self.get_data().keys() {
120      let dqe = self.get_data().get(rbid).unwrap(); //uwrap is fine, bc we checked earlier
121      for moni in dqe {
122        match moni.get(varname) {
123          None => {
124            error!("This type of MoniData does not have a key called {}", varname);
125            return None;
126          }
127          Some(var) => {
128            data.push(var);
129          }
130        }
131      }
132    }
133    let series = Series::new(varname.into(), data);
134    Some(series)
135  }
136
137  /// A list of the variables in this MoniSeries
138  fn keys() -> Vec<&'static str> {
139    T::keys()
140  }
141
142  /// A list of boards in this series
143  fn get_board_ids(&self) -> Vec<u8> {
144    self.get_data().keys().cloned().collect()
145  }
146
147  /// Add another instance of the data container to the series
148  fn add(&mut self, data : T) {
149    let board_id = data.get_board_id();
150    if !self.get_data().contains_key(&board_id) {
151      self.get_data_mut().insert(board_id, VecDeque::<T>::new());
152    } 
153    self.get_data_mut().get_mut(&board_id).unwrap().push_back(data);
154    if self.get_data_mut().get_mut(&board_id).unwrap().len() > self.get_max_size() {
155      error!("The queue is too large, returning the first element! If you need a larger series size, set the max_size field");
156      self.get_data_mut().get_mut(&board_id).unwrap().pop_front();
157    }
158  }
159  
160  fn get_last_moni(&self, board_id : u8) -> Option<T> {
161    let size = self.get_data().get(&board_id)?.len();
162    Some(self.get_data().get(&board_id).unwrap()[size - 1])
163  }
164}
165
166////////////////////////////////////////////////////////////////////
167
168#[derive(Debug, Clone, PartialEq)]
169pub struct RBMoniDataSeries {
170  data        : HashMap<u8, VecDeque<RBMoniData>>,
171  max_size    : usize
172}
173
174impl RBMoniDataSeries {
175  pub fn new() -> Self {
176    Self {
177      data     : HashMap::<u8, VecDeque::<RBMoniData>>::new(),
178      max_size : 10000
179    }
180  }
181}
182
183impl Default for RBMoniDataSeries {
184  fn default() -> Self {
185    Self::new()
186  }
187}
188
189impl fmt::Display for RBMoniDataSeries {
190  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
191    write!(f, ">RBMoniDataSeries : {} boards>", self.data.len())
192  }
193}
194
195impl MoniSeries<RBMoniData> for RBMoniDataSeries {
196  fn get_data(&self) -> &HashMap<u8,VecDeque<RBMoniData>> {
197    return &self.data;
198  }
199
200  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<RBMoniData>> {
201    return &mut self.data;
202  }
203 
204  fn get_max_size(&self) -> usize {
205    return self.max_size;
206  }
207}
208
209////////////////////////////////////////////////////////////////////
210
211
212#[derive(Debug, Clone, PartialEq)]
213pub struct PAMoniDataSeries {
214  data        : HashMap<u8, VecDeque<PAMoniData>>,
215  max_size    : usize,
216}
217
218impl PAMoniDataSeries {
219  pub fn new() -> Self {
220    Self {
221      data     : HashMap::<u8, VecDeque<PAMoniData>>::new(),
222      max_size : 10000,
223    }
224  }
225} 
226
227impl Default for PAMoniDataSeries {
228  fn default() -> Self {
229    Self::new()
230  }
231}
232
233impl fmt::Display for PAMoniDataSeries {
234  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235    write!(f, "<PAMoniDataSeries : {} boards>", self.data.len())
236  }
237}
238
239//impl<T: std::marker::Copy + HasBoardId> Series<T> for PAMoniDataSeries {
240impl MoniSeries<PAMoniData> for PAMoniDataSeries {
241
242  fn get_data(&self) -> &HashMap<u8,VecDeque<PAMoniData>> {
243    return &self.data;
244  }
245
246  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<PAMoniData>> {
247    return &mut self.data;
248  }
249 
250  fn get_max_size(&self) -> usize {
251    return self.max_size;
252  }
253}
254
255
256#[derive(Debug, Clone, PartialEq)]
257pub struct PBMoniDataSeries {
258  data        : HashMap<u8, VecDeque<PBMoniData>>,
259  max_size    : usize,
260}
261
262impl PBMoniDataSeries {
263  pub fn new() -> Self {
264    Self {
265      data     : HashMap::<u8, VecDeque<PBMoniData>>::new(),
266      max_size : 10000,
267    }
268  }
269} 
270
271impl Default for PBMoniDataSeries {
272  fn default() -> Self {
273    Self::new()
274  }
275}
276
277impl fmt::Display for PBMoniDataSeries {
278  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
279    write!(f, "<PBMoniDataSeries : {} boards>", self.data.len())
280  }
281}
282
283impl MoniSeries<PBMoniData> for PBMoniDataSeries {
284
285  fn get_data(&self) -> &HashMap<u8,VecDeque<PBMoniData>> {
286    return &self.data;
287  }
288
289  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<PBMoniData>> {
290    return &mut self.data;
291  }
292 
293  fn get_max_size(&self) -> usize {
294    return self.max_size;
295  }
296}
297
298#[derive(Debug, Clone, PartialEq)]
299pub struct LTBMoniDataSeries {
300  data        : HashMap<u8, VecDeque<LTBMoniData>>,
301  max_size    : usize,
302}
303
304impl LTBMoniDataSeries {
305  pub fn new() -> Self {
306    Self {
307      data     : HashMap::<u8, VecDeque<LTBMoniData>>::new(),
308      max_size : 10000,
309    }
310  }
311} 
312
313impl Default for LTBMoniDataSeries {
314  fn default() -> Self {
315    Self::new()
316  }
317}
318
319impl fmt::Display for LTBMoniDataSeries {
320  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
321    write!(f, "<LTBMoniDataSeries : {} boards>", self.data.len())
322  }
323}
324
325impl MoniSeries<LTBMoniData> for LTBMoniDataSeries {
326
327  fn get_data(&self) -> &HashMap<u8,VecDeque<LTBMoniData>> {
328    return &self.data;
329  }
330
331  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<LTBMoniData>> {
332    return &mut self.data;
333  }
334 
335  fn get_max_size(&self) -> usize {
336    return self.max_size;
337  }
338}
339
340////////////////////////////////////////////////////////////////////
341
342#[derive(Debug, Clone, PartialEq)]
343pub struct MtbMoniDataSeries {
344  data        : HashMap<u8, VecDeque<MtbMoniData>>,
345  max_size    : usize,
346}
347
348impl MtbMoniDataSeries {
349  pub fn new() -> Self {
350    Self {
351      data     : HashMap::<u8, VecDeque<MtbMoniData>>::new(),
352      max_size : 10000,
353    }
354  }
355} 
356
357impl Default for MtbMoniDataSeries {
358  fn default() -> Self {
359    Self::new()
360  }
361}
362
363impl fmt::Display for MtbMoniDataSeries {
364  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
365    write!(f, "<MtbMoniDataSeries : {} boards>", self.data.len())
366  }
367}
368
369impl MoniSeries<MtbMoniData> for MtbMoniDataSeries {
370
371  fn get_data(&self) -> &HashMap<u8,VecDeque<MtbMoniData>> {
372    return &self.data;
373  }
374
375  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<MtbMoniData>> {
376    return &mut self.data;
377  }
378 
379  fn get_max_size(&self) -> usize {
380    return self.max_size;
381  }
382}
383
384////////////////////////////////////////////////////////////////////
385
386#[derive(Debug, Clone, PartialEq)]
387pub struct CPUMoniDataSeries {
388  data        : HashMap<u8, VecDeque<CPUMoniData>>,
389  max_size    : usize,
390}
391
392impl CPUMoniDataSeries {
393  pub fn new() -> Self {
394    Self {
395      data     : HashMap::<u8, VecDeque<CPUMoniData>>::new(),
396      max_size : 10000,
397    }
398  }
399} 
400
401impl Default for CPUMoniDataSeries {
402  fn default() -> Self {
403    Self::new()
404  }
405}
406
407impl fmt::Display for CPUMoniDataSeries {
408  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409    write!(f, "<CPUMoniDataSeries : {} boards>", self.data.len())
410  }
411}
412
413impl MoniSeries<CPUMoniData> for CPUMoniDataSeries {
414
415  fn get_data(&self) -> &HashMap<u8,VecDeque<CPUMoniData>> {
416    return &self.data;
417  }
418
419  fn get_data_mut(&mut self) -> &mut HashMap<u8,VecDeque<CPUMoniData>> {
420    return &mut self.data;
421  }
422 
423  fn get_max_size(&self) -> usize {
424    return self.max_size;
425  }
426}
427
428