112 lines
5.2 KiB
C#
112 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
|
|
using bts;
|
|
using bts.SilverPine;
|
|
|
|
namespace BCG {
|
|
//[DataStreamUserRespresentation("AggregateStream")]
|
|
public class NonIncGroupByStream : ViewStream {
|
|
private class DelNonIncGroupByEvaluator : INonIncGroupByEvaluator {
|
|
Func<string[]> _groupby_cols_func;
|
|
Func<List<IBlueRow>, List<IBlueRow>> _evaluate_func;
|
|
public DelNonIncGroupByEvaluator(Func<string[]> groupby_cols_func,
|
|
Func<List<IBlueRow>, List<IBlueRow>> evaluate_func) {
|
|
_groupby_cols_func = groupby_cols_func;
|
|
_evaluate_func = evaluate_func;
|
|
}
|
|
public IBlueRowCollection evaluate(IDictionary<int, List<IBlueRow>> grouped_rows) {
|
|
BlueTable rv = null;
|
|
foreach (var kv in grouped_rows) {
|
|
List<IBlueRow> g_rows = kv.Value;
|
|
List<IBlueRow> out_rows = _evaluate_func.Invoke(g_rows);
|
|
if (out_rows != null && out_rows.Count > 0) {
|
|
rv = rv ?? new BlueTable(out_rows.ElementAt(0).schema);
|
|
}
|
|
rv.addRows(out_rows);
|
|
}
|
|
return rv;
|
|
}
|
|
public string[] get_group_by_cols() {
|
|
return _groupby_cols_func();
|
|
}
|
|
|
|
}
|
|
private ViewStream _main_stream;
|
|
private INonIncGroupByEvaluator _groupby_func;
|
|
private BlueRowHasher _row_hasher;
|
|
private string[] _groupby_cols;
|
|
private Stopwatch _timer = new();
|
|
private int _n_total_fires = 0, _n_expensive_fires = 0;
|
|
|
|
public NonIncGroupByStream(ViewStream main_stream, INonIncGroupByEvaluator groupby_func) {
|
|
_main_stream = main_stream;
|
|
_groupby_func = groupby_func;
|
|
_groupby_cols = _groupby_func.get_group_by_cols();
|
|
_row_hasher = new BlueRowHasher(_groupby_cols == null ? 0 : _groupby_cols.Length);
|
|
}
|
|
|
|
public NonIncGroupByStream(ViewStream main_stream, Func<string[]> groupby_cols_func,
|
|
Func<List<IBlueRow>, List<IBlueRow>> evaluate_func)
|
|
: this(main_stream, new DelNonIncGroupByEvaluator(groupby_cols_func, evaluate_func)) { }
|
|
|
|
public NonIncGroupByStream(ViewStream parent_str, string groupby_col, string standard_group_by_text)
|
|
: this(parent_str, groupby_col, standard_group_by_text, String.Empty) { }
|
|
|
|
public NonIncGroupByStream(ViewStream parent_str, string groupby_col, string standard_group_by_text, string optional_sort_col) {
|
|
MultiRowOpHelper std_group_by = MultiRowOpHelper.parse(standard_group_by_text);
|
|
Func<string[]> groupby_cols_func = () => {
|
|
return String.IsNullOrEmpty(groupby_col) ? null : new string[] { groupby_col };
|
|
};
|
|
Func<List<IBlueRow>, List<IBlueRow>> evaluate_func = (group_rows) => {
|
|
if (!String.IsNullOrEmpty(optional_sort_col))
|
|
group_rows.Sort((r1, r2) => BlueDataUtility.compare_rows(r2, r1, optional_sort_col));
|
|
IBlueRow rv = MultiRowOpHelper.group_by(group_rows, std_group_by);
|
|
return new IBlueRow[] { rv }.ToList();
|
|
};
|
|
ViewStream rs = new NonIncGroupByStream(parent_str, groupby_cols_func, evaluate_func);
|
|
mySetRealSource(rs);
|
|
}
|
|
public NonIncGroupByStream(ViewStream parent_str, string standard_group_by_text) :
|
|
this(parent_str, String.Empty, standard_group_by_text) {
|
|
}
|
|
|
|
|
|
protected override void myOnFirstSubscriber() { _main_stream.onChange += _main_stream_onchange; }
|
|
|
|
protected override void myOnLastSubscriber() { _main_stream.onChange -= _main_stream_onchange; }
|
|
|
|
private void _main_stream_onchange(object nv) {
|
|
DataPacket dp = nv as DataPacket;
|
|
///grouping together
|
|
_timer.Restart();
|
|
IDictionary<int, List<IBlueRow>> grouped_rows = (_groupby_cols == null || _groupby_cols.Length == 0) ?
|
|
new Dictionary<int, List<IBlueRow>>() { { 0, dp.newRows.to_list() } } :
|
|
BlueDataUtility.group_together(dp.newRows, _groupby_cols, ref _row_hasher);
|
|
_timer.Stop();
|
|
var groupElapsedMs = _timer.ElapsedMilliseconds;
|
|
if (groupElapsedMs >= 50) {
|
|
Log.DataFlow.info($"Time to group {dp.newRows.Count} rows into {grouped_rows.Count} groups: {_timer.ElapsedMilliseconds}ms");
|
|
}
|
|
// Log.log(bts.Log.Level.Info, bts.log.Subsystem.gen, String.Format("Time to group {0} rows into {1} groups = {2}", dp.newRows.Count, grouped_rows.Count, _timer.lastIntervalTime));
|
|
/// reducing
|
|
_timer.Restart();
|
|
IBlueRowCollection total_state = _groupby_func.evaluate(grouped_rows);
|
|
_timer.Stop();
|
|
var applyElapsedMs = _timer.ElapsedMilliseconds;
|
|
_n_total_fires++;
|
|
if (applyElapsedMs >= 50) {
|
|
_n_expensive_fires++;
|
|
Log.DataFlow.info(
|
|
$"Time to evaluate {grouped_rows.Count} groups {_timer.ElapsedMilliseconds}ms, ratio {_n_expensive_fires}/{_n_total_fires}");
|
|
}
|
|
// TODO,
|
|
if (total_state != null)
|
|
myFireEvent(new DataPacket(total_state, total_state, myLastValue == null ? new BlueTable(total_state.schema) : (myLastValue as DataPacket).newRows));
|
|
}
|
|
}
|
|
}
|