liftof_tui/tabs/
tab_mt.rs

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Master Trigger tab
//! 
//! Show current data from the master trigger

use std::collections::{
  VecDeque,
  HashMap
};

use std::sync::{
  Arc,
  Mutex,
};

use std::time::{
  Instant
};

use ratatui::{
  symbols,
  layout::{Alignment, Constraint, Direction, Layout, Rect},
  style::{
      Color,
      //Modifier,
      Style},
  text::Span,
  Frame,
  //terminal::Frame,
  widgets::{
      Block,
      Dataset,
      Axis,
      GraphType,
      BorderType,
      Chart,
      //BarChart,
      Borders,
      Paragraph
  },
};

use crossbeam_channel::Receiver;

use ndhistogram::{
  ndhistogram,
  Histogram,
  Hist1D,
};

use ndhistogram::axis::{
  Uniform,
};

use tof_dataclasses::packets::{
  TofPacket,
  PacketType
};

use tof_dataclasses::events::MasterTriggerEvent;
use tof_dataclasses::monitoring::MtbMoniData;
use tof_dataclasses::errors::SerializationError;
use tof_dataclasses::database::DsiJChPidMapping;
use tof_dataclasses::events::master_trigger::LTBThreshold;

use tof_dataclasses::alerts::*;

use crate::colors::{
  ColorTheme,
};

use crate::widgets::{
  prep_data,
  create_labels,
  histogram,
  timeseries
};

#[derive(Debug, Clone)]
pub struct MTTab<'a> {
  pub event_queue    : VecDeque<MasterTriggerEvent>,
  pub moni_queue     : VecDeque<MtbMoniData>,
  pub met_queue      : VecDeque<f64>,
  pub rate_queue     : VecDeque<(f64,f64)>,
  pub lost_r_queue   : VecDeque<(f64,f64)>,
  pub fpgatmp_queue  : VecDeque<(f64,f64)>,
  pub tp_receiver    : Receiver<TofPacket>,
  pub mte_receiver   : Receiver<MasterTriggerEvent>,
  pub queue_size     : usize,
 
  pub n_events       : usize,
  pub n_moni         : usize,
  pub miss_evid      : usize,
  pub last_evid      : u32,
  pub nch_histo      : Hist1D<Uniform<f32>>,
  pub mtb_link_histo : Hist1D<Uniform<f32>>,
  pub panel_histo    : Hist1D<Uniform<f32>>,
  pub theme          : ColorTheme,

  pub mapping        : DsiJChPidMapping,
  pub mtlink_rb_map  : HashMap<u8,u8>,
  pub problem_hits   : Vec<(u8, u8, (u8, u8), LTBThreshold)>,
  timer              : Instant,
  moni_old_check     : Instant,
  alerts             : Arc<Mutex<HashMap<&'a str,TofAlert<'a>>>>,
  alerts_active      : bool
}

impl<'a> MTTab<'a> {

