From 4a7e4ab4949dc5e20ed7926078a024fcadb76707 Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 11 Sep 2024 08:24:08 +0900 Subject: [PATCH] Remove unnecessary "Giganto" prefix Closed #819 Closed #820 Remove unnecessary "Giganto" prefix Change setConfig to return Result --- CHANGELOG.md | 7 +++++-- src/graphql.rs | 4 ++-- src/graphql/status.rs | 47 ++++++++++++++++++++++--------------------- src/settings.rs | 12 +++++------ 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 689f290..4ad65b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,8 +69,11 @@ Versioning](https://semver.org/spec/v2.0.0.html). - Changed command line interface. - Removed `cert`, `key`, `root` fields from config file. - Added cli options `-c`, `--cert`, `--key` and `--ca-certs`. -- Changed `setGigantoConfig` to receive toml-string with full configuration. -- Updated `gigantoConfig` to respond full configuration. +- Renamed GraphQL API `gigantoConfig` to `config` and updated it to respond + the full configuration. +- Renamed GraphQL API `setGigantoConfig` to `setConfig`. The endpoint now + accepts a full configuration as a TOML string and returns `Result`, + instead of `Result`. ### Removed diff --git a/src/graphql.rs b/src/graphql.rs index 7adb02f..00de962 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -57,7 +57,7 @@ pub struct Query( export::ExportQuery, packet::PacketQuery, timeseries::TimeSeriesQuery, - status::GigantoStatusQuery, + status::StatusQuery, source::SourceQuery, statistics::StatisticsQuery, sysmon::SysmonQuery, @@ -66,7 +66,7 @@ pub struct Query( ); #[derive(Default, MergedObject)] -pub struct Mutation(status::GigantoConfigMutation); +pub struct Mutation(status::ConfigMutation); #[derive(InputObject, Serialize, Clone)] pub struct TimeRange { diff --git a/src/graphql/status.rs b/src/graphql/status.rs index 0dfdba4..89ccfea 100644 --- a/src/graphql/status.rs +++ b/src/graphql/status.rs @@ -11,7 +11,7 @@ use tracing::info; use super::{PowerOffNotify, RebootNotify, ReloadNotify, TerminateNotify}; use crate::peer::PeerIdentity; -use crate::settings::GigantoConfig; +use crate::settings::Config; #[cfg(debug_assertions)] use crate::storage::Database; use crate::AckTransmissionCount; @@ -28,7 +28,7 @@ pub trait TomlPeers { } #[derive(SimpleObject, Debug)] -struct GigantoStatus { +struct Status { name: String, cpu_usage: f32, total_memory: u64, @@ -50,7 +50,7 @@ struct Properties { } #[Object] -impl GigantoConfig { +impl Config { async fn ingest_srv_addr(&self) -> String { self.ingest_srv_addr.to_string() } @@ -120,17 +120,17 @@ impl PeerIdentity { } #[derive(Default)] -pub(super) struct GigantoStatusQuery; +pub(super) struct StatusQuery; #[derive(Default)] -pub(super) struct GigantoConfigMutation; +pub(super) struct ConfigMutation; #[Object] -impl GigantoStatusQuery { - async fn giganto_status(&self) -> Result { +impl StatusQuery { + async fn status(&self) -> Result { let usg = roxy::resource_usage().await; let hostname = roxy::hostname(); - let usg = GigantoStatus { + let usg = Status { name: hostname, cpu_usage: usg.cpu_usage, total_memory: usg.total_memory, @@ -161,11 +161,11 @@ impl GigantoStatusQuery { } #[allow(clippy::unused_async)] - async fn giganto_config<'ctx>(&self, ctx: &Context<'ctx>) -> Result { + async fn config<'ctx>(&self, ctx: &Context<'ctx>) -> Result { let cfg_path = ctx.data::()?; let toml = fs::read_to_string(cfg_path).context("toml not found")?; - let config: GigantoConfig = toml::from_str(&toml)?; + let config: Config = toml::from_str(&toml)?; Ok(config) } @@ -177,15 +177,15 @@ impl GigantoStatusQuery { } #[Object] -impl GigantoConfigMutation { +impl ConfigMutation { #[allow(clippy::unused_async)] - async fn set_giganto_config<'ctx>(&self, ctx: &Context<'ctx>, draft: String) -> Result { - let config_draft: GigantoConfig = toml::from_str(&draft)?; + async fn set_config<'ctx>(&self, ctx: &Context<'ctx>, draft: String) -> Result { + let config_draft: Config = toml::from_str(&draft)?; let cfg_path = ctx.data::()?; let config_toml = fs::read_to_string(cfg_path).context("toml not found")?; - let config: GigantoConfig = toml::from_str(&config_toml)?; + let config: Config = toml::from_str(&config_toml)?; if config == config_draft { info!("No changes. config: {config:?}, draft: {config_draft:?}"); @@ -206,7 +206,7 @@ impl GigantoConfigMutation { }); info!("Draft applied. config: {config:?}, draft: {config_draft:?}"); - Ok("Done".to_string()) + Ok(true) } async fn set_ack_transmission_count<'ctx>( @@ -341,7 +341,7 @@ mod tests { let query = r#" { - gigantoStatus { + status { name cpuUsage totalMemory @@ -357,7 +357,7 @@ mod tests { } #[tokio::test] - async fn test_giganto_config() { + async fn test_config() { let dir = tempfile::tempdir().unwrap(); let config_path = dir.path().join("config.toml"); @@ -370,7 +370,7 @@ mod tests { let query = r#" { - gigantoConfig { + config { ingestSrvAddr publishSrvAddr graphqlSrvAddr @@ -397,12 +397,13 @@ mod tests { let data = res.data.to_string(); assert_eq!( data, - "{gigantoConfig: {ingestSrvAddr: \"0.0.0.0:38370\", publishSrvAddr: \"0.0.0.0:38371\", graphqlSrvAddr: \"127.0.0.1:8442\", dataDir: \"tests/data\", retention: \"3months 8days 16h 19m 12s\", logDir: \"/data/logs/apps\", exportDir: \"tests/export\", ackTransmission: 1024, maxOpenFiles: 8000, maxMbOfLevelBase: \"512\", numOfThread: 8, maxSubCompactions: \"2\", addrToPeers: \"127.0.0.1:48383\", peers: [{addr: \"127.0.0.1:60192\", hostname: \"node2\"}]}}".to_string() + "{config: {ingestSrvAddr: \"0.0.0.0:38370\", publishSrvAddr: \"0.0.0.0:38371\", \ + graphqlSrvAddr: \"127.0.0.1:8442\", dataDir: \"tests/data\", retention: \"3months 8days 16h 19m 12s\", logDir: \"/data/logs/apps\", exportDir: \"tests/export\", ackTransmission: 1024, maxOpenFiles: 8000, maxMbOfLevelBase: \"512\", numOfThread: 8, maxSubCompactions: \"2\", addrToPeers: \"127.0.0.1:48383\", peers: [{addr: \"127.0.0.1:60192\", hostname: \"node2\"}]}}".to_string() ); } #[tokio::test] - async fn test_set_giganto_config() { + async fn test_set_config() { let dir = tempfile::tempdir().unwrap(); let config_path = dir.path().join("config.toml"); @@ -417,7 +418,7 @@ mod tests { let query = format!( r#" mutation {{ - setGigantoConfig(draft: {toml_content:?}) + setConfig(draft: {toml_content:?}) }} "# ); @@ -450,14 +451,14 @@ mod tests { let query = format!( r#" mutation {{ - setGigantoConfig(draft: {new_draft:?}) + setConfig(draft: {new_draft:?}) }} "# ); let res = schema.execute(&query).await; - assert_eq!(res.data.to_string(), "{setGigantoConfig: \"Done\"}"); + assert_eq!(res.data.to_string(), "{setConfig: true}"); // check config temp file changes let config_temp_file = diff --git a/src/settings.rs b/src/settings.rs index f21a97f..f864c99 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, net::SocketAddr, path::PathBuf, time::Duration}; use clap::{ArgAction, Parser}; -use config::{builder::DefaultState, Config, ConfigBuilder, ConfigError, File}; +use config::{builder::DefaultState, Config as ConfConfig, ConfigBuilder, ConfigError, File}; use serde::{de::Error, Deserialize, Deserializer}; use crate::peer::PeerIdentity; @@ -44,7 +44,7 @@ pub struct Args { /// The application settings. #[derive(Clone, Debug, Deserialize, PartialEq, Eq)] -pub struct GigantoConfig { +pub struct Config { #[serde(deserialize_with = "deserialize_socket_addr")] pub ingest_srv_addr: SocketAddr, // IP address & port to ingest data #[serde(deserialize_with = "deserialize_socket_addr")] @@ -74,7 +74,7 @@ pub struct GigantoConfig { #[derive(Clone, Debug, Deserialize)] pub struct Settings { - pub config: GigantoConfig, + pub config: Config, // config file path pub cfg_path: String, @@ -96,7 +96,7 @@ impl Settings { )) } } else { - let config: GigantoConfig = default_config_builder().build()?.try_deserialize()?; + let config: Config = default_config_builder().build()?.try_deserialize()?; let cfg_path = config_path.to_str().expect("path to string").to_string(); Ok(Self { config, cfg_path }) @@ -109,7 +109,7 @@ impl Settings { let s = default_config_builder() .add_source(File::with_name(cfg_path)) .build()?; - let config: GigantoConfig = s.try_deserialize()?; + let config: Config = s.try_deserialize()?; Ok(Self { config, @@ -133,7 +133,7 @@ fn default_config_builder() -> ConfigBuilder { .to_str() .expect("unreachable export path"); - Config::builder() + ConfConfig::builder() .set_default("ingest_srv_addr", DEFAULT_INGEST_SRV_ADDR) .expect("valid address") .set_default("publish_srv_addr", DEFAULT_PUBLISH_SRV_ADDR)