Stats.cxx 2.22 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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"
Max Kellermann's avatar
Max Kellermann committed
21 22

extern "C" {
Warren Dukes's avatar
Warren Dukes committed
23
#include "stats.h"
Max Kellermann's avatar
Max Kellermann committed
24 25
}

26
#include "PlayerControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "ClientInternal.hxx"
28
#include "DatabaseSelection.hxx"
Max Kellermann's avatar
Max Kellermann committed
29 30
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
31
#include "DatabaseSimple.hxx"
Max Kellermann's avatar
Max Kellermann committed
32

Max Kellermann's avatar
Max Kellermann committed
33
struct stats stats;
Warren Dukes's avatar
Warren Dukes committed
34

Max Kellermann's avatar
Max Kellermann committed
35
void stats_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
36
{
37 38 39 40 41 42
	stats.timer = g_timer_new();
}

void stats_global_finish(void)
{
	g_timer_destroy(stats.timer);
Warren Dukes's avatar
Warren Dukes committed
43 44
}

45 46
void stats_update(void)
{
47
	GError *error = nullptr;
48

49
	DatabaseStats stats2;
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	const DatabaseSelection selection("", true);
	if (GetDatabase()->GetStats(selection, stats2, &error)) {
		stats.song_count = stats2.song_count;
		stats.song_duration = stats2.total_duration;
		stats.artist_count = stats2.artist_count;
		stats.album_count = stats2.album_count;
	} else {
		g_warning("%s", error->message);
		g_error_free(error);

		stats.song_count = 0;
		stats.song_duration = 0;
		stats.artist_count = 0;
		stats.album_count = 0;
	}
66 67
}

68
void
69
stats_print(Client *client)
Avuton Olrich's avatar
Avuton Olrich committed
70
{
71
	client_printf(client,
72 73
		      "artists: %u\n"
		      "albums: %u\n"
74 75 76
		      "songs: %i\n"
		      "uptime: %li\n"
		      "playtime: %li\n"
77
		      "db_playtime: %li\n",
78 79
		      stats.artist_count,
		      stats.album_count,
Max Kellermann's avatar
Max Kellermann committed
80
		      stats.song_count,
81
		      (long)g_timer_elapsed(stats.timer, NULL),
82
		      (long)(client->player_control->GetTotalPlayTime() + 0.5),
83 84 85 86 87 88
		      stats.song_duration);

	if (db_is_simple())
		client_printf(client,
			      "db_update: %li\n",
			      (long)db_get_mtime());
Warren Dukes's avatar
Warren Dukes committed
89
}