2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stats.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-09-06 20:28:31 +02:00
|
|
|
#include "tag.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-09-07 14:02:57 +02:00
|
|
|
#include "client.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2008-09-08 11:47:57 +02:00
|
|
|
#include "strset.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-18 15:22:26 +01:00
|
|
|
struct stats stats;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-18 15:22:26 +01:00
|
|
|
void stats_global_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-18 15:40:53 +01:00
|
|
|
stats.timer = g_timer_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stats_global_finish(void)
|
|
|
|
{
|
|
|
|
g_timer_destroy(stats.timer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-09-08 11:47:57 +02:00
|
|
|
struct visit_data {
|
2009-01-18 15:40:50 +01:00
|
|
|
struct strset *artists;
|
|
|
|
struct strset *albums;
|
2008-09-08 11:47:57 +02:00
|
|
|
};
|
|
|
|
|
2009-01-18 15:40:50 +01:00
|
|
|
static void
|
|
|
|
visit_tag(struct visit_data *data, const struct tag *tag)
|
2008-09-08 11:47:57 +02:00
|
|
|
{
|
2009-01-18 15:40:50 +01:00
|
|
|
if (tag->time > 0)
|
|
|
|
stats.song_duration += tag->time;
|
2008-09-08 11:47:57 +02:00
|
|
|
|
2009-02-27 09:01:55 +01:00
|
|
|
for (unsigned i = 0; i < tag->num_items; ++i) {
|
2009-01-18 15:40:50 +01:00
|
|
|
const struct tag_item *item = tag->items[i];
|
2008-09-08 11:47:57 +02:00
|
|
|
|
2009-01-18 15:40:50 +01:00
|
|
|
switch (item->type) {
|
2009-10-13 16:12:45 +02:00
|
|
|
case TAG_ARTIST:
|
2009-01-18 15:40:50 +01:00
|
|
|
strset_add(data->artists, item->value);
|
|
|
|
break;
|
2008-09-08 11:47:57 +02:00
|
|
|
|
2009-10-13 16:12:45 +02:00
|
|
|
case TAG_ALBUM:
|
2009-01-18 15:40:50 +01:00
|
|
|
strset_add(data->albums, item->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-08 11:47:57 +02:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:40:50 +01:00
|
|
|
static int
|
|
|
|
stats_collect_song(struct song *song, void *_data)
|
2008-09-08 11:47:57 +02:00
|
|
|
{
|
2009-01-18 15:40:50 +01:00
|
|
|
struct visit_data *data = _data;
|
|
|
|
|
|
|
|
++stats.song_count;
|
2008-09-08 11:47:57 +02:00
|
|
|
|
2009-01-18 15:40:50 +01:00
|
|
|
if (song->tag != NULL)
|
|
|
|
visit_tag(data, song->tag);
|
2008-09-08 11:47:57 +02:00
|
|
|
|
2009-01-18 15:40:50 +01:00
|
|
|
return 0;
|
2008-09-08 11:47:57 +02:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:40:28 +01:00
|
|
|
void stats_update(void)
|
|
|
|
{
|
2009-01-18 15:40:50 +01:00
|
|
|
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);
|
2009-01-18 15:40:28 +01:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:22:26 +01:00
|
|
|
int stats_print(struct client *client)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-09-07 14:02:57 +02:00
|
|
|
client_printf(client,
|
2008-09-08 11:47:57 +02:00
|
|
|
"artists: %u\n"
|
|
|
|
"albums: %u\n"
|
2008-09-07 14:02:57 +02:00
|
|
|
"songs: %i\n"
|
|
|
|
"uptime: %li\n"
|
|
|
|
"playtime: %li\n"
|
|
|
|
"db_playtime: %li\n"
|
|
|
|
"db_update: %li\n",
|
2009-01-18 15:40:28 +01:00
|
|
|
stats.artist_count,
|
|
|
|
stats.album_count,
|
2009-01-18 15:22:26 +01:00
|
|
|
stats.song_count,
|
2009-01-18 15:40:53 +01:00
|
|
|
(long)g_timer_elapsed(stats.timer, NULL),
|
2009-10-08 21:12:57 +02:00
|
|
|
(long)(pc_get_total_play_time() + 0.5),
|
2009-01-18 15:22:26 +01:00
|
|
|
stats.song_duration,
|
2008-10-08 11:07:55 +02:00
|
|
|
db_get_mtime());
|
2004-02-24 00:41:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|