Skip to content

Commit

Permalink
rename update error to chart error for more accurate meaning
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Dec 4, 2024
1 parent 6150c7a commit 92ae8e9
Show file tree
Hide file tree
Showing 46 changed files with 185 additions and 183 deletions.
14 changes: 7 additions & 7 deletions stats/stats-server/src/read_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use stats::{
range::UniversalRange,
types::Timespan,
utils::{day_start, MarkedDbConnection},
RequestedPointsLimit, ResolutionKind, UpdateError,
RequestedPointsLimit, ResolutionKind, ChartError,
};
use stats_proto::blockscout::stats::v1 as proto_v1;
use tonic::{Request, Response, Status};
Expand Down Expand Up @@ -60,10 +60,10 @@ impl From<LimitsSettings> for ReadLimits {
}
}

fn map_update_error(err: UpdateError) -> Status {
fn map_update_error(err: ChartError) -> Status {
match &err {
UpdateError::ChartNotFound(_) => Status::not_found(err.to_string()),
UpdateError::IntervalTooLarge { limit: _ } => Status::invalid_argument(err.to_string()),
ChartError::ChartNotFound(_) => Status::not_found(err.to_string()),
ChartError::IntervalTooLarge { limit: _ } => Status::invalid_argument(err.to_string()),
_ => {
tracing::error!(err = ?err, "internal read error");
Status::internal(err.to_string())
Expand Down Expand Up @@ -100,10 +100,10 @@ impl ReadService {
range: UniversalRange<DateTime<Utc>>,
points_limit: Option<RequestedPointsLimit>,
query_time: DateTime<Utc>,
) -> Result<Data, UpdateError> {
) -> Result<Data, ChartError> {
let migrations = BlockscoutMigrations::query_from_db(self.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?;
.map_err(ChartError::BlockscoutDB)?;
let context = UpdateContext::from_params_now_or_override(UpdateParameters {
db: &self.db,
blockscout: &self.blockscout,
Expand Down Expand Up @@ -143,7 +143,7 @@ impl ReadService {
range: UniversalRange<DateTime<Utc>>,
points_limit: Option<RequestedPointsLimit>,
query_time: DateTime<Utc>,
) -> Result<proto_v1::LineChart, UpdateError> {
) -> Result<proto_v1::LineChart, ChartError> {
let data = self
.query_with_handle(query_handle, range, points_limit, query_time)
.await?;
Expand Down
11 changes: 5 additions & 6 deletions stats/stats/src/charts/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use super::{
query_dispatch::{ChartTypeSpecifics, QuerySerialized, QuerySerializedDyn},
};

// todo: rename to `ChartError` or similar
#[derive(Error, Debug)]
pub enum UpdateError {
pub enum ChartError {
#[error("blockscout database error: {0}")]
BlockscoutDB(DbErr),
#[error("stats database error: {0}")]
Expand All @@ -32,12 +31,12 @@ pub enum UpdateError {
Internal(String),
}

impl From<ReadError> for UpdateError {
impl From<ReadError> for ChartError {
fn from(read: ReadError) -> Self {
match read {
ReadError::DB(db) => UpdateError::StatsDB(db),
ReadError::ChartNotFound(err) => UpdateError::ChartNotFound(err),
ReadError::IntervalTooLarge(limit) => UpdateError::IntervalTooLarge { limit },
ReadError::DB(db) => ChartError::StatsDB(db),
ReadError::ChartNotFound(err) => ChartError::ChartNotFound(err),
ReadError::IntervalTooLarge(limit) => ChartError::IntervalTooLarge { limit },
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions stats/stats/src/charts/counters/average_block_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
range::UniversalRange,
types::TimespanValue,
utils::NANOS_PER_SEC,
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartProperties, MissingDatePolicy, Named, ChartError,
};

use blockscout_db::entity::blocks;
Expand Down Expand Up @@ -52,12 +52,12 @@ struct BlockTimestamp {
async fn query_average_block_time(
cx: &UpdateContext<'_>,
offset: u64,
) -> Result<Option<TimespanValue<NaiveDate, f64>>, UpdateError> {
) -> Result<Option<TimespanValue<NaiveDate, f64>>, ChartError> {
let query = average_block_time_statement(offset);
let block_timestamps = BlockTimestamp::find_by_statement(query)
.all(cx.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?;
.map_err(ChartError::BlockscoutDB)?;
Ok(calculate_average_block_time(block_timestamps))
}

Expand All @@ -69,12 +69,12 @@ impl RemoteQueryBehaviour for AverageBlockTimeQuery {
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<TimespanValue<NaiveDate, f64>, UpdateError> {
) -> Result<TimespanValue<NaiveDate, f64>, ChartError> {
match query_average_block_time(cx, OFFSET_BLOCKS).await? {
Some(avg_block_time) => Ok(avg_block_time),
None => query_average_block_time(cx, 0)
.await?
.ok_or(UpdateError::Internal(
.ok_or(ChartError::Internal(
"No blocks were returned to calculate average block time".into(),
)),
}
Expand Down
4 changes: 2 additions & 2 deletions stats/stats/src/charts/counters/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
range::UniversalRange,
types::timespans::DateValue,
ChartProperties, Named, UpdateError,
ChartProperties, Named, ChartError,
};

use chrono::{DateTime, NaiveDate, Utc};
Expand All @@ -32,7 +32,7 @@ where
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
if cx.time >= PointDateTime::get() {
Ok(DateValue::<String> {
timespan: PointDateTime::get().date_naive(),
Expand Down
12 changes: 6 additions & 6 deletions stats/stats/src/charts/counters/total_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
range::UniversalRange,
types::timespans::DateValue,
utils::MarkedDbConnection,
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartProperties, MissingDatePolicy, Named, ChartError,
};

use blockscout_db::entity::blocks;
Expand All @@ -36,7 +36,7 @@ impl RemoteQueryBehaviour for TotalBlocksQueryBehaviour {
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
let data = blocks::Entity::find()
.select_only()
.column_as(Expr::col(blocks::Column::Number).count(), "number")
Expand All @@ -45,8 +45,8 @@ impl RemoteQueryBehaviour for TotalBlocksQueryBehaviour {
.into_model::<TotalBlocksData>()
.one(cx.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?
.ok_or_else(|| UpdateError::Internal("query returned nothing".into()))?;
.map_err(ChartError::BlockscoutDB)?
.ok_or_else(|| ChartError::Internal("query returned nothing".into()))?;

let data = DateValue::<String> {
timespan: data.timestamp.date(),
Expand Down Expand Up @@ -85,7 +85,7 @@ pub static TOTAL_BLOCKS_ESTIMATION_CACHE_LIVENESS_SEC: OnceLock<u64> = OnceLock:
static CACHED_BLOCKS_ESTIMATION: OnceLock<Mutex<cached::TimedCache<String, i64>>> = OnceLock::new();

impl ValueEstimation for CachedBlocksEstimation {
async fn estimate(blockscout: &MarkedDbConnection) -> Result<DateValue<String>, UpdateError> {
async fn estimate(blockscout: &MarkedDbConnection) -> Result<DateValue<String>, ChartError> {
async fn cached_blocks_estimation(
blockscout: &DatabaseConnection,
db_id: &str,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl ValueEstimation for CachedBlocksEstimation {
let now = Utc::now();
let value = cached_blocks_estimation(blockscout.connection.as_ref(), &blockscout.db_name)
.await
.map_err(UpdateError::BlockscoutDB)?
.map_err(ChartError::BlockscoutDB)?
.map(|b| {
let b = b as f64 * 0.9;
b as i64
Expand Down
6 changes: 3 additions & 3 deletions stats/stats/src/charts/counters/total_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
},
range::UniversalRange,
types::timespans::DateValue,
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartProperties, MissingDatePolicy, Named, ChartError,
};

use blockscout_db::entity::addresses;
Expand All @@ -24,13 +24,13 @@ impl RemoteQueryBehaviour for TotalContractsQueryBehaviour {
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
let value = addresses::Entity::find()
.filter(addresses::Column::ContractCode.is_not_null())
.filter(addresses::Column::InsertedAt.lte(cx.time))
.count(cx.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?;
.map_err(ChartError::BlockscoutDB)?;
let timespan = cx.time.date_naive();
Ok(DateValue::<String> {
timespan,
Expand Down
8 changes: 4 additions & 4 deletions stats/stats/src/charts/counters/yesterday_txns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
range::UniversalRange,
types::TimespanValue,
utils::day_start,
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartProperties, MissingDatePolicy, Named, ChartError,
};
use chrono::{DateTime, Days, NaiveDate, Utc};
use entity::sea_orm_active_enums::ChartType;
Expand All @@ -24,11 +24,11 @@ impl RemoteQueryBehaviour for YesterdayTxnsQuery {
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
let today = cx.time.date_naive();
let yesterday = today
.checked_sub_days(Days::new(1))
.ok_or(UpdateError::Internal(
.ok_or(ChartError::Internal(
"Update time is incorrect: ~ minimum possible date".into(),
))?;
let yesterday_range = day_start(&yesterday)..day_start(&today);
Expand All @@ -39,7 +39,7 @@ impl RemoteQueryBehaviour for YesterdayTxnsQuery {
let data = Self::Output::find_by_statement(query)
.one(cx.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?
.map_err(ChartError::BlockscoutDB)?
// no transactions for yesterday
.unwrap_or(TimespanValue::with_zero_value(yesterday));
Ok(data)
Expand Down
12 changes: 6 additions & 6 deletions stats/stats/src/charts/db_interaction/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
timespans::{DateValue, Month, Week, Year},
ExtendedTimespanValue, Timespan, TimespanDuration, TimespanValue,
},
ChartProperties, MissingDatePolicy, UpdateError,
ChartProperties, MissingDatePolicy, ChartError,
};

use blockscout_db::entity::blocks;
Expand Down Expand Up @@ -482,7 +482,7 @@ pub async fn last_accurate_point<ChartProps, Query>(
force_full: bool,
approximate_trailing_points: u64,
policy: MissingDatePolicy,
) -> Result<Option<TimespanValue<ChartProps::Resolution, String>>, UpdateError>
) -> Result<Option<TimespanValue<ChartProps::Resolution, String>>, ChartError>
where
ChartProps: ChartProperties + ?Sized,
ChartProps::Resolution: Ord + Clone + Debug,
Expand All @@ -499,7 +499,7 @@ where
.into_model()
.one(db)
.await
.map_err(UpdateError::StatsDB)?;
.map_err(ChartError::StatsDB)?;
let metadata = get_chart_metadata(db, &ChartProps::key()).await?;

match recorded_min_blockscout_block {
Expand Down Expand Up @@ -536,7 +536,7 @@ where
}
});
let Some(last_accurate_point) = last_accurate_point else {
return Err(UpdateError::Internal("Failure while reading chart data: did not return accurate data (with `fill_missing_dates`=true)".into()));
return Err(ChartError::Internal("Failure while reading chart data: did not return accurate data (with `fill_missing_dates`=true)".into()));
};

if let Some(block) = recorded_min_blockscout_block.min_blockscout_block {
Expand Down Expand Up @@ -656,10 +656,10 @@ impl RemoteQueryBehaviour for QueryAllBlockTimestampRange {
async fn query_data(
cx: &UpdateContext<'_>,
_range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
let start_timestamp = get_min_date_blockscout(cx.blockscout.connection.as_ref())
.await
.map_err(UpdateError::BlockscoutDB)?
.map_err(ChartError::BlockscoutDB)?
.and_utc();
Ok(start_timestamp..cx.time)
}
Expand Down
4 changes: 2 additions & 2 deletions stats/stats/src/charts/lines/gas_used_growth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
define_and_impl_resolution_properties,
types::timespans::{DateValue, Month, Week, Year},
utils::sql_with_range_filter_opt,
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartError, ChartProperties, MissingDatePolicy, Named,
};

use chrono::{DateTime, NaiveDate, Utc};
Expand Down Expand Up @@ -61,7 +61,7 @@ pub struct IncrementsFromPartialSum;

impl MapFunction<Vec<DateValue<Decimal>>> for IncrementsFromPartialSum {
type Output = Vec<DateValue<Decimal>>;
fn function(inner_data: Vec<DateValue<Decimal>>) -> Result<Self::Output, UpdateError> {
fn function(inner_data: Vec<DateValue<Decimal>>) -> Result<Self::Output, ChartError> {
Ok(inner_data
.into_iter()
.scan(Decimal::ZERO, |state, mut next| {
Expand Down
6 changes: 3 additions & 3 deletions stats/stats/src/charts/lines/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
missing_date::fit_into_range,
range::{Incrementable, UniversalRange},
types::{timespans::DateValue, Timespan, TimespanValue},
ChartProperties, MissingDatePolicy, Named, UpdateError,
ChartProperties, MissingDatePolicy, Named, ChartError,
};

use chrono::{DateTime, Duration, NaiveDate, Utc};
Expand Down Expand Up @@ -100,7 +100,7 @@ where
async fn query_data(
cx: &UpdateContext<'_>,
range: UniversalRange<DateTime<Utc>>,
) -> Result<Vec<DateValue<String>>, UpdateError> {
) -> Result<Vec<DateValue<String>>, ChartError> {
let full_data = mocked_lines(DateRange::get(), ValueRange::get());
Ok(mock_trim_lines(full_data, cx.time, range, Policy::get()))
}
Expand Down Expand Up @@ -146,7 +146,7 @@ where
async fn query_data(
cx: &UpdateContext<'_>,
range: UniversalRange<DateTime<Utc>>,
) -> Result<Self::Output, UpdateError> {
) -> Result<Self::Output, ChartError> {
Ok(mock_trim_lines(Data::get(), cx.time, range, Policy::get()))
}
}
Expand Down
Loading

0 comments on commit 92ae8e9

Please sign in to comment.