  pub fn new(tp_receiver   : Receiver<TofPacket>,
             mte_receiver  : Receiver<MasterTriggerEvent>,
             mapping       : DsiJChPidMapping,
             mtlink_rb_map : HashMap<u8,u8>,
             alerts        : Arc<Mutex<HashMap<&'a str, TofAlert<'a>>>>,
             theme         : ColorTheme) -> MTTab {
    // check if the alerts are active
    let mut alerts_active = false;
    match alerts.lock() {
      Ok(al) => {
        if al.len() > 0 {
          alerts_active = true;
          info!("Found {} active alerts!", al.len());
        }
      }
      Err(err) => {
        error!("Unable to lock alert mutex! {err}");
      }
    }
    let bins          = Uniform::new(50, 0.0, 50.0).unwrap();
    let mtb_link_bins = Uniform::new(50, 0.0, 50.0).unwrap();
    let panel_bins    = Uniform::new(22, 1.0, 22.0).unwrap();
    Self {
      event_queue    : VecDeque::<MasterTriggerEvent>::with_capacity(1000),
      moni_queue     : VecDeque::<MtbMoniData>::with_capacity(1000),
      met_queue      : VecDeque::<f64>::with_capacity(1000),
      rate_queue     : VecDeque::<(f64,f64)>::with_capacity(1000),
      lost_r_queue   : VecDeque::<(f64,f64)>::with_capacity(1000),
      fpgatmp_queue  : VecDeque::<(f64,f64)>::with_capacity(1000),
      tp_receiver    : tp_receiver,
      mte_receiver   : mte_receiver,
      queue_size     : 1000, // random
      n_events       : 0,
      n_moni         : 0,
      miss_evid      : 0,
      last_evid      : 0,
      nch_histo      : ndhistogram!(bins),
      mtb_link_histo : ndhistogram!(mtb_link_bins),
      panel_histo    : ndhistogram!(panel_bins),
      theme          : theme,
      mapping        : mapping.clone(),
      mtlink_rb_map  : mtlink_rb_map,
      problem_hits   : Vec::<(u8, u8, (u8, u8), LTBThreshold)>::new(),
      timer          : Instant::now(),
      moni_old_check : Instant::now(),
      alerts         : alerts,
      alerts_active  : alerts_active,
    }
  }

  pub fn receive_packet(&mut self) -> Result<(), SerializationError> {
    let mut mte = MasterTriggerEvent::new();
    let met     = self.timer.elapsed().as_secs_f64();
    match self.tp_receiver.try_recv() {
      Err(_err)   => {
        match self.mte_receiver.try_recv() {
          Err(_err) => (),
          Ok(_ev)   => {
            mte = _ev;
          }
        }
      },
      Ok(pack)    => {
        //error!("Got next packet {}!", pack);
        match pack.packet_type {
          PacketType::MasterTrigger => {
            match pack.unpack() {
              Ok(_ev) => {
                mte = _ev;
              },
              Err(err) => {
                error!("Unable to unpack MasterTriggerEvent! {err}");
                return Err(err);
              }
            }
          },
          PacketType::MonitorMtb   => {
            info!("Got new MtbMoniData!");
            let moni : MtbMoniData = pack.unpack()?;
            self.n_moni += 1;
            // check alert conditions
            if self.alerts_active {
              match self.alerts.lock() {
                Ok(mut al) => {
                  // we can work with mtb relevant alerts here
                  al.get_mut("mtb_lost_rate").unwrap()     .trigger(moni.lost_rate as f32);
                  al.get_mut("mtb_fpga_temp").unwrap() .trigger(moni.get_fpga_temp());
                  al.get_mut("mtb_rate_zero").unwrap() .trigger(moni.rate as f32);
                  al.get_mut("mtb_hk_too_old").unwrap().trigger(self.moni_old_check.elapsed().as_secs() as f32);
                },
                Err(err)   =>  error!("Unable to lock global alerts! {err}"),
              }
            }
            self.moni_old_check = Instant::now();

            self.moni_queue.push_back(moni);
            if self.moni_queue.len() > self.queue_size {
              self.moni_queue.pop_front();
            }
            self.rate_queue.push_back((met, moni.rate as f64));
            if self.rate_queue.len() > self.queue_size {
              self.rate_queue.pop_front();
            }
            self.lost_r_queue.push_back((met, moni.lost_rate as f64));
            if self.lost_r_queue.len() > self.queue_size {
              self.lost_r_queue.pop_front();
            }
            self.fpgatmp_queue.push_back((met, moni.get_fpga_temp() as f64));
            if self.fpgatmp_queue.len() > self.queue_size {
              self.fpgatmp_queue.pop_front();
            }
            self.met_queue.push_back(met);
            if self.met_queue.len() > self.queue_size {
              self.met_queue.pop_front();
            }
            return Ok(());
          },
          _ => (),
        }
      } // end Ok
    } // end match
    if mte.event_id != 0 {
      let hits     = mte.get_trigger_hits();
      let rb_links = mte.get_rb_link_ids();
      for h in &hits {
        match self.mapping.get(&h.0) {
          None => {
            error!("Can't get mapping for hit {:?}", h);
            //self.problem_hits.push(*h);
          },
          Some(jmap) => {
            match jmap.get(&h.1) {
              None => {
                error!("Can't get mapping for hit {:?}", h);
                self.problem_hits.push(*h);
              },
              Some(chmap) => {
                // let's just consider one side of the paddle 
                // here. If the two sides are not connected to 
                // the same LTB we have bigger problems
                match chmap.get(&h.2.0) {
                  None => {
                    error!("Can't get mapping for hit {:?}", h);
                    self.problem_hits.push(*h);
                  },
                  Some((_,panel_id)) => {
                    self.panel_histo.fill(&(*panel_id as f32));
                  }
                }
              }
            }
          }
        }
        //self.panel_histo.fill(&(self.mapping[&h.0][&h.1][&h.2].1 as f32));
      }
      self.nch_histo.fill(&(hits.len() as f32));
      for k in rb_links {
        // FIXME unwrap
        let linked_rbid = self.mtlink_rb_map.get(&k).unwrap_or(&0);
        self.mtb_link_histo.fill(&(*linked_rbid as f32));
      }
      self.n_events += 1;
      self.event_queue.push_back(mte.clone());
      if self.event_queue.len() > self.queue_size {
        self.event_queue.pop_front();
      }
      if self.last_evid != 0 {
        if mte.event_id - self.last_evid != 1 {
          self.miss_evid += (mte.event_id - self.last_evid) as usize;
        }
      }
      self.last_evid = mte.event_id;
    }
    Ok(())
  }

