You need to sign in or sign up before continuing.
Stats.cxx 3.05 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "Stats.hxx"
22
#include "player/Control.hxx"
23
#include "client/Response.hxx"
24 25
#include "Partition.hxx"
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "db/Selection.hxx"
27 28
#include "db/Interface.hxx"
#include "db/Stats.hxx"
29
#include "system/Clock.hxx"
30
#include "Log.hxx"
31
#include "util/ChronoUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
32

33 34
#include <chrono>

35
#ifndef _WIN32
36 37 38 39
/**
 * The monotonic time stamp when MPD was started.  It is used to
 * calculate the uptime.
 */
40 41
static const std::chrono::steady_clock::time_point start_time =
	std::chrono::steady_clock::now();
42 43
#endif

44 45
#ifdef ENABLE_DATABASE

46
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
47

48 49 50 51 52 53 54 55
enum class StatsValidity : uint8_t {
	INVALID, VALID, FAILED,
};

static StatsValidity stats_validity = StatsValidity::INVALID;

void
stats_invalidate()
56
{
57 58 59 60
	stats_validity = StatsValidity::INVALID;
}

static bool
61
stats_update(const Database &db)
62 63 64 65 66 67 68 69 70 71 72
{
	switch (stats_validity) {
	case StatsValidity::INVALID:
		break;

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
73

74 75
	const DatabaseSelection selection("", true);

76
	try {
77 78 79
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
80 81
	} catch (...) {
		LogError(std::current_exception());
82
		stats_validity = StatsValidity::FAILED;
83
		return false;
84
	}
85 86
}

87
static void
88
db_stats_print(Response &r, const Database &db)
Avuton Olrich's avatar
Avuton Olrich committed
89
{
90
	if (!stats_update(db))
91
		return;
92

93 94 95
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

96 97 98 99 100 101 102 103
	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);
104

105 106
	const auto update_stamp = db.GetUpdateStamp();
	if (!IsNegative(update_stamp))
107
		r.Format("db_update: %lu\n",
108
			 (unsigned long)std::chrono::system_clock::to_time_t(update_stamp));
Warren Dukes's avatar
Warren Dukes committed
109
}
110

111 112
#endif

113
void
114
stats_print(Response &r, const Partition &partition)
115
{
116 117
	r.Format("uptime: %u\n"
		 "playtime: %lu\n",
118
#ifdef _WIN32
119
		 GetProcessUptimeS(),
120
#else
121
		 (unsigned)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start_time).count(),
122
#endif
123
		 (unsigned long)(partition.pc.GetTotalPlayTime() + 0.5));
124

125
#ifdef ENABLE_DATABASE
126
	const Database *db = partition.instance.database;
127
	if (db != nullptr)
128
		db_stats_print(r, *db);
129
#endif
130
}