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
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::sc::constants::WILDLAND_SIGNATURE_HEADER;
use minreq::{Error, Response};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct RequestMetricsReq {
    #[serde(rename(serialize = "credentialID"))]
    pub credential_id: String,
    pub timestamp: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RequestMetricsRes {
    #[serde(rename(deserialize = "credentialID"))]
    pub credentials_id: String,

    #[serde(rename(deserialize = "usageCred"))]
    pub usage_cred: UsageReq,

    #[serde(rename(deserialize = "usageStorage"))]
    pub usage_storage: UsageReq,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UsageReq {
    pub rx: i64,
    pub tx: i64,
}

#[derive(Clone, Default, Debug)]
pub(crate) struct SCMetricsClient {
    pub(crate) base_url: String,
}

impl SCMetricsClient {
    #[tracing::instrument(level = "debug", ret, skip(self))]
    pub(crate) fn request_metrics(
        &self,
        request: RequestMetricsReq,
        signature: &str,
    ) -> Result<Response, Error> {
        let url = format!("{}/metrics", self.base_url);
        minreq::post(url)
            .with_header(WILDLAND_SIGNATURE_HEADER, signature)
            .with_json(&request)?
            .send()
    }
}

#[cfg(test)]
mod tests {
    use crate::sc::constants::test_utilities::{CREDENTIALS_ID, SIGNATURE, TIMESTAMP};
    use mockito::{mock, server_url};
    use serde_json::json;

    use super::*;

    fn client() -> SCMetricsClient {
        SCMetricsClient {
            base_url: server_url(),
        }
    }

    #[test]
    fn should_get_storage_metrics() {
        let request = RequestMetricsReq {
            credential_id: CREDENTIALS_ID.to_string(),
            timestamp: TIMESTAMP.to_string(),
        };

        let m = mock("POST", "/metrics")
            .with_body(
                json!({
                    "credentialID" : CREDENTIALS_ID,
                    "usageCred" : {
                        "rx" : 433_i64,
                        "tx" : 523_i64
                    },
                    "usageStorage" : {
                        "rx" : 433_i64,
                        "tx" : 523_i64
                    }
                })
                .to_string(),
            )
            .create();

        let response = client()
            .request_metrics(request, SIGNATURE)
            .unwrap()
            .json::<RequestMetricsRes>()
            .unwrap();

        m.assert();
        assert_eq!(response.credentials_id, CREDENTIALS_ID);
        assert_eq!(response.usage_cred.rx, 433);
        assert_eq!(response.usage_cred.tx, 523);
        assert_eq!(response.usage_storage.rx, 433);
        assert_eq!(response.usage_cred.tx, 523);
    }
}