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
//! Usage service for Appwrite SDK
use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Usage {
client: Client,
}
impl Usage {
pub fn new(client: &Client) -> Self {
Self { client: client.clone() }
}
pub fn client(&self) -> &Client {
&self.client
}
/// Query usage event metrics from the usage database. Returns individual event
/// rows with full metadata. Pass Query objects as JSON strings to filter,
/// paginate, and order results. Supported query methods: equal,
/// greaterThanEqual, lessThanEqual, orderAsc, orderDesc, limit, offset.
/// Supported filter attributes: metric, path, method, status, resource,
/// resourceId, country, userAgent, time (these match the underlying column
/// names — note that the response surfaces `resource` as `resourceType` and
/// `country` as `countryCode`). When no time filter is supplied the endpoint
/// defaults to the last 7 days. Default `limit(100)` is applied if none is
/// given; user-supplied limits are capped at 500. The `total` field is capped
/// at 5000 to keep counts predictable — pass `total=false` to skip the count
/// entirely.
pub async fn list_events(
&self,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::UsageEventList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let path = "/usage/events".to_string();
self.client.call(Method::GET, &path, None, Some(params)).await
}
/// Query usage gauge metrics (point-in-time resource snapshots) from the usage
/// database. Returns individual gauge snapshots with metric, value, and
/// timestamp. Pass Query objects as JSON strings to filter, paginate, and
/// order results. Supported query methods: equal, greaterThanEqual,
/// lessThanEqual, orderAsc, orderDesc, limit, offset. Supported filter
/// attributes: metric, time. Use `orderDesc("time"), limit(1)` to fetch the
/// most recent snapshot. When no time filter is supplied the endpoint defaults
/// to the last 7 days. Default `limit(100)` is applied if none is given;
/// user-supplied limits are capped at 500. The `total` field is capped at 5000
/// to keep counts predictable — pass `total=false` to skip the count
/// entirely.
pub async fn list_gauges(
&self,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::UsageGaugeList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let path = "/usage/gauges".to_string();
self.client.call(Method::GET, &path, None, Some(params)).await
}
}
impl crate::services::Service for Usage {
fn client(&self) -> &Client {
&self.client
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_usage_creation() {
let client = Client::new();
let service = Usage::new(&client);
assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
}
}