Count.cxx 3.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "Count.hxx"
#include "Selection.hxx"
#include "Interface.hxx"
23 24
#include "Partition.hxx"
#include "client/Response.hxx"
25
#include "song/LightSong.hxx"
26
#include "tag/Tag.hxx"
27
#include "tag/VisitFallback.hxx"
28
#include "TagPrint.hxx"
29

30 31
#include <fmt/format.h>

32
#include <functional>
33
#include <map>
34 35

struct SearchStats {
36
	unsigned n_songs{0};
37
	std::chrono::duration<std::uint64_t, SongTime::period> total_duration;
38 39

	constexpr SearchStats()
40
		: total_duration(0) {}
41 42
};

43 44 45
class TagCountMap : public std::map<std::string, SearchStats> {
};

46
static void
47
PrintSearchStats(Response &r, const SearchStats &stats) noexcept
48
{
49 50 51
	unsigned total_duration_s =
		std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();

52 53 54
	r.Fmt(FMT_STRING("songs: {}\n"
			 "playtime: {}\n"),
	      stats.n_songs, total_duration_s);
55 56
}

57
static void
58
Print(Response &r, TagType group, const TagCountMap &m) noexcept
59 60 61
{
	assert(unsigned(group) < TAG_NUM_OF_ITEM_TYPES);

62 63 64
	for (const auto &[tag, stats] : m) {
		tag_print(r, group, tag.c_str());
		PrintSearchStats(r, stats);
65 66 67
	}
}

68
static void
69
stats_visitor_song(SearchStats &stats, const LightSong &song) noexcept
70 71
{
	stats.n_songs++;
72

73
	if (const auto duration = song.GetDuration(); !duration.IsNegative())
74
		stats.total_duration += duration;
75 76
}

77 78 79
static void
CollectGroupCounts(TagCountMap &map, const Tag &tag,
		   const char *value) noexcept
80
{
Rosen Penev's avatar
Rosen Penev committed
81
	auto &s = map.emplace(value, SearchStats()).first->second;
82 83 84
	++s.n_songs;
	if (!tag.duration.IsNegative())
		s.total_duration += tag.duration;
85 86
}

87
static void
88 89
GroupCountVisitor(TagCountMap &map, TagType group,
		  const LightSong &song) noexcept
90
{
91
	const Tag &tag = song.tag;
92 93
	VisitTagWithFallbackOrEmpty(tag, group, [&](const auto &val)
		{ return CollectGroupCounts(map, tag, val);  });
94 95
}

96
void
97
PrintSongCount(Response &r, const Partition &partition, const char *name,
98
	       const SongFilter *filter,
99
	       TagType group)
100
{
101
	const Database &db = partition.GetDatabaseOrThrow();
102 103 104

	const DatabaseSelection selection(name, true, filter);

105 106
	if (group == TAG_NUM_OF_ITEM_TYPES) {
		/* no grouping */
107

108 109
		SearchStats stats;

110 111 112
		const auto f = [&](const auto &song)
			{ return stats_visitor_song(stats, song); };

113
		db.Visit(selection, f);
114

115
		PrintSearchStats(r, stats);
116 117 118 119 120 121
	} else {
		/* group by the specified tag: store counts in a
		   std::map */

		TagCountMap map;

122 123 124
		const auto f = [&map,group](const auto &song)
			{ return GroupCountVisitor(map, group, song); };

125
		db.Visit(selection, f);
126

127
		Print(r, group, map);
128
	}
129
}