79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
using BCG;
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using ZstdSharp.Unsafe;
|
|
|
|
namespace bts {
|
|
public class RowDiffStream : ViewStream {
|
|
private ViewStream _vs;
|
|
private readonly string _order_col, _col_1, _col_2;
|
|
private readonly int _diff;
|
|
private IBlueTableSchema _out_schema;
|
|
private readonly string _diff_col_name;
|
|
public RowDiffStream(ViewStream vs, string order_col, string col_1, string col_2, int diff, string diff_col_name) {
|
|
_vs = vs;
|
|
_order_col = order_col;
|
|
_col_1 = col_1;
|
|
_col_2 = col_2;
|
|
_diff = diff;
|
|
_diff_col_name = diff_col_name;
|
|
}
|
|
|
|
private string _diff_col_name_perc {
|
|
get {
|
|
return $"{_diff_col_name}_percentage";
|
|
}
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
_vs.onChange += _handle_vs;
|
|
}
|
|
|
|
protected override void myOnLastSubscriber() {
|
|
_vs.onChange -= _handle_vs;
|
|
}
|
|
|
|
private void _handle_vs(object dp_obj) {
|
|
var dp = dp_obj as DataPacket;
|
|
IBlueRowCollection nrows = dp.newRows;
|
|
_out_schema = BlueDataUtility.add_column(nrows.schema, _diff_col_name, typeof(double));
|
|
_out_schema = BlueDataUtility.add_column(_out_schema, _diff_col_name_perc, typeof(double));
|
|
List<IBlueRow> row_list = new List<IBlueRow>();
|
|
foreach (IBlueRow r in nrows)
|
|
row_list.Add(r);
|
|
//OrderBy sorts in ascedning order
|
|
var sorted_rows = row_list.OrderBy(r => r[_order_col]).ToList();
|
|
var sorted_row_array = sorted_rows.ToArray();
|
|
// reverse it to make it descending
|
|
sorted_rows.Reverse();
|
|
//sort rows so we can do the diff
|
|
OrderedBlueTable rv = new OrderedBlueTable(_out_schema);
|
|
int i = 0;
|
|
foreach (IBlueRow r in sorted_rows) {
|
|
MutableBlueRow nr = new MutableBlueRow(_out_schema, r);
|
|
//So, in the expression "x - y," where y is subtracted from x, x is the minuend, and y is the subtrahend.
|
|
// we assume x - y, and name variables like that
|
|
double x = r.get<double>(_col_1);
|
|
int y_row_idx = i + _diff;
|
|
double diff = System.Double.NaN;
|
|
double perc = System.Double.NaN;
|
|
if (y_row_idx < sorted_rows.Count()) {
|
|
var y = sorted_rows[y_row_idx].get<double>(_col_2);
|
|
diff = x - y;
|
|
perc = 100.0 * diff / y;
|
|
}
|
|
nr.setValue(_diff_col_name, diff);
|
|
nr.setValue(_diff_col_name_perc, perc);
|
|
rv.addRow(nr);
|
|
i++;
|
|
}
|
|
|
|
var dp_out = BlueDataUtility.build_dp_inc(new_rows: rv, myLastValue as DataPacket, true);
|
|
myFireEvent(dp_out);
|
|
|
|
}
|
|
}
|
|
} |