stats.c 2.61 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * 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 "stats.h"
21
#include "database.h"
Max Kellermann's avatar
Max Kellermann committed
22
#include "tag.h"
23
#include "song.h"
24
#include "client.h"
25
#include "player_control.h"
26
#include "strset.h"
Warren Dukes's avatar
Warren Dukes committed
27

Max Kellermann's avatar
Max Kellermann committed
28
struct stats stats;
Warren Dukes's avatar
Warren Dukes committed
29

Max Kellermann's avatar
Max Kellermann committed
30
void stats_global_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
31
{
32 33 34 35 36 37
	stats.timer = g_timer_new();
}

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

40
struct visit_data {
41 42
	struct strset *artists;
	struct strset *albums;
43 44
};

45 46
static void
visit_tag(struct visit_data *data, const struct tag *tag)
47
{
48 49
	if (tag->time > 0)
		stats.song_duration += tag->time;
50

Max Kellermann's avatar
Max Kellermann committed
51
	for (unsigned i = 0; i < tag->num_items; ++i) {
52
		const struct tag_item *item = tag->items[i];
53

54
		switch (item->type) {
55
		case TAG_ARTIST:
56 57
			strset_add(data->artists, item->value);
			break;
58

59
		case TAG_ALBUM:
60 61 62 63 64 65 66
			strset_add(data->albums, item->value);
			break;

		default:
			break;
		}
	}
67 68
}

69 70
static int
stats_collect_song(struct song *song, void *_data)
71
{
72 73 74
	struct visit_data *data = _data;

	++stats.song_count;
75

76 77
	if (song->tag != NULL)
		visit_tag(data, song->tag);
78

79
	return 0;
80 81
}

82 83
void stats_update(void)
{
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	struct visit_data data;

	stats.song_count = 0;
	stats.song_duration = 0;
	stats.artist_count = 0;

	data.artists = strset_new();
	data.albums = strset_new();

	db_walk(NULL, stats_collect_song, NULL, &data);

	stats.artist_count = strset_size(data.artists);
	stats.album_count = strset_size(data.albums);

	strset_free(data.artists);
	strset_free(data.albums);
100 101
}

Max Kellermann's avatar
Max Kellermann committed
102
int stats_print(struct client *client)
Avuton Olrich's avatar
Avuton Olrich committed
103
{
104
	client_printf(client,
105 106
		      "artists: %u\n"
		      "albums: %u\n"
107 108 109 110 111
		      "songs: %i\n"
		      "uptime: %li\n"
		      "playtime: %li\n"
		      "db_playtime: %li\n"
		      "db_update: %li\n",
112 113
		      stats.artist_count,
		      stats.album_count,
Max Kellermann's avatar
Max Kellermann committed
114
		      stats.song_count,
115
		      (long)g_timer_elapsed(stats.timer, NULL),
116
		      (long)(pc_get_total_play_time() + 0.5),
Max Kellermann's avatar
Max Kellermann committed
117
		      stats.song_duration,
118
		      db_get_mtime());
Warren Dukes's avatar
Warren Dukes committed
119 120
	return 0;
}