Skip to content

Commit

Permalink
Add colored stats field to GET /colors API
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Mar 24, 2024
1 parent 8b9f08b commit 48864d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/new_index/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl Query {
Ok(map)
}

pub fn get_colors(&self, last_seen_color_id: Option<ColorIdentifier>, limit: usize) -> Vec<(ColorIdentifier, u32)> {
pub fn get_colors(&self, last_seen_color_id: Option<ColorIdentifier>, 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)),
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,12 @@ fn handle_request(
let color_id: Option<ColorIdentifier> = 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::<Vec<_>>();
json_response(values, TTL_SHORT)
}

(&Method::GET, Some(&"color"), Some(color_id), None, None, None) => {
Expand Down

0 comments on commit 48864d2

Please sign in to comment.