diff --git a/src/backend/core.rs b/src/backend/core.rs index 136f7de..4d4eed1 100644 --- a/src/backend/core.rs +++ b/src/backend/core.rs @@ -74,7 +74,9 @@ impl Core { channel_list.select(Some(0)); channel_list.set_filter(current_filter); - let playback_history = History::load(); + let mut history = History::load(); + history.add_start(); + let (status_sender, status_receiver) = channel(); @@ -83,7 +85,7 @@ impl Core { config, current_screen: Channels, channel_list, - history: playback_history, + history, status_sender, status_receiver, }; @@ -201,6 +203,7 @@ impl Core { } // call video player + #[cfg(not(debug_assertions))] let command = Command::new("setsid") .arg("-f") .arg(&self.config.video_player) @@ -211,6 +214,7 @@ impl Core { self.history.video_opened(&video); + #[cfg(not(debug_assertions))] match command { Ok(_) => notify_open(&video.get_details()), Err(error) => notify_error(&error.to_string()), diff --git a/src/backend/draw.rs b/src/backend/draw.rs index 6c5afec..150cecb 100644 --- a/src/backend/draw.rs +++ b/src/backend/draw.rs @@ -141,11 +141,12 @@ pub fn draw(app: AppState) { ////////////////////////////// - let counter = match app.history.stat_today() { - Some(stat) => stat.watched, - None => 0, + let stats = app.history.stats(); + let videos = match stats.today() { + Some(stats) => stats.watched, + None => 0 }; - let info = Paragraph::new(Span::from(format!("{} - Videos Today: {}", INFO_LINE, counter))) + let info = Paragraph::new(Span::from(format!("{} - Videos Today: {} - Starts: {}", INFO_LINE, videos, stats.starts))) .style(Style::default()) .alignment(Alignment::Left); diff --git a/src/backend/io/history.rs b/src/backend/io/history.rs index 112acfa..b4d0a89 100644 --- a/src/backend/io/history.rs +++ b/src/backend/io/history.rs @@ -1,7 +1,7 @@ use crate::backend::{ - data::video::Video, - io::{read_config, write_config, FileType::HistoryFile}, + io::{read_config, write_config, FileType::HistoryFile, stats::Stats}, ToTuiListItem, + data::video::Video, }; use serde::{Deserialize, Serialize}; use tui::{ @@ -9,34 +9,12 @@ use tui::{ text::{Span, Spans}, widgets::ListItem, }; -use std::collections::HashMap; -use chrono::prelude::*; - -#[derive(Clone, Deserialize, Serialize, Default)] -pub(crate) struct Stats { - // starts: usize, - pub watched: usize, - pub channels: HashMap, -} - -impl Stats { - pub(crate) fn add(&mut self, video: &Video) { - self.watched += 1; // increase total counter - - let video_name = video.origin_channel_name(); - let channel = self.channels.get_mut(video_name); - match channel { - Some(number) => *number += 1, // channel already there - None => { let _ = self.channels.insert(video_name.clone(), 1); }, - } - } -} #[derive(Clone, Deserialize, Serialize, Default)] pub(crate) struct History { list: Vec, #[serde(default)] - stats: HashMap, + stats: Stats, } impl History { @@ -48,7 +26,7 @@ impl History { // this is only for compatability reasons with old History struct match serde_json::from_str::>(&history) { Ok(list) => { - let stats = HashMap::default(); + let stats = Stats::default(); return Self { list, stats }; } _ => (), @@ -75,32 +53,26 @@ impl History { self.list.push(mimimal_video); - match self.stat_today_mut() { - Some(stat) => stat.add(video), - None => { - let mut stat = Stats::default(); - stat.add(video); - self.stat_insert_today(stat); - }, - } + self.stats.add(video); + // match self.stat_today_mut() { + // Some(stat) => stat.add(video), + // None => { + // let mut stat = Stats::default(); + // stat.add(video); + // self.stat_insert_today(stat); + // }, + // } self.save() } - pub(crate) fn stat_today(&self) -> Option<&Stats> { - let now: NaiveDate = Local::now().date_naive(); - self.stats.get(&now) + pub(crate) fn add_start(&mut self) { + self.stats.starts += 1; + self.save(); } - pub fn stat_insert_today(&mut self, stat: Stats) { - let now: NaiveDate = Local::now().date_naive(); - self.stats.insert(now, stat); - } - - - fn stat_today_mut(&mut self) -> Option<&mut Stats> { - let now: NaiveDate = Local::now().date_naive(); - self.stats.get_mut(&now) + pub(crate) fn stats(&self) -> &Stats { + &self.stats } pub(crate) fn to_list_items(&self) -> Vec { diff --git a/src/backend/io/mod.rs b/src/backend/io/mod.rs index 99f112e..c4c25d8 100644 --- a/src/backend/io/mod.rs +++ b/src/backend/io/mod.rs @@ -13,6 +13,7 @@ use std::{ }; pub(crate) mod config; +pub(crate) mod stats; pub(crate) mod history; pub(crate) mod subscriptions; diff --git a/src/backend/io/stats.rs b/src/backend/io/stats.rs new file mode 100644 index 0000000..bc8c8cb --- /dev/null +++ b/src/backend/io/stats.rs @@ -0,0 +1,76 @@ +use std::collections::HashMap; +use serde::{Deserialize, Serialize}; +use chrono::prelude::*; +use crate::backend::data::video::Video; + +/// Statistics collected in total +#[derive(Clone, Deserialize, Serialize, Default)] +pub(crate) struct Stats { + pub starts: usize, + pub per_day: HashMap, +} + +/// Statistics collected per day +#[derive(Clone, Deserialize, Serialize, Default)] +pub(crate) struct DailyStats { + pub watched: usize, + pub channels: HashMap, +} + +impl Stats { + pub fn today(&self) -> Option<&DailyStats> { + let now: NaiveDate = Local::now().date_naive(); + self.per_day.get(&now) + } + + pub fn today_mut(&mut self) -> &mut DailyStats { + let now: NaiveDate = Local::now().date_naive(); + + + if let None = self.per_day.get_mut(&now) { + let daily_stat = DailyStats::default(); + self.per_day.insert(now, daily_stat); + } + + self.per_day.get_mut(&now).unwrap() + } + + pub fn add(&mut self, video: &Video) { + let stat_today = self.today_mut(); + stat_today.add_video(video); + } + + pub fn add_start(&mut self) { + self.starts += 1; + } + + + + // pub(crate) fn stat_today(&self) -> Option<&Stats> { + // let now: NaiveDate = Local::now().date_naive(); + // self.stats.get(&now) + // } + + // pub fn stat_insert_today(&mut self, stat: Stats) { + // let now: NaiveDate = Local::now().date_naive(); + // self.stats.insert(now, stat); + // } + + + // fn stat_today_mut(&mut self) -> Option<&mut Stats> { + // let now: NaiveDate = Local::now().date_naive(); + // self.stats.get_mut(&now) + // } +} + +impl DailyStats { + fn add_video(&mut self, video: &Video) { + self.watched += 1; // increase total counter + let video_name = video.origin_channel_name(); + let channel = self.channels.get_mut(video_name); + match channel { + Some(number) => *number += 1, // channel already there + None => { let _ = self.channels.insert(video_name.clone(), 1); }, + } + } +}