Stats.cxx 2.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "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(Response &r, 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 106 107 108 109 110 111 112
	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);
113

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

120 121
#endif

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

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