143 lines
3.9 KiB
C++
143 lines
3.9 KiB
C++
/*
|
|
This file is part of Telegram Desktop,
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
For license and copyright information please follow this link:
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
*/
|
|
#include "api/api_statistics.h"
|
|
|
|
#include "apiwrap.h"
|
|
#include "data/data_channel.h"
|
|
#include "data/data_peer.h"
|
|
#include "main/main_session.h"
|
|
#include "statistics/statistics_data_deserialize.h"
|
|
|
|
namespace Api {
|
|
namespace {
|
|
|
|
[[nodiscard]] Data::StatisticalGraph StatisticalGraphFromTL(
|
|
const MTPStatsGraph &tl) {
|
|
return tl.match([&](const MTPDstatsGraph &d) {
|
|
using namespace Statistic;
|
|
return Data::StatisticalGraph{
|
|
StatisticalChartFromJSON(qs(d.vjson().data().vdata()).toUtf8()),
|
|
};
|
|
}, [&](const MTPDstatsGraphAsync &data) {
|
|
return Data::StatisticalGraph{ Data::StatisticalChart() };
|
|
}, [&](const MTPDstatsGraphError &data) {
|
|
return Data::StatisticalGraph{ Data::StatisticalChart() };
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] Data::StatisticalValue StatisticalValueFromTL(
|
|
const MTPStatsAbsValueAndPrev &tl) {
|
|
const auto current = tl.data().vcurrent().v;
|
|
const auto previous = tl.data().vprevious().v;
|
|
return Data::StatisticalValue{
|
|
.value = current,
|
|
.previousValue = previous,
|
|
.growthRatePercentage = previous
|
|
? std::abs((current - previous) / float64(previous) * 100.)
|
|
: 0,
|
|
};
|
|
}
|
|
|
|
[[nodiscard]] Data::ChannelStatistics ChannelStatisticsFromTL(
|
|
const MTPDstats_broadcastStats &data) {
|
|
const auto &tlUnmuted = data.venabled_notifications().data();
|
|
const auto unmuted = (!tlUnmuted.vtotal().v)
|
|
? 0.
|
|
: std::clamp(
|
|
tlUnmuted.vpart().v / tlUnmuted.vtotal().v * 100.,
|
|
0.,
|
|
100.);
|
|
using Recent = MTPMessageInteractionCounters;
|
|
auto recentMessages = ranges::views::all(
|
|
data.vrecent_message_interactions().v
|
|
) | ranges::views::transform([&](const Recent &tl) {
|
|
return Data::StatisticsMessageInteractionInfo{
|
|
.messageId = tl.data().vmsg_id().v,
|
|
.viewsCount = tl.data().vviews().v,
|
|
.forwardsCount = tl.data().vforwards().v,
|
|
};
|
|
}) | ranges::to_vector;
|
|
|
|
return {
|
|
.startDate = data.vperiod().data().vmin_date().v,
|
|
.endDate = data.vperiod().data().vmax_date().v,
|
|
|
|
.memberCount = StatisticalValueFromTL(data.vfollowers()),
|
|
.meanViewCount = StatisticalValueFromTL(data.vviews_per_post()),
|
|
.meanShareCount = StatisticalValueFromTL(data.vshares_per_post()),
|
|
|
|
.enabledNotificationsPercentage = unmuted,
|
|
|
|
.memberCountGraph = StatisticalGraphFromTL(
|
|
data.vgrowth_graph()),
|
|
|
|
.joinGraph = StatisticalGraphFromTL(
|
|
data.vfollowers_graph()),
|
|
|
|
.muteGraph = StatisticalGraphFromTL(
|
|
data.vmute_graph()),
|
|
|
|
.viewCountByHourGraph = StatisticalGraphFromTL(
|
|
data.vtop_hours_graph()),
|
|
|
|
.viewCountBySourceGraph = StatisticalGraphFromTL(
|
|
data.vviews_by_source_graph()),
|
|
|
|
.joinBySourceGraph = StatisticalGraphFromTL(
|
|
data.vnew_followers_by_source_graph()),
|
|
|
|
.languageGraph = StatisticalGraphFromTL(
|
|
data.vlanguages_graph()),
|
|
|
|
.messageInteractionGraph = StatisticalGraphFromTL(
|
|
data.vinteractions_graph()),
|
|
|
|
.instantViewInteractionGraph = StatisticalGraphFromTL(
|
|
data.viv_interactions_graph()),
|
|
|
|
.recentMessageInteractions = std::move(recentMessages),
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Statistics::Statistics(not_null<ApiWrap*> api)
|
|
: _api(&api->instance()) {
|
|
}
|
|
|
|
rpl::producer<rpl::no_value, QString> Statistics::request(
|
|
not_null<PeerData*> peer) {
|
|
return [=](auto consumer) {
|
|
auto lifetime = rpl::lifetime();
|
|
const auto channel = peer->asChannel();
|
|
if (!channel) {
|
|
return lifetime;
|
|
}
|
|
|
|
if (!channel->isMegagroup()) {
|
|
_api.request(MTPstats_GetBroadcastStats(
|
|
MTP_flags(MTPstats_GetBroadcastStats::Flags(0)),
|
|
channel->inputChannel
|
|
)).done([=](const MTPstats_BroadcastStats &result) {
|
|
_channelStats = ChannelStatisticsFromTL(result.data());
|
|
consumer.put_done();
|
|
}).fail([=](const MTP::Error &error) {
|
|
consumer.put_error_copy(error.type());
|
|
}).send();
|
|
}
|
|
|
|
return lifetime;
|
|
};
|
|
}
|
|
|
|
Data::ChannelStatistics Statistics::channelStats() const {
|
|
return _channelStats;
|
|
}
|
|
|
|
} // namespace Api
|