Stats.cxx 3.06 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "PlayerControl.hxx"
23
#include "client/Client.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 "util/Error.hxx"
30
#include "system/Clock.hxx"
31
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
32

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

41 42
#ifdef ENABLE_DATABASE

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

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

static StatsValidity stats_validity = StatsValidity::INVALID;

51 52
#endif

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

60 61
#ifdef ENABLE_DATABASE

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

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

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
81

82
	Error error;
83

84
	const DatabaseSelection selection("", true);
85
	if (db.GetStats(selection, stats, error)) {
86 87
		stats_validity = StatsValidity::VALID;
		return true;
88
	} else {
89
		LogError(error);
90

91
		stats_validity = StatsValidity::FAILED;
92
		return false;
93
	}
94 95
}

96
static void
97
db_stats_print(Client &client, const Database &db)
Avuton Olrich's avatar
Avuton Olrich committed
98
{
99
	if (!stats_update(db))
100
		return;
101

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

105
	client_printf(client,
106 107
		      "artists: %u\n"
		      "albums: %u\n"
108
		      "songs: %u\n"
109
		      "db_playtime: %u\n",
110 111
		      stats.artist_count,
		      stats.album_count,
Max Kellermann's avatar
Max Kellermann committed
112
		      stats.song_count,
113
		      total_duration_s);
114

115
	const time_t update_stamp = db.GetUpdateStamp();
116
	if (update_stamp > 0)
117
		client_printf(client,
118
			      "db_update: %lu\n",
119
			      (unsigned long)update_stamp);
Warren Dukes's avatar
Warren Dukes committed
120
}
121

122 123
#endif

124 125 126 127
void
stats_print(Client &client)
{
	client_printf(client,
128
		      "uptime: %u\n"
129
		      "playtime: %lu\n",
130 131 132
#ifdef WIN32
		      GetProcessUptimeS(),
#else
133
		      MonotonicClockS() - start_time,
134
#endif
135 136
		      (unsigned long)(client.player_control.GetTotalPlayTime() + 0.5));

137
#ifdef ENABLE_DATABASE
138
	const Database *db = client.partition.instance.database;
139 140
	if (db != nullptr)
		db_stats_print(client, *db);
141
#endif
142
}