From f1380e581b81ff0c6cbf13826fe72ff60235a754 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 14 Oct 2021 14:29:06 +0200 Subject: [PATCH] TxFiatHistory: simplify logic --- src/utils/AppData.cpp | 6 +- src/utils/AppData.h | 2 +- src/utils/TxFiatHistory.cpp | 100 +++++++++++++++ .../{txfiathistory.h => TxFiatHistory.h} | 8 +- src/utils/WebsocketNotifier.h | 2 +- src/utils/txfiathistory.cpp | 117 ------------------ 6 files changed, 109 insertions(+), 126 deletions(-) create mode 100644 src/utils/TxFiatHistory.cpp rename src/utils/{txfiathistory.h => TxFiatHistory.h} (88%) delete mode 100644 src/utils/txfiathistory.cpp diff --git a/src/utils/AppData.cpp b/src/utils/AppData.cpp index 7db6e7b..09c0ddd 100644 --- a/src/utils/AppData.cpp +++ b/src/utils/AppData.cpp @@ -11,17 +11,13 @@ AppData::AppData(QObject *parent) this->initRestoreHeights(); auto genesis_timestamp = this->restoreHeights[NetworkType::Type::MAINNET]->data.firstKey(); - this->txFiatHistory = new TxFiatHistory(genesis_timestamp, Config::defaultConfigDir().path()); + this->txFiatHistory = new TxFiatHistory(genesis_timestamp, Config::defaultConfigDir().path(), this); connect(&websocketNotifier()->websocketClient, &WebsocketClient::connectionEstablished, this->txFiatHistory, &TxFiatHistory::onUpdateDatabase); connect(this->txFiatHistory, &TxFiatHistory::requestYear, [](int year){ QByteArray data = QString(R"({"cmd": "txFiatHistory", "data": {"year": %1}})").arg(year).toUtf8(); websocketNotifier()->websocketClient.sendMsg(data); }); - connect(this->txFiatHistory, &TxFiatHistory::requestYearMonth, [](int year, int month){ - QByteArray data = QString(R"({"cmd": "txFiatHistory", "data": {"year": %1, "month": %2}})").arg(year).arg(month).toUtf8(); - websocketNotifier()->websocketClient.sendMsg(data); - }); connect(websocketNotifier(), &WebsocketNotifier::CryptoRatesReceived, &this->prices, &Prices::cryptoPricesReceived); connect(websocketNotifier(), &WebsocketNotifier::FiatRatesReceived, &this->prices, &Prices::fiatPricesReceived); diff --git a/src/utils/AppData.h b/src/utils/AppData.h index a5415f3..2560b1b 100644 --- a/src/utils/AppData.h +++ b/src/utils/AppData.h @@ -9,7 +9,7 @@ #include #include "prices.h" -#include "txfiathistory.h" +#include "TxFiatHistory.h" #include "RestoreHeightLookup.h" class AppData : public QObject { diff --git a/src/utils/TxFiatHistory.cpp b/src/utils/TxFiatHistory.cpp new file mode 100644 index 0000000..a573463 --- /dev/null +++ b/src/utils/TxFiatHistory.cpp @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#include "TxFiatHistory.h" +#include "utils/Utils.h" + +TxFiatHistory::TxFiatHistory(int genesis_timestamp, const QString &configDirectory, QObject *parent) + : QObject(parent) + , m_genesis_timestamp(genesis_timestamp) + , m_databasePath(QString("%1/fiatHistory.db").arg(configDirectory)) +{ + this->loadDatabase(); +} + +double TxFiatHistory::get(int timestamp) { + QDateTime ts; + ts.setTime_t(timestamp); + auto key = ts.toString("yyyyMMdd"); + return this->get(key); // USD +} + +double TxFiatHistory::get(const QString &date) { + if (m_database.contains(date)) { + return m_database[date]; // USD + } + return 0.0; +} + +void TxFiatHistory::loadDatabase() { + if (!Utils::fileExists(m_databasePath)) { + return; + } + + m_database.clear(); + + QString contents = Utils::barrayToString(Utils::fileOpen(m_databasePath)); + for (auto &line: contents.split("\n")) { + line = line.trimmed(); + if (line.isEmpty()) { + continue; + } + QStringList spl = line.split(":"); + if (spl.length() == 2) { + m_database[spl.at(0)] = spl.at(1).toDouble(); + } + } +} + +void TxFiatHistory::writeDatabase() { + QString data; + for (const auto &line: m_database.toStdMap()) { + data += QString("%1:%2\n").arg(line.first).arg(QString::number(line.second)); + } + Utils::fileWrite(m_databasePath, data); +} + +void TxFiatHistory::onUpdateDatabase() { + // update local txFiatHistory database + if (m_initialized) { + return; + } + + QDateTime genesis; + genesis.setSecsSinceEpoch(m_genesis_timestamp); + QDate genesis_date = genesis.date(); + + QDate now = QDate::currentDate(); + + QSet missingYears; + for (QDate date = genesis_date; date <= now;) { + + if (!m_database.contains(this->dateToKey(date))) { + qInfo() << "TxFiatHistory: Can't find value for date: " << this->dateToKey(date); + missingYears << date.year(); + date.setDate(date.year()+1, 1, 1); + continue; + } + + date = date.addDays(1); + } + + for (const int year : missingYears) { + emit requestYear(year); + } + + m_initialized = true; +} + +QString TxFiatHistory::dateToKey(const QDate &date) { + return date.toString("yyyyMMdd"); +} + +void TxFiatHistory::onWSData(const QJsonObject &data) { + foreach(const QString &key, data.keys()) { + QJsonValue value = data.value(key); + m_database[key] = value.toDouble(); + } + + this->writeDatabase(); +} diff --git a/src/utils/txfiathistory.h b/src/utils/TxFiatHistory.h similarity index 88% rename from src/utils/txfiathistory.h rename to src/utils/TxFiatHistory.h index cbb7d80..87cd14f 100644 --- a/src/utils/txfiathistory.h +++ b/src/utils/TxFiatHistory.h @@ -4,6 +4,10 @@ #ifndef FEATHER_TXFIATHISTORY_H #define FEATHER_TXFIATHISTORY_H +#include +#include +#include + class TxFiatHistory : public QObject { Q_OBJECT @@ -18,14 +22,14 @@ public slots: signals: void requestYear(int year); - void requestYearMonth(int year, int month); private: void loadDatabase(); void writeDatabase(); + QString dateToKey(const QDate &date); + int m_genesis_timestamp; QString m_databasePath; - QString m_configDirectory; bool m_initialized = false; QMap m_database; }; diff --git a/src/utils/WebsocketNotifier.h b/src/utils/WebsocketNotifier.h index 9609603..c159e41 100644 --- a/src/utils/WebsocketNotifier.h +++ b/src/utils/WebsocketNotifier.h @@ -13,7 +13,7 @@ #include "prices.h" #include "widgets/RedditPost.h" #include "widgets/CCSEntry.h" -#include "txfiathistory.h" +#include "TxFiatHistory.h" class WebsocketNotifier : public QObject { Q_OBJECT diff --git a/src/utils/txfiathistory.cpp b/src/utils/txfiathistory.cpp deleted file mode 100644 index 73cd471..0000000 --- a/src/utils/txfiathistory.cpp +++ /dev/null @@ -1,117 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// Copyright (c) 2020-2021, The Monero Project. - -#include -#include - -#include "txfiathistory.h" -#include "utils/Utils.h" - -TxFiatHistory::TxFiatHistory(int genesis_timestamp, const QString &configDirectory, QObject *parent) - : QObject(parent) - , m_genesis_timestamp(genesis_timestamp) - , m_configDirectory(configDirectory) -{ - m_databasePath = QString("%1/fiatHistory.db").arg(configDirectory); - this->loadDatabase(); -} - -double TxFiatHistory::get(int timestamp) { - QDateTime ts; - ts.setTime_t(timestamp); - auto key = ts.toString("yyyyMMdd"); - return this->get(key); // USD -} - -double TxFiatHistory::get(const QString &date) { - if(m_database.contains(date)) - return m_database[date]; // USD - return 0.0; -} - -void TxFiatHistory::loadDatabase() { - if(!Utils::fileExists(m_databasePath)) - return; - - m_database.clear(); - QString contents = Utils::barrayToString(Utils::fileOpen(m_databasePath)); - for(auto &line: contents.split("\n")){ - line = line.trimmed(); - if(line.isEmpty()) continue; - auto spl = line.split(":"); - m_database[spl.at(0)] = spl.at(1).toDouble(); - } -} - -void TxFiatHistory::writeDatabase() { - QString data; - for(const auto &line: m_database.toStdMap()) - data += QString("%1:%2\n").arg(line.first).arg(QString::number(line.second)); - Utils::fileWrite(m_databasePath, data); -} - -void TxFiatHistory::onUpdateDatabase() { - // update local txFiatHistory database - if(m_initialized) return; - - QDateTime genesis; - genesis.setTime_t(m_genesis_timestamp); - auto genesis_date = genesis.date(); - - auto now = QDate::currentDate(); - auto nowKey = now.toString("yyyyMMdd"); - int year = genesis.toString("yyyy").toInt(); - auto yearCurrent = now.year(); - - // if current year is genesis year we'll refresh regardless. - if(yearCurrent == genesis_date.year()) { - emit requestYear(year); - m_initialized = true; - return; - } - - // keep local fiatTxHistory database up to date, loop for missing dates - for(; year != yearCurrent + 1; year += 1){ - for(int month = 1; month != 13; month++) { - if(year == yearCurrent && month == now.month() && now.day() == 1) break; - QDateTime _now; - _now.setDate(QDate(year, month, 1)); - if(_now.toSecsSinceEpoch() < m_genesis_timestamp) continue; - if(_now.toSecsSinceEpoch() > std::time(nullptr) - 86400) continue; - QString key = ""; - - // genesis year we'll only fetch once - if(year == genesis_date.year()){ - key = QString("%1%2%3").arg(year).arg(12).arg("31"); - if(!m_database.contains(key)) - emit requestYear(year); - break; - } - - auto _month = QString::number(month); - if(_month.length() == 1) - _month = QString("0%1").arg(_month); // how2fill - - key = QString("%1%2%3").arg(year).arg(_month).arg("01"); - if(!m_database.contains(key)){ - if(year != yearCurrent) { - emit requestYear(year); - break; - } else - emit requestYearMonth(year, month); - } else if (year == yearCurrent && month == now.month() && !m_database.contains(nowKey)) - emit requestYearMonth(year, month); - } - } - - m_initialized = true; -} - -void TxFiatHistory::onWSData(const QJsonObject &data) { - foreach(const QString &key, data.keys()) { - QJsonValue value = data.value(key); - m_database[key] = value.toDouble(); - } - - this->writeDatabase(); -}