17 using System.Collections.Generic;
18 using System.Threading;
27 internal class ConsolidatorWrapper : IDisposable
30 private static long _counter;
33 private readonly LocalTimeKeeper _localTimeKeeper;
34 private readonly TimeSpan _minimumIncrement;
36 private readonly
long _id;
37 private TimeSpan? _barSpan;
42 public bool Disposed {
get;
private set; }
47 public DateTime UtcScanTime {
get;
private set; }
52 public ConsolidatorScanPriority Priority =>
new (UtcScanTime, _id);
57 public ConsolidatorWrapper(
IDataConsolidator consolidator, TimeSpan configIncrement,
ITimeKeeper timeKeeper, LocalTimeKeeper localTimeKeeper)
59 _id = Interlocked.Increment(ref _counter);
61 _timeKeeper = timeKeeper;
62 _consolidator = consolidator;
63 _localTimeKeeper = localTimeKeeper;
65 _minimumIncrement = configIncrement < Time.OneSecond ? Time.OneSecond : configIncrement;
76 _consolidator.Scan(_localTimeKeeper.LocalTime);
86 _consolidator.DataConsolidated -= AdvanceScanTime;
92 private void AdvanceScanTime(
object _ =
null, IBaseData consolidated =
null)
94 if (consolidated ==
null && UtcScanTime > _timeKeeper.UtcTime)
100 if (_barSpan.HasValue)
102 var reference = _timeKeeper.UtcTime;
103 if (consolidated !=
null)
105 reference = consolidated.EndTime.ConvertToUtc(_localTimeKeeper.TimeZone);
107 UtcScanTime = reference + _barSpan.Value;
111 if (consolidated !=
null)
113 _barSpan = consolidated.EndTime - consolidated.Time;
114 if (_barSpan < _minimumIncrement)
116 _barSpan = _minimumIncrement;
119 UtcScanTime = consolidated.EndTime.ConvertToUtc(_localTimeKeeper.TimeZone) + _barSpan.Value;
121 else if (_consolidator.WorkingData ==
null)
124 UtcScanTime = _timeKeeper.UtcTime + _minimumIncrement;
128 var pontetialEndTime = _consolidator.WorkingData.EndTime.ConvertToUtc(_localTimeKeeper.TimeZone);
129 if (pontetialEndTime > _timeKeeper.UtcTime)
131 UtcScanTime = pontetialEndTime;
135 UtcScanTime = _timeKeeper.UtcTime + _minimumIncrement;
142 internal class ConsolidatorScanPriority
144 private sealed
class UtcScanTimeIdRelationalComparer : IComparer<ConsolidatorScanPriority>
146 public int Compare(ConsolidatorScanPriority? x, ConsolidatorScanPriority? y)
148 if (ReferenceEquals(x, y))
return 0;
149 if (y is
null)
return 1;
150 if (x is
null)
return -1;
151 var utcScanTimeComparison = x.UtcScanTime.CompareTo(y.UtcScanTime);
152 if (utcScanTimeComparison != 0)
return utcScanTimeComparison;
153 return x.Id.CompareTo(y.Id);
157 public static IComparer<ConsolidatorScanPriority> Comparer {
get; } =
158 new UtcScanTimeIdRelationalComparer();
163 public DateTime UtcScanTime {
get; }
168 public long Id {
get; }
170 public ConsolidatorScanPriority(DateTime utcScanTime,
long id)
173 UtcScanTime = utcScanTime;