@jayesol/jayeson.lib.sports 2.2.4 → 2.2.6-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/receive.js CHANGED
@@ -1 +1,484 @@
1
- "use strict";var __extends=this&&this.__extends||function(){var r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,s){e.__proto__=s}||function(e,s){for(var t in s)s.hasOwnProperty(t)&&(e[t]=s[t])};return function(e,s){function t(){this.constructor=e}r(e,s),e.prototype=null===s?Object.create(s):(t.prototype=s.prototype,new t)}}(),__decorate=this&&this.__decorate||function(e,s,t,r){var a,o=arguments.length,n=o<3?s:null===r?r=Object.getOwnPropertyDescriptor(s,t):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,s,t,r);else for(var i=e.length-1;0<=i;i--)(a=e[i])&&(n=(o<3?a(n):3<o?a(s,t,n):a(s,t))||n);return 3<o&&n&&Object.defineProperty(s,t,n),n},__metadata=this&&this.__metadata||function(e,s){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,s)},__param=this&&this.__param||function(t,r){return function(e,s){r(e,s,t)}};Object.defineProperty(exports,"__esModule",{value:!0});var D=require("@jayesol/jayeson.lib.delivery"),Collections=require("typescript-collections"),core_1=require("./core"),message_class_1=require("./message_class"),merge_1=require("./merge"),data_structure_1=require("./data_structure"),circular=require("circular-json");require("reflect-metadata");var injection_js_1=require("injection-js"),codec_1=require("./codec"),InEndPointEventHandler=function(){function e(e,s,t){this._streamSources={},this._filterData=null,this._startMessage=e,this._sportsMsgGrp=s,this._streamNameCode=t,this._exclude=e.exclude,this._wireFormat=e.wireFormat}return e.prototype.setfilterData=function(e){this._filterData=e},e.prototype.streamSources=function(){return this._streamSources},e.prototype.exclude=function(){return this._exclude},e.prototype.wireFormat=function(){return this._wireFormat},e.prototype.startMessage=function(){return this._startMessage},e.prototype.sportsMsgGrp=function(){return this._sportsMsgGrp},e.prototype.streamNameCode=function(){return this._streamNameCode},e.prototype.filterData=function(){return this._filterData},e.prototype.onEvent=function(e){if(e instanceof D.Subscriber.Event.ConsumptionStart){for(var s in e.streams)if(e.streams.hasOwnProperty(s))for(var t=0;t<e.streams[s].length;t++){if(parseInt(s)==this.sportsMsgGrp().id()){var r=e.streams[s][t];this._streamSources[r]=e.endPoint;var a=new codec_1.FilterRequest(codec_1.FilterRequest.IGNORED_REQUEST_ID,this.filterData());this.sendFilter(a,r),console.log("Sending Start Message to stream : '"+r+"'");var o=new D.MessageWrapper(this._startMessage,this._sportsMsgGrp.ADMIN_START_SUBSCRIBE_FEED());this.sendMessage(r,o)}}}else if(e instanceof D.Subscriber.Event.ConsumptionError){var n=e.client.config.url+" has errors on consumption: ";for(var s in e.streams)if(e.streams.hasOwnProperty(s)){n+="\n"+s+":[";for(t=0;t<e.streams[s].length;t++)n+=e.streams[s][t]+",";n=n.substring(1,n.length-1),n+="]"}console.log(n)}else console.log("Unidentified event received by onEvent: "+circular.stringify(e))},e.prototype.sendMessage=function(e,s){s.addMetaInfo(this._streamNameCode.code(),e);var t=this._streamSources[e];null==t?console.log("No Endpoint present corresponding to stream '"+e+"'. Unable to send message"):t.send(s)},e.prototype.updateFilter=function(e){this._filterData=e.getFilterData(),this.sendFilter(e)},e.prototype.sendFilter=function(e,s){if(void 0===s&&(s=null),null!==s)this._sendFilter(e,s);else for(var t in this._streamSources)this._streamSources.hasOwnProperty(t)&&this._sendFilter(e,t)},e.prototype._sendFilter=function(e,s){var t=e.getFilterData(),r=null;null!=t?(r=new D.MessageWrapper(e,this._sportsMsgGrp.FILTER_SET()),console.log("Sending FILTER_SET to stream: '"+s+"' with filter data: "+JSON.stringify(t))):(r=new D.MessageWrapper(e.getRequestId().toString(),this._sportsMsgGrp.FILTER_REMOVE()),console.log("Sending FILTER_REMOVE to stream: '"+s+"'")),this.sendMessage(s,r)},e=__decorate([injection_js_1.Injectable(),__metadata("design:paramtypes",[message_class_1.StartSubscribeFeed,message_class_1.SportsFeedMessageGroup,D.StreamNameCode])],e)}();exports.InEndPointEventHandler=InEndPointEventHandler;var StreamCommandProcessor=function(){function e(e,s){this._fsRepo=e,this._sportsGrp=s,this.messageClassName={0:"DATA_RESET",1:"DATA_INSERT_ODD",2:"DATA_UPDATE_ODD",3:"DATA_DELETE_ODD",4:"DATA_INSERT_EVENT",5:"DATA_UPDATE_EVENT",6:"DATA_DELETE_EVENT",7:"DATA_INSERT_MATCH",8:"DATA_UPDATE_MATCH",9:"DATA_DELETE_MATCH",10:"TTL_REMOVE",11:"TTL_RESTORE",13:"FILTER_REMOVE",14:"FILTER_SET",16:"ADMIN_START_SUBSCRIBE_FEED",17:"ADMIN_REFRESH"}}return e.prototype.process=function(e,s,t){if(void 0===t&&(t=null),null!=s&&s instanceof data_structure_1.Incoming){var r=s,a=this.messageClassName[r.msgType().id()];console.debug("SCP processing: "+a)}if(null==t)for(var o=0,n=this._fsRepo.appendSnapshot(e,s);o<n.length;o++){var i=n[o];this._fsRepo.push(i)}else this._fsRepo.push(t)},e.prototype.fsRepo=function(){return this._fsRepo},e.prototype.sportsGrp=function(){return this._sportsGrp},e.prototype.printMessage=function(e){var s=this.getClassName(e),t=e.delta();switch(this.printPartitionKeys(s,t),e.msgType().id()){case this._sportsGrp.DATA_INSERT_MATCH().id():case this._sportsGrp.DATA_UPDATE_MATCH().id():case this._sportsGrp.DATA_DELETE_MATCH().id():this.printMatch(s,t);break;case this._sportsGrp.DATA_INSERT_ODD().id():case this._sportsGrp.DATA_UPDATE_ODD().id():case this._sportsGrp.DATA_DELETE_ODD().id():this.printRecord(s,t)}},e.prototype.getClassName=function(e){return e instanceof core_1.TTLOutgoing?e.getTtlType()==core_1.TTLType.REMOVE?"TTL_REM":"TTL_RES":this.messageClassName[e.msgType().id()]},e.prototype.printMatch=function(e,s){for(var t=s.matches(),r=0;r<t.length;r++)console.log("["+e+"] "+t[r].id())},e.prototype.printRecord=function(e,s){for(var t=s.matches(),r=0;r<t.length;r++)for(var a=t[r].events(),o=0;o<a.length;o++)for(var n=a[o].records(),i=0;i<n.length;i++){var c=n[i];console.log("["+e+"] "+c.matchId()+"_"+c.eventId()+"_"+c.id()+"_"+c.source()+"_"+c.oddType()+"_"+c.oddFormat())}},e.prototype.printPartitionKeys=function(e,s){var t=s.getPartitions().toString();console.log("["+e+"][PK]"+t)},e=__decorate([injection_js_1.Injectable(),__param(0,injection_js_1.Inject(core_1.FSREPO_IMPL)),__metadata("design:paramtypes",[Object,message_class_1.SportsFeedMessageGroup])],e)}();exports.StreamCommandProcessor=StreamCommandProcessor;var SportsFeedInProcessor=function(o){function e(e,s,t,r){var a=o.call(this,s)||this;return a.scp=e,a.sportsFeedMsgGroup=s,a.snCode=t,a.recycleBin=r,a}return __extends(e,o),e.prototype.process=function(e){var s=e.getMetaInfo(this.snCode.code());if(null!=s&&""!==s)if(this.isFeedMessage(e))if(this.sportsFeedMsgGroup.isIndicatorMessage(e.messageClass)){var t=this.processIndicatorMessage(e);this.scp.process(s,null,t)}else{var r=new MergeableIncoming(this.sportsFeedMsgGroup,e.messageClass,s,e.message,this.recycleBin);this.triggerTtlRestoreIfApplicable(r),this.scp.process(r.stream(),r)}else console.log("SportsFeedInProcessor received unknown messageclass: "+circular.stringify(e.messageClass)+". Ignoring");else console.log("SportsFeedInProcessor received messagewrapper with no StreamName. Ignoring.")},e.prototype.processIndicatorMessage=function(e){var s=this;return e.messageClass==this.sportsFeedMsgGroup.TTL_RESTORE_START()&&e.message.getPartitions().forEach(function(e){s.recycleBin.clearBin(e)}),new data_structure_1.OutgoingImpl(e.messageClass,e.message)},e.prototype.triggerTtlRestoreIfApplicable=function(e){if(null!=this.getRecycleBin()&&null!=this.getRecycleBin().getTtlConfig()&&this.getRecycleBin().getTtlConfig().isEnableTtl()){var s=this.getRecycleBin().getTtlRestoreSnapshot(e);null!=s&&this.scp.process(s.getStream(),s)}},e.prototype.triggerTtlRemove=function(){var s=this;null!=this.getRecycleBin()&&this.getRecycleBin().getTtlRemoveSnapshot().forEach(function(e){s.scp.process(e.getStream(),e)})},e.prototype.startTtlVerification=function(){var e=this;if(null!=this.getRecycleBin()){var s=this.getRecycleBin().getTtlConfig();if(null!=s&&s.isEnableTtl()){var t=s.getRunInterval();setInterval(function(){return e.triggerTtlRemove()},t),console.log("Scheduled TTLCheck with interval of "+t)}else console.log("No TTL set. Not scheduling any TTL.")}},e.prototype.isFeedMessage=function(e){var s=e.messageClass;return s instanceof message_class_1.InsertOddMessageClass||s instanceof message_class_1.UpdateOddMessageClass||s instanceof message_class_1.DeleteOddMessageClass||s instanceof message_class_1.InsertEventMessageClass||s instanceof message_class_1.UpdateEventMessageClass||s instanceof message_class_1.DeleteEventMessageClass||s instanceof message_class_1.InsertMatchMessageClass||s instanceof message_class_1.UpdateMatchMessageClass||s instanceof message_class_1.DeleteMatchMessageClass||s instanceof message_class_1.RefreshMessageClass||s instanceof message_class_1.TTLRemoveMessageClass||s instanceof message_class_1.TTLRestoreMessageClass||s instanceof message_class_1.ResetMessageClass||s instanceof message_class_1.SwitchFilterStartMessageClass||s instanceof message_class_1.SwitchFilterFailMessageClass||s==this.sportsFeedMsgGroup.SWITCH_FILTER_START()||s==this.sportsFeedMsgGroup.SWITCH_FILTER_END()||s==this.sportsFeedMsgGroup.FULLSNAPSHOT_START()||s==this.sportsFeedMsgGroup.FULLSNAPSHOT_END()||s==this.sportsFeedMsgGroup.SWITCH_FILTER_FAIL()||s==this.sportsFeedMsgGroup.TTL_RESTORE_START()||s==this.sportsFeedMsgGroup.TTL_RESTORE_END()},e.prototype.getScp=function(){return this.scp},e.prototype.getRecycleBin=function(){return this.recycleBin},e=__decorate([injection_js_1.Injectable(),__metadata("design:paramtypes",[StreamCommandProcessor,message_class_1.SportsFeedMessageGroup,D.StreamNameCode,core_1.RecycleBin])],e)}(D.IMessageGroupProcessor);exports.SportsFeedInProcessor=SportsFeedInProcessor;var MergeableIncoming=function(n){function e(e,s,t,r,a){var o=n.call(this,s,t,r)||this;return o.sportsGrp=e,o.bin=a,o}return __extends(e,n),e.prototype.apply=function(e){null==e&&(e=data_structure_1.IndexedSnapshotImpl.EMPTY_SNAPSHOT);var s=[];this.beforeSnap=e;var t=new data_structure_1.MergeableWrapper;if(this.msgType()instanceof message_class_1.InsertMatchMessageClass||this.msgType()instanceof message_class_1.InsertEventMessageClass||this.msgType()instanceof message_class_1.DeleteMatchMessageClass||this.msgType()instanceof message_class_1.DeleteEventMessageClass||this.msgType()instanceof message_class_1.UpdateEventMessageClass){for(var r=merge_1.SnapshotUtil.combineSnapshots(this.msgType(),this.data(),e),a=0,o=merge_1.DeltaTransformingLogicImpl.transform(this.sportsGrp,this.stream(),this.msgType(),this.data(),r);a<o.length;a++){var n=o[a],i=new core_1.Delta(n,r.getAfterSs(),this.beforeSnap);s.push(i),this.beforeSnap=r.getAfterSs()}t.setAfter(r.getAfterSs()),t.setDeltaOut(s)}else if(this.msgType()instanceof message_class_1.TTLRemoveMessageClass){for(var c=null,l=e,p=new Collections.Dictionary,g=[],_=!1,u=e.getPartitionMap(),f=0,h=this.data().getPartitions().toArray();f<h.length;f++){var m=h[f];console.log("removing keys from upstream "+JSON.stringify(m)),u.containsKey(m)&&(_=!0,p.setValue(m,u.getValue(m)),l=(c=this.bin.removeData(new data_structure_1.TTLWrapper,l,m)).getRemainingSs(),g.push(c.getRemovedSs()))}_?(console.log("reseting -----\x3e"+JSON.stringify(p.keys())),t=merge_1.DeltaTransformingLogicImpl.transformTTLRemove(g,this.bin.getGrp(),this.stream(),e,l,p)):(t.setDeltaOut([]),t.setAfter(e))}else if(this.msgType()instanceof message_class_1.TTLRestoreMessageClass){c=null,l=e;for(var d=new Collections.Dictionary,T=!1,y={},E=[],S=0,M=this.data().getPartitions().toArray();S<M.length;S++){m=M[S];if(console.log("restoring keys from upstream "+JSON.stringify(m)),this.bin.containData(m)){T=!0;var R=(new Date).getTime();d.setValue(m,R),y=this.bin.copyData(m,y),l=(c=this.bin.restoreData(new data_structure_1.TTLWrapper,l,m,R)).getRemainingSs(),E.push(c.getRestoredSs())}}T?(console.log("restoring -----\x3e"+JSON.stringify(d.keys())),t=merge_1.DeltaTransformingLogicImpl.transformTTLRestore(E,this.bin.getGrp(),this.stream(),e,l,y,d)):(t.setAfter(e),t.setDeltaOut([]))}else{r=merge_1.SnapshotUtil.combineSnapshots(this.msgType(),this.data(),e),i=new core_1.Delta(this,r.getAfterSs(),e);s.push(i),t.setDeltaOut(s),t.setAfter(r.getAfterSs())}return t},e}(data_structure_1.Incoming);exports.MergeableIncoming=MergeableIncoming;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ var __metadata = (this && this.__metadata) || function (k, v) {
32
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
33
+ };
34
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
35
+ return function (target, key) { decorator(target, key, paramIndex); }
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.MergeableIncoming = exports.SportsFeedInProcessor = exports.StreamCommandProcessor = exports.InEndPointEventHandler = void 0;
39
+ const D = __importStar(require("@jayesol/jayeson.lib.delivery"));
40
+ const Collections = __importStar(require("typescript-collections"));
41
+ const core_1 = require("./core");
42
+ const message_class_1 = require("./message_class");
43
+ const merge_1 = require("./merge");
44
+ const data_structure_1 = require("./data_structure");
45
+ let circular = require("circular-json");
46
+ //imports required for dependency injection
47
+ require("reflect-metadata");
48
+ const injection_js_1 = require("injection-js");
49
+ const codec_1 = require("./codec");
50
+ let InEndPointEventHandler = class InEndPointEventHandler {
51
+ constructor(startMessage, sportsMsgGrp, streamNameCode) {
52
+ this._streamSources = {};
53
+ this._filterData = null;
54
+ //to be injected
55
+ this._startMessage = startMessage;
56
+ this._sportsMsgGrp = sportsMsgGrp;
57
+ this._streamNameCode = streamNameCode;
58
+ this._exclude = startMessage.exclude;
59
+ this._wireFormat = startMessage.wireFormat;
60
+ }
61
+ setfilterData(rule) {
62
+ this._filterData = rule;
63
+ }
64
+ streamSources() {
65
+ return this._streamSources;
66
+ }
67
+ exclude() {
68
+ return this._exclude;
69
+ }
70
+ wireFormat() {
71
+ return this._wireFormat;
72
+ }
73
+ startMessage() {
74
+ return this._startMessage;
75
+ }
76
+ sportsMsgGrp() {
77
+ return this._sportsMsgGrp;
78
+ }
79
+ streamNameCode() {
80
+ return this._streamNameCode;
81
+ }
82
+ filterData() {
83
+ return this._filterData;
84
+ }
85
+ onEvent(event) {
86
+ if (event instanceof D.Subscriber.Event.ConsumptionStart) {
87
+ for (let property in event.streams) {
88
+ if (event.streams.hasOwnProperty(property)) {
89
+ for (let i = 0; i < event.streams[property].length; i++) {
90
+ let grpId = parseInt(property);
91
+ //only connect to sports streams
92
+ if (grpId == this.sportsMsgGrp().id()) {
93
+ //set end point mapping.
94
+ let streamId = event.streams[property][i];
95
+ this._streamSources[streamId] = event.endPoint;
96
+ //send filter
97
+ let filterRequest = new codec_1.FilterRequest(codec_1.FilterRequest.IGNORED_REQUEST_ID, this.filterData());
98
+ this.sendFilter(filterRequest, streamId);
99
+ //send start message
100
+ console.log("Sending Start Message to stream : '" + streamId + "'");
101
+ let mw = new D.MessageWrapper(this._startMessage, this._sportsMsgGrp.ADMIN_START_SUBSCRIBE_FEED());
102
+ this.sendMessage(streamId, mw);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ else if (event instanceof D.Subscriber.Event.ConsumptionError) {
109
+ let logMessage = event.client.config.url + " has errors on consumption: ";
110
+ for (let property in event.streams) {
111
+ if (event.streams.hasOwnProperty(property)) {
112
+ logMessage += "\n" + property + ":[";
113
+ for (let i = 0; i < event.streams[property].length; i++) {
114
+ logMessage += event.streams[property][i] + ",";
115
+ }
116
+ logMessage = logMessage.substring(1, logMessage.length - 1);
117
+ logMessage += "]";
118
+ }
119
+ }
120
+ console.log(logMessage);
121
+ }
122
+ else {
123
+ console.log("Unidentified event received by onEvent: " + circular.stringify(event));
124
+ }
125
+ }
126
+ sendMessage(streamId, mw) {
127
+ //Append Stream name as meta info
128
+ mw.addMetaInfo(this._streamNameCode.code(), streamId);
129
+ //Get EndPoint to corresponding to this stream
130
+ let ep = this._streamSources[streamId];
131
+ //If EndPoint is not present, no action
132
+ if (ep == null) {
133
+ console.log("No Endpoint present corresponding to stream '" + streamId + "'. Unable to send message");
134
+ }
135
+ else {
136
+ ep.send(mw);
137
+ }
138
+ }
139
+ updateFilter(filterRequest) {
140
+ this._filterData = filterRequest.getFilterData();
141
+ this.sendFilter(filterRequest);
142
+ }
143
+ /**
144
+ * Sends filter to server
145
+ * @param streamId - sends filter for a particular streamId
146
+ * if null, sends filter for all stream ids
147
+ */
148
+ sendFilter(filterRequest, streamId = null) {
149
+ if (streamId !== null) {
150
+ this._sendFilter(filterRequest, streamId);
151
+ }
152
+ else {
153
+ for (let streamId in this._streamSources) {
154
+ if (this._streamSources.hasOwnProperty(streamId)) {
155
+ this._sendFilter(filterRequest, streamId);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ _sendFilter(filterRequest, streamId) {
161
+ let filterData = filterRequest.getFilterData();
162
+ let mw = null;
163
+ if (filterData != null) {
164
+ mw = new D.MessageWrapper(filterRequest, this._sportsMsgGrp.FILTER_SET());
165
+ console.log("Sending FILTER_SET to stream: '" + streamId + "' with filter data: " + JSON.stringify(filterData));
166
+ }
167
+ else {
168
+ mw = new D.MessageWrapper(filterRequest.getRequestId().toString(), this._sportsMsgGrp.FILTER_REMOVE());
169
+ console.log("Sending FILTER_REMOVE to stream: '" + streamId + "'");
170
+ }
171
+ this.sendMessage(streamId, mw);
172
+ }
173
+ };
174
+ InEndPointEventHandler = __decorate([
175
+ (0, injection_js_1.Injectable)(),
176
+ __metadata("design:paramtypes", [message_class_1.StartSubscribeFeed, message_class_1.SportsFeedMessageGroup, D.StreamNameCode])
177
+ ], InEndPointEventHandler);
178
+ exports.InEndPointEventHandler = InEndPointEventHandler;
179
+ let StreamCommandProcessor = class StreamCommandProcessor {
180
+ constructor(_fsRepo, _sportsGrp) {
181
+ this._fsRepo = _fsRepo;
182
+ this._sportsGrp = _sportsGrp;
183
+ this.messageClassName = {
184
+ 0: 'DATA_RESET',
185
+ 1: 'DATA_INSERT_ODD',
186
+ 2: 'DATA_UPDATE_ODD',
187
+ 3: 'DATA_DELETE_ODD',
188
+ 4: 'DATA_INSERT_EVENT',
189
+ 5: 'DATA_UPDATE_EVENT',
190
+ 6: 'DATA_DELETE_EVENT',
191
+ 7: 'DATA_INSERT_MATCH',
192
+ 8: 'DATA_UPDATE_MATCH',
193
+ 9: 'DATA_DELETE_MATCH',
194
+ 10: 'TTL_REMOVE',
195
+ 11: 'TTL_RESTORE',
196
+ 13: 'FILTER_REMOVE',
197
+ 14: 'FILTER_SET',
198
+ 16: 'ADMIN_START_SUBSCRIBE_FEED',
199
+ 17: 'ADMIN_REFRESH'
200
+ };
201
+ }
202
+ //bypass the append snapshot call if overwrite is provided
203
+ process(stream, input, overwrite = null) {
204
+ //see what message
205
+ if (input != null && input instanceof data_structure_1.Incoming) {
206
+ let ss = input;
207
+ let className = this.messageClassName[ss.msgType().id()];
208
+ console.debug("SCP processing: " + className);
209
+ }
210
+ if (overwrite == null) {
211
+ let outgoing = this._fsRepo.appendSnapshot(stream, input);
212
+ for (let o of outgoing) {
213
+ //this.printMessage(o);
214
+ this._fsRepo.push(o);
215
+ }
216
+ }
217
+ else {
218
+ //bypass appending to snapshot
219
+ this._fsRepo.push(overwrite);
220
+ }
221
+ }
222
+ fsRepo() {
223
+ return this._fsRepo;
224
+ }
225
+ sportsGrp() {
226
+ return this._sportsGrp;
227
+ }
228
+ printMessage(outgoing) {
229
+ //print partition keys
230
+ let className = this.getClassName(outgoing);
231
+ let snapshot = outgoing.delta();
232
+ this.printPartitionKeys(className, snapshot);
233
+ //print contents
234
+ let id = outgoing.msgType().id();
235
+ switch (id) {
236
+ case this._sportsGrp.DATA_INSERT_MATCH().id():
237
+ case this._sportsGrp.DATA_UPDATE_MATCH().id():
238
+ case this._sportsGrp.DATA_DELETE_MATCH().id():
239
+ this.printMatch(className, snapshot);
240
+ break;
241
+ case this._sportsGrp.DATA_INSERT_ODD().id():
242
+ case this._sportsGrp.DATA_UPDATE_ODD().id():
243
+ case this._sportsGrp.DATA_DELETE_ODD().id():
244
+ this.printRecord(className, snapshot);
245
+ break;
246
+ }
247
+ }
248
+ getClassName(outgoing) {
249
+ if (!(outgoing instanceof core_1.TTLOutgoing)) {
250
+ return this.messageClassName[outgoing.msgType().id()];
251
+ }
252
+ else {
253
+ let TTLOutgoing = outgoing;
254
+ if (TTLOutgoing.getTtlType() == core_1.TTLType.REMOVE) {
255
+ return "TTL_REM";
256
+ }
257
+ else {
258
+ return "TTL_RES";
259
+ }
260
+ }
261
+ }
262
+ printMatch(identifier, snapshot) {
263
+ let matches = snapshot.matches();
264
+ for (let i = 0; i < matches.length; i++) {
265
+ console.log(`[${identifier}] ${matches[i].id()}`);
266
+ }
267
+ }
268
+ printRecord(identifier, snapshot) {
269
+ let matches = snapshot.matches();
270
+ for (let i = 0; i < matches.length; i++) {
271
+ let events = matches[i].events();
272
+ for (let j = 0; j < events.length; j++) {
273
+ let records = events[j].records();
274
+ for (let k = 0; k < records.length; k++) {
275
+ let rec = records[k];
276
+ console.log(`[${identifier}] ${rec.matchId()}_${rec.eventId()}_${rec.id()}_${rec.source()}_${rec.oddType()}_${rec.oddFormat()}`);
277
+ }
278
+ }
279
+ }
280
+ }
281
+ printPartitionKeys(identifier, snapshot) {
282
+ let partitionsString = snapshot.getPartitions().toString();
283
+ console.log(`[${identifier}][PK]${partitionsString}`);
284
+ }
285
+ };
286
+ StreamCommandProcessor = __decorate([
287
+ (0, injection_js_1.Injectable)(),
288
+ __param(0, (0, injection_js_1.Inject)(core_1.FSREPO_IMPL)),
289
+ __metadata("design:paramtypes", [Object, message_class_1.SportsFeedMessageGroup])
290
+ ], StreamCommandProcessor);
291
+ exports.StreamCommandProcessor = StreamCommandProcessor;
292
+ let SportsFeedInProcessor = class SportsFeedInProcessor extends D.IMessageGroupProcessor {
293
+ constructor(scp, sportsFeedMsgGroup, snCode, recycleBin) {
294
+ super(sportsFeedMsgGroup);
295
+ this.scp = scp;
296
+ this.sportsFeedMsgGroup = sportsFeedMsgGroup;
297
+ this.snCode = snCode;
298
+ this.recycleBin = recycleBin;
299
+ }
300
+ process(mw) {
301
+ //validate stream name
302
+ let streamName = mw.getMetaInfo(this.snCode.code());
303
+ if (streamName == null || streamName === "") {
304
+ console.log("SportsFeedInProcessor received messagewrapper with no StreamName. Ignoring.");
305
+ return;
306
+ }
307
+ if (!this.isFeedMessage(mw)) {
308
+ console.log("SportsFeedInProcessor received unknown messageclass: " + circular.stringify(mw.messageClass) + ". Ignoring");
309
+ return;
310
+ }
311
+ if (this.sportsFeedMsgGroup.isIndicatorMessage(mw.messageClass)) {
312
+ let outgoing = this.processIndicatorMessage(mw);
313
+ this.scp.process(streamName, null, outgoing);
314
+ }
315
+ else {
316
+ let incoming = new MergeableIncoming(this.sportsFeedMsgGroup, mw.messageClass, streamName, mw.message, this.recycleBin);
317
+ this.triggerTtlRestoreIfApplicable(incoming);
318
+ this.scp.process(incoming.stream(), incoming);
319
+ }
320
+ }
321
+ processIndicatorMessage(mw) {
322
+ //clear RecycleBin for TTL_RESTORE_START
323
+ if (mw.messageClass == this.sportsFeedMsgGroup.TTL_RESTORE_START()) {
324
+ mw.message.getPartitions().forEach(partitionKey => {
325
+ this.recycleBin.clearBin(partitionKey);
326
+ });
327
+ }
328
+ //generate outgoing message
329
+ let outgoing = new data_structure_1.OutgoingImpl(mw.messageClass, mw.message);
330
+ return outgoing;
331
+ }
332
+ triggerTtlRestoreIfApplicable(incomingMsg) {
333
+ if (this.getRecycleBin() != null) {
334
+ if (this.getRecycleBin().getTtlConfig() != null && this.getRecycleBin().getTtlConfig().isEnableTtl()) {
335
+ //check if TTLRestore is triggered for the incoming set of partition keys
336
+ let ttlRestore = this.getRecycleBin().getTtlRestoreSnapshot(incomingMsg);
337
+ //pass to SCP if there is TTLRestore
338
+ if (ttlRestore != null) {
339
+ this.scp.process(ttlRestore.getStream(), ttlRestore);
340
+ }
341
+ }
342
+ }
343
+ }
344
+ triggerTtlRemove() {
345
+ if (this.getRecycleBin() != null) {
346
+ let removeSnapshotList = this.getRecycleBin().getTtlRemoveSnapshot();
347
+ removeSnapshotList.forEach(removeSS => {
348
+ this.scp.process(removeSS.getStream(), removeSS);
349
+ });
350
+ }
351
+ }
352
+ startTtlVerification() {
353
+ if (this.getRecycleBin() != null) {
354
+ let ttlConfig = this.getRecycleBin().getTtlConfig();
355
+ if (ttlConfig != null && ttlConfig.isEnableTtl()) {
356
+ let runInterval = ttlConfig.getRunInterval();
357
+ setInterval(() => this.triggerTtlRemove(), runInterval);
358
+ console.log("Scheduled TTLCheck with interval of " + runInterval);
359
+ }
360
+ else {
361
+ console.log("No TTL set. Not scheduling any TTL.");
362
+ }
363
+ }
364
+ }
365
+ isFeedMessage(mw) {
366
+ let msgCls = mw.messageClass;
367
+ if (msgCls instanceof message_class_1.InsertOddMessageClass || msgCls instanceof message_class_1.UpdateOddMessageClass
368
+ || msgCls instanceof message_class_1.DeleteOddMessageClass || msgCls instanceof message_class_1.InsertEventMessageClass
369
+ || msgCls instanceof message_class_1.UpdateEventMessageClass || msgCls instanceof message_class_1.DeleteEventMessageClass
370
+ || msgCls instanceof message_class_1.InsertMatchMessageClass || msgCls instanceof message_class_1.UpdateMatchMessageClass
371
+ || msgCls instanceof message_class_1.DeleteMatchMessageClass || msgCls instanceof message_class_1.RefreshMessageClass
372
+ || msgCls instanceof message_class_1.TTLRemoveMessageClass || msgCls instanceof message_class_1.TTLRestoreMessageClass
373
+ || msgCls instanceof message_class_1.ResetMessageClass || msgCls instanceof message_class_1.SwitchFilterStartMessageClass
374
+ || msgCls instanceof message_class_1.SwitchFilterFailMessageClass
375
+ || msgCls == this.sportsFeedMsgGroup.SWITCH_FILTER_START() || msgCls == this.sportsFeedMsgGroup.SWITCH_FILTER_END()
376
+ || msgCls == this.sportsFeedMsgGroup.FULLSNAPSHOT_START() || msgCls == this.sportsFeedMsgGroup.FULLSNAPSHOT_END()
377
+ || msgCls == this.sportsFeedMsgGroup.SWITCH_FILTER_FAIL()
378
+ || msgCls == this.sportsFeedMsgGroup.TTL_RESTORE_START() || msgCls == this.sportsFeedMsgGroup.TTL_RESTORE_END()) {
379
+ return true;
380
+ }
381
+ return false;
382
+ }
383
+ getScp() {
384
+ return this.scp;
385
+ }
386
+ getRecycleBin() {
387
+ return this.recycleBin;
388
+ }
389
+ };
390
+ SportsFeedInProcessor = __decorate([
391
+ (0, injection_js_1.Injectable)(),
392
+ __metadata("design:paramtypes", [StreamCommandProcessor, message_class_1.SportsFeedMessageGroup, D.StreamNameCode, core_1.RecycleBin])
393
+ ], SportsFeedInProcessor);
394
+ exports.SportsFeedInProcessor = SportsFeedInProcessor;
395
+ class MergeableIncoming extends data_structure_1.Incoming {
396
+ constructor(sportsGrp, snapshotType, streamName, data, bin) {
397
+ super(snapshotType, streamName, data);
398
+ this.sportsGrp = sportsGrp;
399
+ this.bin = bin;
400
+ }
401
+ apply(before) {
402
+ if (before == null) {
403
+ before = data_structure_1.IndexedSnapshotImpl.EMPTY_SNAPSHOT;
404
+ }
405
+ let outgoCol = [];
406
+ this.beforeSnap = before;
407
+ let wrap = new data_structure_1.MergeableWrapper();
408
+ if (this.msgType() instanceof message_class_1.InsertMatchMessageClass || this.msgType() instanceof message_class_1.InsertEventMessageClass || this.msgType() instanceof message_class_1.DeleteMatchMessageClass || this.msgType() instanceof message_class_1.DeleteEventMessageClass || this.msgType() instanceof message_class_1.UpdateEventMessageClass) {
409
+ let msgBefore = merge_1.SnapshotUtil.combineSnapshots(this.msgType(), this.data(), before);
410
+ let incomeCol = merge_1.DeltaTransformingLogicImpl.transform(this.sportsGrp, this.stream(), this.msgType(), this.data(), msgBefore);
411
+ for (let i of incomeCol) {
412
+ let iDelta = new core_1.Delta(i, msgBefore.getAfterSs(), this.beforeSnap);
413
+ outgoCol.push(iDelta);
414
+ this.beforeSnap = msgBefore.getAfterSs();
415
+ }
416
+ wrap.setAfter(msgBefore.getAfterSs());
417
+ wrap.setDeltaOut(outgoCol);
418
+ }
419
+ else if (this.msgType() instanceof message_class_1.TTLRemoveMessageClass) {
420
+ let ttlWrapper = null;
421
+ let snapshot = before;
422
+ let keyRemoved = new Collections.Dictionary();
423
+ let ttlRemoveList = [];
424
+ let atleastOneKeyRemoved = false;
425
+ let allKeys = before.getPartitionMap();
426
+ for (let key of this.data().getPartitions().toArray()) {
427
+ console.log("removing keys from upstream " + JSON.stringify(key));
428
+ if (!allKeys.containsKey(key)) {
429
+ continue;
430
+ }
431
+ atleastOneKeyRemoved = true;
432
+ keyRemoved.setValue(key, allKeys.getValue(key));
433
+ ttlWrapper = this.bin.removeData(new data_structure_1.TTLWrapper(), snapshot, key);
434
+ snapshot = ttlWrapper.getRemainingSs();
435
+ ttlRemoveList.push(ttlWrapper.getRemovedSs());
436
+ }
437
+ if (atleastOneKeyRemoved) {
438
+ console.log("reseting ----->" + JSON.stringify(keyRemoved.keys()));
439
+ wrap = merge_1.DeltaTransformingLogicImpl.transformTTLRemove(ttlRemoveList, this.bin.getGrp(), this.stream(), before, snapshot, keyRemoved);
440
+ }
441
+ else {
442
+ wrap.setDeltaOut([]);
443
+ wrap.setAfter(before);
444
+ }
445
+ }
446
+ else if (this.msgType() instanceof message_class_1.TTLRestoreMessageClass) {
447
+ let ttlWrapper = null;
448
+ let snapshot = before;
449
+ let restoredKeys = new Collections.Dictionary();
450
+ let atleastOneKeyRestored = false;
451
+ let matches = {};
452
+ let ttlRestoreList = [];
453
+ for (let key of this.data().getPartitions().toArray()) {
454
+ console.log("restoring keys from upstream " + JSON.stringify(key));
455
+ if (this.bin.containData(key)) {
456
+ atleastOneKeyRestored = true;
457
+ let restoredTime = new Date().getTime();
458
+ restoredKeys.setValue(key, restoredTime);
459
+ matches = this.bin.copyData(key, matches);
460
+ ttlWrapper = this.bin.restoreData(new data_structure_1.TTLWrapper, snapshot, key, restoredTime);
461
+ snapshot = ttlWrapper.getRemainingSs();
462
+ ttlRestoreList.push(ttlWrapper.getRestoredSs());
463
+ }
464
+ }
465
+ if (atleastOneKeyRestored) {
466
+ console.log("restoring ----->" + JSON.stringify(restoredKeys.keys()));
467
+ wrap = merge_1.DeltaTransformingLogicImpl.transformTTLRestore(ttlRestoreList, this.bin.getGrp(), this.stream(), before, snapshot, matches, restoredKeys);
468
+ }
469
+ else {
470
+ wrap.setAfter(before);
471
+ wrap.setDeltaOut([]);
472
+ }
473
+ }
474
+ else {
475
+ let msgBefore = merge_1.SnapshotUtil.combineSnapshots(this.msgType(), this.data(), before);
476
+ let iDelta = new core_1.Delta(this, msgBefore.getAfterSs(), before);
477
+ outgoCol.push(iDelta);
478
+ wrap.setDeltaOut(outgoCol);
479
+ wrap.setAfter(msgBefore.getAfterSs());
480
+ }
481
+ return wrap;
482
+ }
483
+ }
484
+ exports.MergeableIncoming = MergeableIncoming;
@@ -9,5 +9,5 @@ export declare class SoccerCodec implements ICodec {
9
9
  decodeMatch(input: proto.BaseMatch[], pool: string[], action: EncodeAction, key: rec.PartitionKey): rec.IBetMatch[];
10
10
  decodeEvent(input: proto.BaseEvent[], pool: string[], action: EncodeAction, partition: rec.PartitionKey): rec.IBetMatch[];
11
11
  decodeRecord(input: proto.BaseRecord[], pool: string[], action: EncodeAction, partition: rec.PartitionKey): rec.IBetMatch[];
12
- decodeState(matchId: string, eventId: string, input: proto.IBaseEventState, pool: string[], action: EncodeAction, partition: rec.PartitionKey, createTime?: number): rec.SoccerEventState;
12
+ decodeState(matchId: string, eventId: string, input: proto.BaseEventState, pool: string[], action: EncodeAction, partition: rec.PartitionKey, createTime?: number): rec.SoccerEventState;
13
13
  }