Skip to content

Commit

Permalink
Recompute playcounts after the queue ends
Browse files Browse the repository at this point in the history
  • Loading branch information
ruuda committed May 19, 2024
1 parent 888ca70 commit b5b6746
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::mvar::Var;
use crate::player::QueueId;
use crate::{MetaIndex, MemoryMetaIndex, TrackId};
use crate::user_data::{Rating, UserData};
use crate::playcount::PlayCounter;

/// Changes in the playback state or library to be recorded.
pub enum PlaybackEvent {
Expand All @@ -39,6 +40,7 @@ pub fn main(
db_path: &Path,
index_var: Var<MemoryMetaIndex>,
user_data: Arc<Mutex<UserData>>,
mut counter: PlayCounter,
events: Receiver<PlaybackEvent>,
) -> Result<()> {
let connection = database_utils::connect_read_write(db_path)?;
Expand Down Expand Up @@ -104,6 +106,29 @@ pub fn main(
// of having to sync the WAL as well. We checkpoint after
// the queue ends, before the post-playback program runs.
connection.execute("PRAGMA wal_checkpoint(PASSIVE);")?;

// Recompute the playcounts as well. We load these from the
// database rather than updating the counts on the go for two
// reasons:
// 1. We need to anyway at startup, so this way there is only
// one code path.
// 2. We can pick up imported listens, though only when they
// are not backdated to before our last update there. When
// they are backdated, it requires an application restart.
// The count import is incremental. Computing the ranking is
// not, but that's fast enough anyway. (The full import +
// ranking is 140ms on a Raspberry Pi for ~22k tracks.)
let index = index_var.get();
let mut tx = db.begin()?;
counter.count_from_database(&index, &mut tx)?;
tx.commit()?;
let counts = counter.into_counts();
let rank = counts.get_discover_rank();
user_data
.lock()
.unwrap()
.replace_discover_score(&rank);
counter = counts.into_counter();
}
PlaybackEvent::Rated { track_id, rating } => {
let mut tx = db.begin()?;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ fn main() -> Result<()> {
println!("Index loaded.");

println!("Loading user data and playcounts ...");
let user_data = UserData::load_from_database(&index, &mut tx)?;
let (user_data, counts) = UserData::load_from_database(&index, &mut tx)?;
let user_data_arc = Arc::new(Mutex::new(user_data));
println!("User data loaded.");

Expand All @@ -315,6 +315,7 @@ fn main() -> Result<()> {
let player = musium::player::Player::new(
index_var.clone(),
user_data_arc.clone(),
counts.into_counter(),
&config,
);
let service = MetaServer::new(
Expand Down
3 changes: 3 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::error::Error;
use crate::exec_pre_post;
use crate::filter::StateVariableFilter;
use crate::history::PlaybackEvent;
use crate::playcount::PlayCounter;
use crate::history;
use crate::mvar::Var;
use crate::playback;
Expand Down Expand Up @@ -1081,6 +1082,7 @@ impl Player {
pub fn new(
index_var: Var<MemoryMetaIndex>,
user_data: Arc<Mutex<UserData>>,
counter: PlayCounter,
config: &Config,
) -> Player {
// Build the channel to send playback events to the history thread. That
Expand Down Expand Up @@ -1138,6 +1140,7 @@ impl Player {
&db_path,
index_for_history,
user_data,
counter,
hist_receiver,
);
// The history thread should not exit. When it does, that's a
Expand Down
6 changes: 3 additions & 3 deletions src/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::convert::TryFrom;

use crate::MemoryMetaIndex;
use crate::album_table::AlbumTable;
use crate::playcount::PlayCounter;
use crate::playcount::{PlayCounter, PlayCounts};
use crate::prim::{AlbumId, ArtistId, TrackId};
use crate::{database as db};

Expand Down Expand Up @@ -119,7 +119,7 @@ impl UserData {
pub fn load_from_database(
index: &MemoryMetaIndex,
tx: &mut db::Transaction,
) -> db::Result<Self> {
) -> db::Result<(Self, PlayCounts)> {
let mut stats = Self::default();

for opt_rating in db::iter_ratings(tx)? {
Expand All @@ -134,7 +134,7 @@ impl UserData {
let counts = counter.into_counts();
stats.replace_discover_score(&counts.get_discover_rank());

Ok(stats)
Ok((stats, counts))
}

pub fn set_track_rating(&mut self, track_id: TrackId, rating: Rating) {
Expand Down

0 comments on commit b5b6746

Please sign in to comment.