Skip to content

Commit

Permalink
feat: show starts
Browse files Browse the repository at this point in the history
  • Loading branch information
jooooscha committed Nov 28, 2022
1 parent b047fe9 commit ba81b70
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 52 deletions.
8 changes: 6 additions & 2 deletions src/backend/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -83,7 +85,7 @@ impl Core {
config,
current_screen: Channels,
channel_list,
history: playback_history,
history,
status_sender,
status_receiver,
};
Expand Down Expand Up @@ -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)
Expand All @@ -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()),
Expand Down
9 changes: 5 additions & 4 deletions src/backend/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
64 changes: 18 additions & 46 deletions src/backend/io/history.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
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::{
style::{Color, Modifier, Style},
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<String, usize>,
}

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<MinimalVideo>,
#[serde(default)]
stats: HashMap<NaiveDate, Stats>,
stats: Stats,
}

impl History {
Expand All @@ -48,7 +26,7 @@ impl History {
// this is only for compatability reasons with old History struct
match serde_json::from_str::<Vec<MinimalVideo>>(&history) {
Ok(list) => {
let stats = HashMap::default();
let stats = Stats::default();
return Self { list, stats };
}
_ => (),
Expand All @@ -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<ListItem> {
Expand Down
1 change: 1 addition & 0 deletions src/backend/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{
};

pub(crate) mod config;
pub(crate) mod stats;
pub(crate) mod history;
pub(crate) mod subscriptions;

Expand Down
76 changes: 76 additions & 0 deletions src/backend/io/stats.rs
Original file line number Diff line number Diff line change
@@ -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<NaiveDate, DailyStats>,
}

/// Statistics collected per day
#[derive(Clone, Deserialize, Serialize, Default)]
pub(crate) struct DailyStats {
pub watched: usize,
pub channels: HashMap<String, usize>,
}

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); },
}
}
}

0 comments on commit ba81b70

Please sign in to comment.