aboutsummaryrefslogtreecommitdiffstats
path: root/src/Stats.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Stats.cxx104
1 files changed, 71 insertions, 33 deletions
diff --git a/src/Stats.cxx b/src/Stats.cxx
index f224bdf49..39d371ace 100644
--- a/src/Stats.cxx
+++ b/src/Stats.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -20,85 +20,123 @@
#include "config.h"
#include "Stats.hxx"
#include "PlayerControl.hxx"
-#include "Client.hxx"
-#include "DatabaseSelection.hxx"
-#include "DatabaseGlue.hxx"
-#include "DatabasePlugin.hxx"
-#include "DatabaseSimple.hxx"
+#include "client/Client.hxx"
+#include "Partition.hxx"
+#include "Instance.hxx"
+#include "db/Selection.hxx"
+#include "db/Interface.hxx"
+#include "db/Stats.hxx"
#include "util/Error.hxx"
+#include "system/Clock.hxx"
#include "Log.hxx"
-#include <glib.h>
+#ifndef WIN32
+/**
+ * The monotonic time stamp when MPD was started. It is used to
+ * calculate the uptime.
+ */
+static unsigned start_time;
+#endif
+
+#ifdef ENABLE_DATABASE
-static GTimer *uptime;
static DatabaseStats stats;
+enum class StatsValidity : uint8_t {
+ INVALID, VALID, FAILED,
+};
+
+static StatsValidity stats_validity = StatsValidity::INVALID;
+
+#endif
+
void stats_global_init(void)
{
- uptime = g_timer_new();
+#ifndef WIN32
+ start_time = MonotonicClockS();
+#endif
}
-void stats_global_finish(void)
+#ifdef ENABLE_DATABASE
+
+void
+stats_invalidate()
{
- g_timer_destroy(uptime);
+ stats_validity = StatsValidity::INVALID;
}
-void stats_update(void)
+static bool
+stats_update(const Database &db)
{
- assert(GetDatabase() != nullptr);
+ switch (stats_validity) {
+ case StatsValidity::INVALID:
+ break;
- Error error;
+ case StatsValidity::VALID:
+ return true;
+
+ case StatsValidity::FAILED:
+ return false;
+ }
- DatabaseStats stats2;
+ Error error;
const DatabaseSelection selection("", true);
- if (GetDatabase()->GetStats(selection, stats2, error)) {
- stats = stats2;
+ if (db.GetStats(selection, stats, error)) {
+ stats_validity = StatsValidity::VALID;
+ return true;
} else {
LogError(error);
- stats.Clear();
+ stats_validity = StatsValidity::FAILED;
+ return false;
}
}
static void
-db_stats_print(Client &client)
+db_stats_print(Client &client, const Database &db)
{
- assert(GetDatabase() != nullptr);
+ if (!stats_update(db))
+ return;
- 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 */
- stats_update();
+ unsigned total_duration_s =
+ std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();
client_printf(client,
"artists: %u\n"
"albums: %u\n"
"songs: %u\n"
- "db_playtime: %lu\n",
+ "db_playtime: %u\n",
stats.artist_count,
stats.album_count,
stats.song_count,
- stats.total_duration);
+ total_duration_s);
- const time_t update_stamp = GetDatabase()->GetUpdateStamp();
+ const time_t update_stamp = db.GetUpdateStamp();
if (update_stamp > 0)
client_printf(client,
"db_update: %lu\n",
(unsigned long)update_stamp);
}
+#endif
+
void
stats_print(Client &client)
{
client_printf(client,
- "uptime: %lu\n"
+ "uptime: %u\n"
"playtime: %lu\n",
- (unsigned long)g_timer_elapsed(uptime, NULL),
+#ifdef WIN32
+ GetProcessUptimeS(),
+#else
+ MonotonicClockS() - start_time,
+#endif
(unsigned long)(client.player_control.GetTotalPlayTime() + 0.5));
- if (GetDatabase() != nullptr)
- db_stats_print(client);
+#ifdef ENABLE_DATABASE
+ const Database *db = client.partition.instance.database;
+ if (db != nullptr)
+ db_stats_print(client, *db);
+#endif
}