1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-12-27 03:10:26 +00:00

Fix: improve speed for listing large queues

This commit is contained in:
jcorporation 2018-08-21 23:56:17 +01:00
parent a62d4a2995
commit fbb32ae17a
4 changed files with 62 additions and 63 deletions

View File

@ -734,8 +734,8 @@
<tr><th>Artists</th><td id="mpdstats_artists"></td></tr> <tr><th>Artists</th><td id="mpdstats_artists"></td></tr>
<tr><th>Albums</th><td id="mpdstats_albums"></td></tr> <tr><th>Albums</th><td id="mpdstats_albums"></td></tr>
<tr><th>Songs</th><td id="mpdstats_songs"></td></tr> <tr><th>Songs</th><td id="mpdstats_songs"></td></tr>
<tr><th>DB Play Time</th><td id="mpdstats_dbplaytime"></td></tr> <tr><th>DB Play Time</th><td id="mpdstats_dbPlaytime"></td></tr>
<tr><th>DB Updated</th><td id="mpdstats_dbupdated"></td></tr> <tr><th>DB Updated</th><td id="mpdstats_dbUpdated"></td></tr>
<tr><td colspan="2" class="pt-3"><h5>Play Statistics</h5></td></tr> <tr><td colspan="2" class="pt-3"><h5>Play Statistics</h5></td></tr>
<tr><th>Uptime</th><td id="mpdstats_uptime"></td></tr> <tr><th>Uptime</th><td id="mpdstats_uptime"></td></tr>
<tr><th>Play Time</th><td id="mpdstats_playtime"></td></tr> <tr><th>Play Time</th><td id="mpdstats_playtime"></td></tr>

View File

