cs431_homework/hello_server/
statistics.rs

1//! Server statisics
2
3use std::collections::HashMap;
4
5/// Report for each operation
6#[derive(Debug)]
7pub struct Report {
8    _id: usize,
9    key: Option<String>, // None represents invalid request
10}
11
12impl Report {
13    /// Creates a new report with the given id and key.
14    pub fn new(id: usize, key: Option<String>) -> Self {
15        Report { _id: id, key }
16    }
17}
18
19/// Operation statisics
20#[derive(Debug, Default)]
21pub struct Statistics {
22    hits: HashMap<Option<String>, usize>,
23}
24
25impl Statistics {
26    /// Add a report to the statisics.
27    pub fn add_report(&mut self, report: Report) {
28        let hits = self.hits.entry(report.key).or_default();
29        *hits += 1;
30    }
31}