  pub fn render(&mut self, main_window : &Rect, frame : &mut Frame) {
    let main_chunks = Layout::default()
      .direction(Direction::Horizontal)
      .constraints(
          [Constraint::Percentage(70),
           Constraint::Percentage(30)].as_ref(),
      )
      .split(*main_window);
   
    // these are the 3 plots on the right side
    let info_chunks = Layout::default()
      .direction(Direction::Vertical)
      .constraints(
          [Constraint::Percentage(30),
           Constraint::Percentage(30),
           Constraint::Percentage(40),
          ].as_ref(),
      )
      .split(main_chunks[1]);
       
    let detail_chunks = Layout::default()
      .direction(Direction::Vertical)
      .constraints(
          [Constraint::Percentage(60),
           Constraint::Percentage(40),
          ].as_ref(),
      )
      .split(main_chunks[0]);

    let view_chunks = Layout::default()
      .direction(Direction::Horizontal)
      .constraints(
          [Constraint::Percentage(33),
           Constraint::Percentage(33),
           Constraint::Percentage(34),
          ].as_ref(),
      )
      .split(detail_chunks[0]);
    let trig_pan_and_hits = Layout::default()
      .direction(Direction::Vertical)
      .constraints(
          [Constraint::Percentage(50),
           Constraint::Percentage(50)].as_ref(),
      )
      .split(view_chunks[2]);
      
    let bottom_row = detail_chunks[1];

    //let main_layout   = main_chunks.to_vec();
    //let detail_layout = detail_chunks.to_vec();
    let info_layout   = info_chunks.to_vec();
    let view_layout   = view_chunks.to_vec();

    let t_min = *self.met_queue.front().unwrap_or(&0.0) as u64;
    let t_max = *self.met_queue.back().unwrap_or(&0.0)  as u64;
    let t_spacing = (t_max - t_min)/5;

    let t_labels = vec![Span::from(t_min.to_string()),
                        Span::from((t_min + t_spacing).to_string()),
                        Span::from((t_min + 2*t_spacing).to_string()),
                        Span::from((t_min + 3*t_spacing).to_string()),
                        Span::from((t_min + 4*t_spacing).to_string()),
                        Span::from((t_min + 5*t_spacing).to_string())];
    
     let rate_only : Vec::<i64> = self.rate_queue.iter().map(|z| z.1.round() as i64).collect();
     let r_max = *rate_only.iter().max().unwrap_or(&0) + 5;
     let r_min = *rate_only.iter().min().unwrap_or(&0) - 5;
     let rate_spacing = (r_max - r_min)/5;
     let rate_labels = vec![Span::from(r_min.to_string()),
                            Span::from((r_min + rate_spacing).to_string()),
                            Span::from((r_min + 2*rate_spacing).to_string()),
                            Span::from((r_min + 3*rate_spacing).to_string()),
                            Span::from((r_min + 4*rate_spacing).to_string()),
                            Span::from((r_min + 5*rate_spacing).to_string())];
     
     let rate_dataset = vec![Dataset::default()
         .name("MTB Rate")
         .marker(symbols::Marker::Braille)
         .graph_type(GraphType::Line)
         //.style(Style::default().fg(pl.get_fg_light()).bg(pl.get_bg_dark()))
         .style(self.theme.style())
         .data(self.rate_queue.make_contiguous())];

    let rate_chart = Chart::new(rate_dataset)
      .block(
        Block::default()
          .borders(Borders::ALL)
          .style(Style::default().patch(self.theme.style()))
          .title("MT rate ".to_owned() )
          .border_type(BorderType::Rounded),
      )
      .x_axis(Axis::default()
        .title(Span::styled("MET [s]", Style::default().patch(self.theme.style())))
        .style(Style::default().patch(self.theme.style()))
        .bounds([t_min as f64, t_max as f64])
        //.bounds([0.0, 1000.0])
        //.labels(t_labels.clone().iter().cloned().map(Span::from).collect()))
        .labels(t_labels.clone())
      )
      .y_axis(Axis::default()
        .title(Span::styled("Hz", Style::default().patch(self.theme.style())))
        .style(Style::default().patch(self.theme.style()))
        .bounds([r_min as f64, r_max as f64])
        //.bounds([0.0,1000.0])
        //.labels(rate_labels.clone().iter().cloned().map(Span::from).collect()))
        .labels(rate_labels.clone())
      )
      .style(self.theme.style()); 
     
    // NChannel distribution
    let nch_labels  = create_labels(&self.nch_histo);
    let nch_data    = prep_data(&self.nch_histo, &nch_labels, 2, true); 
    let nch_chart   = histogram(nch_data, String::from("N Hits (N CH)"), 2, 0, &self.theme);

    // FPGA temperature (future)
    //let fpga_t_label    = String::from("FPGA T [\u{00B0}C] ");
    //let fpga_t_theme    = self.theme.clone();
    //let mut fpga_t_data = self.fpgatmp_queue.clone(); //.make_contiguous();
    //let mut fpga_t_ts   = timeseries(&mut fpga_t_data,
    //                                 fpga_t_label.clone(),
    //                                 fpga_t_label.clone(),
    //                                 &fpga_t_theme);
    //frame.render_widget(fpga_t_ts, info_layout[2]);

    // Lost Trigger rate
    let lost_t_label    = String::from("Lost Trigger Rate [Hz]");
    let lost_t_theme    = self.theme.clone();
    let mut lost_t_data = self.lost_r_queue.clone(); //.make_contiguous();
    let lost_t_ts       = timeseries(&mut lost_t_data,
                                     lost_t_label.clone(),
                                     lost_t_label.clone(),
                                     &lost_t_theme);

    
    let tmp_only : Vec::<i64> = self.fpgatmp_queue.iter().map(|z| z.1.round() as i64).collect();
    let tmp_max = *tmp_only.iter().max().unwrap_or(&0) + 5;
    let tmp_min = *tmp_only.iter().min().unwrap_or(&0) - 5;
    let tmp_spacing = (tmp_max - tmp_min)/5;
    let tmp_labels = vec![Span::from(tmp_min.to_string()),
                          Span::from((tmp_min + tmp_spacing).to_string()),
                          Span::from((tmp_min + 2*tmp_spacing).to_string()),
                          Span::from((tmp_min + 3*tmp_spacing).to_string()),
                          Span::from((tmp_min + 4*tmp_spacing).to_string()),
                          Span::from((tmp_min + 5*tmp_spacing).to_string())];
    let fpga_temp_dataset = vec![Dataset::default()
        .name("FPGA T")
        .marker(symbols::Marker::Braille)
        .graph_type(GraphType::Line)
        .style(Style::default().patch(self.theme.style()))
        .data(self.fpgatmp_queue.make_contiguous())];

    let fpga_temp_chart = Chart::new(fpga_temp_dataset)
      .block(
        Block::default()
          .borders(Borders::ALL)
          .style(Style::default().patch(self.theme.style()))
          .title("FPGA T [\u{00B0}C] ".to_owned() )
          .border_type(BorderType::Rounded),
      )
      .x_axis(Axis::default()
        .title(Span::styled("MET [s]", Style::default().fg(Color::White)))
        .style(Style::default().patch(self.theme.style()))
        .bounds([t_min as f64, t_max as f64])
        //.labels(t_labels.clone().iter().cloned().map(Span::from).collect()))
        .labels(t_labels.clone())
      )
      .y_axis(Axis::default()
        //.title(Span::styled("T [\u{00B0}C]", Style::default().fg(Color::White)))
        .style(self.theme.style())
        .bounds([tmp_min as f64, tmp_max as f64])
        //.labels(tmp_labels.clone().iter().cloned().map(Span::from).collect()))
        .labels(tmp_labels.clone())
      )
      .style(self.theme.style());
    
    let last_event = self.event_queue.back();
    let view_string : String;
    match last_event {
      Some(event) => { 
        view_string = event.to_string();
      }, 
      None => {
        view_string = String::from("EVT QUEUE EMPTY!");
      }
    }
    let event_view = Paragraph::new(view_string)
      .style(Style::default().fg(Color::LightCyan))
      .alignment(Alignment::Left)
      //.scroll((5, 10))
      .block(
        Block::default()
          .borders(Borders::ALL)
          .style(self.theme.style())
          .title("Last MasterTriggerEvent")
          .border_type(BorderType::Rounded),
      );

    let last_moni = self.moni_queue.back();
    let view_moni : String;
    match last_moni {
      Some(moni) => { 
        view_moni = moni.to_string();
      }, 
      None => {
        view_moni = String::from("MTBMONI QUEUE EMPTY!");
      }
    }
    
    let moni_view = Paragraph::new(view_moni)
    .style(self.theme.style())
    .alignment(Alignment::Left)
    .block(
      Block::default()
        .borders(Borders::ALL)
        .style(self.theme.style())
        .title("Last MtbMoniData")
        .border_type(BorderType::Rounded),
    );
    
    // histograms
    let ml_labels  = create_labels(&self.mtb_link_histo);
    let mlh_data   = prep_data(&self.mtb_link_histo, &ml_labels, 10, true); 
    // this actually now is the RB ID!
    let mlh_chart  = histogram(mlh_data, String::from("RB ID"), 3, 0, &self.theme);
    frame.render_widget(mlh_chart, bottom_row);   
    
    let tp_labels  = create_labels(&self.panel_histo);
    let tph_data   = prep_data(&self.panel_histo, &tp_labels, 2, true); 
    let tpc_chart  = histogram(tph_data, String::from("Triggered Panel ID"), 3, 0, &self.theme);
    frame.render_widget(tpc_chart, trig_pan_and_hits[0]);
    frame.render_widget(nch_chart, trig_pan_and_hits[1]);

    // render everything
    frame.render_widget(rate_chart,      info_layout[0]); 
    frame.render_widget(lost_t_ts,       info_layout[1]);
    frame.render_widget(fpga_temp_chart, info_layout[2]);
    frame.render_widget(event_view,      view_layout[0]);
    frame.render_widget(moni_view,       view_layout[1]);
    //frame.render_widget(summary_view,    bottom_row[0]);
  }
}