Skip to content

Commit

Permalink
refactor for namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed May 26, 2024
1 parent e69d0ac commit 603df02
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
8 changes: 4 additions & 4 deletions bin/nanocld/src/repositories/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use diesel::prelude::*;

use nanocl_error::io::IoResult;
Expand All @@ -12,10 +14,8 @@ use crate::{
use super::generic::*;

impl RepositoryBase for EventDb {
fn get_columns<'a>(
) -> std::collections::HashMap<&'a str, (crate::models::ColumnType, &'a str)>
{
std::collections::HashMap::from([
fn get_columns<'a>() -> HashMap<&'a str, (ColumnType, &'a str)> {
HashMap::from([
("key", (ColumnType::Uuid, "events.key")),
(
"reporting_node",
Expand Down
7 changes: 4 additions & 3 deletions bin/nanocld/src/repositories/job.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use diesel::prelude::*;

use futures_util::StreamExt;
Expand All @@ -24,9 +26,8 @@ use super::generic::*;

impl RepositoryBase for JobDb {
fn get_columns<'a>(
) -> std::collections::HashMap<&'a str, (crate::models::ColumnType, &'a str)>
{
std::collections::HashMap::from([
) -> std::collections::HashMap<&'a str, (ColumnType, &'a str)> {
HashMap::from([
("key", (ColumnType::Text, "jobs.key")),
("data", (ColumnType::Json, "jobs.data")),
("metadata", (ColumnType::Json, "jobs.metadata")),
Expand Down
19 changes: 16 additions & 3 deletions bin/nanocld/src/repositories/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ use diesel::prelude::*;
use nanocl_stubs::generic::GenericFilter;

use crate::{
gen_sql_multiple, gen_sql_where4json, gen_sql_where4uuid,
gen_sql_where4string, models::MetricDb, schema::metrics,
gen_sql_multiple, gen_sql_where4json, gen_sql_where4string,
gen_sql_where4uuid,
models::{ColumnType, MetricDb},
schema::metrics,
};

use super::generic::*;

impl RepositoryBase for MetricDb {}
impl RepositoryBase for MetricDb {
fn get_columns<'a>(
) -> std::collections::HashMap<&'a str, (ColumnType, &'a str)> {
std::collections::HashMap::from([
("key", (ColumnType::Uuid, "metrics.key")),
("node_name", (ColumnType::Text, "metrics.node_name")),
("kind", (ColumnType::Text, "metrics.kind")),
("data", (ColumnType::Json, "metrics.data")),
// ("created_at", (ColumnType::Timestamp, "metrics.created_at")),
])
}

Check warning on line 23 in bin/nanocld/src/repositories/metric.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/repositories/metric.rs#L14-L23

Added lines #L14 - L23 were not covered by tests
}

impl RepositoryCreate for MetricDb {}

Expand Down
31 changes: 18 additions & 13 deletions bin/nanocld/src/repositories/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use diesel::prelude::*;

use bollard_next::network::InspectNetworkOptions;
Expand All @@ -6,14 +8,21 @@ use nanocl_error::http::{HttpError, HttpResult};
use nanocl_stubs::{generic::GenericFilter, namespace::NamespaceSummary};

use crate::{
gen_sql_multiple, gen_sql_order_by, gen_sql_query,
schema::namespaces,
gen_sql_multiple, gen_sql_where4string,
models::{CargoDb, NamespaceDb, ProcessDb, SystemState},
models::{CargoDb, ColumnType, NamespaceDb, ProcessDb, SystemState},
};

use super::generic::*;

impl RepositoryBase for NamespaceDb {}
impl RepositoryBase for NamespaceDb {
fn get_columns<'a>() -> HashMap<&'a str, (ColumnType, &'a str)> {
HashMap::from([
("name", (ColumnType::Text, "namespaces.name")),
// ("created_at", (crate::models::ColumnType::Timestamp, "namespaces.created_at")),
])
}
}

impl RepositoryCreate for NamespaceDb {}

Expand All @@ -34,11 +43,11 @@ impl RepositoryReadBy for NamespaceDb {
diesel::pg::PgConnection,
Self::Output,
> {
let condition = filter.r#where.to_owned().unwrap_or_default();
let r#where = condition.conditions;
let mut query = namespaces::table.into_boxed();
if let Some(name) = r#where.get("name") {
gen_sql_where4string!(query, namespaces::name, name);
let columns = Self::get_columns();
query = gen_sql_query!(query, filter, columns);
if let Some(orders) = &filter.order_by {
query = gen_sql_order_by!(query, orders, columns);

Check warning on line 50 in bin/nanocld/src/repositories/namespace.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/repositories/namespace.rs#L50

Added line #L50 was not covered by tests
}
if is_multiple {
gen_sql_multiple!(query, namespaces::created_at, filter);
Expand All @@ -51,13 +60,9 @@ impl RepositoryCountBy for NamespaceDb {
fn gen_count_query(
filter: &GenericFilter,
) -> impl diesel::query_dsl::LoadQuery<'static, diesel::PgConnection, i64> {
let condition = filter.r#where.to_owned().unwrap_or_default();
let r#where = condition.conditions;
let mut query = namespaces::table.into_boxed();
if let Some(name) = r#where.get("name") {
gen_sql_where4string!(query, namespaces::name, name);
}
query.count()
let columns = Self::get_columns();
gen_sql_query!(query, filter, columns).count()

Check warning on line 65 in bin/nanocld/src/repositories/namespace.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/repositories/namespace.rs#L64-L65

Added lines #L64 - L65 were not covered by tests
}
}

Expand Down

0 comments on commit 603df02

Please sign in to comment.