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"
24 25
#include "Partition.hxx"
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
26 27
#include "db/Selection.hxx"
#include "db/DatabasePlugin.hxx"
28
#include "util/Error.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
	Error error;
82

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

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

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

101
	client_printf(client,
102 103
		      "artists: %u\n"
		      "albums: %u\n"
104 105
		      "songs: %u\n"
		      "db_playtime: %lu\n",
106 107
		      stats.artist_count,
		      stats.album_count,
Max Kellermann's avatar
Max Kellermann committed
108
		      stats.song_count,
109
		      stats.total_duration);
110

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

118 119
#endif

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

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