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
use crate::client::CliObj;
use crate::client::TimestampHolder;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Response of real time data of origins of a service
#[derive(Debug, Serialize, Deserialize)]
pub struct OriginResponse {
    /// Offset of entry timestamps from the current time due to processing time.
    #[serde(alias = "AggregateDelay")]
    pub aggregate_delay: u64,

    /// A list of report entries, each representing one second of time.
    #[serde(alias = "Data")]
    pub data: Vec<OriginDataInSecond>,

    /// Timestamp value to use for subsequent requests.
    #[serde(alias = "Timestamp")]
    pub timestamp: u64,
}

impl TimestampHolder for OriginResponse {
    fn get_timestamp(&self) -> u64 {
        self.timestamp
    }
}

/// Hold data of all origins in one second of a service
#[derive(Debug, Serialize, Deserialize)]
pub struct OriginDataInSecond {
    /// The Unix timestamp at which this record's data was generated
    #[serde(default)]
    pub recorded: u64,

    /// Aggregates measurements across all Fastly POPs, of each origin
    /// Map of `origin_name` --> [`OriginStats`]
    #[serde(default)]
    pub aggregated: HashMap<String, OriginStats>,

    /// Measurements breakdown by POP
    /// Map of `pop_name` --> (map of `origin_name` --> [`OriginStats`])
    #[serde(default)]
    pub datacenter: HashMap<String, HashMap<String, OriginStats>>,
}

/// Statistics of origin
/// See explanation of members [here](https://developer.fastly.com/reference/api/metrics-stats/origin-inspector/real-time/#measurements-data-model)
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct OriginStats {
    #[serde(default)]
    pub resp_body_bytes: u64,

    #[serde(default)]
    pub resp_header_bytes: u64,

    #[serde(default)]
    pub responses: u64,

    #[serde(default)]
    status_1xx: u64,

    #[serde(default)]
    status_200: u64,

    #[serde(default)]
    status_204: u64,

    #[serde(default)]
    status_206: u64,

    #[serde(default)]
    status_2xx: u64,

    #[serde(default)]
    status_301: u64,

    #[serde(default)]
    status_302: u64,

    #[serde(default)]
    status_304: u64,

    #[serde(default)]
    status_3xx: u64,

    #[serde(default)]
    status_400: u64,

    #[serde(default)]
    status_401: u64,

    #[serde(default)]
    status_403: u64,

    #[serde(default)]
    status_404: u64,

    #[serde(default)]
    status_416: u64,

    #[serde(default)]
    status_429: u64,

    #[serde(default)]
    status_4xx: u64,

    #[serde(default)]
    status_500: u64,

    #[serde(default)]
    status_501: u64,

    #[serde(default)]
    status_502: u64,

    #[serde(default)]
    status_503: u64,

    #[serde(default)]
    status_504: u64,

    #[serde(default)]
    status_505: u64,

    #[serde(default)]
    status_5xx: u64,
}

/// Client to get origin real time data
pub struct OriginClient {
    cli: CliObj,
}

impl OriginClient {
    const ENDPOINT: &'static str = "https://rt.fastly.com/v1/origins";

    /// Create an OriginClient object
    pub fn new(api_key: &str, service_id: &str) -> Result<OriginClient> {
        Ok(OriginClient {
            cli: CliObj::new(api_key, service_id, OriginClient::ENDPOINT)?,
        })
    }

    /// Reset internal timestamp which used to track consecutive stats to 0
    /// After calling this function, calling get_stats_consecutive function will be the first call
    pub fn reset_stats_consecutive(&mut self) {
        self.cli.reset_stats_consecutive();
    }

    /// The first call of the function will get data of latest one second
    /// The consecutive call of the function will get consecutive data of last call to last second
    pub async fn get_stats_consecutive(&mut self) -> Result<OriginResponse> {
        self.cli.get_stats_consecutive().await
    }

    /// Get stats from start_timestamp to latest timestamp available for a service
    pub async fn get_stats_from(&mut self, start_timestamp: u64) -> Result<OriginResponse> {
        self.cli.get_stats_from(start_timestamp).await
    }

    /// Get data for the 120 seconds preceding the latest timestamp available for a service.
    pub async fn get_stats_120s(&self) -> Result<OriginResponse> {
        self.cli.get_stats_120s().await
    }

    /// Get data for the 120 seconds preceding the latest timestamp available for a service, up to a maximum of max_entries entries.
    pub async fn get_stats_max(&self, max_entries: u64) -> Result<OriginResponse> {
        self.cli.get_stats_max(max_entries).await
    }
}