简体   繁体   English

被动扩展:事件发生的频率

[英]Reactive Extensions : Frequency of event

Lets say I have a class like MyClass { string id, string eventType, datetime ts} 可以说我有一个像MyClass这样的类{字符串ID,字符串eventType,日期时间ts}

ts is the timestamp of the event and Id is on which I want to calculate frequency ts是事件的时间戳,Id是我要计算频率的时间

I have a hot observable of MyClass , I want to calculate number of events recvd per stringId in the last 30 seconds 我有一个MyClass的热门观察站,我想计算最近30秒内每个stringId接收的事件数

If number of events is more than 5 , I raise another event of MyClass (with same Id, and eventType ="New" ) and if it falls down below 3 again , I need to update the previously raised event (with same Id, and eventType ="New" ). 如果事件数大于5,则引发另一个MyClass事件(具有相同的ID,并且eventType =“ New”),如果事件再次降至3以下,则需要更新先前引发的事件(具有相同的ID,并且eventType =“ New”)。

I think I need to use sliding window, I have reached so far 我想我需要使用滑动窗,到目前为止

public static IObservable<MyClass> CountFrequency(this IObservable<MyClass> source, TimeSpan withinPeriod, string marker)
{
    // var scheduler = new HistoricalScheduler();
    //  var driveSchedule = source.Subscribe(e => scheduler.AdvanceTo(e.Timestamp)); 
    return source.Window(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(5))
        .SelectMany(sl => sl)
        .GroupBy(a => a.id)
        .SelectMany(go => go
        .Aggregate(new MyClass(), (acc, evt) => CustomAggFrequency(acc, evt, marker))
        .Select(count => count)));
}

I am not able to understand 我听不懂

a) How to relate scheduler to the timestamp of the data not system time b) How to code the logic of CustomAggFrequency a)如何将调度程序与数据的时间戳而非系统时间相关联b)如何对CustomAggFrequency的逻辑进行编码

Any suggestions 有什么建议么

Will this not work? 这行不通吗? Your question is phrased such that it's hard to figure out what you want. 您的问题用语如此,以致于很难弄清您想要什么。

var span = TimeSpan.FromSeconds(30);
var shift = TimeSpan.FromSeconds(5);
var query = source
    .Window(span, shift)
    .Select(window => window
        .GroupBy(item => item.id)
        .ToDictionary(g => x.Key, g => g.Count() / span.TotalSeconds));

This query will emit a dictionary every 5 seconds that maps each id to its frequency in hertz during the last 30 seconds. query每5秒钟发出一次字典,该字典将每个id映射到最近30秒钟内以赫兹为单位的频率。 Its type is IObservable<Dictionary<string, double>> . 其类型为IObservable<Dictionary<string, double>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM