1

As said in this previous question, I am using the Reactive library in a C# project to group incoming data according to pre-configured policies. All these data implement the following interface:

public interface IPoint
{
    object Value { get; }
    DateTimeOffset Timestamp { get; }
}

My goal is to implement a "hopping" buffer based on the Timestamps of the received data (both buffer size and hop/shift size are declared at the beginning as TimeSpans). Hop/shift size can be less than the buffer size, which means that some IPoint instances can belong to more than one group.

An example: considering the following IPoint

Value: 1, Timestamp: "2021-05-25T00:00:01"
Value: 2, Timestamp: "2021-05-25T00:00:02"
Value: 3, Timestamp: "2021-05-25T00:00:03"
Value: 4, Timestamp: "2021-05-25T00:00:04"
Value: 5, Timestamp: "2021-05-25T00:00:05"
  • with a buffer size of 3 seconds and a hop/shift size of 2 seconds, I am expecting them to be grouped as [1, 2, 3], [3, 4, 5].
  • with a buffer size of 2 seconds and a hop/shift size of 3 seconds, I am expecting them to be grouped as [1, 2], [4, 5]

I've seen that there is a Buffer(timeSpan, timeShift) extension doing this job, but it considers a runtime-calculated timestamp instead of the ones of the passed IPoints.

I've tried to look for a solution, but I couldn't find anything helpful.

I am a newbie at Reactive, so any helpful comment is welcome (also for the other question). Thank you.

Edit: as in the previous question, I am using an ISubject<IPoint> in this way:

ISubject<IPoint> subject = new Subject<IPoint>();
// ...
// when new data come from an external source
public void Add(IPoint newPoint)
{
    subject.OnNext(newPoint);
}

// subscription made by another class in order to be called when "hopping" buffer is ready
public void Subscribe(Action<IEnumerable<IPoint>> callback)
{
    // TODO: implement buffer + .Subscribe(callback)
}
maradev21
  • 576
  • 2
  • 6
  • 20
  • Rx doesn't depend on real time. You can specify a different Scheduler and eg advance time during testing. It looks like what you really need, both in this questioni and the previous one, is to use `Tiimestamp` as the "clock" – Panagiotis Kanavos May 25 '21 at 13:17
  • @PanagiotisKanavos could you give me a code example? – maradev21 May 25 '21 at 13:24
  • Check [this question](https://stackoverflow.com/questions/21763826/historicalscheduler-in-rx). I knew I've seen this somewhere and using historical data is *very* important but the [Observable.Generate](https://learn.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh229270(v=vs.103)) doc page is a bit ... messy – Panagiotis Kanavos May 25 '21 at 13:28
  • @PanagiotisKanavos I edited my question adding some more information: actually I am not re-simulating historical data, since `IPoint`s are continuously added to my `ISubject` (with some delay since they are previously processed by other services). I don't understand how `Observable.Generate` can be included in my structure (assuming my structure is fine.. of course I can change it if it's wrong) – maradev21 May 25 '21 at 15:45

0 Answers0