From c0219cb95d51c0da21b330c4ae62c8dbb154bbaa Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 8 Sep 2023 17:18:42 +0300 Subject: [PATCH] Added deserialization from JSON to statistics data. --- Telegram/CMakeLists.txt | 2 + .../statistics_data_deserialize.cpp | 94 +++++++++++++++++++ .../statistics/statistics_data_deserialize.h | 21 +++++ 3 files changed, 117 insertions(+) create mode 100644 Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp create mode 100644 Telegram/SourceFiles/statistics/statistics_data_deserialize.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 7be00c5e5..e3b10de2b 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1286,6 +1286,8 @@ PRIVATE statistics/segment_tree.h statistics/statistics_box.cpp statistics/statistics_box.h + statistics/statistics_data_deserialize.cpp + statistics/statistics_data_deserialize.h storage/details/storage_file_utilities.cpp storage/details/storage_file_utilities.h storage/details/storage_settings_scheme.cpp diff --git a/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp b/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp new file mode 100644 index 000000000..d0d8543f7 --- /dev/null +++ b/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp @@ -0,0 +1,94 @@ +/* +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_data_deserialize.h" + +#include "data/data_statistics.h" + +#include +#include +#include +#include + +namespace Statistic { + +Data::StatisticalChart StatisticalChartFromJSON(const QByteArray &json) { + auto error = QJsonParseError{ 0, QJsonParseError::NoError }; + const auto document = QJsonDocument::fromJson(json, &error); + if (error.error != QJsonParseError::NoError || !document.isObject()) { + LOG(("API Error: Bad stats graph json received.")); + return {}; + } + const auto root = document.object(); + const auto columns = root.value(u"columns"_q).toArray(); + if (columns.empty()) { + LOG(("API Error: Empty columns list from stats graph received.")); + return {}; + } + auto result = Data::StatisticalChart(); + for (const auto &column : columns) { + const auto array = column.toArray(); + if (array.empty()) { + LOG(("API Error: Empty column from stats graph received.")); + return {}; + } + const auto columnId = array.first().toString(); + if (columnId == u"x"_q) { + const auto length = array.size() - 1; + result.x.resize(length); + for (auto i = 0; i < length; i++) { + result.x[i] = array.at(i + 1).toDouble(); + } + } else { + auto line = Data::StatisticalChart::Line(); + const auto length = array.size() - 1; + line.id = columnId; + line.y.resize(length); + for (auto i = 0; i < length; i++) { + const auto value = array.at(i + 1).toInt(); + line.y[i] = value; + if (value > line.maxValue) { + line.maxValue = value; + } + if (value < line.minValue) { + line.minValue = value; + } + } + result.lines.push_back(std::move(line)); + } + if (result.x.size() > 1) { + result.timeStep = result.x[1] - result.x[0]; + } else { + constexpr auto kOneDay = 3600 * 24 * 1000; + result.timeStep = kOneDay; + } + result.measure(); + } + const auto colors = root.value(u"colors"_q).toObject(); + const auto names = root.value(u"names"_q).toObject(); + + const auto colorPattern = u"(.*)(#.*)"_q; + for (auto &line : result.lines) { + const auto colorIt = colors.constFind(line.id); + if (colorIt != colors.constEnd() && (*colorIt).isString()) { + const auto match = QRegularExpression(colorPattern).match( + colorIt->toString()); + if (match.hasMatch()) { + // const auto colorKey = match.captured(1); + line.color = QColor(match.captured(2)); + } + } + const auto nameIt = names.constFind(line.id); + if (nameIt != names.constEnd() && (*nameIt).isString()) { + line.name = nameIt->toString().replace('-', QChar(8212)); + } + } + + return result; +} + +} // namespace Statistic diff --git a/Telegram/SourceFiles/statistics/statistics_data_deserialize.h b/Telegram/SourceFiles/statistics/statistics_data_deserialize.h new file mode 100644 index 000000000..8e04b70a7 --- /dev/null +++ b/Telegram/SourceFiles/statistics/statistics_data_deserialize.h @@ -0,0 +1,21 @@ +/* +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 +*/ +#pragma once + +namespace Data { +struct StatisticalChart; +} // namespace Data + +class QByteArray; + +namespace Statistic { + +[[nodiscard]] Data::StatisticalChart StatisticalChartFromJSON( + const QByteArray &json); + +} // namespace Statistic