2023-04-26 19:06:54 +00:00
|
|
|
/*
|
|
|
|
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 "statistics/statistics_box.h"
|
|
|
|
|
2023-05-24 15:30:30 +00:00
|
|
|
#include "api/api_statistics.h"
|
2023-04-26 19:06:54 +00:00
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2023-05-24 15:30:30 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "statistics/chart_widget.h"
|
2023-09-08 13:27:29 +00:00
|
|
|
#include "statistics/statistics_common.h"
|
2023-08-24 17:40:58 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2023-04-26 19:06:54 +00:00
|
|
|
#include "ui/layers/generic_box.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
|
2023-05-24 15:30:30 +00:00
|
|
|
const auto chartWidget = box->addRow(
|
|
|
|
object_ptr<Statistic::ChartWidget>(box));
|
2023-09-08 09:12:34 +00:00
|
|
|
const auto chartWidget2 = box->addRow(
|
|
|
|
object_ptr<Statistic::ChartWidget>(box));
|
|
|
|
const auto chartWidget3 = box->addRow(
|
|
|
|
object_ptr<Statistic::ChartWidget>(box));
|
|
|
|
const auto chartWidget4 = box->addRow(
|
|
|
|
object_ptr<Statistic::ChartWidget>(box));
|
|
|
|
const auto chartWidget5 = box->addRow(
|
|
|
|
object_ptr<Statistic::ChartWidget>(box));
|
2023-05-24 15:30:30 +00:00
|
|
|
const auto api = chartWidget->lifetime().make_state<Api::Statistics>(
|
|
|
|
&peer->session().api());
|
|
|
|
|
2023-07-27 04:08:05 +00:00
|
|
|
const auto processZoom = [=](
|
|
|
|
not_null<Statistic::ChartWidget*> widget,
|
2023-09-08 13:27:29 +00:00
|
|
|
const QString &zoomToken,
|
|
|
|
Statistic::ChartViewType type) {
|
2023-07-27 04:08:05 +00:00
|
|
|
if (!zoomToken.isEmpty()) {
|
|
|
|
widget->zoomRequests(
|
|
|
|
) | rpl::start_with_next([=](float64 x) {
|
|
|
|
api->requestZoom(
|
|
|
|
peer,
|
|
|
|
zoomToken,
|
|
|
|
x
|
|
|
|
) | rpl::start_with_next_error_done([=](
|
|
|
|
const Data::StatisticalGraph &graph) {
|
|
|
|
if (graph.chart) {
|
2023-09-08 13:27:29 +00:00
|
|
|
widget->setZoomedChartData(graph.chart, x, type);
|
2023-08-24 17:40:58 +00:00
|
|
|
} else if (!graph.error.isEmpty()) {
|
|
|
|
Ui::Toast::Show(
|
|
|
|
box->uiShow()->toastParent(),
|
|
|
|
graph.error);
|
2023-07-27 04:08:05 +00:00
|
|
|
}
|
|
|
|
}, [=](const QString &error) {
|
|
|
|
}, [=] {
|
|
|
|
}, widget->lifetime());
|
|
|
|
}, widget->lifetime());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-08 09:12:34 +00:00
|
|
|
const auto processChart = [=](
|
|
|
|
not_null<Statistic::ChartWidget*> widget,
|
|
|
|
const Data::StatisticalGraph &graphData,
|
2023-09-08 13:27:29 +00:00
|
|
|
rpl::producer<QString> &&title,
|
|
|
|
Statistic::ChartViewType type) {
|
2023-09-08 09:12:34 +00:00
|
|
|
if (graphData.chart) {
|
2023-09-08 13:27:29 +00:00
|
|
|
widget->setChartData(graphData.chart, type);
|
|
|
|
processZoom(widget, graphData.zoomToken, type);
|
2023-09-08 09:12:34 +00:00
|
|
|
widget->setTitle(std::move(title));
|
|
|
|
} else if (!graphData.zoomToken.isEmpty()) {
|
|
|
|
api->requestZoom(
|
|
|
|
peer,
|
|
|
|
graphData.zoomToken,
|
|
|
|
0
|
|
|
|
) | rpl::start_with_next_error_done([=](
|
|
|
|
const Data::StatisticalGraph &graph) {
|
|
|
|
if (graph.chart) {
|
2023-09-08 13:27:29 +00:00
|
|
|
widget->setChartData(graph.chart, type);
|
|
|
|
processZoom(widget, graph.zoomToken, type);
|
2023-09-08 09:12:34 +00:00
|
|
|
widget->setTitle(rpl::duplicate(title));
|
|
|
|
} else if (!graph.error.isEmpty()) {
|
|
|
|
Ui::Toast::Show(
|
|
|
|
box->uiShow()->toastParent(),
|
|
|
|
graph.error);
|
|
|
|
}
|
|
|
|
}, [=](const QString &error) {
|
|
|
|
}, [=] {
|
|
|
|
}, widget->lifetime());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-24 15:30:30 +00:00
|
|
|
api->request(
|
|
|
|
peer
|
|
|
|
) | rpl::start_with_done([=] {
|
2023-09-08 13:27:29 +00:00
|
|
|
const auto stats = api->channelStats();
|
|
|
|
if (!stats) {
|
|
|
|
return;
|
2023-05-24 15:30:30 +00:00
|
|
|
}
|
2023-09-08 13:27:29 +00:00
|
|
|
using Type = Statistic::ChartViewType;
|
|
|
|
processChart(
|
|
|
|
chartWidget,
|
|
|
|
stats.memberCountGraph,
|
|
|
|
tr::lng_chart_title_member_count(),
|
|
|
|
Type::Linear);
|
|
|
|
processChart(
|
|
|
|
chartWidget2,
|
|
|
|
stats.joinGraph,
|
|
|
|
tr::lng_chart_title_join(),
|
|
|
|
Type::Linear);
|
|
|
|
processChart(
|
|
|
|
chartWidget3,
|
|
|
|
stats.muteGraph,
|
|
|
|
tr::lng_chart_title_mute(),
|
|
|
|
Type::Linear);
|
|
|
|
processChart(
|
|
|
|
chartWidget4,
|
|
|
|
stats.viewCountBySourceGraph,
|
|
|
|
tr::lng_chart_title_view_count_by_source(),
|
|
|
|
Type::Stack);
|
|
|
|
processChart(
|
|
|
|
chartWidget5,
|
|
|
|
stats.joinBySourceGraph,
|
|
|
|
tr::lng_chart_title_join_by_source(),
|
|
|
|
Type::Stack);
|
2023-05-24 15:30:30 +00:00
|
|
|
}, chartWidget->lifetime());
|
2023-04-26 19:06:54 +00:00
|
|
|
box->setTitle(tr::lng_stats_title());
|
|
|
|
}
|