refactor(market): extract pure price-series math to data/market_series.h + tests

Move the no-I/O, no-ImGui series helpers (resampleHistory, bucketBySeconds,
sparklineSeries, chartSeries) out of market_tab.cpp into an inline header under
data/ (mirroring data/portfolio.h) and cover them with a testMarketSeries()
group in test_phase4.cpp (resample block averaging + partial blocks, window
bucketing + guards, sparkline fallback, chart time-window filtering + live
timestamp synthesis). market_tab now calls data::sparklineSeries/chartSeries.
No behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-04 00:03:19 -05:00
parent 14b08d1358
commit a0fd9aff82
3 changed files with 175 additions and 84 deletions

99
src/data/market_series.h Normal file
View File

@@ -0,0 +1,99 @@
// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3
#pragma once
#include <vector>
#include <utility>
#include <ctime>
#include "wallet_state.h" // MarketInfo
// Pure (no-I/O, no-ImGui) price-series math backing the market chart and the portfolio-group
// sparklines. Kept header-only + inline so it can be unit-tested without the GUI (see
// tests/test_phase4.cpp), mirroring data/portfolio.h.
namespace dragonx {
namespace data {
// Resample a ~1-sample/minute price history to the given interval by averaging each block of K
// minute-samples into one point. interval: 0=min 1=hour 2=day 3=week 4=month.
inline std::vector<double> resampleHistory(const std::vector<double>& hist, int interval)
{
static const int kMins[5] = {1, 60, 1440, 10080, 43200};
int k = kMins[(interval >= 0 && interval < 5) ? interval : 0];
if (k <= 1) return hist;
std::vector<double> out;
for (size_t i = 0; i < hist.size(); i += (size_t)k) {
double sum = 0.0; size_t cnt = 0;
for (size_t j = i; j < hist.size() && j < i + (size_t)k; ++j) { sum += hist[j]; ++cnt; }
if (cnt) out.push_back(sum / (double)cnt);
}
return out;
}
// Bucket a timestamped (unix-seconds, price) series into fixed-width time windows, averaging the
// samples in each window into one point. Input is oldest->newest; output preserves that order.
inline std::vector<double> bucketBySeconds(
const std::vector<std::pair<std::time_t, double>>& series, long windowSec)
{
std::vector<double> out;
if (series.empty() || windowSec <= 0) return out;
long curBucket = 0; double sum = 0.0; int cnt = 0;
for (const auto& sample : series) {
long b = (long)(sample.first / windowSec);
if (cnt > 0 && b != curBucket) { out.push_back(sum / cnt); sum = 0.0; cnt = 0; }
curBucket = b;
sum += sample.second; ++cnt;
}
if (cnt > 0) out.push_back(sum / cnt);
return out;
}
// Resolve the price series backing a group's sparkline for the chosen interval:
// 0=minute -> the live in-session buffer; 1=hour -> intraday (5-min) data bucketed to hours;
// 2=day / 3=week / 4=month -> the ~1yr daily series bucketed accordingly.
// Falls back to the in-session buffer when the historical fetch hasn't populated yet.
inline std::vector<double> sparklineSeries(const MarketInfo& m, int interval)
{
const long kDay = 86400;
switch (interval) {
case 1: { auto v = bucketBySeconds(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; }
case 2: { auto v = bucketBySeconds(m.price_chart_daily, kDay); if (v.size() >= 2) return v; break; }
case 3: { auto v = bucketBySeconds(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; }
case 4: { auto v = bucketBySeconds(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; }
default: break; // minute interval, or no historical data yet -> live buffer below
}
return resampleHistory(m.price_history, interval);
}
// Timestamped price series backing the main chart for the selected RANGE (Live/1H/1D/1W/1M),
// ending at `now`. The interval buttons select a time window ending at `now`: 1H = last hour,
// 1D = last 24h, 1W = last 7 days, 1M = last 30 days. 1H/1D come from the 5-minute intraday
// series; 1W/1M from the daily series. Live (or an un-fetched/empty range) uses the minute buffer.
inline std::vector<std::pair<std::time_t, double>> chartSeries(const MarketInfo& m, int interval,
std::time_t now)
{
const long kDay = 86400;
auto lastWindow = [now](const std::vector<std::pair<std::time_t, double>>& src, long rangeSec) {
std::vector<std::pair<std::time_t, double>> out;
std::time_t cutoff = now - (std::time_t)rangeSec;
for (const auto& s : src) if (s.first >= cutoff) out.push_back(s);
return out;
};
switch (interval) {
case 1: { auto v = lastWindow(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; } // 1H
case 2: { auto v = lastWindow(m.price_chart_intraday, kDay); if (v.size() >= 2) return v; break; } // 1D
case 3: { auto v = lastWindow(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; } // 1W
case 4: { auto v = lastWindow(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; } // 1M
default: break;
}
std::vector<std::pair<std::time_t, double>> out;
const auto& h = m.price_history;
for (size_t i = 0; i < h.size(); i++)
out.push_back({ now - (std::time_t)((h.size() - 1 - i) * 60), h[i] });
return out;
}
} // namespace data
} // namespace dragonx

View File

@@ -8,6 +8,7 @@
#include "../../data/wallet_state.h"
#include "../../config/settings.h"
#include "../../data/portfolio.h"
#include "../../data/market_series.h"
#include "../../data/exchange_info.h"
#include "../../util/i18n.h"
#include "../schema/ui_schema.h"
@@ -174,89 +175,10 @@ static void pfEndScrollChild(effects::ScrollFadeShader& fade, ImDrawList* dl)
ImGui::EndChild(); // outer
}
// price_history is ~1 sample/minute; resample it to the group's chosen interval by averaging
// each block of K minute-samples into one point.
static std::vector<double> pfResampleHistory(const std::vector<double>& hist, int interval)
{
static const int kMins[5] = {1, 60, 1440, 10080, 43200};
int k = kMins[(interval >= 0 && interval < 5) ? interval : 0];
if (k <= 1) return hist;
std::vector<double> out;
for (size_t i = 0; i < hist.size(); i += (size_t)k) {
double sum = 0.0; size_t cnt = 0;
for (size_t j = i; j < hist.size() && j < i + (size_t)k; ++j) { sum += hist[j]; ++cnt; }
if (cnt) out.push_back(sum / (double)cnt);
}
return out;
}
// Bucket a timestamped (unix-seconds, price) series into fixed-width time windows, averaging the
// samples in each window into one point. Input is oldest->newest; output preserves that order.
static std::vector<double> pfBucketBySeconds(
const std::vector<std::pair<std::time_t, double>>& series, long windowSec)
{
std::vector<double> out;
if (series.empty() || windowSec <= 0) return out;
long curBucket = 0; double sum = 0.0; int cnt = 0;
for (const auto& sample : series) {
long b = (long)(sample.first / windowSec);
if (cnt > 0 && b != curBucket) { out.push_back(sum / cnt); sum = 0.0; cnt = 0; }
curBucket = b;
sum += sample.second; ++cnt;
}
if (cnt > 0) out.push_back(sum / cnt);
return out;
}
// Resolve the price series backing a group's sparkline for the chosen interval:
// 0=minute -> the live in-session buffer; 1=hour -> intraday (5-min) data bucketed to hours;
// 2=day / 3=week / 4=month -> the ~1yr daily series bucketed accordingly.
// Falls back to the in-session buffer when the historical fetch hasn't populated yet.
static std::vector<double> pfSparklineSeries(const MarketInfo& m, int interval)
{
const long kDay = 86400;
switch (interval) {
case 1: { auto v = pfBucketBySeconds(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; }
case 2: { auto v = pfBucketBySeconds(m.price_chart_daily, kDay); if (v.size() >= 2) return v; break; }
case 3: { auto v = pfBucketBySeconds(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; }
case 4: { auto v = pfBucketBySeconds(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; }
default: break; // minute interval, or no historical data yet -> live buffer below
}
return pfResampleHistory(m.price_history, interval);
}
// Main price-chart interval selector (same semantics as pfSparklineSeries):
// 0=Live (in-session minute buffer) 1=hour 2=day 3=week 4=month. Session-scoped.
// Main price-chart interval selector: 0=Live (in-session minute buffer) 1=hour 2=day 3=week
// 4=month. Session-scoped. The pure series math lives in data/market_series.h.
static int s_chart_interval = 4; // default: 1M (last 30 days)
// Timestamped price series backing the main chart for the selected RANGE (Live/1H/1D/1W/1M).
// The interval buttons select a time window ending at `now`: 1H = last hour, 1D = last 24h,
// 1W = last 7 days, 1M = last 30 days. 1H/1D come from the 5-minute intraday series; 1W/1M from
// the daily series. Live (or an un-fetched/empty range) uses the in-session minute buffer.
static std::vector<std::pair<std::time_t, double>> pfChartSeries(const MarketInfo& m, int interval,
std::time_t now)
{
const long kDay = 86400;
auto lastWindow = [now](const std::vector<std::pair<std::time_t, double>>& src, long rangeSec) {
std::vector<std::pair<std::time_t, double>> out;
std::time_t cutoff = now - (std::time_t)rangeSec;
for (const auto& s : src) if (s.first >= cutoff) out.push_back(s);
return out;
};
switch (interval) {
case 1: { auto v = lastWindow(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; } // 1H
case 2: { auto v = lastWindow(m.price_chart_intraday, kDay); if (v.size() >= 2) return v; break; } // 1D
case 3: { auto v = lastWindow(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; } // 1W
case 4: { auto v = lastWindow(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; } // 1M
default: break;
}
std::vector<std::pair<std::time_t, double>> out;
const auto& h = m.price_history;
for (size_t i = 0; i < h.size(); i++)
out.push_back({ now - (std::time_t)((h.size() - 1 - i) * 60), h[i] });
return out;
}
// A group's value trend tracks the DRGX price, so a group sparkline plots the price history
// (normalized) across a rect. Colored by overall direction. Only meaningful for market bases.
@@ -732,7 +654,7 @@ static void RenderPortfolioEditor(App* app)
pdl->AddRect(pMin, pMax, WithAlpha(accent, oa), 10.0f, 0, 2.0f);
}
if (s_pf_show_sparkline && (s_pf_price_basis == 0 || s_pf_price_basis == 1)) {
std::vector<double> h = pfSparklineSeries(state.market, s_pf_spark_interval);
std::vector<double> h = data::sparklineSeries(state.market, s_pf_spark_interval);
if (h.size() >= 2) {
ImU32 spCol = WithAlpha(h.back() >= h.front() ? Success() : Error(), 80);
ImVec2 spMin(pMin.x + ppad, pMin.y + ph * 0.46f);
@@ -1322,7 +1244,7 @@ void RenderMarketTab(App* app)
std::time_t nowSec = std::time(nullptr);
std::vector<std::time_t> chartTimes;
s_price_history.clear();
for (const auto& pr : pfChartSeries(market, s_chart_interval, nowSec)) {
for (const auto& pr : data::chartSeries(market, s_chart_interval, nowSec)) {
s_price_history.push_back(pr.second);
chartTimes.push_back(pr.first);
}
@@ -1993,7 +1915,7 @@ void RenderMarketTab(App* app)
// Body: sparkline fills from below the header to the card bottom.
if (e.showSparkline && (e.priceBasis == 0 || e.priceBasis == 1)) {
std::vector<double> hh = pfSparklineSeries(market, e.sparklineInterval);
std::vector<double> hh = data::sparklineSeries(market, e.sparklineInterval);
if (hh.size() >= 2) {
float sparkTop = std::max(leftBottom, rightBottom) + Layout::spacingXs();
float sparkBot = gcMax.y - hpad;