Stats.cxx 2.94 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"
Max Kellermann's avatar
Max Kellermann committed
24 25 26
#include "db/Selection.hxx"
#include "db/DatabaseGlue.hxx"
#include "db/DatabasePlugin.hxx"
27
#include "util/Error.hxx"
28
#include "system/Clock.hxx"
29
#include "Log.hxx"
Max Kellermann's avatar
Max Kellermann committed
30

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

39 40
#ifdef ENABLE_DATABASE

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

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

static StatsValidity stats_validity = StatsValidity::INVALID;

49 50
#endif

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

58 59
#ifdef ENABLE_DATABASE

60 61
void
stats_invalidate()
62
{
63 64
	assert(GetDatabase() != nullptr);

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
	client_printf(client,
103 104
		      "artists: %u\n"
		      "albums: %u\n"
105 106
		      "songs: %u\n"
		      "db_playtime: %lu\n",
107 108
		      stats.artist_count,
		      stats.album_count,
Max Kellermann's avatar
Max Kellermann committed
109
		      stats.song_count,
110
		      stats.total_duration);
111

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

119 120
#endif

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

134
#ifdef ENABLE_DATABASE
135 136 137
	const Database *db = GetDatabase();
	if (db != nullptr)
		db_stats_print(client, *db);
138
#endif
139
}