<commit_before>use LalafellBot;
use tasks::RunsTask;
use commands::tag::Tagger;
use discord::model::UserId;
use chrono::prelude::*;
use chrono::Duration;

use std::sync::Arc;
use std::thread;

pub struct AutoTagTask {
  pub next_sleep: i64
}

impl AutoTagTask {
  pub fn new() -> AutoTagTask {
    AutoTagTask {
      next_sleep: 30
    }
  }

  pub fn run_once(&mut self, s: Arc<LalafellBot>) {
    thread::sleep(Duration::seconds(self.next_sleep).to_std().unwrap());
    info!("Autotag task running");
    let time_to_update = {
      let database = s.database.read().unwrap();
      database.autotags.last_updated + Duration::days(1).num_seconds() < UTC::now().timestamp()
    };
    if !time_to_update {
      info!("Not yet time to update, sleeping 30 minutes");
      self.next_sleep = Duration::minutes(30).num_seconds();
      return;
    }
    info!("Time to update autotags");
    let users: Vec<(u64, u64, u64)> = {
      let database = s.database.read().unwrap();
      database.autotags.users.iter().map(|u| (u.user_id, u.server_id, u.character_id)).collect()
    };
    {
      let option_state = s.state.read().unwrap();
      let state = match option_state.as_ref() {
        Some(st) => st,
        None => {
          info!("Bot not connected. Will try again in 30 seconds.");
          self.next_sleep = 30;
          return;
        }
      };
      for (user_id, server_id, character_id) in users {
        let server = match state.servers().iter().find(|s| s.id.0 == server_id) {
          Some(ser) => ser,
          None => {
            warn!("Couldn't find server for user ID {}", user_id);
            continue;
          }
        };
        if let Err(e) = Tagger::tag(s.clone(), UserId(user_id), server, character_id, false) {
          warn!("Couldn't update tag for user ID {}: {}", user_id, e);
        }
      }
    }
    {
      let mut database = s.database.write().unwrap();
      database.autotags.last_updated = UTC::now().timestamp();
    };
    info!("Done updating autotags");
  }
}

impl RunsTask for AutoTagTask {
  fn start(mut self, s: Arc<LalafellBot>) {
    info!("Autotag task waiting {} seconds", self.next_sleep);
    loop {
      self.run_once(s.clone());
    }
  }
}
<commit_msg>feat: update tags twice a day<commit_after>use LalafellBot;
use tasks::RunsTask;
use commands::tag::Tagger;
use discord::model::UserId;
use chrono::prelude::*;
use chrono::Duration;

use std::sync::Arc;
use std::thread;

pub struct AutoTagTask {
  pub next_sleep: i64
}

impl AutoTagTask {
  pub fn new() -> AutoTagTask {
    AutoTagTask {
      next_sleep: 30
    }
  }

  pub fn run_once(&mut self, s: Arc<LalafellBot>) {
    thread::sleep(Duration::seconds(self.next_sleep).to_std().unwrap());
    info!("Autotag task running");
    let time_to_update = {
      let database = s.database.read().unwrap();
      database.autotags.last_updated + Duration::hours(12).num_seconds() < UTC::now().timestamp()
    };
    if !time_to_update {
      info!("Not yet time to update, sleeping 30 minutes");
      self.next_sleep = Duration::minutes(30).num_seconds();
      return;
    }
    info!("Time to update autotags");
    let users: Vec<(u64, u64, u64)> = {
      let database = s.database.read().unwrap();
      database.autotags.users.iter().map(|u| (u.user_id, u.server_id, u.character_id)).collect()
    };
    {
      let option_state = s.state.read().unwrap();
      let state = match option_state.as_ref() {
        Some(st) => st,
        None => {
          info!("Bot not connected. Will try again in 30 seconds.");
          self.next_sleep = 30;
          return;
        }
      };
      for (user_id, server_id, character_id) in users {
        let server = match state.servers().iter().find(|s| s.id.0 == server_id) {
          Some(ser) => ser,
          None => {
            warn!("Couldn't find server for user ID {}", user_id);
            continue;
          }
        };
        if let Err(e) = Tagger::tag(s.clone(), UserId(user_id), server, character_id, false) {
          warn!("Couldn't update tag for user ID {}: {}", user_id, e);
        }
      }
    }
    {
      let mut database = s.database.write().unwrap();
      database.autotags.last_updated = UTC::now().timestamp();
    };
    info!("Done updating autotags");
  }
}

impl RunsTask for AutoTagTask {
  fn start(mut self, s: Arc<LalafellBot>) {
    info!("Autotag task waiting {} seconds", self.next_sleep);
    loop {
      self.run_once(s.clone());
    }
  }
}

{'fraction_non_alphanumeric': 0.15684962070504238, 'fraction_numerical': 0.009147701918786257, 'mean_word_length': 2.6387987012987013, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3266186', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1494, 'n_words': 333}