Skip to content

Commit

Permalink
feat: display rewards received by each individual node apart from cur…
Browse files Browse the repository at this point in the history
…rent balance
  • Loading branch information
bochaco committed Nov 15, 2024
1 parent 9308f4b commit e7b0d73
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn spawn_nodes_list_polling() {
cn.peer_id = updated.peer_id.clone();
cn.status_info = updated.status_info.clone();
cn.bin_version = updated.bin_version.clone();
cn.rewards = updated.rewards;
cn.balance = updated.balance;
cn.records = updated.records;
cn.relevant_records = updated.relevant_records;
Expand Down
14 changes: 12 additions & 2 deletions src/db_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::{
node_instance::{ContainerId, NodeInstanceInfo},
};

use alloy::primitives::U256;
use leptos::*;
use serde::{Deserialize, Serialize};
use sqlx::{
Expand Down Expand Up @@ -40,7 +41,7 @@ pub struct CachedNodeMetadata {
pub bin_version: String,
pub port: u16,
pub rpc_api_port: u16,
pub rewards: String, // TODO: currently unused, fetch value from ledger
pub rewards: String,
pub balance: String,
pub records: String,
pub connected_peers: String,
Expand All @@ -63,8 +64,13 @@ impl CachedNodeMetadata {
if self.rpc_api_port > 0 {
info.rpc_api_port = Some(self.rpc_api_port);
}
if !self.rewards.is_empty() {
if let Ok(v) = U256::from_str(&self.rewards) {
info.rewards = Some(v.into());
}
}
if !self.balance.is_empty() {
if let Ok(v) = alloy::primitives::U256::from_str(&self.balance) {
if let Ok(v) = U256::from_str(&self.balance) {
info.balance = Some(v.into());
}
}
Expand Down Expand Up @@ -188,6 +194,10 @@ impl DbClient {
updates.push("bin_version=?");
params.push(bin_version.clone());
}
if let Some(rewards) = &info.rewards {
updates.push("rewards=?");
params.push(rewards.to_string());
}
if let Some(balance) = &info.balance {
updates.push("balance=?");
params.push(balance.to_string());
Expand Down
10 changes: 8 additions & 2 deletions src/metrics_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ use super::{
node_instance::{ContainerId, NodeInstanceInfo},
};

use alloy::primitives::U256;
use chrono::Utc;
use leptos::*;
use std::collections::HashMap;
use std::{collections::HashMap, str::FromStr};
use thiserror::Error;

// Default value for the nodes metrics host
const DEFAULT_NODES_METRICS_HOST: &str = "127.0.0.1";

// Predefined set of metrics to monitor and collect.
const NODE_METRICS_TO_COLLECT: [&str; 9] = [
const NODE_METRICS_TO_COLLECT: [&str; 10] = [
METRIC_KEY_BALANCE,
METRIC_KEY_STORE_COST,
METRIC_KEY_MEM_USED_MB,
METRIC_KEY_CPU_USEAGE,
Expand Down Expand Up @@ -136,6 +138,10 @@ impl NodesMetrics {
// Update given node instance info with in-memory cached metrics
pub fn update_node_info(&self, info: &mut NodeInstanceInfo) {
if let Some(metrics) = self.data.get(&info.container_id) {
if let Some(metric) = metrics.get(METRIC_KEY_BALANCE) {
info.rewards = U256::from_str(&metric.value).ok();
}

if let Some(metric) = metrics.get(METRIC_KEY_STORE_COST) {
info.store_cost = metric.value.parse::<u64>().ok();
}
Expand Down
1 change: 1 addition & 0 deletions src/node_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct NodeInstanceInfo {
pub node_ip: Option<String>,
pub balance: Option<U256>,
pub rewards_addr: Option<String>, // hex-encoded rewards address
pub rewards: Option<U256>,
pub records: Option<usize>,
pub relevant_records: Option<usize>,
pub store_cost: Option<u64>,
Expand Down
16 changes: 14 additions & 2 deletions src/nodes_list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,20 @@ fn NodeInstanceView(
{move || info.get().bin_version.unwrap_or_else(|| "unknown".to_string())}
</p>
<p>
<span class="node-info-item">"Balance: "</span>
{move || info.get().balance.map_or("unknown".to_string(), |v| v.to_string())}
<div class="flex flex-row">
<div class="basis-1/2">
<span class="node-info-item">"Balance: "</span>
{move || {
info.get().balance.map_or("unknown".to_string(), |v| v.to_string())
}}
</div>
<div class="basis-1/2">
<span class="node-info-item">"Rewards: "</span>
{move || {
info.get().rewards.map_or("unknown".to_string(), |v| v.to_string())
}}
</div>
</div>
</p>
<p>
<span class="node-info-item">"Rewards addr: "</span>
Expand Down

0 comments on commit e7b0d73

Please sign in to comment.