Skip to content

Commit

Permalink
extract common logic of sorting according to layout + do not omit cha…
Browse files Browse the repository at this point in the history
…rts missing from layout
  • Loading branch information
bragov4ik committed Dec 5, 2024
1 parent 25a5bc0 commit 35b9d54
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
48 changes: 48 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,53 @@ 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`.
///
/// `push_missing_items_back`:
/// - if `true`, then items not present in
/// `layout` are placed at the end of the vector in their original
/// relative order.
/// - if `false` - at the beginning with the same logic.
pub fn sorted_items_according_to_layout<Item, Key, F>(
to_sort: Vec<Item>,
layout: &Vec<Key>,
get_key: F,
push_missing_items_back: bool,
) -> Vec<Item>
where
Key: Ord,
F: Fn(&Item) -> &Key,
{
let assigned_positions: BTreeMap<_, _> = layout
.iter()
.enumerate()
.map(|(pos, key)| (key, pos))
.collect();
let mut to_sort_with_new_positions: Vec<_> = to_sort
.into_iter()
.map(|item| {
let mut key = assigned_positions.get(get_key(&item)).copied();
if push_missing_items_back {
key.get_or_insert(usize::MAX);
}
(item, key)
})
.collect();
to_sort_with_new_positions.sort_by_key(|p| p.1);
to_sort_with_new_positions
.into_iter()
.map(|p| p.0)
.collect()
}
19 changes: 14 additions & 5 deletions stats/stats-server/src/config/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Common types for the configs
use std::collections::{BTreeMap, HashSet};
use std::collections::{BTreeMap, HashMap, HashSet};

use cron::Schedule;
use serde::{Deserialize, Serialize};
Expand All @@ -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,18 @@ 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,
true,
);
proto_v1::LineChartSection {
id: self.id,
title: self.title,
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

0 comments on commit 35b9d54

Please sign in to comment.