Skip to content
This repository has been archived by the owner on Mar 18, 2023. It is now read-only.

Commit

Permalink
♻️ Crawler: refactor NextCheckAt
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Sep 8, 2022
1 parent 2e40a38 commit 7b597e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Crawler {
last_battle_time: Some(account_info.last_battle_time),
partial_tank_stats,
prio: false,
next_check_at: Some(NextCheckAt::from(account_info.last_battle_time).into()),
next_check_at: Some(NextCheckAt::new(account_info.last_battle_time).into()),
};
let account_snapshot =
database::AccountSnapshot::new(self.realm, &account_info, tank_last_battle_times);
Expand Down
33 changes: 19 additions & 14 deletions src/crawler/next_check_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ use rand_distr::Exp1;

use crate::prelude::*;

pub struct NextCheckAt(DateTime);

impl NextCheckAt {
const MAX_EXP1_SAMPLE: f64 = 4.0;
pub struct NextCheckAt {
last_battle_time: DateTime,
offset: f64,
scale: f64,
max_exp1: f64,
}

impl From<DateTime> for NextCheckAt {
fn from(last_battle_time: DateTime) -> Self {
let elapsed_secs = (now() - last_battle_time).num_seconds() as f64;
let sample = thread_rng()
.sample::<f64, _>(Exp1)
.min(Self::MAX_EXP1_SAMPLE);
let interval_secs = elapsed_secs * sample;
Self(last_battle_time + Duration::seconds(interval_secs as i64))
impl NextCheckAt {
pub const fn new(last_battle_time: DateTime) -> Self {
Self {
last_battle_time,
offset: 0.0,
scale: 1.0,
max_exp1: 3.5,
}
}
}

impl From<NextCheckAt> for DateTime {
fn from(next_check_at: NextCheckAt) -> Self {
next_check_at.0
fn from(this: NextCheckAt) -> Self {
let elapsed_secs = (now() - this.last_battle_time).num_seconds() as f64;
let sample = thread_rng().sample::<f64, _>(Exp1) * this.scale + this.offset;
let sample = sample.min(this.max_exp1);
let next_check_in = Duration::seconds((elapsed_secs * sample) as i64);
this.last_battle_time + next_check_in
}
}

0 comments on commit 7b597e7

Please sign in to comment.