using System; using BCG; namespace bts.BlueOrb { // Keeps adding changing rows to the stream, hence preserving the history // and creating a time series, for example this can be used to make bbo updates // into a time series of prices/qty public class TimeSeriesStream : ViewStream { private BlueTable _table; private ViewStream _stream; private int _id = 0; public TimeSeriesStream(ViewStream stream) { _stream = stream; } protected override void myOnFirstSubscriber() { _stream.onChange += _onstream_change; } protected override void myOnLastSubscriber() { _stream.onChange -= _onstream_change; } private void _onstream_change(object o) { DataPacket dp = o as DataPacket; DateTime now = DateTime.Now; if (_table == null) { IBlueTableSchema schema = new BlueTableSchema(dp.newRows.schema.columnInfo, dp.newRows.schema.tableName); schema = schema.add_columns(Tuple.Create(typeof(DateTime), "timestamp")); _table = new BlueTable(schema); } BlueTable added_rows = new BlueTable(_table.schema); foreach (IBlueRow r in dp.addedRows) { MutableBlueRow ar = new MutableBlueRow(_table.schema, r); ar.setValue("timestamp", now); ar.setValue("id", _id++); added_rows.addRow(ar); } _table.addRows(added_rows); myFireEvent(new DataPacket(_table, added_rows, new BlueTable(_table.schema))); } } }