Stats.cxx 3.14 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "Log.hxx"
30
#include "time/ChronoUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
31

32 33 34 35
#ifdef _WIN32
#include "system/Clock.hxx"
#endif

36
#include <chrono>
37
#include <cmath>
38

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

48 49
#ifdef ENABLE_DATABASE

50
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
51

52 53 54 55 56 57 58 59
enum class StatsValidity : uint8_t {
	INVALID, VALID, FAILED,
};

static StatsValidity stats_validity = StatsValidity::INVALID;

void
stats_invalidate()
60
{
61 62 63 64
	stats_validity = StatsValidity::INVALID;
}

static bool
65
stats_update(const Database &db)
66 67 68 69 70 71 72 73 74 75 76
{
	switch (stats_validity) {
	case StatsValidity::INVALID:
		break;

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
77

78 79
	const DatabaseSelection selection("", true);

80
	try {
81 82 83
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
84 85
	} catch (...) {
		LogError(std::current_exception());
86
		stats_validity = StatsValidity::FAILED;
87
		return false;
88
	}
89 90
}

91
static void
92
db_stats_print(Response &r, const Database &db)
Avuton Olrich's avatar
Avuton Olrich committed
93
{
94
	if (!stats_update(db))
95
		return;
96

97 98 99
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

100 101 102 103 104 105 106 107
	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);
108

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

115 116
#endif

117
void
118
stats_print(Response &r, const Partition &partition)
119
{
120
#ifdef _WIN32
121
	const auto uptime = GetProcessUptimeS();
122
#else
123
	const auto uptime = std::chrono::steady_clock::now() - start_time;
124
#endif
125 126 127 128

	r.Format("uptime: %u\n"
		 "playtime: %lu\n",
		 (unsigned)std::chrono::duration_cast<std::chrono::seconds>(uptime).count(),
129
		 std::lround(partition.pc.GetTotalPlayTime().count()));
130

131
#ifdef ENABLE_DATABASE
132
	const Database *db = partition.instance.GetDatabase();
133
	if (db != nullptr)
134
		db_stats_print(r, *db);
135
#endif
136
}