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
use std::time::{Duration, Instant};

use reqwest::StatusCode;

use crate::event::Event;
use crate::response::Response;

pub(crate) type Events = Vec<Event>;

pub(crate) trait EventsResponse {
    fn to_response(
        &self,
        status: Option<StatusCode>,
        body: Option<String>,
        clock: Instant,
        error: Option<String>,
    ) -> Vec<Response>;
}

impl EventsResponse for Events {
    fn to_response(
        &self,
        status: Option<StatusCode>,
        body: Option<String>,
        clock: Instant,
        error: Option<String>,
    ) -> Vec<Response> {
        let total_events = if self.is_empty() {
            1
        } else {
            self.len() as u64
        };

        let spent = Duration::from_secs(clock.elapsed().as_secs() / total_events);
        self.iter()
            .map(|ev| Response {
                status_code: status,
                body: body.clone(),
                duration: spent,
                metadata: ev.metadata.clone(),
                error: error.clone(),
            })
            .collect()
    }
}