-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#22 Add relative plots for albums and songs
- Loading branch information
Showing
6 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,93 @@ | ||
use super::{create_plot, find_dates}; | ||
use crate::display::date; | ||
use crate::types::{Artist, SongEntries}; | ||
use crate::types::{HasArtist, Music, Song, SongEntries}; | ||
|
||
/// Creates a plot of the amount of plays of an [`Artist`] relative to all plays | ||
/// Creates a plot of the amount of plays of a [`Music`] relative to all plays | ||
/// | ||
/// Opens the plot in the browser | ||
pub fn artist(entries: &SongEntries, art: &Artist) { | ||
pub fn to_all<Asp: Music>(entries: &SongEntries, aspect: &Asp) { | ||
let mut times = Vec::<i64>::new(); | ||
// percentages relative to the sum of all plays | ||
let mut plays = Vec::<f64>::new(); | ||
|
||
// TODO! | ||
// each data point lies at the occurrence -> looks weird when you haven't listened in a long time | ||
// maybe make it so there's at least a data point once a week? | ||
let dates = find_dates(entries, art, false); | ||
let dates = find_dates(entries, aspect, false); | ||
|
||
let start = dates.first().unwrap(); | ||
let sum_start = &entries.first_date(); | ||
|
||
#[allow(clippy::cast_precision_loss)] | ||
for date in &dates { | ||
times.push(date.timestamp()); | ||
let sum_of_plays = date::gather_plays(entries, art, start, date) as f64; | ||
let sum_of_plays = date::gather_plays(entries, aspect, start, date) as f64; | ||
let sum_of_all_plays = date::sum_plays(entries, sum_start, date) as f64; | ||
plays.push(sum_of_plays / sum_of_all_plays); | ||
// *100 so that the percentage is easier to read... | ||
plays.push(100.0 * (sum_of_plays / sum_of_all_plays)); | ||
} | ||
|
||
create_plot(times, plays, format!("{art} - relative").as_str()); | ||
let title = format!("{aspect} | relative to all plays"); | ||
create_plot(times, plays, title.as_str()); | ||
} | ||
|
||
/// Creates a plot of the amount of plays of an [`Album`] or [`Song`] | ||
/// relative to total plays of the affiated [`Artist`] | ||
/// | ||
/// Opens the plot in the browser | ||
pub fn to_artist<Asp: HasArtist>(entries: &SongEntries, aspect: &Asp) { | ||
let mut times = Vec::<i64>::new(); | ||
// percentages relative to the sum of all plays | ||
let mut plays = Vec::<f64>::new(); | ||
|
||
// TODO! | ||
// each data point lies at the occurrence -> looks weird when you haven't listened in a long time | ||
// maybe make it so there's at least a data point once a week? | ||
let dates = find_dates(entries, aspect, false); | ||
|
||
let start = dates.first().unwrap(); | ||
let sum_start = &entries.first_date(); | ||
|
||
#[allow(clippy::cast_precision_loss)] | ||
for date in &dates { | ||
times.push(date.timestamp()); | ||
let sum_of_plays = date::gather_plays(entries, aspect, start, date) as f64; | ||
let sum_of_artist_plays = | ||
date::gather_plays(entries, aspect.artist(), sum_start, date) as f64; | ||
// *100 so that the percentage is easier to read... | ||
plays.push(100.0 * (sum_of_plays / sum_of_artist_plays)); | ||
} | ||
|
||
let title = format!("{aspect} | relative to the artist"); | ||
create_plot(times, plays, title.as_str()); | ||
} | ||
|
||
/// Creates a plot of the amount of plays of a [`Song`] | ||
/// relative to total plays of the affiated [`Album`] | ||
/// | ||
/// Opens the plot in the browser | ||
pub fn to_album(entries: &SongEntries, aspect: &Song) { | ||
let mut times = Vec::<i64>::new(); | ||
// percentages relative to the sum of all plays | ||
let mut plays = Vec::<f64>::new(); | ||
|
||
// TODO! | ||
// each data point lies at the occurrence -> looks weird when you haven't listened in a long time | ||
// maybe make it so there's at least a data point once a week? | ||
let dates = find_dates(entries, aspect, false); | ||
|
||
let start = dates.first().unwrap(); | ||
let sum_start = &entries.first_date(); | ||
|
||
#[allow(clippy::cast_precision_loss)] | ||
for date in &dates { | ||
times.push(date.timestamp()); | ||
let sum_of_plays = date::gather_plays(entries, aspect, start, date) as f64; | ||
let sum_of_album_plays = date::gather_plays(entries, &aspect.album, sum_start, date) as f64; | ||
// *100 so that the percentage is easier to read... | ||
plays.push(100.0 * (sum_of_plays / sum_of_album_plays)); | ||
} | ||
|
||
let title = format!("{aspect} | relative to the album"); | ||
create_plot(times, plays, title.as_str()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters