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

Commit

Permalink
🚧 Trainer: try and set the due field on account
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Sep 7, 2022
1 parent 01e6b41 commit c255e2f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ use itertools::Itertools;
use mongodb::bson::oid::ObjectId;
use tokio::sync::Mutex;

use crate::crawler::crawled_data::CrawledData;
use crate::crawler::metrics::CrawlerMetrics;
use self::crawled_data::CrawledData;
use self::metrics::CrawlerMetrics;
use self::next_check_at::NextCheckAt;
use crate::opts::{CrawlAccountsOpts, CrawlerOpts, SharedCrawlerOpts};
use crate::prelude::*;
use crate::wargaming::WargamingApi;
use crate::{database, wargaming};

mod crawled_data;
mod metrics;
mod next_check_at;

pub struct Crawler {
api: WargamingApi,
Expand Down Expand Up @@ -228,6 +230,7 @@ impl Crawler {
last_battle_time: Some(account_info.last_battle_time),
partial_tank_stats,
prio: false,
next_check_at: NextCheckAt::from(account_info.last_battle_time).into(),
};
let account_snapshot =
database::AccountSnapshot::new(self.realm, &account_info, tank_last_battle_times);
Expand Down
27 changes: 27 additions & 0 deletions src/crawler/next_check_at.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rand::prelude::*;
use rand_distr::Exp1;

use crate::prelude::*;

pub struct NextCheckAt(DateTime);

impl NextCheckAt {
const MAX_EXP1_SAMPLE: f64 = 4.0;
}

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 From<NextCheckAt> for DateTime {
fn from(next_check_at: NextCheckAt) -> Self {
next_check_at.0
}
}
14 changes: 11 additions & 3 deletions src/database/mongodb/models/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct Account {

#[serde(default)]
pub prio: bool,

#[serde(rename = "due", default)]
pub next_check_at: DateTime,
}

impl TypedDocument for Account {
Expand All @@ -51,7 +54,7 @@ impl TypedDocument for Account {

#[async_trait]
impl Indexes for Account {
type I = [IndexModel; 3];
type I = [IndexModel; 4];

fn indexes() -> Self::I {
[
Expand All @@ -70,18 +73,22 @@ impl Indexes for Account {
.build(),
)
.build(),
IndexModel::builder()
.keys(doc! { "rlm": 1, "due": 1 })
.build(),
]
}
}

impl Account {
pub const fn new(realm: wargaming::Realm, account_id: wargaming::AccountId) -> Self {
pub fn new(realm: wargaming::Realm, account_id: wargaming::AccountId) -> Self {
Self {
id: account_id,
realm,
last_battle_time: None,
partial_tank_stats: Vec::new(),
prio: false,
next_check_at: DateTime::default(),
}
}
}
Expand Down Expand Up @@ -137,7 +144,7 @@ impl Account {
let filter = doc! { "rlm": realm.to_str(), "aid": account_id };
let update = doc! {
"$setOnInsert": { "lbts": null, "pts": [] },
"$set": { "prio": true },
"$set": { "prio": true, "due": now() },
};
let options = UpdateOptions::builder().upsert(true).build();
Self::collection(in_)
Expand Down Expand Up @@ -218,6 +225,7 @@ impl Account {
pub async fn sample_account(from: &Database, realm: wargaming::Realm) -> Result<Account> {
let filter = doc! {
"rlm": realm.to_str(),
"lbts": { "$lte": now() - Duration::seconds(thread_rng().gen_range(0..14400)) },
};
let options = FindOneOptions::builder().sort(doc! { "lbts": -1 }).build();
let start_instant = Instant::now();
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::Duration;
use serde::{Deserialize, Serializer};

use crate::prelude::*;

pub fn serialize_duration_seconds<S: Serializer>(
value: &Duration,
serializer: S,
Expand Down

0 comments on commit c255e2f

Please sign in to comment.