Stats.cxx 2.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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"
Max Kellermann's avatar
Max Kellermann committed
31

32 33
#include <chrono>

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

43 44
#ifdef ENABLE_DATABASE

45
static DatabaseStats stats;
Warren Dukes's avatar
Warren Dukes committed
46

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

static StatsValidity stats_validity = StatsValidity::INVALID;

void
stats_invalidate()
55
{
56 57 58 59
	stats_validity = StatsValidity::INVALID;
}

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

	case StatsValidity::VALID:
		return true;

	case StatsValidity::FAILED:
		return false;
	}
72

73 74
	const DatabaseSelection selection("", true);

75
	try {
76 77 78
		stats = db.GetStats(selection);
		stats_validity = StatsValidity::VALID;
		return true;
79 80
	} catch (const std::runtime_error &e) {
		LogError(e);
81
		stats_validity = StatsValidity::FAILED;
82
		return false;
83
	}
84 85
}

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

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

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

104
	const time_t update_stamp = db.GetUpdateStamp();
105
	if (update_stamp > 0)
106 107
		r.Format("db_update: %lu\n",
			 (unsigned long)update_stamp);
Warren Dukes's avatar
Warren Dukes committed
108
}
109

110 111
#endif

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

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