mpd/src/db/Count.cxx

130 lines
3.2 KiB
C++
Raw Normal View History

/*
2021-01-01 19:54:25 +01:00
* Copyright 2003-2021 The Music Player Daemon Project
* 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"
#include "Partition.hxx"
#include "client/Response.hxx"
#include "song/LightSong.hxx"
2014-11-08 19:21:42 +01:00
#include "tag/Tag.hxx"
#include "tag/VisitFallback.hxx"
#include "TagPrint.hxx"
#include <fmt/format.h>
#include <functional>
2014-04-26 23:15:31 +02:00
#include <map>
struct SearchStats {
unsigned n_songs{0};
std::chrono::duration<std::uint64_t, SongTime::period> total_duration;
2014-04-27 22:27:28 +02:00
constexpr SearchStats()
: total_duration(0) {}
};
2014-04-26 23:15:31 +02:00
class TagCountMap : public std::map<std::string, SearchStats> {
};
static void
2018-01-21 11:33:53 +01:00
PrintSearchStats(Response &r, const SearchStats &stats) noexcept
{
unsigned total_duration_s =
std::chrono::duration_cast<std::chrono::seconds>(stats.total_duration).count();
r.Fmt(FMT_STRING("songs: {}\n"
"playtime: {}\n"),
stats.n_songs, total_duration_s);
}
2014-04-26 23:15:31 +02:00
static void
2018-01-21 11:33:53 +01:00
Print(Response &r, TagType group, const TagCountMap &m) noexcept
2014-04-26 23:15:31 +02:00
{
assert(unsigned(group) < TAG_NUM_OF_ITEM_TYPES);
for (const auto &[tag, stats] : m) {
tag_print(r, group, tag.c_str());
PrintSearchStats(r, stats);
2014-04-26 23:15:31 +02:00
}
}
static void
2018-01-21 11:33:53 +01:00
stats_visitor_song(SearchStats &stats, const LightSong &song) noexcept
{
stats.n_songs++;
if (const auto duration = song.GetDuration(); !duration.IsNegative())
stats.total_duration += duration;
}
static void
CollectGroupCounts(TagCountMap &map, const Tag &tag,
const char *value) noexcept
2014-04-26 23:15:31 +02:00
{
auto &s = map.emplace(value, SearchStats()).first->second;
++s.n_songs;
if (!tag.duration.IsNegative())
s.total_duration += tag.duration;
2014-04-26 23:15:31 +02:00
}
static void
2018-01-21 11:33:53 +01:00
GroupCountVisitor(TagCountMap &map, TagType group,
const LightSong &song) noexcept
2014-04-26 23:15:31 +02:00
{
const Tag &tag = song.tag;
VisitTagWithFallbackOrEmpty(tag, group, [&](const auto &val)
{ return CollectGroupCounts(map, tag, val); });
2014-04-26 23:15:31 +02:00
}
void
PrintSongCount(Response &r, const Partition &partition, const char *name,
const SongFilter *filter,
TagType group)
{
2016-10-26 18:47:19 +02:00
const Database &db = partition.GetDatabaseOrThrow();
const DatabaseSelection selection(name, true, filter);
2014-04-26 23:15:31 +02:00
if (group == TAG_NUM_OF_ITEM_TYPES) {
/* no grouping */
2014-04-26 23:15:31 +02:00
SearchStats stats;
const auto f = [&](const auto &song)
{ return stats_visitor_song(stats, song); };
db.Visit(selection, f);
2014-04-26 23:15:31 +02:00
PrintSearchStats(r, stats);
2014-04-26 23:15:31 +02:00
} else {
/* group by the specified tag: store counts in a
std::map */
TagCountMap map;
const auto f = [&map,group](const auto &song)
{ return GroupCountVisitor(map, group, song); };
db.Visit(selection, f);
2014-04-26 23:15:31 +02:00
Print(r, group, map);
2014-04-26 23:15:31 +02:00
}
}