Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

9.9 KB
Raw Blame
// src/services/economics/EconomicsService.cpp
#include "services/economics/EconomicsService.h"

#include "core/logging/Logger.h"
#include "datahub/DataHub.h"
#include "datahub/DataHubMetaTypes.h"
#include "python/PythonRunner.h"
#include "storage/cache/CacheManager.h"

#include <QJsonDocument>
#include <QJsonParseError>
#include <QRegularExpression>

namespace fincept::services {

namespace {

/// Mask credentials a provider (or the HTTP library, which echoes the full request URL in its
/// exception text) copied into an error message before it reaches the UI or the log: query-string
/// keys ("...&api_key=SECRET&...") and BLS-style "The key:SECRET provided by the User is invalid".
QString econ_svc_redact_secrets(QString text) {
    static const QRegularExpression kQueryKey(
        QStringLiteral("\\b(api[_-]?key|apikey|access[_-]?token|token|key|userid|subscription-key|password|secret)=[^&\\s\"']+"),
        QRegularExpression::CaseInsensitiveOption);
    static const QRegularExpression kBlsKey(QStringLiteral("(the key:)\\S+"), QRegularExpression::CaseInsensitiveOption);
    text.replace(kQueryKey, QStringLiteral("\\1=***"));
    text.replace(kBlsKey, QStringLiteral("\\1***"));
    return text;
}

} // namespace

EconomicsService& EconomicsService::instance() {
    static EconomicsService inst;
    return inst;
}

EconomicsService::EconomicsService(QObject* parent) : QObject(parent) {}

// ── Cache ─────────────────────────────────────────────────────────────────────

// "v2" entries are {"fetched_at": <ISO UTC>, "data": {...}} so a cache hit can report when the data
// was actually retrieved. The prefix keeps pre-v2 entries (bare data objects) from being misread.
QString EconomicsService::cache_key(const QString& script, const QString& command, const QStringList& args) const {
    return "economics:v2:" + script + ":" + command + ":" + args.join(",");
}

// ── Execute ───────────────────────────────────────────────────────────────────

void EconomicsService::execute(const QString& source_id, const QString& script, const QString& command,
                               const QStringList& args, const QString& request_id) {
    const QString key = cache_key(script, command, args);

    // Remember the dispatch so hub-driven refresh() can replay it later.
    const QString topic = hub_topic(source_id, request_id);
    dispatch_records_.insert(topic, DispatchRecord{source_id, script, command, args, request_id});

    {
        const QVariant cached = fincept::CacheManager::instance().get(key);
        if (!cached.isNull()) {
            LOG_DEBUG("EconomicsService", "Cache hit: " + key);
            const QJsonObject wrapper = QJsonDocument::fromJson(cached.toString().toUtf8()).object();
            EconomicsResult res;
            res.success = true;
            res.data = wrapper.value(QStringLiteral("data")).toObject();
            res.fetched_at = QDateTime::fromString(wrapper.value(QStringLiteral("fetched_at")).toString(), Qt::ISODate);
            res.from_cache = true;
            res.source_id = source_id;
            emit result_ready(request_id, res);
            if (hub_registered_)
                fincept::datahub::DataHub::instance().publish(topic, QVariant::fromValue(res));
            return;
        }
    }

    QStringList full_args;
    full_args << command << args;

    LOG_INFO("EconomicsService", QString("Run %1 %2 [%3]").arg(script, command, args.join(", ")));

    QPointer<EconomicsService> self = this;
    python::PythonRunner::instance().run(
        script, full_args, [self, source_id, request_id, key](python::PythonResult py) {
            if (!self)
                return;

            EconomicsResult res;
            res.source_id = source_id;
            res.fetched_at = QDateTime::currentDateTimeUtc();

            // A failed refresh must clear the hub's in_flight flag, or the topic stays "busy" until the
            // scheduler's refresh_timeout fires and a retry is silently refused.
            auto report_failure = [self, &res, &source_id, &request_id]() {
                emit self->result_ready(request_id, res);
                if (self->hub_registered_)
                    fincept::datahub::DataHub::instance().publish_error(
                        EconomicsService::hub_topic(source_id, request_id), res.error);
            };

            if (!py.success) {
                res.success = false;
                res.error = econ_svc_redact_secrets(
                    py.error.isEmpty() ? QString("Script exited with code %1").arg(py.exit_code) : py.error);
                LOG_ERROR("EconomicsService", "Script failed: " + res.error);
                report_failure();
                return;
            }

            QString json_str = python::extract_json(py.output);
            if (json_str.isEmpty()) {
                res.success = false;
                res.error = "No JSON output from script";
                LOG_ERROR("EconomicsService", res.error);
                report_failure();
                return;
            }

            QJsonParseError err;
            QJsonDocument doc = QJsonDocument::fromJson(json_str.toUtf8(), &err);
            if (doc.isNull()) {
                res.success = false;
                res.error = "JSON parse error: " + err.errorString();
                LOG_ERROR("EconomicsService", res.error);
                report_failure();
                return;
            }

            // If the root is an array, wrap it
            if (doc.isArray())
                res.data = QJsonObject{{"data", doc.array()}};
            else
                res.data = doc.object();

            // Scripts echo provider/HTTP-library errors verbatim — scrub credentials before the text
            // is shown, logged or cached.
            for (const char* field : {"error", "message"}) {
                const QJsonValue v = res.data.value(QLatin1String(field));
                if (v.isString())
                    res.data[QLatin1String(field)] = econ_svc_redact_secrets(v.toString());
            }

            // Treat script-level error field as failure
            if (res.data.contains("error") && !res.data["error"].isNull() && !res.data["error"].toString().isEmpty() &&
                !res.data.contains("data")) {
                res.success = false;
                const QString error_code = res.data.value("error_code").toString();
                const QString message = res.data["error"].toString();
                // Prefix with [CODE] so panels can branch on it without a schema change.
                res.error = error_code.isEmpty() ? message
                                                 : (QStringLiteral("[") + error_code + QStringLiteral("] ") + message);
                report_failure();
                return;
            }

            res.success = true;
            const QJsonObject wrapper{{QStringLiteral("fetched_at"), res.fetched_at.toString(Qt::ISODate)},
                                      {QStringLiteral("data"), res.data}};
            fincept::CacheManager::instance().put(
                key, QVariant(QString::fromUtf8(QJsonDocument(wrapper).toJson(QJsonDocument::Compact))), kCacheTtlSec,
                "economics");
            LOG_INFO("EconomicsService", "Result ready: " + request_id);
            emit self->result_ready(request_id, res);
            if (self->hub_registered_) {
                const QString topic = EconomicsService::hub_topic(source_id, request_id);
                fincept::datahub::DataHub::instance().publish(topic, QVariant::fromValue(res));
            }
        });
}

// ── DataHub producer wiring ─────────────────────────────────────────────────

QString EconomicsService::hub_topic(const QString& source_id, const QString& request_id) {
    return QStringLiteral("econ:") + source_id + QLatin1Char(':') + request_id;
}

QStringList EconomicsService::topic_patterns() const {
    // Single broad pattern — individual source refactor to explicit
    // patterns can happen later once panels migrate. Keeps producer
    // registration simple across 32 upstream sources.
    return {QStringLiteral("econ:*")};
}

void EconomicsService::refresh(const QStringList& topics) {
    // Replay the last-known dispatch for each topic. If we've never
    // seen an execute() for this topic, nothing to do — we don't know
    // which script + args produce it.
    for (const auto& topic : topics) {
        auto it = dispatch_records_.constFind(topic);
        if (it == dispatch_records_.constEnd()) {
            LOG_DEBUG("EconomicsService", "refresh() for unknown topic (no prior execute): " + topic);
            continue;
        }
        const DispatchRecord rec = it.value();
        // Force re-fetch: clear CacheManager entry so execute() goes
        // through the Python path rather than returning the cached copy.
        fincept::CacheManager::instance().remove(cache_key(rec.script, rec.command, rec.args));
        execute(rec.source_id, rec.script, rec.command, rec.args, rec.request_id);
    }
}

int EconomicsService::max_requests_per_sec() const {
    return 2; // Python spawn pacing — upstream rate limits are usually higher
}

void EconomicsService::ensure_registered_with_hub() {
    if (hub_registered_)
        return;
    auto& hub = fincept::datahub::DataHub::instance();
    hub.register_producer(this);

    // 1-hour TTL, 60s min interval — economics series move slowly.
    fincept::datahub::TopicPolicy policy;
    policy.ttl_ms = 60 * 60 * 1000;
    policy.min_interval_ms = 60 * 1000;
    policy.push_only = false;
    hub.set_policy_pattern(QStringLiteral("econ:*"), policy);

    hub_registered_ = true;
    LOG_INFO("EconomicsService", "Registered with DataHub (econ:*)");
}

} // namespace fincept::services