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

33 34 35 36
#ifdef _WIN32
#include "system/Clock.hxx"
#endif

37 38
#include <fmt/format.h>

39 40
#include <chrono>

41
#ifndef _WIN32
42 43 44 45
/**
 * The monotonic time stamp when MPD was started.  It is used to
 * calculate the uptime.
 */
46 47
static const std::chrono::steady_clock::time_point start_time =
	std::chrono::steady_clock::now();
48 49
#endif

50 51
#ifdef ENABLE_DATABASE

52
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
53

54 55 56 57 58 59 60 61
enum class StatsValidity : uint8_t {
	INVALID, VALID, FAILED,
};

static StatsValidity stats_validity = StatsValidity::INVALID;

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

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

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
79

80 81
	const DatabaseSelection selection("", true);

82
	try {
83 84 85
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
86 87
	} catch (...) {
		LogError(std::current_exception());
88
		stats_validity = StatsValidity::FAILED;
89
		return false;
90
	}
91 92
}

93
static void
94
db_stats_print(Response &r, const Database &db)
Avuton Olrich's avatar
Avuton Olrich committed
95
{
96
	if (!stats_update(db))
97
		return;
98

99 100 101
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

102 103 104 105 106 107 108 109
	r.Fmt(FMT_STRING("artists: {}\n"
			 "albums: {}\n"
			 "songs: {}\n"
			 "db_playtime: {}\n"),
	      stats.artist_count,
	      stats.album_count,
	      stats.song_count,
	      total_duration_s);
110

111 112
	const auto update_stamp = db.GetUpdateStamp();
	if (!IsNegative(update_stamp))
113 114
		r.Fmt(FMT_STRING("db_update: {}\n"),
		      std::chrono::system_clock::to_time_t(update_stamp));
Warren Dukes's avatar
Warren Dukes committed
115
}
116

117 118
#endif

119
void
120
stats_print(Response &r, const Partition &partition)
121
{
122
#ifdef _WIN32
123
	const auto uptime = GetProcessUptimeS();
124
#else
125
	const auto uptime = std::chrono::steady_clock::now() - start_time;
126
#endif
127

128 129 130 131
	r.Fmt(FMT_STRING("uptime: {}\n"
			 "playtime: {}\n"),
	      std::chrono::duration_cast<std::chrono::seconds>(uptime).count(),
	      lround(partition.pc.GetTotalPlayTime().count()));
132

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