using System; using System.Collections.Generic; using Bts.DataStreamExpressions; using BCG.Utilities.General; using bts; namespace BCG { // listens to the requests posted on "cloud_streams" request table, and // if unfulfilled starts a ViewPubsliher public class RemoteQueryManager : IWorker { public class StreamPubUnit { public string name; public string stream_expression; public ViewStream stream; public bool persist; public ViewPublisher view_publisher; public StreamPubUnit(string name_, string stream_expression_, ViewStream stream_, bool persist_, ViewPublisher view_publisher_) { name = name_; stream_expression = stream_expression_; stream = stream_; persist = persist_; view_publisher = view_publisher_; } } static ViewStream? build_stream(string stream_expression) { try { var exp = DataStreamExpressionsInterpreter.Evaluate(stream_expression); var rv = exp.getView(new Dictionary(0)); return rv; } catch (Exception ex) { bts.Log.assert(false, $"Could not build stream: {stream_expression} ex: {ex}"); return null; } } private IDictionary _stream_pub_map = new Dictionary(); private ViewStream _stream = new TableStream("cloud_streams"); private PubTableBroker _broker = new PubTableBroker(); private ManagedSubscriber _sub = ManagedSubscriber.instance; public RemoteQueryManager() { _stream.onChange += _on_str; _sub.subscribe("RemoteRequest", (string tag, byte[] data) => { RemoteRequest req = RemoteRequest.construct_from_bytes(data); _spawn_remote_stream(req.published_table_name, req.stream_expression, false); }); } private void _on_str(object obj) { if (obj is not DataPacket dp) { return; } foreach (IBlueRow r in dp.newRows) { string name = r.get("name"); bool persist = r.get("persist"); string stream_exp = r.get("stream_exp"); if (!_stream_pub_map.ContainsKey(name)) { _spawn_remote_stream(name, stream_exp, persist); } } } private void _spawn_remote_stream(string name, string stream_expression, bool persist) { bts.Log.Gen.info($"CloudStreamManager.assign name: {name} stream_expression: {stream_expression}"); ViewStream stream = build_stream(stream_expression); _expose_to_ws(name, stream); if (persist) { stream = new PersistorStream(stream, name, "C:\\Temp\\", 1000000); // 10MBs } ViewPublisher view_pub = new ViewPublisher(name, _broker, stream); StreamPubUnit u = new StreamPubUnit(name, stream_expression, stream, persist, view_pub); _stream_pub_map.Add(name, u); } private void _expose_to_ws(string name, ViewStream stream) { // TODO: john } public void doWork() { } } }