@ -735,13 +735,13 @@ function parseStats(obj) {
document.getElementById('mpdstats_artists').innerText = obj.data.artists; document.getElementById('mpdstats_artists').innerText = obj.data.artists;
document.getElementById('mpdstats_albums').innerText = obj.data.albums; document.getElementById('mpdstats_albums').innerText = obj.data.albums;
document.getElementById('mpdstats_songs').innerText = obj.data.songs; document.getElementById('mpdstats_songs').innerText = obj.data.songs;
document.getElementById('mpdstats_dbplaytime').innerText = beautifyDuration(obj.data.dbplaytime); document.getElementById('mpdstats_dbPlaytime').innerText = beautifyDuration(obj.data.dbPlaytime);
document.getElementById('mpdstats_playtime').innerText = beautifyDuration(obj.data.playtime); document.getElementById('mpdstats_playtime').innerText = beautifyDuration(obj.data.playtime);
document.getElementById('mpdstats_uptime').innerText = beautifyDuration(obj.data.uptime); document.getElementById('mpdstats_uptime').innerText = beautifyDuration(obj.data.uptime);
var d = new Date(obj.data.dbupdated * 1000); var d = new Date(obj.data.dbUpdated * 1000);
document.getElementById('mpdstats_dbupdated').innerText = d.toUTCString(); document.getElementById('mpdstats_dbUpdated').innerText = d.toUTCString();
document.getElementById('mympdVersion').innerText = obj.data.mympd_version; document.getElementById('mympdVersion').innerText = obj.data.mympdVersion;
document.getElementById('mpdVersion').innerText = obj.data.mpd_version; document.getElementById('mpdVersion').innerText = obj.data.mpdVersion;
} }
function toggleBtn(btn, state) { function toggleBtn(btn, state) {
@ -974,7 +974,7 @@ function parseQueue(obj) {
if (app.current.app !== 'Queue') if (app.current.app !== 'Queue')
return; return;
if (typeof(obj.totalTime) != undefined && obj.totalTime > 0 ) if (typeof(obj.totalTime) != undefined && obj.totalTime > 0 && obj.totalEntities <= settings.maxElementsPerPage )
document.getElementById('panel-heading-queue').innerText = obj.totalEntities + ' Songs ' + beautifyDuration(obj.totalTime); document.getElementById('panel-heading-queue').innerText = obj.totalEntities + ' Songs ' + beautifyDuration(obj.totalTime);
else if (obj.totalEntities > 0) else if (obj.totalEntities > 0)
document.getElementById('panel-heading-queue').innerText = obj.totalEntities + ' Songs'; document.getElementById('panel-heading-queue').innerText = obj.totalEntities + ' Songs';
@ -1313,18 +1313,18 @@ function parseListTitles(obj) {
} }
function setPagination(number) { function setPagination(number) {
var totalPages = Math.ceil(number / settings.max_elements_per_page); var totalPages = Math.ceil(number / settings.maxElementsPerPage);
var cat = app.current.app + (app.current.tab == undefined ? '': app.current.tab); var cat = app.current.app + (app.current.tab == undefined ? '': app.current.tab);
if (totalPages == 0) if (totalPages == 0)
totalPages = 1; totalPages = 1;
var p = ['PaginationTop', 'PaginationBottom']; var p = ['PaginationTop', 'PaginationBottom'];
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
document.getElementById(cat + p[i] + 'Page').innerText = (app.current.page / settings.max_elements_per_page + 1) + ' / ' + totalPages; document.getElementById(cat + p[i] + 'Page').innerText = (app.current.page / settings.maxElementsPerPage + 1) + ' / ' + totalPages;
if (totalPages > 1) { if (totalPages > 1) {
document.getElementById(cat + p[i] + 'Page').removeAttribute('disabled'); document.getElementById(cat + p[i] + 'Page').removeAttribute('disabled');
var pl = ''; var pl = '';
for (var j = 0; j < totalPages; j++) { for (var j = 0; j < totalPages; j++) {
pl += '<button data-page="' + (j * settings.max_elements_per_page) + '" type="button" class="mr-1 mb-1 btn-sm btn btn-secondary">' + pl += '<button data-page="' + (j * settings.maxElementsPerPage) + '" type="button" class="mr-1 mb-1 btn-sm btn btn-secondary">' +
( j + 1) + '</button>'; ( j + 1) + '</button>';
} }
document.getElementById(cat + p[i] + 'Pages').innerHTML = pl; document.getElementById(cat + p[i] + 'Pages').innerHTML = pl;
@ -1332,7 +1332,7 @@ function setPagination(number) {
document.getElementById(cat + p[i] + 'Page').setAttribute('disabled', 'disabled'); document.getElementById(cat + p[i] + 'Page').setAttribute('disabled', 'disabled');
} }
if (number > app.current.page + settings.max_elements_per_page) { if (number > app.current.page + settings.maxElementsPerPage) {
document.getElementById(cat + p[i] + 'Next').removeAttribute('disabled'); document.getElementById(cat + p[i] + 'Next').removeAttribute('disabled');
document.getElementById(cat + p[i]).classList.remove('hide'); document.getElementById(cat + p[i]).classList.remove('hide');
document.getElementById(cat + 'ButtonsBottom').classList.remove('hide'); document.getElementById(cat + 'ButtonsBottom').classList.remove('hide');
@ -1474,7 +1474,7 @@ function getAllPlaylists(obj) {
} }
document.getElementById('addToPlaylistPlaylist').innerHTML += playlists; document.getElementById('addToPlaylistPlaylist').innerHTML += playlists;
if (obj.totalEntities > obj.returnedEntities) { if (obj.totalEntities > obj.returnedEntities) {
obj.offset += settings.max_elements_per_page; obj.offset += settings.maxElementsPerPage;
sendAPI({"cmd": "MPD_API_GET_PLAYLISTS","data": {"offset": obj.offset, "filter": "-"}}, getAllPlaylists); sendAPI({"cmd": "MPD_API_GET_PLAYLISTS","data": {"offset": obj.offset, "filter": "-"}}, getAllPlaylists);
} }
} }
@ -1867,10 +1867,10 @@ function scrollTo(pos) {
function gotoPage(x) { function gotoPage(x) {
switch (x) { switch (x) {
case 'next': case 'next':
app.current.page += settings.max_elements_per_page; app.current.page += settings.maxElementsPerPage;
break; break;
case 'prev': case 'prev':
app.current.page -= settings.max_elements_per_page; app.current.page -= settings.maxElementsPerPage;
if (app.current.page < 0) if (app.current.page < 0)
app.current.page = 0; app.current.page = 0;
break; break;

View File

@ -93,7 +93,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
} }
break; break;
case MPD_API_GET_STATE: case MPD_API_GET_STATE:
n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version); n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version, &mpd.queue_length);
break; break;
case MPD_API_SET_SETTINGS: case MPD_API_SET_SETTINGS:
je = json_scanf(msg.p, msg.len, "{data: { notificationWeb: %d, notificationPage: %d}}", &state.a, &state.b); je = json_scanf(msg.p, msg.len, "{data: { notificationWeb: %d, notificationPage: %d}}", &state.a, &state.b);
@ -669,7 +669,7 @@ void mympd_parse_idle(struct mg_mgr *s) {
enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false); enum mpd_idle idle_bitmask = mpd_recv_idle(mpd.conn, false);
int len; int len;
for (unsigned j = 0;; ++j) { for (unsigned j = 0;; j ++) {
enum mpd_idle idle_event = 1 << j; enum mpd_idle idle_event = 1 << j;
const char *idle_name = mpd_idle_name(idle_event); const char *idle_name = mpd_idle_name(idle_event);
if (idle_name == NULL) if (idle_name == NULL)
@ -689,14 +689,14 @@ void mympd_parse_idle(struct mg_mgr *s) {
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_queue\"}"); len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_queue\"}");
break; break;
case MPD_IDLE_PLAYER: case MPD_IDLE_PLAYER:
len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version); len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version, &mpd.queue_length);
if (config.stickers) { if (config.stickers) {
mympd_count_song_id(mpd.song_id, "playCount", 1); mympd_count_song_id(mpd.song_id, "playCount", 1);
mympd_last_played_song_id(mpd.song_id); mympd_last_played_song_id(mpd.song_id);
} }
break; break;
case MPD_IDLE_MIXER: case MPD_IDLE_MIXER:
len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version); len = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.queue_version, &mpd.queue_length);
break; break;
case MPD_IDLE_OUTPUT: case MPD_IDLE_OUTPUT:
len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_outputs\"}"); len = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_outputs\"}");
@ -736,7 +736,7 @@ char* mympd_get_tag(struct mpd_song const *song, enum mpd_tag_type tag) {
return str; return str;
} }
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version) { int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version, unsigned *queue_length) {
struct mpd_status *status; struct mpd_status *status;
const struct mpd_audio_format *audioformat; const struct mpd_audio_format *audioformat;
int len; int len;
@ -777,6 +777,7 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsig
*current_song_id = mpd_status_get_song_id(status); *current_song_id = mpd_status_get_song_id(status);
*next_song_id = mpd_status_get_next_song_id(status); *next_song_id = mpd_status_get_next_song_id(status);
*queue_version = mpd_status_get_queue_version(status); *queue_version = mpd_status_get_queue_version(status);
*queue_length = mpd_status_get_queue_length(status);
mpd_status_free(status); mpd_status_free(status);
if (len > MAX_SIZE) if (len > MAX_SIZE)
@ -833,7 +834,7 @@ int mympd_put_settings(char *buffer) {
"repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, " "repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, "
"mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, " "mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, "
"streamport: %d, coverimage: %Q, stickers: %B, mixramp: %B, " "streamport: %d, coverimage: %Q, stickers: %B, mixramp: %B, "
"max_elements_per_page: %d, replaygain: %Q," "maxElementsPerPage: %d, replaygain: %Q,"
"notificationWeb: %d, notificationPage: %d" "notificationWeb: %d, notificationPage: %d"
"}}", "}}",
mpd_status_get_repeat(status), mpd_status_get_repeat(status),
@ -1017,7 +1018,7 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
int len; int len;
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
if (!mpd_send_list_queue_range_meta(mpd.conn, 0, -1)) if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + MAX_ELEMENTS_PER_PAGE))
RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta"); RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta");
len = json_printf(&out, "{type:queue, data :[ "); len = json_printf(&out, "{type:queue, data :[ ");
@ -1030,7 +1031,6 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
drtn = mpd_song_get_duration(song); drtn = mpd_song_get_duration(song);
totalTime += drtn; totalTime += drtn;
entity_count ++; entity_count ++;
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out,","); if (entities_returned ++) len += json_printf(&out,",");
len += json_printf(&out, "{id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q}", len += json_printf(&out, "{id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q}",
mpd_song_get_id(song), mpd_song_get_id(song),
@ -1042,13 +1042,12 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
mpd_song_get_uri(song) mpd_song_get_uri(song)
); );
} }
}
mpd_entity_free(entity); mpd_entity_free(entity);
} }
len += json_printf(&out, "], totalTime: %d, totalEntities: %d, offset: %d, returnedEntities: %d, queue_version: %d }", len += json_printf(&out, "], totalTime: %d, totalEntities: %d, offset: %d, returnedEntities: %d, queue_version: %d }",
totalTime, totalTime,
entity_count, mpd.queue_length,
offset, offset,
entities_returned, entities_returned,
mpd.queue_version mpd.queue_version
@ -1475,8 +1474,6 @@ int mympd_queue_crop(char *buffer) {
int length = mpd_status_get_queue_length(status) - 1; int length = mpd_status_get_queue_length(status) - 1;
int playing_song_pos = mpd_status_get_song_pos(status); int playing_song_pos = mpd_status_get_song_pos(status);
mpd_status_free(status);
if (length < 0) { if (length < 0) {
len = json_printf(&out, "{type: error, data: %Q }", "A playlist longer than 1 song in length is required to crop."); len = json_printf(&out, "{type: error, data: %Q }", "A playlist longer than 1 song in length is required to crop.");
} }
@ -1492,6 +1489,8 @@ int mympd_queue_crop(char *buffer) {
len = json_printf(&out, "{type: error, data: %Q}", "You need to be playing to crop the playlist"); len = json_printf(&out, "{type: error, data: %Q}", "You need to be playing to crop the playlist");
} }
mpd_status_free(status);
if (len > MAX_SIZE) if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n"); fprintf(stderr, "Buffer truncated\n");
return len; return len;
@ -1554,16 +1553,15 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
int mympd_put_stats(char *buffer) { int mympd_put_stats(char *buffer) {
struct mpd_stats *stats = mpd_run_stats(mpd.conn); struct mpd_stats *stats = mpd_run_stats(mpd.conn);
const unsigned *version = mpd_connection_get_server_version(mpd.conn); const unsigned *version = mpd_connection_get_server_version(mpd.conn);
char mpd_version[20];
int len; int len;
char mpd_version[20];
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
snprintf(mpd_version, 20, "%i.%i.%i", version[0], version[1], version[2]); snprintf(mpd_version, 20, "%i.%i.%i", version[0], version[1], version[2]);
if (stats == NULL) if (stats == NULL)
RETURN_ERROR_AND_RECOVER("mympd_put_stats"); RETURN_ERROR_AND_RECOVER("mympd_put_stats");
len = json_printf(&out, "{type: mpdstats, data: { artists: %d, albums: %d, songs: %d, " len = json_printf(&out, "{type: mpdstats, data: { artists: %d, albums: %d, songs: %d, "
"playtime: %d, uptime: %d, dbupdated: %d, dbplaytime: %d, mympd_version: %Q, mpd_version: %Q}}", "playtime: %d, uptime: %d, dbUpdated: %d, dbPlaytime: %d, mympdVersion: %Q, mpdVersion: %Q}}",
mpd_stats_get_number_of_artists(stats), mpd_stats_get_number_of_artists(stats),
mpd_stats_get_number_of_albums(stats), mpd_stats_get_number_of_albums(stats),
mpd_stats_get_number_of_songs(stats), mpd_stats_get_number_of_songs(stats),

View File

@ -119,6 +119,7 @@ struct t_mpd {
int song_id; int song_id;
int next_song_id; int next_song_id;
unsigned queue_version; unsigned queue_version;
unsigned queue_length;
int timeout; int timeout;
} mpd; } mpd;
@ -162,7 +163,7 @@ void mympd_like_song_uri(const char *uri, int value);
void mympd_last_played_song_uri(const char *uri); void mympd_last_played_song_uri(const char *uri);
void mympd_last_played_song_id(int song_id); void mympd_last_played_song_id(int song_id);
void mympd_get_sticker(const char *uri, t_sticker *sticker); void mympd_get_sticker(const char *uri, t_sticker *sticker);
int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version, unsigned *queue_length);
int mympd_put_outputs(char *buffer); int mympd_put_outputs(char *buffer);
int mympd_put_current_song(char *buffer); int mympd_put_current_song(char *buffer);
int mympd_put_queue(char *buffer, unsigned int offset); int mympd_put_queue(char *buffer, unsigned int offset);