From 3e85559e395287930465d87600117e4004d8ab47 Mon Sep 17 00:00:00 2001 From: Gabriel Viganotti Date: Wed, 22 Jan 2025 17:52:19 -0300 Subject: [PATCH] refactor: move server api data types to its own module --- src/app.rs | 66 +--------------------------------- src/bg_tasks.rs | 3 +- src/db_client.rs | 2 +- src/helpers.rs | 3 +- src/lcd.rs | 2 +- src/lib.rs | 1 + src/main.rs | 2 +- src/nodes_list_view.rs | 3 +- src/server_api.rs | 18 ++++------ src/server_api_types.rs | 79 +++++++++++++++++++++++++++++++++++++++++ src/settings.rs | 2 +- 11 files changed, 96 insertions(+), 85 deletions(-) create mode 100644 src/server_api_types.rs diff --git a/src/app.rs b/src/app.rs index 2733738..0d22f71 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,6 +10,7 @@ use super::{ node_actions::NodesActionsView, node_instance::{ContainerId, NodeInstanceInfo}, nodes_list_view::NodesListView, + server_api_types::{AppSettings, BatchInProgress, Stats}, sort_nodes::{NodesSortStrategy, SortStrategyView}, stats::AggregatedStatsView, }; @@ -26,7 +27,6 @@ use tokio::{ time::Instant, }; -use alloy::primitives::U256; #[cfg(feature = "hydrate")] use gloo_timers::future::sleep; use leptos::prelude::*; @@ -35,7 +35,6 @@ use leptos_router::{ components::{Route, Router, Routes}, StaticSegment, }; -use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, time::Duration, @@ -47,48 +46,6 @@ extern "C" { pub async fn get_addr_from_metamask() -> JsValue; } -// Application settings values. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct AppSettings { - pub nodes_auto_upgrade: bool, - pub nodes_auto_upgrade_delay: Duration, - pub node_bin_version_polling_freq: Duration, - pub nodes_metrics_polling_freq: Duration, - pub rewards_balances_retrieval_freq: Duration, - pub l2_network_rpc_url: String, - pub token_contract_address: String, - pub lcd_display_enabled: bool, - pub lcd_device: String, - pub lcd_addr: String, -} - -impl Default for AppSettings { - fn default() -> Self { - Self { - // Node auto-upgrading is disabled by default. - nodes_auto_upgrade: false, - // Delay 10 secs. between each node being auto-upgraded. - nodes_auto_upgrade_delay: Duration::from_secs(10), - // Check latest version of node binary every couple of hours. - node_bin_version_polling_freq: Duration::from_secs(60 * 60 * 2), - // How often to fetch metrics and node info from active/running nodes - nodes_metrics_polling_freq: Duration::from_secs(5), - // Retrieve balances every 15 mins. - rewards_balances_retrieval_freq: Duration::from_secs(60 * 15), - // Arbitrum Sepolia testnet. - l2_network_rpc_url: "https://sepolia-rollup.arbitrum.io/rpc".to_string(), - // ANT token contract on Arbitrum Sepolia testnet. - token_contract_address: "0xBE1802c27C324a28aeBcd7eeC7D734246C807194".to_string(), - // External LCD device disabled. - lcd_display_enabled: false, - // I2C bus number 1, i.e. device at /dev/i2c-1. - lcd_device: "1".to_string(), - // I2C backpack address 0x27, another common addr is: 0x3f. Check it out with 'sudo ic2detect -y '. - lcd_addr: "0x27".to_string(), - } - } -} - // Maximum number of metrics data points to be kept per node on DB cache. pub const METRICS_MAX_SIZE_PER_CONTAINER: usize = 5_000; // How often we poll the backedn to retrieve an up to date list of node instances. @@ -108,19 +65,6 @@ pub enum BgTasksCmds { CheckAllBalances, } -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -pub struct Stats { - pub total_balance: U256, - pub total_nodes: usize, - pub active_nodes: usize, - pub inactive_nodes: usize, - pub connected_peers: usize, - pub shunned_count: usize, - pub estimated_net_size: usize, - pub stored_records: usize, - pub relevant_records: usize, -} - #[cfg(feature = "ssr")] #[derive(Clone, FromRef, Debug)] pub struct ServerGlobalState { @@ -141,14 +85,6 @@ pub struct ServerGlobalState { pub stats: Arc>, } -#[derive(Clone, Default, Serialize, Deserialize)] -pub struct BatchInProgress { - pub created: u16, - pub total: u16, - pub auto_start: bool, - pub interval_secs: u64, -} - // List of nodes which status is temporarily immutable/locked, // along with expiration information for when it should be unlocked. #[cfg(feature = "ssr")] diff --git a/src/bg_tasks.rs b/src/bg_tasks.rs index 1ca83e8..fb08c9e 100644 --- a/src/bg_tasks.rs +++ b/src/bg_tasks.rs @@ -1,11 +1,12 @@ use super::{ - app::{AppSettings, BgTasksCmds, ImmutableNodeStatus, Stats, METRICS_MAX_SIZE_PER_CONTAINER}, + app::{BgTasksCmds, ImmutableNodeStatus, METRICS_MAX_SIZE_PER_CONTAINER}, db_client::DbClient, docker_client::DockerClient, lcd::display_stats_on_lcd, metrics_client::{NodeMetricsClient, NodesMetrics}, node_instance::NodeInstanceInfo, server_api::helper_upgrade_node_instance, + server_api_types::{AppSettings, Stats}, }; use alloy::{ primitives::{Address, U256}, diff --git a/src/db_client.rs b/src/db_client.rs index 40b98aa..6da32ce 100644 --- a/src/db_client.rs +++ b/src/db_client.rs @@ -1,7 +1,7 @@ use super::{ - app::AppSettings, metrics::{Metrics, NodeMetric}, node_instance::{ContainerId, NodeInstanceInfo, NodeStatus}, + server_api_types::AppSettings, }; use alloy::primitives::U256; diff --git a/src/helpers.rs b/src/helpers.rs index d3d33ee..69da729 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,5 +1,3 @@ -use crate::app::BatchInProgress; - use super::{ app::ClientGlobalState, node_instance::{ContainerId, NodeInstanceInfo}, @@ -7,6 +5,7 @@ use super::{ create_node_instance, delete_node_instance, prepare_node_instances_batch, start_node_logs_stream, }, + server_api_types::BatchInProgress, }; use alloy::primitives::U256; diff --git a/src/lcd.rs b/src/lcd.rs index a65ace1..d6dd55f 100644 --- a/src/lcd.rs +++ b/src/lcd.rs @@ -1,4 +1,4 @@ -use super::app::{AppSettings, BgTasksCmds}; +use super::{app::BgTasksCmds, server_api_types::AppSettings}; use eyre::eyre; use i2cdev::{ diff --git a/src/lib.rs b/src/lib.rs index 8a57646..d39865a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,7 @@ mod node_actions; pub mod node_instance; mod nodes_list_view; mod server_api; +pub mod server_api_types; mod settings; mod sort_nodes; mod stats; diff --git a/src/main.rs b/src/main.rs index c048638..dc88a9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ async fn main() { use axum::Router; use formicaio::{ app::*, bg_tasks::spawn_bg_tasks, db_client::DbClient, docker_client::DockerClient, - metrics_client::NodesMetrics, + metrics_client::NodesMetrics, server_api_types::Stats, }; use leptos::{logging, prelude::*}; use leptos_axum::{generate_route_list, LeptosRoutes}; diff --git a/src/nodes_list_view.rs b/src/nodes_list_view.rs index 73d3ae0..077eda3 100644 --- a/src/nodes_list_view.rs +++ b/src/nodes_list_view.rs @@ -1,5 +1,5 @@ use super::{ - app::{BatchInProgress, ClientGlobalState}, + app::ClientGlobalState, chart_view::{node_metrics_update, ChartSeriesData, NodeChartView}, helpers::{node_logs_stream, show_alert_msg, truncated_balance_str}, icons::{ @@ -9,6 +9,7 @@ use super::{ node_actions::NodeAction, node_instance::NodeInstanceInfo, server_api::cancel_node_instances_batch, + server_api_types::BatchInProgress, }; use alloy::primitives::utils::format_units; diff --git a/src/server_api.rs b/src/server_api.rs index b2ca6b4..501e89b 100644 --- a/src/server_api.rs +++ b/src/server_api.rs @@ -1,11 +1,11 @@ use super::{ - app::{BatchInProgress, Stats}, node_instance::{ContainerId, NodeInstanceInfo}, + server_api_types::BatchInProgress, + server_api_types::NodesInstancesInfo, }; use self::server_fn::codec::{ByteStream, Streaming}; use leptos::prelude::*; -use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[cfg(feature = "ssr")] @@ -24,14 +24,6 @@ use std::time::Duration; #[cfg(feature = "ssr")] use tokio::{select, time::sleep}; -#[derive(Clone, Serialize, Deserialize)] -pub struct NodesInstancesInfo { - pub latest_bin_version: Option, - pub nodes: HashMap, - pub stats: Stats, - pub batch_in_progress: Option, -} - // Obtain the list of existing nodes instances with their info #[server(ListNodeInstances, "/api", "Url", "/list_nodes")] pub async fn nodes_instances() -> Result { @@ -368,7 +360,7 @@ pub async fn node_metrics( // Retrieve the settings #[server(GetSettings, "/api", "Url", "/get_settings")] -pub async fn get_settings() -> Result { +pub async fn get_settings() -> Result { let context = expect_context::(); let settings = context.db_client.get_settings().await; @@ -377,7 +369,9 @@ pub async fn get_settings() -> Result { // Update the settings #[server(UpdateSettings, "/api", "Url", "/update_settings")] -pub async fn update_settings(settings: super::app::AppSettings) -> Result<(), ServerFnError> { +pub async fn update_settings( + settings: super::server_api_types::AppSettings, +) -> Result<(), ServerFnError> { let context = expect_context::(); context.db_client.update_settings(&settings).await?; context diff --git a/src/server_api_types.rs b/src/server_api_types.rs new file mode 100644 index 0000000..050ddb6 --- /dev/null +++ b/src/server_api_types.rs @@ -0,0 +1,79 @@ +use super::node_instance::NodeInstanceInfo; + +use alloy::primitives::U256; +use serde::{Deserialize, Serialize}; +use std::{collections::HashMap, time::Duration}; + +/// List of nodes, stats and currently running batch. +#[derive(Clone, Serialize, Deserialize)] +pub struct NodesInstancesInfo { + pub latest_bin_version: Option, + pub nodes: HashMap, + pub stats: Stats, + pub batch_in_progress: Option, +} + +/// Application settings values. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct AppSettings { + pub nodes_auto_upgrade: bool, + pub nodes_auto_upgrade_delay: Duration, + pub node_bin_version_polling_freq: Duration, + pub nodes_metrics_polling_freq: Duration, + pub rewards_balances_retrieval_freq: Duration, + pub l2_network_rpc_url: String, + pub token_contract_address: String, + pub lcd_display_enabled: bool, + pub lcd_device: String, + pub lcd_addr: String, +} + +impl Default for AppSettings { + fn default() -> Self { + Self { + // Node auto-upgrading is disabled by default. + nodes_auto_upgrade: false, + // Delay 10 secs. between each node being auto-upgraded. + nodes_auto_upgrade_delay: Duration::from_secs(10), + // Check latest version of node binary every couple of hours. + node_bin_version_polling_freq: Duration::from_secs(60 * 60 * 2), + // How often to fetch metrics and node info from active/running nodes + nodes_metrics_polling_freq: Duration::from_secs(5), + // Retrieve balances every 15 mins. + rewards_balances_retrieval_freq: Duration::from_secs(60 * 15), + // Arbitrum Sepolia testnet. + l2_network_rpc_url: "https://sepolia-rollup.arbitrum.io/rpc".to_string(), + // ANT token contract on Arbitrum Sepolia testnet. + token_contract_address: "0xBE1802c27C324a28aeBcd7eeC7D734246C807194".to_string(), + // External LCD device disabled. + lcd_display_enabled: false, + // I2C bus number 1, i.e. device at /dev/i2c-1. + lcd_device: "1".to_string(), + // I2C backpack address 0x27, another common addr is: 0x3f. Check it out with 'sudo ic2detect -y '. + lcd_addr: "0x27".to_string(), + } + } +} + +/// Node stats collected by the backend and retrievable through the public server API. +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct Stats { + pub total_balance: U256, + pub total_nodes: usize, + pub active_nodes: usize, + pub inactive_nodes: usize, + pub connected_peers: usize, + pub shunned_count: usize, + pub estimated_net_size: usize, + pub stored_records: usize, + pub relevant_records: usize, +} + +/// Information about any actively running nodes creation batch. +#[derive(Clone, Default, Serialize, Deserialize)] +pub struct BatchInProgress { + pub created: u16, + pub total: u16, + pub auto_start: bool, + pub interval_secs: u64, +} diff --git a/src/settings.rs b/src/settings.rs index 660e6d6..decb182 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,8 +1,8 @@ use super::{ - app::AppSettings, helpers::show_alert_msg, icons::IconCancel, server_api::{get_settings, update_settings}, + server_api_types::AppSettings, }; use alloy::primitives::Address;