Skip to content

Commit

Permalink
Added statistics to about dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed May 27, 2018
1 parent edd318d commit 1da825c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
19 changes: 19 additions & 0 deletions htdocs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,25 @@ <h4><a style="color:#28a745" href="https://github.com/jcorporation/ympd"><span c
<li>Homepage: <a style="color:#28a745" target="_blank" href="https://github.com/jcorporation/mympd">https://github.com/jcorporation/mympd</a></li>
<li>Autor: Juergen Mang &lt;<a style="color:#28a745" href="mailto:[email protected]">[email protected]</a>&gt;</li>
</ul>
<hr/>
<h5>Database Statistics</h5>
<table class="table table-sm">
<tbody>
<tr><th>Artists</th><td id="mpdstats_artists"></td></tr>
<tr><th>Albums</th><td id="mpdstats_albums"></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 Updated</th><td id="mpdstats_dbupdated"></td></tr>
</tbody>
</table>
<hr/>
<h5>Play Statistics</h5>
<table class="table table-sm">
<tbody>
<tr><th>Uptime</th><td id="mpdstats_uptime"></td></tr>
<tr><th>Play Time</th><td id="mpdstats_playtime"></td></tr>
</tbody>
</table>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
Expand Down
27 changes: 26 additions & 1 deletion htdocs/js/mpd.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var pagination = 0;
var browsepath = "";
var lastSongTitle = "";
var current_song = new Object();
var MAX_ELEMENTS_PER_PAGE = 5;
var MAX_ELEMENTS_PER_PAGE = 100;
var isTouch = Modernizr.touch ? 1 : 0;
var filter = "";
var playstate = "";
Expand Down Expand Up @@ -149,6 +149,10 @@ $(document).ready(function(){
}
});

$('#about').on('shown.bs.modal', function () {
socket.send("MPD_API_GET_STATS");
})

$('#addstream').on('shown.bs.modal', function () {
$('#streamurl').focus();
})
Expand Down Expand Up @@ -581,6 +585,16 @@ function webSocketConnect() {
setLocalStream(obj.data.mpdhost,obj.data.streamport);
coverImageFile=obj.data.coverimage;
break;
case 'mpdstats':
$('#mpdstats_artists').text(obj.data.artists);
$('#mpdstats_albums').text(obj.data.albums);
$('#mpdstats_songs').text(obj.data.songs);
$('#mpdstats_dbplaytime').text(beautifyDuration(obj.data.dbplaytime));
$('#mpdstats_playtime').text(beautifyDuration(obj.data.playtime));
$('#mpdstats_uptime').text(beautifyDuration(obj.data.uptime));
var d = new Date(obj.data.dbupdated * 1000);
$('#mpdstats_dbupdated').text(d.toUTCString());
break;
case 'error':
showNotification(obj.data,'','','danger');
default:
Expand Down Expand Up @@ -1014,4 +1028,15 @@ function chVolume (increment) {
else if (newValue > 100) { newValue=100; }
volumeBar.slider('setValue',newValue);
socket.send("MPD_API_SET_VOLUME,"+newValue);
}

function beautifyDuration(x) {
var days = Math.floor(x / 86400);
var hours = Math.floor(x / 3600) - days * 24;
var minutes = Math.floor(x / 60) - hours * 60 - days * 1440;
var seconds = x - days * 86400 - hours * 3600 - minutes * 60;

return (days > 0 ? days + '\u2009d ' : '') +
(hours > 0 ? hours + '\u2009h ' + (minutes < 10 ? '0' : '') : '') +
minutes + '\u2009m ' + (seconds < 10 ? '0' : '') + seconds + '\u2009s';
}
32 changes: 31 additions & 1 deletion src/mpd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ int callback_mpd(struct mg_connection *c)
if ( (token = strtok(NULL, ",")) == NULL )
goto out_send_message;

mpd_run_send_message(mpd.conn, p_charbuf, token);
mpd_run_send_message(mpd.conn, p_charbuf, token);
out_send_message:
free(p_charbuf);
break;
Expand All @@ -305,6 +305,9 @@ int callback_mpd(struct mg_connection *c)
"\"streamport\": \"%d\",\"coverimage\": \"%s\"}"
"}", mpd.host, mpd.port, mpd.password ? "true" : "false", streamport, coverimage);
break;
case MPD_API_GET_STATS:
n = mympd_get_stats(mpd.buf);
break;
}

if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS)
Expand Down Expand Up @@ -877,6 +880,33 @@ int mpd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char *
return cur - buffer;
}

int mympd_get_stats(char *buffer)
{
char *cur = buffer;
const char *end = buffer + MAX_SIZE;
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
if (stats == NULL)
RETURN_ERROR_AND_RECOVER("mympd_get_stats");
cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"mpdstats\",\"data\": {");
cur += json_emit_raw_str(cur, end - cur, "\"artists\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_number_of_artists(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"albums\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_number_of_albums(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"songs\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_number_of_songs(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"playtime\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_play_time(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"uptime\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_uptime(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"dbupdated\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_db_update_time(stats));
cur += json_emit_raw_str(cur, end - cur, ",\"dbplaytime\":");
cur += json_emit_int(cur, end - cur, mpd_stats_get_db_play_time(stats));
cur += json_emit_raw_str(cur, end - cur, "}}");

mpd_stats_free(stats);
return cur - buffer;
}

void mpd_disconnect()
{
Expand Down
8 changes: 5 additions & 3 deletions src/mpd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
} while(0)


#define MAX_SIZE 1024 * 5
#define MAX_ELEMENTS_PER_PAGE 5
#define MAX_SIZE 1024 * 100
#define MAX_ELEMENTS_PER_PAGE 100

#define GEN_ENUM(X) X,
#define GEN_STR(X) #X,
Expand Down Expand Up @@ -73,7 +73,8 @@
X(MPD_API_TOGGLE_CROSSFADE) \
X(MPD_API_TOGGLE_REPEAT) \
X(MPD_API_GET_OPTIONS) \
X(MPD_API_SEND_SHUFFLE)
X(MPD_API_SEND_SHUFFLE) \
X(MPD_API_GET_STATS)

enum mpd_cmd_ids {
MPD_CMDS(GEN_ENUM)
Expand Down Expand Up @@ -123,6 +124,7 @@ int mpd_put_queue(char *buffer, unsigned int offset);
int mpd_put_browse(char *buffer, char *path, unsigned int offset);
int mpd_search(char *buffer, char *searchstr);
int mpd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char *searchstr);
int mympd_get_stats();
void mpd_disconnect();
#endif

0 comments on commit 1da825c

Please sign in to comment.