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
5 changes: 4 additions & 1 deletion stats/stats-proto/proto/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ message Point {
bool is_approximate = 3;
}

message LineChart { repeated Point chart = 1; }
message LineChart {
repeated Point chart = 1;
LineChartInfo info = 2;
}

message GetLineChartsRequest {}

Expand Down
2 changes: 2 additions & 0 deletions stats/stats-proto/swagger/stats.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ definitions:
items:
type: object
$ref: '#/definitions/v1Point'
info:
$ref: '#/definitions/v1LineChartInfo'
v1LineChartInfo:
type: object
properties:
Expand Down
24 changes: 1 addition & 23 deletions stats/stats-server/src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::collections::{BTreeMap, HashSet};

use cron::Schedule;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use stats::ResolutionKind;
Expand Down Expand Up @@ -193,24 +192,6 @@ pub struct LineChartCategory {
}

impl LineChartCategory {
fn build_proto_line_chart_info(
id: String,
entry: &EnabledChartEntry,
) -> proto_v1::LineChartInfo {
let settings = entry.settings.clone();
proto_v1::LineChartInfo {
id,
title: settings.title,
description: settings.description,
units: settings.units,
resolutions: entry
.enabled_resolutions
.keys()
.map(|r| String::from(*r))
.collect_vec(),
}
}

/// Add settings to the charts within category.
///
/// If the settings are not present - remove the chart (i.e. remove disabled
Expand All @@ -222,10 +203,7 @@ impl LineChartCategory {
let charts: Vec<_> = self
.charts_order
.into_iter()
.flat_map(|c: String| {
info.get(&c)
.map(|e| Self::build_proto_line_chart_info(c, e))
})
.flat_map(|c: String| info.get(&c).map(|e| e.build_proto_line_chart_info(c)))
.collect();
proto_v1::LineChartSection {
id: self.id,
Expand Down
25 changes: 14 additions & 11 deletions stats/stats-server/src/read_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,32 @@ impl StatsService for ReadService {
) -> Result<Response<proto_v1::LineChart>, Status> {
let request = request.into_inner();
let resolution = convert_resolution(request.resolution());
let chart_info = self
.charts
.charts_info
.get(&request.name)
.and_then(|e| e.enabled_resolutions.get(&resolution))
let chart_name = request.name;
let chart_entry = self.charts.charts_info.get(&chart_name).ok_or_else(|| {
Status::not_found(format!("chart with name '{}' was not found", chart_name))
})?;
let resolution_info = chart_entry
.enabled_resolutions
.get(&resolution)
.filter(|static_info| static_info.chart_type == ChartType::Line)
.ok_or_else(|| {
Status::not_found(format!(
"line chart {}({}) not found",
request.name,
String::from(resolution)
"resolution '{}' for chart '{}' was not found",
String::from(resolution),
chart_name,
))
})?;

let from = request
.from
.and_then(|date| NaiveDate::from_str(&date).ok());
let to = request.to.and_then(|date| NaiveDate::from_str(&date).ok());
let policy = chart_info.missing_date_policy;
let mark_approx = chart_info.approximate_trailing_points;
let policy = resolution_info.missing_date_policy;
let mark_approx = resolution_info.approximate_trailing_points;
let interval_limit = Some(self.limits.request_interval_limit);
let serialized_chart = get_serialized_line_chart_data_resolution_dispatch(
&self.db,
request.name,
chart_name.clone(),
resolution,
from,
to,
Expand All @@ -270,6 +272,7 @@ impl StatsService for ReadService {
.map_err(map_read_error)?;
Ok(Response::new(proto_v1::LineChart {
chart: serialized_chart,
info: Some(chart_entry.build_proto_line_chart_info(chart_name)),
}))
}

Expand Down
20 changes: 20 additions & 0 deletions stats/stats-server/src/runtime_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ pub struct EnabledChartEntry {
pub enabled_resolutions: HashMap<ResolutionKind, EnabledResolutionEntry>,
}

impl EnabledChartEntry {
pub fn build_proto_line_chart_info(
&self,
id: String,
) -> stats_proto::blockscout::stats::v1::LineChartInfo {
let settings = self.settings.clone();
stats_proto::blockscout::stats::v1::LineChartInfo {
id,
title: settings.title,
description: settings.description,
units: settings.units,
resolutions: self
.enabled_resolutions
.keys()
.map(|r| String::from(*r))
.collect_vec(),
}
}
}

#[derive(Clone, Debug)]
pub struct EnabledResolutionEntry {
pub name: String,
Expand Down
14 changes: 12 additions & 2 deletions stats/stats-server/tests/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn test_lines_ok() {
&format!("/api/v1/lines/{line_name}?resolution={resolution}"),
)
.await;
let chart = chart
let chart_data = chart
.as_object()
.expect("response has to be json object")
.get("chart")
Expand All @@ -101,9 +101,19 @@ async fn test_lines_ok() {
.expect("'chart' field has to be json array");

assert!(
!chart.is_empty(),
!chart_data.is_empty(),
"chart '{line_name}' '{resolution}' is empty"
);

let info = chart
.get("info")
.expect("response doesn't have 'info' field");
let info: stats_proto::blockscout::stats::v1::LineChartInfo =
serde_json::from_value(info.clone()).expect("must return valid chart info");
assert_eq!(
info.id, line_name,
"returned chart id (left) doesn't match requested (right)",
)
}
// should work even without `resolution` parameter
let _chart: serde_json::Value =
Expand Down
Loading