Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stats): apply counters layout settings #1142

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions stats/stats-server/src/config/json/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mod tests {
const EXAMPLE_CONFIG: &str = r#"{
"counters_order": [
"total_blocks",
"total_txns"
"total_txns",
"best_block_number"
],
"line_chart_categories": [
{
Expand All @@ -36,7 +37,9 @@ mod tests {
assert_eq!(
c,
Config {
counters_order: ["total_blocks", "total_txns"].map(|s| s.to_owned()).into(),
counters_order: ["total_blocks", "total_txns", "best_block_number"]
.map(|s| s.to_owned())
.into(),
line_chart_categories: vec![LineChartCategory {
id: "accounts".into(),
title: "Accounts".into(),
Expand Down
41 changes: 41 additions & 0 deletions stats/stats-server/src/config/read/layout.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::collections::BTreeMap;

use crate::config::{json, types::LineChartCategory};
use convert_case::{Case, Casing};
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
pub counters_order: Vec<String>,
pub line_chart_categories: Vec<LineChartCategory>,
}

Expand All @@ -16,8 +19,46 @@ impl From<json::layout::Config> for Config {
*chart_name = chart_name.from_case(Case::Snake).to_case(Case::Camel)
}
}
let counters_order = value
.counters_order
.into_iter()
.map(|id| id.from_case(Case::Snake).to_case(Case::Camel))
.collect();
Self {
counters_order,
line_chart_categories,
}
}
}

/// Arranges the items `to_sort` according to order in `layout`.
///
/// Items not present in `layout` are placed at the end of the
/// vector in their original relative order.
pub fn sorted_items_according_to_layout<Item, Key, F>(
to_sort: Vec<Item>,
layout: &[Key],
get_key: F,
) -> Vec<Item>
where
Key: Ord,
F: Fn(&Item) -> &Key,
{
let assigned_positions: BTreeMap<_, _> = layout
.iter()
.enumerate()
.map(|(pos, key)| (key, pos))
.collect();
let mut result: Vec<_> = to_sort
.into_iter()
.map(|item| {
let position = assigned_positions
.get(get_key(&item))
.copied()
.unwrap_or(usize::MAX);
(item, position)
})
.collect();
result.sort_by_key(|p| p.1);
result.into_iter().map(|p| p.0).collect()
}
16 changes: 12 additions & 4 deletions stats/stats-server/src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use stats_proto::blockscout::stats::v1 as proto_v1;

use crate::runtime_setup::EnabledChartEntry;

use super::layout::sorted_items_according_to_layout;

/// `None` means 'enable if present'
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
Expand Down Expand Up @@ -192,11 +194,17 @@ impl LineChartCategory {
self,
info: &BTreeMap<String, EnabledChartEntry>,
) -> proto_v1::LineChartSection {
let charts: Vec<_> = self
.charts_order
.into_iter()
.flat_map(|c: String| info.get(&c).map(|e| e.build_proto_line_chart_info(c)))
let category_charts = HashSet::<_>::from_iter(self.charts_order.iter());
let category_infos_alphabetic_order: Vec<_> = info
.iter()
.filter(|(id, _)| category_charts.contains(id))
.map(|(id, e)| e.build_proto_line_chart_info(id.to_string()))
.collect();
let charts = sorted_items_according_to_layout(
category_infos_alphabetic_order,
&self.charts_order,
|chart_info| &chart_info.id,
);
proto_v1::LineChartSection {
id: self.id,
title: self.title,
Expand Down
8 changes: 6 additions & 2 deletions stats/stats-server/src/read_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{clone::Clone, cmp::Ord, collections::BTreeMap, fmt::Debug, str::FromStr, sync::Arc};

use crate::{
config::types,
config::{layout::sorted_items_according_to_layout, types},
runtime_setup::{EnabledChartEntry, RuntimeSetup},
serializers::serialize_line_points,
settings::LimitsSettings,
Expand Down Expand Up @@ -225,7 +225,11 @@ impl StatsService for ReadService {
})
})
.collect();
let counters = proto_v1::Counters { counters };
let counters_sorted =
sorted_items_according_to_layout(counters, &self.charts.counters_layout, |c| &c.id);
let counters = proto_v1::Counters {
counters: counters_sorted,
};
Ok(Response::new(counters))
}

Expand Down
2 changes: 2 additions & 0 deletions stats/stats-server/src/runtime_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub struct UpdateGroupEntry {

pub struct RuntimeSetup {
pub lines_layout: Vec<LineChartCategory>,
pub counters_layout: Vec<String>,
pub update_groups: BTreeMap<String, UpdateGroupEntry>,
pub charts_info: BTreeMap<String, EnabledChartEntry>,
}
Expand Down Expand Up @@ -128,6 +129,7 @@ impl RuntimeSetup {
let update_groups = Self::init_update_groups(update_groups, &charts_info)?;
Ok(Self {
lines_layout: layout.line_chart_categories,
counters_layout: layout.counters_order,
update_groups,
charts_info,
})
Expand Down
Loading