Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding an endpoint to return full metrics record #83

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions integrationos-api/src/endpoints/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
use std::sync::Arc;

use super::{ApiResult, ReadResponse};
use crate::{
internal_server_error,
metrics::{DAILY_KEY, MONTHLY_KEY, PLATFORMS_KEY, TOTAL_KEY},
not_found,
server::AppState,
};
use axum::{
extract::{Path, Query, State},
routing::get,
Json, Router,
Extension, Json, Router,
};
use bson::Document;
use integrationos_domain::Store;
use integrationos_domain::{event_access::EventAccess, Store};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::error;

use crate::{
internal_server_error,
metrics::{DAILY_KEY, MONTHLY_KEY, PLATFORMS_KEY, TOTAL_KEY},
not_found,
server::AppState,
};

use super::ApiResult;

pub fn get_router() -> Router<Arc<AppState>> {
Router::new()
.route("/", get(get_metrics))
.route("/:client_id", get(get_metrics))
.route("/total", get(get_full_record))
}

#[derive(Debug, Default, Deserialize)]
Expand Down Expand Up @@ -59,6 +57,38 @@ pub struct MetricResponse {
pub count: i32,
}

pub async fn get_full_record(
state: State<Arc<AppState>>,
Extension(user_event_access): Extension<Arc<EventAccess>>,
) -> ApiResult<ReadResponse<Document>> {
let coll = state
.app_stores
.db
.collection::<Document>(&Store::Metrics.to_string());

let doc = match coll
.find_one(
bson::doc! { "clientId": user_event_access.ownership.client_id.clone()},
None,
)
.await
{
Ok(Some(doc)) => doc,
Ok(None) => return Err(not_found!("Client")),
Err(e) => {
error!("Could not fetch metric: {e}");
return Err(internal_server_error!());
}
};

Ok(Json(ReadResponse {
rows: vec![doc],
total: 1,
skip: 0,
limit: 1,
}))
}

pub async fn get_metrics(
state: State<Arc<AppState>>,
path: Option<Path<String>>,
Expand Down
Loading