Stats.cxx 3.07 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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"
31
#include "util/ChronoUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
32

33
#include <chrono>
34
#include <cmath>
35

36
#ifndef _WIN32
37 38 39 40
/**
 * The monotonic time stamp when MPD was started.  It is used to
 * calculate the uptime.
 */
41 42
static const std::chrono::steady_clock::time_point start_time =
	std::chrono::steady_clock::now();
43 44
#endif

45 46
#ifdef ENABLE_DATABASE

47
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
48

49 50 51 52 53 54 55 56
enum class StatsValidity : uint8_t {
	INVALID, VALID, FAILED,
};

static StatsValidity stats_validity = StatsValidity::INVALID;

void
stats_invalidate()
57
{
58 59 60 61
	stats_validity = StatsValidity::INVALID;
}

static bool
62
stats_update(const Database &db)
63 64 65 66 67 68 69 70 71 72 73
{
	switch (stats_validity) {
	case StatsValidity::INVALID:
		break;

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
74

75 76
	const DatabaseSelection selection("", true);

77
	try {
78 79 80
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
81 82
	} catch (...) {
		LogError(std::current_exception());
83
		stats_validity = StatsValidity::FAILED;
84
		return false;
85
	}
86 87
}

88
static void
89
db_stats_print(Response &r, const Database &db)
Avuton Olrich's avatar
Avuton Olrich committed
90
{
91
	if (!stats_update(db))
92
		return;
93

94 95 96
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

97 98 99 100 101 102 103 104
	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);
105

106 107
	const auto update_stamp = db.GetUpdateStamp();
	if (!IsNegative(update_stamp))
108
		r.Format("db_update: %lu\n",
109
			 (unsigned long)std::chrono::system_clock::to_time_t(update_stamp));
Warren Dukes's avatar
Warren Dukes committed
110
}
111

112 113
#endif

114
void
115
stats_print(Response &r, const Partition &partition)
116
{
117 118
	r.Format("uptime: %u\n"
		 "playtime: %lu\n",
119
#ifdef _WIN32
120
		 GetProcessUptimeS(),
121
#else
122
		 (unsigned)std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start_time).count(),
123
#endif
124
		 std::lround(partition.pc.GetTotalPlayTime().count()));
125

126
#ifdef ENABLE_DATABASE
127
	const Database *db = partition.instance.database;
128
	if (db != nullptr)
129
		db_stats_print(r, *db);
130
#endif
131
}