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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use tokio::sync::Mutex;

#[derive(Debug, Eq, Clone, Serialize, Deserialize)]
pub struct HttpErrKey {
    pub name: String,
    pub code: u16,
    pub msg: String,
    pub url: String,
    pub source: String,
}

impl PartialEq for HttpErrKey {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.url == other.url
            && self.code == other.code
            && self.msg == other.msg
            && self.source == other.source
    }
}

impl Hash for HttpErrKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        format!(
            "{:?}{:?}{:?}{:?}{:?}",
            self.name, self.url, self.code, self.msg, self.source
        )
        .hash(state);
    }
}

pub struct HttpErrorStats {
    pub(crate) errors: Arc<Mutex<HashMap<HttpErrKey, u32>>>,
}

impl HttpErrorStats {
    pub(crate) fn new() -> Self {
        HttpErrorStats {
            errors: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    pub(crate) async fn increment(
        &self,
        name: String,
        url: String,
        code: u16,
        msg: String,
        source: String,
    ) {
        let mut errors = self.errors.lock().await;
        *errors
            .entry(HttpErrKey {
                name,
                url,
                code,
                msg,
                source,
            })
            .or_insert(0) += 1;
    }
}