2024-08-21 13:03:20 +00:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
using TestNetRewarder;
|
|
|
|
|
|
2024-08-21 11:59:54 +00:00
|
|
|
|
namespace MarketInsights
|
|
|
|
|
{
|
|
|
|
|
public class Updater
|
|
|
|
|
{
|
2024-08-21 13:03:20 +00:00
|
|
|
|
private readonly Random random = new Random();
|
2024-08-21 11:59:54 +00:00
|
|
|
|
private readonly AppState appState;
|
2024-08-21 13:03:20 +00:00
|
|
|
|
private readonly CancellationToken ct;
|
2024-08-21 11:59:54 +00:00
|
|
|
|
private readonly Tracker[] trackers;
|
2024-08-21 13:03:20 +00:00
|
|
|
|
private readonly AverageHistory averageHistory;
|
2024-08-21 11:59:54 +00:00
|
|
|
|
|
2024-08-21 13:03:20 +00:00
|
|
|
|
public Updater(AppState appState, ICodexContracts contracts, CancellationToken ct)
|
2024-08-21 11:59:54 +00:00
|
|
|
|
{
|
|
|
|
|
this.appState = appState;
|
2024-08-21 13:03:20 +00:00
|
|
|
|
this.ct = ct;
|
|
|
|
|
|
2024-08-21 11:59:54 +00:00
|
|
|
|
trackers = CreateTrackers();
|
2024-08-21 13:03:20 +00:00
|
|
|
|
averageHistory = new AverageHistory(appState, contracts, trackers.Max(t => t.NumberOfSegments));
|
2024-08-21 11:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Tracker[] CreateTrackers()
|
|
|
|
|
{
|
|
|
|
|
var tokens = appState.Config.TimeSegments.Split(";", StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
var nums = tokens.Select(t => Convert.ToInt32(t)).ToArray();
|
2024-08-21 13:03:20 +00:00
|
|
|
|
return nums.Select(n => new Tracker(n, averageHistory)).ToArray();
|
2024-08-21 11:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
2024-08-21 13:03:20 +00:00
|
|
|
|
Task.Run(Runner);
|
2024-08-21 11:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 13:03:20 +00:00
|
|
|
|
private async Task Runner()
|
2024-08-21 11:59:54 +00:00
|
|
|
|
{
|
2024-08-21 13:03:20 +00:00
|
|
|
|
var segmenter = new TimeSegmenter(
|
|
|
|
|
appState.Log,
|
|
|
|
|
segmentSize: appState.Config.UpdateInterval,
|
|
|
|
|
historyStartUtc: appState.Config.HistoryStartUtc,
|
|
|
|
|
handler: averageHistory
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
while (!ct.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
await segmenter.ProcessNextSegment();
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(3), ct);
|
|
|
|
|
|
|
|
|
|
var marketTimeSegments = trackers
|
|
|
|
|
.Select(t => t.CreateMarketTimeSegment())
|
|
|
|
|
.Where(t => t != null)
|
|
|
|
|
.Cast<MarketTimeSegment>()
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
appState.MarketOverview = new MarketOverview
|
|
|
|
|
{
|
|
|
|
|
TimeSegments = marketTimeSegments,
|
|
|
|
|
IsUpToDate = segmenter.IsRealtime,
|
|
|
|
|
LastUpdatedUtc = DateTime.UtcNow
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var r = random.Next(appState.Config.MaxRandomIntervalSeconds);
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(r), ct);
|
|
|
|
|
}
|
2024-08-21 11:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|