-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.rs
67 lines (60 loc) · 1.78 KB
/
metrics.rs
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
use crate::api::server::AppState;
use actix_web::{
web::{self, delete, get, post, Data, Path},
HttpResponse, Scope,
};
use serde::Deserialize;
use serde_json::json;
#[derive(Deserialize)]
struct ManualMetricEntry {
label: String,
value: f64,
}
#[derive(Deserialize)]
struct DeleteMetricRequest {
label: String,
address: String,
}
async fn get_metrics(app_state: Data<AppState>) -> HttpResponse {
let metrics = app_state
.store_context
.metrics_store
.get_aggregate_metrics_for_all_tasks();
HttpResponse::Ok().json(json!({"success": true, "metrics": metrics}))
}
async fn get_all_metrics(app_state: Data<AppState>) -> HttpResponse {
let metrics = app_state.store_context.metrics_store.get_all_metrics();
HttpResponse::Ok().json(json!({"success": true, "metrics": metrics}))
}
// for potential backup restore purposes
async fn create_metric(
app_state: Data<AppState>,
metric: web::Json<ManualMetricEntry>,
) -> HttpResponse {
app_state
.store_context
.metrics_store
.store_manual_metrics(metric.label.clone(), metric.value);
HttpResponse::Ok().json(json!({"success": true}))
}
async fn delete_metric(
app_state: Data<AppState>,
task_id: Path<String>,
body: web::Json<DeleteMetricRequest>,
) -> HttpResponse {
let success =
app_state
.store_context
.metrics_store
.delete_metric(&task_id, &body.label, &body.address);
HttpResponse::Ok().json(json!({
"success": success
}))
}
pub fn metrics_routes() -> Scope {
web::scope("/metrics")
.route("", get().to(get_metrics))
.route("/all", get().to(get_all_metrics))
.route("", post().to(create_metric))
.route("/{task_id}", delete().to(delete_metric))
}