From 461a956288ce7ee08ed8e5235bd6f311df9bcf1a Mon Sep 17 00:00:00 2001 From: chme Date: Sun, 1 Nov 2015 09:15:24 +0100 Subject: [PATCH] [mpd] Return the correct values for number of songs, artists, albums and song length sum in the "stats" command (wrong values are still reported for uptime, db_update and playtime) --- src/db.c | 14 +++++++++++++- src/db.h | 6 ++++++ src/mpd.c | 44 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/db.c b/src/db.c index f65ccdc2..f3b70f5a 100644 --- a/src/db.c +++ b/src/db.c @@ -1474,7 +1474,7 @@ db_build_query_count_items(struct query_params *qp, char **q) if (qp->filter) query = sqlite3_mprintf("SELECT COUNT(*), SUM(song_length) FROM files f WHERE f.disabled = 0 AND %s;", qp->filter); else - return -1; + query = sqlite3_mprintf("SELECT COUNT(*), SUM(song_length) FROM files f WHERE f.disabled = 0;"); if (!query) { @@ -2036,6 +2036,18 @@ db_files_get_count(void) return db_get_count("SELECT COUNT(*) FROM files f WHERE f.disabled = 0;"); } +int +db_files_get_artist_count(void) +{ + return db_get_count("SELECT COUNT(DISTINCT songartistid) FROM files f WHERE f.disabled = 0;"); +} + +int +db_files_get_album_count(void) +{ + return db_get_count("SELECT COUNT(DISTINCT songalbumid) FROM files f WHERE f.disabled = 0;"); +} + int db_files_get_count_bymatch(char *path) { diff --git a/src/db.h b/src/db.h index 1d20569e..68fbf4d4 100644 --- a/src/db.h +++ b/src/db.h @@ -416,6 +416,12 @@ db_query_fetch_string_sort(struct query_params *qp, char **string, char **sortst int db_files_get_count(void); +int +db_files_get_artist_count(void); + +int +db_files_get_album_count(void); + int db_files_get_count_bymatch(char *path); diff --git a/src/mpd.c b/src/mpd.c index 8d1d249b..7470097b 100644 --- a/src/mpd.c +++ b/src/mpd.c @@ -869,7 +869,41 @@ mpd_command_status(struct evbuffer *evbuf, int argc, char **argv, char **errmsg) static int mpd_command_stats(struct evbuffer *evbuf, int argc, char **argv, char **errmsg) { - //TODO implement command stats + struct query_params qp; + struct filecount_info fci; + int artists; + int albums; + int ret; + + memset(&qp, 0, sizeof(struct query_params)); + qp.type = Q_COUNT_ITEMS; + + ret = db_query_start(&qp); + if (ret < 0) + { + db_query_end(&qp); + + ret = asprintf(errmsg, "Could not start query"); + if (ret < 0) + DPRINTF(E_LOG, L_MPD, "Out of memory\n"); + return ACK_ERROR_UNKNOWN; + } + + ret = db_query_fetch_count(&qp, &fci); + if (ret < 0) + { + db_query_end(&qp); + + ret = asprintf(errmsg, "Could not fetch query count"); + if (ret < 0) + DPRINTF(E_LOG, L_MPD, "Out of memory\n"); + return ACK_ERROR_UNKNOWN; + } + + artists = db_files_get_artist_count(); + albums = db_files_get_album_count(); + + //TODO [mpd] Implement missing stats attributes (uptime, db_update, playtime) evbuffer_add_printf(evbuf, "artists: %d\n" "albums: %d\n" @@ -878,11 +912,11 @@ mpd_command_stats(struct evbuffer *evbuf, int argc, char **argv, char **errmsg) "db_playtime: %d\n" "db_update: %d\n" "playtime: %d\n", - 1, - 2, - 3, + artists, + albums, + fci.count, 4, - 5, + (fci.length / 1000), 6, 7);