Added initial serialization from TL data to statistics data.
This commit is contained in:
parent
ca863bfb5b
commit
177a7eaf43
|
@ -8,20 +8,92 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "api/api_statistics.h"
|
#include "api/api_statistics.h"
|
||||||
|
|
||||||
#include "apiwrap.h"
|
#include "apiwrap.h"
|
||||||
|
#include "data/data_channel.h"
|
||||||
#include "data/data_peer.h"
|
#include "data/data_peer.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
[[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,
|
||||||
|
|
||||||
|
.recentMessageInteractions = std::move(recentMessages),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Statistics::Statistics(not_null<ApiWrap*> api)
|
Statistics::Statistics(not_null<ApiWrap*> api)
|
||||||
: _api(&api->instance()) {
|
: _api(&api->instance()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
rpl::producer<> Statistics::request(
|
rpl::producer<rpl::no_value, QString> Statistics::request(
|
||||||
not_null<PeerData*> peer) const {
|
not_null<PeerData*> peer) {
|
||||||
return rpl::never<>();
|
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
|
} // namespace Api
|
||||||
|
|
|
@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "data/data_statistics.h"
|
||||||
#include "mtproto/sender.h"
|
#include "mtproto/sender.h"
|
||||||
|
|
||||||
class ApiWrap;
|
class ApiWrap;
|
||||||
|
@ -18,10 +19,13 @@ class Statistics final {
|
||||||
public:
|
public:
|
||||||
explicit Statistics(not_null<ApiWrap*> api);
|
explicit Statistics(not_null<ApiWrap*> api);
|
||||||
|
|
||||||
[[nodiscard]] rpl::producer<> request(
|
[[nodiscard]] rpl::producer<rpl::no_value, QString> request(
|
||||||
not_null<PeerData*> peer) const;
|
not_null<PeerData*> peer);
|
||||||
|
|
||||||
|
[[nodiscard]] Data::ChannelStatistics channelStats() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Data::ChannelStatistics _channelStats;
|
||||||
MTP::Sender _api;
|
MTP::Sender _api;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user