2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 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"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "player/Control.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-02-01 00:26:34 +01:00
|
|
|
#include "Partition.hxx"
|
|
|
|
#include "Instance.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/Selection.hxx"
|
2014-02-19 22:54:52 +01:00
|
|
|
#include "db/Interface.hxx"
|
|
|
|
#include "db/Stats.hxx"
|
2013-11-24 20:41:00 +01:00
|
|
|
#include "system/Clock.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2017-02-11 22:18:28 +01:00
|
|
|
#include "util/ChronoUtil.hxx"
|
2012-08-02 18:55:53 +02:00
|
|
|
|
2016-12-28 10:20:29 +01:00
|
|
|
#include <chrono>
|
2018-09-22 19:08:03 +02:00
|
|
|
#include <cmath>
|
2016-12-28 10:20:29 +01:00
|
|
|
|
2017-12-12 10:22:20 +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.
|
|
|
|
*/
|
2016-12-28 10:20:29 +01:00
|
|
|
static const std::chrono::steady_clock::time_point start_time =
|
|
|
|
std::chrono::steady_clock::now();
|
2013-11-24 20:41:00 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
void
|
|
|
|
stats_invalidate()
|
2009-01-18 15:40:28 +01:00
|
|
|
{
|
2014-01-10 23:40:05 +01:00
|
|
|
stats_validity = StatsValidity::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-02-01 00:38:57 +01:00
|
|
|
stats_update(const Database &db)
|
2014-01-10 23:40:05 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
const DatabaseSelection selection("", true);
|
|
|
|
|
2016-03-18 18:46:43 +01:00
|
|
|
try {
|
2016-10-29 10:21:57 +02:00
|
|
|
stats = db.GetStats(selection);
|
|
|
|
stats_validity = StatsValidity::VALID;
|
|
|
|
return true;
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception());
|
2014-01-10 23:40:05 +01:00
|
|
|
stats_validity = StatsValidity::FAILED;
|
2014-02-01 00:36:36 +01:00
|
|
|
return false;
|
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
|
2015-08-06 22:10:25 +02:00
|
|
|
db_stats_print(Response &r, const Database &db)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-02-01 00:38:57 +01:00
|
|
|
if (!stats_update(db))
|
2014-01-10 23:40:05 +01:00
|
|
|
return;
|
2013-11-21 23:30:49 +01:00
|
|
|
|
2014-08-29 23:22:46 +02:00
|
|
|
unsigned total_duration_s =
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("artists: %u\n"
|
|
|
|
"albums: %u\n"
|
|
|
|
"songs: %u\n"
|
|
|
|
"db_playtime: %u\n",
|
|
|
|
stats.artist_count,
|
|
|
|
stats.album_count,
|
|
|
|
stats.song_count,
|
|
|
|
total_duration_s);
|
2012-08-08 08:19:30 +02:00
|
|
|
|
2017-02-11 22:18:28 +01:00
|
|
|
const auto update_stamp = db.GetUpdateStamp();
|
|
|
|
if (!IsNegative(update_stamp))
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("db_update: %lu\n",
|
2017-02-11 22:18:28 +01:00
|
|
|
(unsigned long)std::chrono::system_clock::to_time_t(update_stamp));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2013-11-22 00:23:17 +01:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
|
|
|
|
2013-11-22 00:23:17 +01:00
|
|
|
void
|
2015-08-06 22:10:25 +02:00
|
|
|
stats_print(Response &r, const Partition &partition)
|
2013-11-22 00:23:17 +01:00
|
|
|
{
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Format("uptime: %u\n"
|
|
|
|
"playtime: %lu\n",
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2015-08-06 22:10:25 +02:00
|
|
|
GetProcessUptimeS(),
|
2013-11-24 20:41:00 +01:00
|
|
|
#else
|
2016-12-28 10:20:29 +01:00
|
|
|
(unsigned)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start_time).count(),
|
2013-11-24 20:41:00 +01:00
|
|
|
#endif
|
2018-09-22 19:08:03 +02:00
|
|
|
std::lround(partition.pc.GetTotalPlayTime().count()));
|
2013-11-22 00:23:17 +01:00
|
|
|
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2019-02-20 20:48:20 +01:00
|
|
|
const Database *db = partition.instance.GetDatabase();
|
2014-02-01 00:38:57 +01:00
|
|
|
if (db != nullptr)
|
2015-08-06 22:10:25 +02:00
|
|
|
db_stats_print(r, *db);
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2013-11-22 00:23:17 +01:00
|
|
|
}
|