feat: Add BboPriceSummaryStream for Brandon Chen
This commit is contained in:
1 parent
c365330b08
commit
4addb58743
12 files changed
+1030
-12
No files matched your search
@@ -0,0 +1,19 @@
|
||||
namespace bts.BlueOrb;
|
||||
|
||||
using BCG;
|
||||
|
||||
public class ColumnMaskRow : IBlueRow {
|
||||
readonly IBlueRow inputRow;
|
||||
readonly (string? Name, int Index)[] columnMap;
|
||||
|
||||
public ColumnMaskRow(IBlueRow inputRow, (string? Name, int Index)[] columnMap, IBlueTableSchema schema) {
|
||||
this.inputRow = inputRow;
|
||||
this.columnMap = columnMap;
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public IBlueTableSchema schema { get; }
|
||||
|
||||
public object? this[int index] =>
|
||||
inputRow[columnMap[index].Index];
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace bts.BlueOrb;
|
||||
|
||||
using BCG;
|
||||
|
||||
using Bts.DataStream;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
public class ColumnMaskStream : ViewStream {
|
||||
readonly ViewStream source;
|
||||
readonly string? columnMaskExpression;
|
||||
|
||||
IDisposable? sub;
|
||||
IBlueTableSchema? outputSchema;
|
||||
// Each item in this array represents an output column as a tuple of
|
||||
// new name, and corresponding index in the input schema.
|
||||
(string? OutputName, int InputIndex)[]? columnMappings;
|
||||
(string From, string To)[]? columnNameMappings;
|
||||
readonly bool includeUnmappedColumns;
|
||||
|
||||
public ColumnMaskStream(ViewStream source, string columnMaskExpression) {
|
||||
this.source = source;
|
||||
this.columnMaskExpression = columnMaskExpression;
|
||||
}
|
||||
|
||||
public ColumnMaskStream(ViewStream source, bool includeUnmappedColumns, params (string From, string To)[] columnNameMappings) {
|
||||
this.source = source;
|
||||
this.columnNameMappings = columnNameMappings;
|
||||
this.includeUnmappedColumns = includeUnmappedColumns;
|
||||
}
|
||||
|
||||
~ColumnMaskStream() =>
|
||||
Disposable.dispose(ref sub);
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
Log.isnull(sub);
|
||||
sub = source.Subscribe(OnSourceChanged);
|
||||
|
||||
void OnSourceChanged(IDataPacket dp) {
|
||||
ensureOutputSchema(dp.Schema);
|
||||
var addedRows = new ColumnMaskTable(dp.addedRows, columnMappings, outputSchema);
|
||||
var deletedRows = new ColumnMaskTable(dp.deletedRows, columnMappings, outputSchema);
|
||||
var allRows = new ColumnMaskTable(dp.newRows, columnMappings, outputSchema);
|
||||
myFireEvent(new DataPacket(allRows, addedRows, deletedRows));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber() =>
|
||||
Disposable.dispose(ref sub);
|
||||
|
||||
[MemberNotNull(nameof(outputSchema), nameof(columnMappings))]
|
||||
void ensureOutputSchema(IBlueTableSchema inputSchema) {
|
||||
if (outputSchema is not null) {
|
||||
Log.notnull(columnMappings);
|
||||
return;
|
||||
}
|
||||
if (columnMaskExpression is not null) {
|
||||
outputSchema = ColumnMaskTable.GetOutputSchema(inputSchema, columnMaskExpression, out columnMappings);
|
||||
} else if (columnNameMappings is not null) {
|
||||
outputSchema = ColumnMaskTable.GetOutputSchema(inputSchema, includeUnmappedColumns, columnNameMappings, out columnMappings);
|
||||
} else {
|
||||
throw new InvalidOperationException("Cannot build output schema");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
namespace bts.BlueOrb;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
using bts.utils;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class ColumnMaskTable : IBlueRowCollection {
|
||||
readonly IBlueRowCollection inputRows;
|
||||
readonly (string? Name, int Index)[] columnMap;
|
||||
|
||||
public ColumnMaskTable(IBlueRowCollection inputRows, (string? Name, int Index)[] columnMap, IBlueTableSchema schema) {
|
||||
this.inputRows = inputRows;
|
||||
this.columnMap = columnMap;
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public IBlueTableSchema schema { get; }
|
||||
|
||||
public int Count => inputRows.Count;
|
||||
|
||||
public IBlueRow this[int id] =>
|
||||
new ColumnMaskRow(inputRows[id], columnMap, schema);
|
||||
|
||||
public static ColumnMaskTable Create(IBlueRowCollection table, string columnMaskExpression) {
|
||||
var outputSchema = GetOutputSchema(table.schema, columnMaskExpression, out var columnMappings);
|
||||
return new ColumnMaskTable(table, columnMappings, outputSchema);
|
||||
}
|
||||
|
||||
public static IBlueTableSchema GetOutputSchema(IBlueTableSchema inputSchema, string columnMaskExpression, out (string? Name, int Index)[] columnMappings) {
|
||||
bool includeOthers;
|
||||
if (columnMaskExpression.StartsWith('!')) {
|
||||
columnMaskExpression = columnMaskExpression[1..].TrimStart();
|
||||
includeOthers = false;
|
||||
} else {
|
||||
includeOthers = true;
|
||||
}
|
||||
var map = columnMaskExpression.Split(' ').Select(MapColumn).ToOrderedDictionary(tup => tup.Index, tup => tup.Name);
|
||||
var inputCols = inputSchema.columnInfo;
|
||||
if (includeOthers) {
|
||||
var implicitCols = inputCols.Where(c => !map.ContainsKey(c.colNo)).ToList();
|
||||
map.AddRange(implicitCols.Select(c => new KeyValuePair<int, string?>(c.colNo, null)));
|
||||
} else if (!map.Any(c => inputCols[c.Key].colName == "id") && inputCols.TryGetFirst(c => c.colName == "id", out var idColInfo)) {
|
||||
// Add 'id' col if missing
|
||||
map.Insert(0, new(idColInfo.colNo, "id"));
|
||||
}
|
||||
var outputCols = new ColumnInfo[map.Count];
|
||||
for (int i = 0; i < outputCols.Length; i++) {
|
||||
var inputColInfo = inputCols[map.Keys[i]];
|
||||
outputCols[i] = new(map.Values[i] ?? inputColInfo.colName, i, inputColInfo.colType);
|
||||
}
|
||||
columnMappings = map.WrapSelector(kv => (kv.Value, kv.Key), null).ToArray();
|
||||
return new BlueTableSchema(outputCols, inputSchema.tableName + "_ColumnMask");
|
||||
|
||||
(string? Name, int Index) MapColumn(string columnStr) {
|
||||
var parts = columnStr.Split(':');
|
||||
return parts.Length switch {
|
||||
1 => ((string?)null, inputSchema[parts[0]].colNo),
|
||||
2 => (parts[1], inputSchema[parts[0]].colNo),
|
||||
_ => throw new InvalidOperationException("Unable to get output schema due to invalid column expression"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static IBlueTableSchema GetOutputSchema(IBlueTableSchema inputSchema, bool includeUnmapped, (string From, string To)[] columnNameMappings, out (string? OutputName, int InputIndex)[] columnMappings) {
|
||||
var inputCols = inputSchema.columnInfo;
|
||||
ColumnInfo[]? outputCols;
|
||||
if (includeUnmapped) {
|
||||
columnMappings = Array.ConvertAll(inputCols, c => ((string?)c.colName, c.colNo));
|
||||
outputCols = ArrayUtils.Clone(inputCols);
|
||||
foreach (var (from, to) in columnNameMappings) {
|
||||
var idx = inputCols.ResolveIndices(from);
|
||||
var c = outputCols[idx];
|
||||
Log.assert(c.colNo == idx);
|
||||
outputCols[idx] = new(to, c.colNo, c.colType);
|
||||
columnMappings[idx] = (to, idx);
|
||||
}
|
||||
} else {
|
||||
outputCols = new ColumnInfo[columnNameMappings.Length];
|
||||
columnMappings = Array.ConvertAll(columnNameMappings, m => ((string?)m.To, inputCols.ResolveIndices(m.From)));
|
||||
Array.Copy(inputCols, outputCols, inputCols.Length);
|
||||
for (int i = 0; i < columnNameMappings.Length; i++) {
|
||||
var (from, to) = columnNameMappings[i];
|
||||
var idx = inputCols.ResolveIndices(from);
|
||||
outputCols[idx] = new(to, i, inputCols[columnMappings[i].InputIndex].colType);
|
||||
}
|
||||
}
|
||||
return new BlueTableSchema(outputCols, inputSchema.tableName + "_ColumnMask");
|
||||
}
|
||||
|
||||
public bool containsID(int id) =>
|
||||
inputRows.containsID(id);
|
||||
|
||||
public IEnumerator<IBlueRow> GetEnumerator() {
|
||||
foreach (var r in inputRows) {
|
||||
yield return new ColumnMaskRow(r, columnMap, schema);
|
||||
}
|
||||
}
|
||||
|
||||
public IBlueRow GetRow(int index) =>
|
||||
new ColumnMaskRow(inputRows.GetRow(index), columnMap, schema);
|
||||
|
||||
public int IndexOf(IBlueRow item) {
|
||||
int id = item.Id();
|
||||
if (inputRows.containsID(id)) {
|
||||
var inputRow = inputRows[id];
|
||||
var outputRow = new ColumnMaskRow(inputRow, columnMap, schema);
|
||||
if (BlueRowEqualityComparer.Equals(item, outputRow)) {
|
||||
return inputRows.IndexOf(inputRow);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using BCG;
|
||||
|
||||
using bts;
|
||||
using bts.api.core;
|
||||
using bts.BlueOrb;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.PubSub;
|
||||
@@ -152,6 +153,14 @@ public static class DataStreamExtensions {
|
||||
IBlueRowCollection? ApplyWrapper(EquatableKeyArrayWrapper groupKey, IBlueRowCollection groupRows) =>
|
||||
apply(groupKey.Values, groupRows);
|
||||
}
|
||||
public static ViewStream RenameColumns(this ViewStream viewStream, bool includeUnmapped, params (string From, string To)[] columnMap) =>
|
||||
new ColumnMaskStream(viewStream, includeUnmapped, columnMap);
|
||||
|
||||
public static IObservable<IDataPacket> TimeSeries(this IObservable<IDataPacket> source, int bufferSize, string timeColumn) =>
|
||||
source.AsViewStream().TimeSeries(bufferSize, timeColumn).AsObservable();
|
||||
|
||||
public static ViewStream TimeSeries(this ViewStream source, int bufferSize, string timeColumn) =>
|
||||
new TimeSeriesStream(source, bufferSize, timeColumn);
|
||||
|
||||
public static IDisposable Subscribe(this ViewStream viewStream, Action<IDataPacket> onChange) {
|
||||
viewStream.onChange += OnChange;
|
||||
|
||||
Reference in new issue
Block a user