Skip to content

Commit

Permalink
refactor/nanocld: cleaner event services with file separation
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Oct 23, 2024
1 parent cf6b6e3 commit 6cf7fd9
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 202 deletions.
2 changes: 1 addition & 1 deletion bin/nanocld/specs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ paths:
get:
tags:
- Events
summary: Get events of all peer nodes
summary: List events with optional filter
operationId: list_event
parameters:
- name: filter
Expand Down
201 changes: 0 additions & 201 deletions bin/nanocld/src/services/event.rs

This file was deleted.

32 changes: 32 additions & 0 deletions bin/nanocld/src/services/event/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::generic::{GenericCount, GenericListQuery};

use crate::{
models::{EventDb, SystemState},
repositories::generic::*,
utils,
};

/// Count events
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Events",
path = "/events/count",
params(
("filter" = Option<String>, Query, description = "Generic filter", example = "{ \"filter\": { \"where\": { \"kind\": { \"eq\": \"CPU\" } } } }"),
),
responses(
(status = 200, description = "Count result", body = GenericCount),
),
))]
#[web::get("/events/count")]
pub async fn count_event(
state: web::types::State<SystemState>,
qs: web::types::Query<GenericListQuery>,
) -> HttpResult<web::HttpResponse> {
let filter = utils::query_string::parse_qs_filter(&qs)?;
let count = EventDb::count_by(&filter, &state.inner.pool).await?;
Ok(web::HttpResponse::Ok().json(&GenericCount { count }))
}

Check warning on line 32 in bin/nanocld/src/services/event/count.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/event/count.rs#L25-L32

Added lines #L25 - L32 were not covered by tests
29 changes: 29 additions & 0 deletions bin/nanocld/src/services/event/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ntex::web;

use nanocl_error::http::HttpResult;

use crate::{
models::{EventDb, SystemState},
repositories::generic::*,
};

/// Get detailed information about an event
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Events",
path = "/events/{key}/inspect",
params(
("key" = String, Path, description = "Key of the event"),
),
responses(
(status = 200, description = "Detailed information about the event", body = Event),
),
))]
#[web::get("/events/{key}/inspect")]
pub async fn inspect_event(
state: web::types::State<SystemState>,
path: web::types::Path<(String, String)>,
) -> HttpResult<web::HttpResponse> {
let event = EventDb::transform_read_by_pk(&path.1, &state.inner.pool).await?;
Ok(web::HttpResponse::Ok().json(&event))
}
32 changes: 32 additions & 0 deletions bin/nanocld/src/services/event/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::generic::GenericListQuery;

use crate::{
models::{EventDb, SystemState},
repositories::generic::*,
utils,
};

/// List events with optional filter
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Events",
path = "/events",
params(
("filter" = Option<String>, Query, description = "Generic filter", example = "{ \"filter\": { \"where\": { \"kind\": { \"eq\": \"normal\" } } } }"),
),
responses(
(status = 200, description = "List of events", body = Vec<Event>),
),
))]
#[web::get("/events")]
pub async fn list_event(
state: web::types::State<SystemState>,
qs: web::types::Query<GenericListQuery>,
) -> HttpResult<web::HttpResponse> {
let filter = utils::query_string::parse_qs_filter(&qs)?;
let events = EventDb::transform_read_by(&filter, &state.inner.pool).await?;
Ok(web::HttpResponse::Ok().json(&events))
}
Loading

0 comments on commit 6cf7fd9

Please sign in to comment.