From 0b64bd6544ebe0a5ffa5c5cb17d41b4bb990ac05 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 17 Sep 2025 15:19:44 -0400 Subject: [PATCH] refactor: Remove hardcoded csv path from ReplayFutureLiveBBOStream Add constructor overload to accept an input ViewStream --- .../BlueData/BlueDataUtility.cs | 6 +++ .../Live/ReplayFutureLiveBBOStream.cs | 43 ++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs index 6e7047d22..d69785728 100644 --- a/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs +++ b/src/DataStreamSuite/DataStreamCore/BlueData/BlueDataUtility.cs @@ -211,6 +211,12 @@ namespace BCG { return tsNanos.HasValue ? DateTime.UnixEpoch.Add(new TimeSpan(tsNanos.Value / 100)) : null; } + public static DateTime? GetDateTime(this IBlueRow row, string colName) => + GetDateTime(row[colName]); + + public static DateTime? GetDateTime(this IBlueRow row, int colNo) => + GetDateTime(row[colNo]); + public static DateTime? GetDateTime(object obj) { var res = _get_datetime_value(obj, DateTime.MinValue); if (res == DateTime.MinValue) { diff --git a/src/DataStreamSuite/SharedStreams/BlueOrb/MarketData/Live/ReplayFutureLiveBBOStream.cs b/src/DataStreamSuite/SharedStreams/BlueOrb/MarketData/Live/ReplayFutureLiveBBOStream.cs index 4f3e6de34..67357fdb5 100644 --- a/src/DataStreamSuite/SharedStreams/BlueOrb/MarketData/Live/ReplayFutureLiveBBOStream.cs +++ b/src/DataStreamSuite/SharedStreams/BlueOrb/MarketData/Live/ReplayFutureLiveBBOStream.cs @@ -2,11 +2,13 @@ using System; using System.Collections.Generic; using System.Linq; - using System.Text; - using System.Threading.Tasks; using BCG; + using Bts.DataStream; + + using Spark.Core.Disposables; + [DataStreamUserRespresentation("ReplayFutureLiveBBOStream")] public class ReplayFutureLiveBBOStream : ViewStream, BCG.Utilities.General.IWorker{ private class LastFireTime { @@ -20,9 +22,10 @@ private readonly ViewStreamDatabase _vsd; - private readonly ContractUtils.FutureContinousContract _fcc; private readonly LastFireTime _last_fire_time = new LastFireTime(); + readonly ViewStream source; private RowsWithPointer _rows = new RowsWithPointer(); + IDisposable? sourceSub; public ReplayFutureLiveBBOStream(string continous_contract) { Func iso_8601_now = () => { @@ -51,29 +54,47 @@ } private ReplayFutureLiveBBOStream(string continous_contract, bool noop) - : this(ViewStreamDatabase.instance, new ContractUtils.FutureContract(continous_contract)) - {} + : this(ViewStreamDatabase.instance, new ContractUtils.FutureContract(continous_contract)) { } public ReplayFutureLiveBBOStream(ViewStreamDatabase vsd, ContractUtils.FutureContract fc) { var fcc = fc.continous_constract; - Log.assert(fcc != null); + Log.notnull(fcc); _vsd = vsd; - _fcc = fcc; + source = new CSVReaderStream($"{SQLiteHelper.location}\\mbp1_{fcc.symbol}_small.csv", false); + } + + public ReplayFutureLiveBBOStream(ViewStreamDatabase vsd, ViewStream source) { + _vsd = vsd; + this.source = source; } protected override void myOnFirstSubscriber() { - var file_path = Path.Combine(SQLiteHelper.location, $"mbp1_{_fcc.symbol}_small.csv".ToLower()); - var csv_rd = new CSVReaderStream(file_path, false); - _rows.rows = csv_rd.rows.to_list().OrderBy(r => r.get("ts_event")).ToList(); - _rows.pointer = 0; _vsd.addWorker(this); + source.onChange += OnSourceChanged; + sourceSub = Disposable.Create(() => source.onChange -= OnSourceChanged); + SyncRows(); + + void SyncRows() { + if (source.rows is null) { + _rows.rows = new List(0); + return; + } + _rows.rows = source.rows.Cast().OrderBy(r => r.GetDateTime("ts_event")).ToList(); + } + + void OnSourceChanged(object obj) => + SyncRows(); } protected override void myOnLastSubscriber() { + Disposable.dispose(ref sourceSub); _vsd.removeWorker(this); } public void doWork() { + if (_rows.rows is null || _rows.rows.Count == 0) { + return; + } IBlueRow cand_row = _rows.rows[_rows.pointer]; string cand_ts_event = cand_row.get("ts_event"); DateTime cand_ts_event_dt = DateTime.Parse(cand_ts_event, null, System.Globalization.DateTimeStyles.RoundtripKind);