Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
domains::{Domain, DomainInfo, ProtocolInfo},
hashes::{Hash, HashType},
interop_messages::{ExtendedInteropMessage, MessageDirection},
order_direction::OrderDirection,
portfolio::AddressPortfolio,
search_results::{QuickSearchResult, Redirect},
tokens::{AggregatedToken, TokenListUpdate, TokenType},
Expand Down Expand Up @@ -158,6 +159,7 @@ impl Cluster {
pub async fn list_chains(
&self,
sort_metric: Option<ChainMetricKind>,
order_direction: Option<OrderDirection>,
) -> Result<Vec<Chain>, ServiceError> {
let chain_ids = self.active_chain_ids().await?.into_iter().collect();

Expand All @@ -167,7 +169,9 @@ impl Cluster {
.map(|c| c.into())
.collect::<Vec<Chain>>();

let metrics = self.list_chain_metrics(sort_metric).await?;
let metrics = self
.list_chain_metrics(sort_metric, order_direction)
.await?;
let order_map = metrics
.iter()
.enumerate()
Expand All @@ -182,6 +186,7 @@ impl Cluster {
pub async fn list_chain_metrics(
&self,
sort_metric: Option<ChainMetricKind>,
order_direction: Option<OrderDirection>,
) -> Result<Vec<ChainMetrics>, ServiceError> {
let chain_ids = self.active_chain_ids().await?;
let key = format!("{}:chain_metrics", self.name);
Expand All @@ -196,12 +201,19 @@ impl Cluster {
let mut metrics = maybe_cache_lookup!(self.caches.chain_metrics.as_ref(), key, get)?;

let sort_metric = sort_metric.unwrap_or_default();
let desc = order_direction.unwrap_or_default() == OrderDirection::Desc;
metrics.sort_by(|left, right| {
let ordering = match (
left.metric_value_for_sorting(sort_metric),
right.metric_value_for_sorting(sort_metric),
) {
(Some(l), Some(r)) => r.total_cmp(&l),
(Some(l), Some(r)) => {
if desc {
r.total_cmp(&l)
} else {
l.total_cmp(&r)
}
}
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
(None, None) => Ordering::Equal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod domains;
pub mod hashes;
pub mod interop_message_transfers;
pub mod interop_messages;
pub mod order_direction;
pub mod poor_reputation_tokens;
pub mod portfolio;
pub mod sea_orm_wrappers;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Sort order for list endpoints (e.g. "asc" or "desc").
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, strum::Display, strum::EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum OrderDirection {
Asc,
#[default]
Desc,
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ message ListClusterChainsRequest {
string cluster_id = 1;
// Metric used to sort chains. Supported values: active_accounts, daily_transactions,
// new_addresses, tps. Defaults to active_accounts.
optional string sort_by = 2;
optional string sort = 2;
// Sort order: "asc" or "desc". Defaults to "desc" (higher values first).
optional string order = 3;
}

message ListClusterChainsResponse {
Expand All @@ -112,7 +114,9 @@ message ListChainMetricsRequest {
string cluster_id = 1;
// Metric used to sort chain metrics. Supported values: active_accounts, daily_transactions,
// new_addresses, tps. Defaults to active_accounts.
optional string sort_by = 2;
optional string sort = 2;
// Sort order: "asc" or "desc". Defaults to "desc" (higher values first).
optional string order = 3;
}

message ListChainMetricsResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,18 @@ paths:
in: path
required: true
type: string
- name: sort_by
- name: sort
description: |-
Metric used to sort chain metrics. Supported values: active_accounts, daily_transactions,
new_addresses, tps. Defaults to active_accounts.
in: query
required: false
type: string
- name: order
description: 'Sort order: "asc" or "desc". Defaults to "desc" (higher values first).'
in: query
required: false
type: string
tags:
- ClusterExplorerService
/api/v1/clusters/{cluster_id}/chains:
Expand All @@ -318,13 +323,18 @@ paths:
in: path
required: true
type: string
- name: sort_by
- name: sort
description: |-
Metric used to sort chains. Supported values: active_accounts, daily_transactions,
new_addresses, tps. Defaults to active_accounts.
in: query
required: false
type: string
- name: order
description: 'Sort order: "asc" or "desc". Defaults to "desc" (higher values first).'
in: query
required: false
type: string
tags:
- ClusterExplorerService
/api/v1/clusters/{cluster_id}/domain-protocols:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ impl ClusterExplorerService for ClusterExplorer {
) -> Result<Response<ListClusterChainsResponse>, Status> {
let inner = request.into_inner();

let sort_metric = inner.sort_by.map(parse_query).transpose()?;
let sort_metric = inner.sort.map(parse_query).transpose()?;
let order_direction = inner.order.map(parse_query).transpose()?;
let cluster = self.try_get_cluster(&inner.cluster_id)?;
let chains = cluster.list_chains(sort_metric).await?;
let chains = cluster.list_chains(sort_metric, order_direction).await?;

let items = chains
.into_iter()
Expand All @@ -65,9 +66,12 @@ impl ClusterExplorerService for ClusterExplorer {
) -> Result<Response<ListChainMetricsResponse>, Status> {
let inner = request.into_inner();

let sort_metric = inner.sort_by.map(parse_query).transpose()?;
let sort_metric = inner.sort.map(parse_query).transpose()?;
let order_direction = inner.order.map(parse_query).transpose()?;
let cluster = self.try_get_cluster(&inner.cluster_id)?;
let metrics = cluster.list_chain_metrics(sort_metric).await?;
let metrics = cluster
.list_chain_metrics(sort_metric, order_direction)
.await?;

let items = metrics.into_iter().map(|m| m.into()).collect();

Expand Down