From 48864d2fcc2fce423478a7ada993bb723458cb96 Mon Sep 17 00:00:00 2001 From: Yamaguchi Date: Mon, 25 Mar 2024 02:37:22 +0900 Subject: [PATCH] Add colored stats field to GET /colors API --- src/new_index/query.rs | 4 ++-- src/rest.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/new_index/query.rs b/src/new_index/query.rs index 4c71a93..9b4981c 100644 --- a/src/new_index/query.rs +++ b/src/new_index/query.rs @@ -275,7 +275,7 @@ impl Query { Ok(map) } - pub fn get_colors(&self, last_seen_color_id: Option, limit: usize) -> Vec<(ColorIdentifier, u32)> { + pub fn get_colors(&self, last_seen_color_id: Option, limit: usize) -> Vec<(ColorIdentifier, u32, (ColoredStats, ColoredStats))> { let (block_height, color_id) = if let Some(color_id) = last_seen_color_id { match self.chain.get_height_by_color_id(&color_id) { Some(height) => (height, Some(color_id)), @@ -289,7 +289,7 @@ impl Query { .chain .get_colors(block_height, &color_id, limit) .expect("failed to get colors"); - colors + colors.into_iter().map(|(color_id, height)| (color_id.clone(), height, self.get_colored_stats(&color_id))).collect() } pub fn get_colored_stats(&self, color_id: &ColorIdentifier) -> (ColoredStats, ColoredStats) { diff --git a/src/rest.rs b/src/rest.rs index 770a25d..5afcb62 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -912,8 +912,12 @@ fn handle_request( let color_id: Option = last_seen_color_id.map_or(None,|hex| { ColorIdentifier::from_hex(hex).ok() }); - let colors: Vec<(ColorIdentifier, u32)> = query.get_colors(color_id, COLOR_IDS_PER_PAGE); - json_response(colors, TTL_SHORT) + let colors: Vec<(ColorIdentifier, u32, (ColoredStats, ColoredStats))> = query.get_colors(color_id, COLOR_IDS_PER_PAGE); + let values = colors.iter().map(|(color_id, height, stats)| { + let stats = stats.clone(); + (color_id, height, ColoredStatsValue::from(stats.0), ColoredStatsValue::from(stats.1)) + }).collect::>(); + json_response(values, TTL_SHORT) } (&Method::GET, Some(&"color"), Some(color_id), None, None, None) => {