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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! 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
}
/// Aggregate usage event metrics. `metrics[]` (1-10) is required; the response
/// always contains one entry per requested metric, each with its own
/// `points[]` time series.
///
/// **Two response shapes**:
/// - Omit `interval` for a flat top-N table — one point per dimension
/// combination, no time axis. Useful for "top 10 paths by bandwidth in the
/// last 7 days".
/// - Pass `interval` (`1m`, `15m`, `30m`, `1h`, `1d`) for a time series —
/// one point per (time bucket × dimension combination).
///
/// `dimensions[]` breaks each point down by one or more attributes (service,
/// path, status, country, …). `queries[]` filters the underlying events
/// using the standard Utopia query syntax — `equal("path",
/// ["/v1/storage/files"])`, `equal("resource", ["bucket"])`,
/// `equal("resourceId", ["abc123"])`, `startsWith("path", ["/v1/storage"])`,
/// `equal("status", ["200", "201"])`, `isNotNull("resourceId")`. Supported
/// attributes: see `queries[]` param. Supported methods: `equal`, `notEqual`,
/// `contains`, `startsWith`, `endsWith`, `isNull`, `isNotNull`. Pass multiple
/// metrics to render stacked charts in one round-trip.
/// `orderBy=value`+`orderDir=desc`+`limit=N` returns the top-N by aggregated
/// value. When `startAt` is omitted, the default window adapts to `interval`
/// (or 7d when interval is omitted).
#[allow(clippy::too_many_arguments)]
pub async fn list_events(
&self,
metrics: impl IntoIterator<Item = impl Into<String>>,
queries: Option<Vec<String>>,
interval: Option<&str>,
dimensions: Option<Vec<String>>,
start_at: Option<&str>,
end_at: Option<&str>,
order_by: Option<&str>,
order_dir: Option<&str>,
limit: Option<i64>,
offset: Option<i64>,
) -> crate::error::Result<crate::models::UsageEventList> {
let mut params = HashMap::new();
params.insert("metrics".to_string(), json!(metrics.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
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) = interval {
params.insert("interval".to_string(), json!(value));
}
if let Some(value) = dimensions {
params.insert("dimensions".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = start_at {
params.insert("startAt".to_string(), json!(value));
}
if let Some(value) = end_at {
params.insert("endAt".to_string(), json!(value));
}
if let Some(value) = order_by {
params.insert("orderBy".to_string(), json!(value));
}
if let Some(value) = order_dir {
params.insert("orderDir".to_string(), json!(value));
}
if let Some(value) = limit {
params.insert("limit".to_string(), json!(value));
}
if let Some(value) = offset {
params.insert("offset".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/usage/events".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
/// Aggregate usage gauge snapshots. Gauges are point-in-time values (storage
/// totals, resource counts, …); each point carries the latest snapshot in
/// its interval via `argMax(value, time)`. `metrics[]` (1-10) is required; the
/// response always contains one entry per requested metric, each with its own
/// `points[]` time series.
///
/// **Two response shapes**:
/// - Omit `interval` for a flat top-N table — `argMax(value, time)` per
/// dimension combination over the whole window, no time axis. Useful for "top
/// 10 resources by current storage".
/// - Pass `interval` (`1m`, `15m`, `30m`, `1h`, `1d`) for a time series —
/// one snapshot per (time bucket × dimension combination).
///
/// `dimensions[]` breaks each point down further. Supported on gauges:
/// `resourceId`, `teamId`, `service`, `resource`. `service` and `resource`
/// enable per-service / per-resource-type panels (e.g. storage-by-service:
/// group `files.storage`, `deployments.storage`, `builds.storage`,
/// `databases.storage` by `service`). `queries[]` filters the underlying rows
/// using the standard Utopia query syntax — `equal("resource", ["bucket"])`,
/// `equal("resourceId", ["abc123"])`, `equal("teamId", ["team_x"])`,
/// `isNotNull("teamId")`. Supported attributes: see `queries[]` param.
/// Supported methods: `equal`, `notEqual`, `isNull`, `isNotNull`. Pass
/// multiple metrics to render stacked charts in one round-trip.
/// `orderBy=value`+`orderDir=desc`+`limit=N` returns the top-N. When `startAt`
/// is omitted, the default window adapts to interval (or 7d when interval is
/// omitted).
#[allow(clippy::too_many_arguments)]
pub async fn list_gauges(
&self,
metrics: impl IntoIterator<Item = impl Into<String>>,
queries: Option<Vec<String>>,
interval: Option<&str>,
dimensions: Option<Vec<String>>,
start_at: Option<&str>,
end_at: Option<&str>,
order_by: Option<&str>,
order_dir: Option<&str>,
limit: Option<i64>,
offset: Option<i64>,
) -> crate::error::Result<crate::models::UsageGaugeList> {
let mut params = HashMap::new();
params.insert("metrics".to_string(), json!(metrics.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
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) = interval {
params.insert("interval".to_string(), json!(value));
}
if let Some(value) = dimensions {
params.insert("dimensions".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = start_at {
params.insert("startAt".to_string(), json!(value));
}
if let Some(value) = end_at {
params.insert("endAt".to_string(), json!(value));
}
if let Some(value) = order_by {
params.insert("orderBy".to_string(), json!(value));
}
if let Some(value) = order_dir {
params.insert("orderDir".to_string(), json!(value));
}
if let Some(value) = limit {
params.insert("limit".to_string(), json!(value));
}
if let Some(value) = offset {
params.insert("offset".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/usage/gauges".to_string();
self.client.call(Method::GET, &path, Some(api_headers), 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"));
}
}