Skip to content

Commit

Permalink
#33 add plot all subaspects commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Aug 21, 2024
1 parent d4924b2 commit 0251a09
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 25 deletions.
82 changes: 80 additions & 2 deletions endsong_ui/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub fn absolute<Asp: Music>(entries: &SongEntries, aspect: &Asp) -> TraceType {
}

/// Creates a trace of the absolute amount of plays of a song
/// with its plays summed across all album it's in
/// with its plays summed across all albums it's in
///
/// Is case-INSENSITIVE
///
/// Creates an empty trace if `song` is not in `entries`
#[must_use]
Expand All @@ -68,7 +70,7 @@ pub fn absolute_ignore_album(entries: &SongEntries, song: &Song) -> TraceType {

for entry in entries
.iter()
.filter(|entry| song.album.artist.name == entry.artist && song.name == entry.track)
.filter(|entry| song.is_entry_lowercase_ignore_album(entry))
{
song_plays += 1;
times.push(format_date(&entry.timestamp));
Expand Down Expand Up @@ -202,4 +204,80 @@ pub mod relative {

TraceType::Relative(trace)
}

/// Creates a trace of the amount of plays of a [`Song`] relative to all plays
/// while ignoring the album the song is in and its capitalization
///
/// Creates an empty trace if `song` is not in `entries`
#[must_use]
pub fn to_all_ignore_album(entries: &SongEntries, song: &Song) -> TraceType {
let mut times = Vec::<String>::with_capacity(entries.len());
// percentages relative to the sum of all plays
let mut plays = Vec::<f64>::with_capacity(entries.len());

let mut aspect_plays = 0.0;
let mut all_plays = 0.0;

// the plot should start at the first time the aspect is played
let mut aspect_found = false;

for entry in entries.iter() {
all_plays += 1.0;

if song.is_entry_lowercase_ignore_album(entry) {
aspect_found = true;
aspect_plays += 1.0;
}
if aspect_found {
times.push(format_date(&entry.timestamp));
// *100 so that the percentage is easier to read...
plays.push(100.0 * (aspect_plays / all_plays));
}
}

let title = format!("{song} | relative to all plays");
let trace = Scatter::new(times, plays).name(title);

TraceType::Relative(trace)
}

/// Creates a plot of the amount of plays of an [`Song`]
/// relative to total plays of the corresponding [`Artist`]
/// while ignoring the album the song is in and its capitalization
///
/// Creates an empty trace if `song` is not in `entries`
#[must_use]
pub fn to_artist_ignore_album(entries: &SongEntries, song: &Song) -> TraceType {
let artist = &song.album.artist;

let mut times = Vec::<String>::new();
// percentages relative to the sum of respective artist plays
let mut plays = Vec::<f64>::new();

let mut aspect_plays = 0.0;
let mut artist_plays = 0.0;

// the plot should start at the first time the aspect is played
let mut aspect_found = false;

for entry in entries.iter().filter(|entry| artist.is_entry(entry)) {
artist_plays += 1.0;

if song.is_entry_lowercase_ignore_album(entry) {
aspect_found = true;
aspect_plays += 1.0;
}

if aspect_found {
times.push(format_date(&entry.timestamp));
// *100 so that the percentage is easier to read...
plays.push(100.0 * (aspect_plays / artist_plays));
}
}

let title = format!("{song} | relative to the artist");
let trace = Scatter::new(times, plays).name(title);

TraceType::Relative(trace)
}
}
27 changes: 26 additions & 1 deletion endsong_ui/src/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,33 @@ const fn plot_commands() -> &'static [Command] {
),
Command(
"plot artist albums",
"gaa",
"garta",
"creates a plot of the absolute traces of all albums of the given artist and opens it in the web browser",
),
Command(
"plot artist albums rel",
"gartar",
"creates a plot of the traces of all albums of the given artist relative to all or this artist and opens it in the web browser",
),
Command(
"plot artist songs",
"garts",
"creates a plot of the absolute traces of all songs of the given artist and opens it in the web browser",
),
Command(
"plot artist songs rel",
"gartsr",
"creates a plot of the traces of all songs of the given artist relative to all or this artist and opens it in the web browser",
),
Command(
"plot album songs",
"galbs",
"creates a plot of the absolute traces of all songs of the given album and opens it in the web browser",
),
Command(
"plot album songs rel",
"galbsr",
"creates a plot of the traces of all songs of the given album relative to all. this artist or to this album and opens it in the web browser",
),
]
}
Loading

0 comments on commit 0251a09

Please sign in to comment.