2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-10-02 12:14:07 +02:00
|
|
|
#include "Stats.hxx"
|
2013-01-04 22:31:53 +01:00
|
|
|
#include "PlayerControl.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/Selection.hxx"
|
|
|
|
#include "db/DatabaseGlue.hxx"
|
|
|
|
#include "db/DatabasePlugin.hxx"
|
|
|
|
#include "db/DatabaseSimple.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-11-24 20:41:00 +01:00
|
|
|
#include "system/Clock.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-08-02 18:55:53 +02:00
|
|
|
|
2013-11-24 20:41:00 +01:00
|
|
|
#ifndef WIN32
|
2013-11-24 21:14:38 +01:00
|
|
|
/**
|
|
|
|
* The monotonic time stamp when MPD was started. It is used to
|
|
|
|
* calculate the uptime.
|
|
|
|
*/
|
|
|
|
static unsigned start_time;
|
2013-11-24 20:41:00 +01:00
|
|
|
#endif
|
|
|
|
|
2013-11-22 00:12:12 +01:00
|
|
|
static DatabaseStats stats;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2014-01-10 23:40:05 +01:00
|
|
|
enum class StatsValidity : uint8_t {
|
|
|
|
INVALID, VALID, FAILED,
|
|
|
|
};
|
|
|
|
|
|
|
|
static StatsValidity stats_validity = StatsValidity::INVALID;
|
|
|
|
|
2009-01-18 15:22:26 +01:00
|
|
|
void stats_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-11-24 20:41:00 +01:00
|
|
|
#ifndef WIN32
|
2013-11-24 21:14:38 +01:00
|
|
|
start_time = MonotonicClockS();
|
2013-11-24 20:41:00 +01:00
|
|
|
#endif
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:40:05 +01:00
|
|
|
void
|
|
|
|
stats_invalidate()
|
2009-01-18 15:40:28 +01:00
|
|
|
{
|
2013-11-22 00:23:17 +01:00
|
|
|
assert(GetDatabase() != nullptr);
|
|
|
|
|
2014-01-10 23:40:05 +01:00
|
|
|
stats_validity = StatsValidity::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
stats_update()
|
|
|
|
{
|
|
|
|
switch (stats_validity) {
|
|
|
|
case StatsValidity::INVALID:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StatsValidity::VALID:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case StatsValidity::FAILED:
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-18 15:40:50 +01:00
|
|
|
|
2014-01-10 23:40:05 +01:00
|
|
|
Error error;
|
2009-01-18 15:40:50 +01:00
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
const DatabaseSelection selection("", true);
|
2014-01-10 23:40:05 +01:00
|
|
|
if (GetDatabase()->GetStats(selection, stats, error)) {
|
|
|
|
stats_validity = StatsValidity::VALID;
|
|
|
|
return true;
|
2012-08-15 22:20:28 +02:00
|
|
|
} else {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2012-08-15 22:20:28 +02:00
|
|
|
|
2014-01-10 23:40:05 +01:00
|
|
|
stats_validity = StatsValidity::FAILED;
|
|
|
|
return true;
|
2012-08-15 22:20:28 +02:00
|
|
|
}
|
2009-01-18 15:40:28 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:23:17 +01:00
|
|
|
static void
|
|
|
|
db_stats_print(Client &client)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-11-22 00:23:17 +01:00
|
|
|
assert(GetDatabase() != nullptr);
|
|
|
|
|
2013-11-21 23:30:49 +01:00
|
|
|
if (!db_is_simple())
|
|
|
|
/* reload statistics if we're using the "proxy"
|
|
|
|
database plugin */
|
|
|
|
/* TODO: move this into the "proxy" database plugin as
|
|
|
|
an "idle" handler */
|
2014-01-10 23:40:05 +01:00
|
|
|
stats_invalidate();
|
|
|
|
|
|
|
|
if (!stats_update())
|
|
|
|
return;
|
2013-11-21 23:30:49 +01:00
|
|
|
|
2008-09-07 14:02:57 +02:00
|
|
|
client_printf(client,
|
2008-09-08 11:47:57 +02:00
|
|
|
"artists: %u\n"
|
|
|
|
"albums: %u\n"
|
2013-11-22 00:23:27 +01:00
|
|
|
"songs: %u\n"
|
|
|
|
"db_playtime: %lu\n",
|
2009-01-18 15:40:28 +01:00
|
|
|
stats.artist_count,
|
|
|
|
stats.album_count,
|
2009-01-18 15:22:26 +01:00
|
|
|
stats.song_count,
|
2013-11-22 00:12:12 +01:00
|
|
|
stats.total_duration);
|
2012-08-08 08:19:30 +02:00
|
|
|
|
2013-11-22 00:35:29 +01:00
|
|
|
const time_t update_stamp = GetDatabase()->GetUpdateStamp();
|
|
|
|
if (update_stamp > 0)
|
2012-08-08 08:19:30 +02:00
|
|
|
client_printf(client,
|
2013-11-21 23:34:32 +01:00
|
|
|
"db_update: %lu\n",
|
2013-11-22 00:35:29 +01:00
|
|
|
(unsigned long)update_stamp);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2013-11-22 00:23:17 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
stats_print(Client &client)
|
|
|
|
{
|
|
|
|
client_printf(client,
|
2013-11-24 20:41:00 +01:00
|
|
|
"uptime: %u\n"
|
2013-11-22 00:23:17 +01:00
|
|
|
"playtime: %lu\n",
|
2013-11-24 20:41:00 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
GetProcessUptimeS(),
|
|
|
|
#else
|
2013-11-24 21:14:38 +01:00
|
|
|
MonotonicClockS() - start_time,
|
2013-11-24 20:41:00 +01:00
|
|
|
#endif
|
2013-11-22 00:23:17 +01:00
|
|
|
(unsigned long)(client.player_control.GetTotalPlayTime() + 0.5));
|
|
|
|
|
|
|
|
if (GetDatabase() != nullptr)
|
|
|
|
db_stats_print(client);
|
|
|
|
}
|