Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(stats):remove resolution dep #1069

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions stats/stats/src/charts/lines/native_coin_holders_growth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl CreateBehaviour for Create {

pub struct Update;

impl UpdateBehaviour<(), (), NaiveDate> for Update {
impl UpdateBehaviour<(), NaiveDate> for Update {
async fn update_values(
cx: &UpdateContext<'_>,
chart_id: i32,
Expand Down Expand Up @@ -383,7 +383,7 @@ define_and_impl_resolution_properties!(
);

pub type NativeCoinHoldersGrowth =
LocalDbChartSource<(), (), Create, Update, DefaultQueryVec<Properties>, Properties>;
LocalDbChartSource<(), Create, Update, DefaultQueryVec<Properties>, Properties>;
pub type NativeCoinHoldersGrowthInt = MapParseTo<NativeCoinHoldersGrowth, i64>;
pub type NativeCoinHoldersGrowthWeekly = DirectVecLocalDbChartSource<
LastValueLowerResolution<NativeCoinHoldersGrowth, Week>,
Expand Down
1 change: 0 additions & 1 deletion stats/stats/src/data_source/kinds/auxiliary/cumulative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ where
Delta: DataSource<Output = Vec<TimespanValue<Resolution, Value>>>,
{
type MainDependencies = Delta;
type ResolutionDependencies = ();
type Output = Vec<TimespanValue<Resolution, Value>>;
fn mutex_id() -> Option<String> {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ where
<Value as FromStr>::Err: Display,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = Vec<TimespanValue<Resolution, Value>>;
fn mutex_id() -> Option<String> {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ where
Properties: ChartProperties,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = DS::Output;

fn mutex_id() -> Option<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ where
TimespanValue<Resolution, Value>: ZeroTimespanValue<Resolution>,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = TimespanValue<Resolution, Value>;
fn mutex_id() -> Option<String> {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ where
F: MapFunction<D::Output>,
{
type MainDependencies = D;
type ResolutionDependencies = ();
type Output = F::Output;
fn mutex_id() -> Option<String> {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ where
LowerRes: Timespan + ConsistsOf<HigherRes> + Eq + Debug + Send,
HigherRes: Ord + Clone + Debug + Send,
{
type MainDependencies = Average;
type ResolutionDependencies = Weight;
type MainDependencies = (Average, Weight);
type Output = Vec<TimespanValue<LowerRes, f64>>;

fn mutex_id() -> Option<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ where
DS: DataSource<Output = Vec<TimespanValue<HigherRes, Value>>>,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = Vec<TimespanValue<LowerRes, Value>>;

fn mutex_id() -> Option<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ where
DS: DataSource<Output = Vec<TimespanValue<HigherRes, Value>>>,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = Vec<TimespanValue<LowerRes, Value>>;

fn mutex_id() -> Option<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ where
DS: DataSource<Output = Vec<TimespanValue<Resolution, Value>>>,
{
type MainDependencies = DS;
type ResolutionDependencies = ();
type Output = TimespanValue<Resolution, Value>;
fn mutex_id() -> Option<String> {
None
Expand Down
55 changes: 19 additions & 36 deletions stats/stats/src/data_source/kinds/local_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ pub mod parameters;
/// with common parameter combinations.
///
/// See [module-level documentation](self) for more details.
pub struct LocalDbChartSource<MainDep, ResolutionDep, Create, Update, Query, ChartProps>(
PhantomData<(MainDep, ResolutionDep, Create, Update, Query, ChartProps)>,
pub struct LocalDbChartSource<MainDep, Create, Update, Query, ChartProps>(
PhantomData<(MainDep, Create, Update, Query, ChartProps)>,
)
where
MainDep: DataSource,
ResolutionDep: DataSource,
Create: CreateBehaviour,
Update: UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution>,
Update: UpdateBehaviour<MainDep, ChartProps::Resolution>,
Query: QueryBehaviour,
ChartProps: ChartProperties;

Expand All @@ -78,11 +77,9 @@ where
/// The opposite logic to [`Delta`](`crate::data_source::kinds::data_manipulation::delta::Delta`)
pub type DailyCumulativeLocalDbChartSource<DeltaDep, C> = LocalDbChartSource<
PartialCumulative<DeltaDep>,
(),
DefaultCreate<C>,
BatchUpdate<
PartialCumulative<DeltaDep>,
(),
AddLastValueStep<C>,
Batch30Days,
DefaultQueryVec<C>,
Expand All @@ -96,31 +93,23 @@ pub type DailyCumulativeLocalDbChartSource<DeltaDep, C> = LocalDbChartSource<
/// any manipulations)
pub type DirectVecLocalDbChartSource<Dependency, BatchSizeUpperBound, C> = LocalDbChartSource<
Dependency,
(),
DefaultCreate<C>,
BatchUpdate<Dependency, (), PassVecStep, BatchSizeUpperBound, DefaultQueryVec<C>, C>,
BatchUpdate<Dependency, PassVecStep, BatchSizeUpperBound, DefaultQueryVec<C>, C>,
DefaultQueryVec<C>,
C,
>;

/// Chart that stores single data point received from provided dependency (without
/// any manipulations)
pub type DirectPointLocalDbChartSource<Dependency, C> = LocalDbChartSource<
Dependency,
(),
DefaultCreate<C>,
PassPoint<Dependency>,
DefaultQueryLast<C>,
C,
>;
pub type DirectPointLocalDbChartSource<Dependency, C> =
LocalDbChartSource<Dependency, DefaultCreate<C>, PassPoint<Dependency>, DefaultQueryLast<C>, C>;

impl<MainDep, ResolutionDep, Create, Update, Query, ChartProps>
LocalDbChartSource<MainDep, ResolutionDep, Create, Update, Query, ChartProps>
impl<MainDep, Create, Update, Query, ChartProps>
LocalDbChartSource<MainDep, Create, Update, Query, ChartProps>
where
MainDep: DataSource + Sync,
ResolutionDep: DataSource + Sync,
Create: CreateBehaviour + Sync,
Update: UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution> + Sync,
Update: UpdateBehaviour<MainDep, ChartProps::Resolution> + Sync,
Query: QueryBehaviour + Sync,
ChartProps: ChartProperties,
ChartProps::Resolution: Ord + Clone + Debug,
Expand Down Expand Up @@ -195,19 +184,17 @@ fn postgres_timestamps_eq(time_1: DateTime<Utc>, time_2: DateTime<Utc>) -> bool
time_1.trunc_subsecs(6).eq(&time_2.trunc_subsecs(6))
}

impl<MainDep, ResolutionDep, Create, Update, Query, ChartProps> DataSource
for LocalDbChartSource<MainDep, ResolutionDep, Create, Update, Query, ChartProps>
impl<MainDep, Create, Update, Query, ChartProps> DataSource
for LocalDbChartSource<MainDep, Create, Update, Query, ChartProps>
where
MainDep: DataSource + Sync,
ResolutionDep: DataSource + Sync,
Create: CreateBehaviour + Sync,
Update: UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution> + Sync,
Update: UpdateBehaviour<MainDep, ChartProps::Resolution> + Sync,
Query: QueryBehaviour + Sync,
ChartProps: ChartProperties,
ChartProps::Resolution: Ord + Clone + Debug + Send,
{
type MainDependencies = MainDep;
type ResolutionDependencies = ResolutionDep;
type Output = Query::Output;

fn mutex_id() -> Option<String> {
Expand Down Expand Up @@ -257,13 +244,12 @@ where

// need to delegate these traits for update groups to use

impl<MainDep, ResolutionDep, Create, Update, Query, ChartProps> Named
for LocalDbChartSource<MainDep, ResolutionDep, Create, Update, Query, ChartProps>
impl<MainDep, Create, Update, Query, ChartProps> Named
for LocalDbChartSource<MainDep, Create, Update, Query, ChartProps>
where
MainDep: DataSource,
ResolutionDep: DataSource,
Create: CreateBehaviour,
Update: UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution>,
Update: UpdateBehaviour<MainDep, ChartProps::Resolution>,
Query: QueryBehaviour,
ChartProps: ChartProperties + Named,
{
Expand All @@ -273,13 +259,12 @@ where
}

#[portrait::fill(portrait::delegate(ChartProps))]
impl<MainDep, ResolutionDep, Create, Update, Query, ChartProps> ChartProperties
for LocalDbChartSource<MainDep, ResolutionDep, Create, Update, Query, ChartProps>
impl<MainDep, Create, Update, Query, ChartProps> ChartProperties
for LocalDbChartSource<MainDep, Create, Update, Query, ChartProps>
where
MainDep: DataSource + Sync,
ResolutionDep: DataSource + Sync,
Create: CreateBehaviour + Sync,
Update: UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution> + Sync,
Update: UpdateBehaviour<MainDep, ChartProps::Resolution> + Sync,
Query: QueryBehaviour + Sync,
ChartProps: ChartProperties,
{
Expand Down Expand Up @@ -343,10 +328,9 @@ mod tests {
}
}

impl<M, R, Resolution> UpdateBehaviour<M, R, Resolution> for UpdateSingleTriggerAsserter
impl<M, Resolution> UpdateBehaviour<M, Resolution> for UpdateSingleTriggerAsserter
where
M: DataSource,
R: DataSource,
Resolution: Send,
{
async fn update_values(
Expand Down Expand Up @@ -387,7 +371,6 @@ mod tests {
}

type TestedChart = LocalDbChartSource<
(),
(),
DefaultCreate<TestedChartProps>,
UpdateSingleTriggerAsserter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ pub trait CreateBehaviour {

// generic parameters are to ensure that dependencies in implementations
// of this trait match dependencies in `impl DataSource for LocalDbChartSource<..>`
pub trait UpdateBehaviour<MainDep, ResolutionDep, Resolution>
pub trait UpdateBehaviour<MainDep, Resolution>
where
MainDep: DataSource,
ResolutionDep: DataSource,
{
/// Update only chart values.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,22 @@ use crate::{
pub mod parameter_traits;
pub mod parameters;

pub struct BatchUpdate<MainDep, ResolutionDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>(
PhantomData<(
MainDep,
ResolutionDep,
BatchStep,
BatchSizeUpperBound,
Query,
ChartProps,
)>,
pub struct BatchUpdate<MainDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>(
PhantomData<(MainDep, BatchStep, BatchSizeUpperBound, Query, ChartProps)>,
)
where
MainDep: DataSource,
ResolutionDep: DataSource,
BatchStep: BatchStepBehaviour<ChartProps::Resolution, MainDep::Output, ResolutionDep::Output>,
BatchStep: BatchStepBehaviour<ChartProps::Resolution, MainDep::Output>,
BatchSizeUpperBound: Get<Value = TimespanDuration<ChartProps::Resolution>>,
Query: QueryBehaviour<Output = Vec<TimespanValue<ChartProps::Resolution, String>>>,
ChartProps: ChartProperties;

impl<MainDep, ResolutionDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>
UpdateBehaviour<MainDep, ResolutionDep, ChartProps::Resolution>
for BatchUpdate<MainDep, ResolutionDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>
impl<MainDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>
UpdateBehaviour<MainDep, ChartProps::Resolution>
for BatchUpdate<MainDep, BatchStep, BatchSizeUpperBound, Query, ChartProps>
where
MainDep: DataSource,
ResolutionDep: DataSource,
BatchStep: BatchStepBehaviour<ChartProps::Resolution, MainDep::Output, ResolutionDep::Output>,
BatchStep: BatchStepBehaviour<ChartProps::Resolution, MainDep::Output>,
BatchSizeUpperBound: Get<Value = TimespanDuration<ChartProps::Resolution>>,
Query: QueryBehaviour<Output = Vec<TimespanValue<ChartProps::Resolution, String>>>,
ChartProps: ChartProperties,
Expand Down Expand Up @@ -91,12 +82,7 @@ where
"run {}/{} step of batch update", i + 1, n
);
let now = Instant::now();
let found = batch_update_values_step::<
MainDep,
ResolutionDep,
BatchStep,
ChartProps::Resolution,
>(
let found = batch_update_values_step::<MainDep, BatchStep, ChartProps::Resolution>(
cx,
chart_id,
min_blockscout_block,
Expand Down Expand Up @@ -151,7 +137,7 @@ where
}

/// Returns how many records were found
async fn batch_update_values_step<MainDep, ResolutionDep, BatchStep, Resolution>(
async fn batch_update_values_step<MainDep, BatchStep, Resolution>(
cx: &UpdateContext<'_>,
chart_id: i32,
min_blockscout_block: i64,
Expand All @@ -161,23 +147,19 @@ async fn batch_update_values_step<MainDep, ResolutionDep, BatchStep, Resolution>
) -> Result<usize, UpdateError>
where
MainDep: DataSource,
ResolutionDep: DataSource,
Resolution: Timespan,
BatchStep: BatchStepBehaviour<Resolution, MainDep::Output, ResolutionDep::Output>,
BatchStep: BatchStepBehaviour<Resolution, MainDep::Output>,
{
let query_range = range.into_date_time_range();
let main_data =
MainDep::query_data(cx, Some(query_range.clone()), dependency_data_fetch_timer).await?;
let resolution_data =
ResolutionDep::query_data(cx, Some(query_range), dependency_data_fetch_timer).await?;
let found = BatchStep::batch_update_values_step_with(
cx.db,
chart_id,
cx.time,
min_blockscout_block,
last_accurate_point,
main_data,
resolution_data,
)
.await?;
Ok(found)
Expand Down Expand Up @@ -402,7 +384,7 @@ mod tests {

use super::{parameters::mock::StepInput, TimespanDuration};

type VecStringStepInput = StepInput<Vec<DateValue<String>>, ()>;
type VecStringStepInput = StepInput<Vec<DateValue<String>>>;
type SharedInputsStorage = Arc<Mutex<Vec<VecStringStepInput>>>;

// `OnceLock` in order to return the same instance each time
Expand Down Expand Up @@ -433,7 +415,6 @@ mod tests {

type ThisRecordingBatchUpdate = BatchUpdate<
AccountsGrowth,
(),
ThisRecordingStep,
Batch1Day,
DefaultQueryVec<ThisTestChartProps>,
Expand All @@ -442,7 +423,6 @@ mod tests {

type RecordingChart = LocalDbChartSource<
AccountsGrowth,
(),
DefaultCreate<ThisTestChartProps>,
ThisRecordingBatchUpdate,
DefaultQueryVec<ThisTestChartProps>,
Expand Down Expand Up @@ -473,7 +453,7 @@ mod tests {
storage: SharedInputsStorage,
expected_update_time: Option<DateTime<Utc>>,
) {
let mut prev_input: Option<&StepInput<Vec<DateValue<String>>, ()>> = None;
let mut prev_input: Option<&StepInput<Vec<DateValue<String>>>> = None;
let expected_update_time = expected_update_time
.unwrap_or(DateTime::<Utc>::from_str("2023-03-01T12:00:00Z").unwrap());
for input in storage.lock().await.deref() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use crate::{
UpdateError,
};

pub trait BatchStepBehaviour<Resolution, MainInput, ResolutionInput>
pub trait BatchStepBehaviour<Resolution, MainInput>
where
Resolution: Timespan,
MainInput: Send,
ResolutionInput: Send,
{
/// Update chart with data from its dependencies.
///
Expand All @@ -24,6 +23,5 @@ where
min_blockscout_block: i64,
last_accurate_point: TimespanValue<Resolution, String>,
main_data: MainInput,
resolution_data: ResolutionInput,
) -> impl Future<Output = Result<usize, UpdateError>> + std::marker::Send;
) -> impl Future<Output = Result<usize, UpdateError>> + Send;
}
Loading
Loading