Stats.cxx 2.92 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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"
Max Kellermann's avatar
Max Kellermann committed
31

32
#ifndef WIN32
33 34 35 36 37
/**
 * The monotonic time stamp when MPD was started.  It is used to
 * calculate the uptime.
 */
static unsigned start_time;
38 39
#endif

40 41
#ifdef ENABLE_DATABASE

42
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
43

44 45 46 47 48 49
enum class StatsValidity : uint8_t {
	INVALID, VALID, FAILED,
};

static StatsValidity stats_validity = StatsValidity::INVALID;

50 51
#endif

Max Kellermann's avatar
Max Kellermann committed
52
void stats_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
53
{
54
#ifndef WIN32
55
	start_time = MonotonicClockS();
56
#endif
Warren Dukes's avatar
Warren Dukes committed
57 58
}

59 60
#ifdef ENABLE_DATABASE

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

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

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
80

81 82
	const DatabaseSelection selection("", true);

83
	try {
84 85 86
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
87 88
	} catch (const std::runtime_error &e) {
		LogError(e);
89
		stats_validity = StatsValidity::FAILED;
90
		return false;
91
	}
92 93
}

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

100 101 102
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

103 104 105 106 107 108 109 110
	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);
111

112
	const time_t update_stamp = db.GetUpdateStamp();
113
	if (update_stamp > 0)
114 115
		r.Format("db_update: %lu\n",
			 (unsigned long)update_stamp);
Warren Dukes's avatar
Warren Dukes committed
116
}
117

118 119
#endif

120
void
121
stats_print(Response &r, const Partition &partition)
122
{
123 124
	r.Format("uptime: %u\n"
		 "playtime: %lu\n",
125
#ifdef WIN32
126
		 GetProcessUptimeS(),
127
#else
128
		 MonotonicClockS() - start_time,
129
#endif
130
		 (unsigned long)(partition.pc.GetTotalPlayTime() + 0.5));
131

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