|
1 /* |
|
2 * Modelled Trace API |
|
3 * |
|
4 * This file is part of ktbs4js. |
|
5 * |
|
6 * ktbs4js is free software: you can redistribute it and/or modify it |
|
7 * under the terms of the GNU Lesser General Public License as |
|
8 * published by the Free Software Foundation, either version 3 of the |
|
9 * License, or (at your option) any later version. |
|
10 * |
|
11 * ktbs4js is distributed in the hope that it will be useful, |
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU Lesser General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU Lesser General Public |
|
17 * License along with ktbs4js. If not, see <http://www.gnu.org/licenses/>. |
|
18 * |
|
19 */ |
|
20 /* FIXME: properly use require.js feature. This will do for debugging in the meantime */ |
|
21 window.tracemanager = (function($) { |
|
22 // If there are more than MAX_FAILURE_COUNT synchronisation |
|
23 // failures, then disable synchronisation |
|
24 MAX_FAILURE_COUNT = 20; |
|
25 |
|
26 // If there are more than MAX_BUFFER_SIZE obsels in the buffer, |
|
27 // then "compress" them as a single "ktbsFullBuffer" |
|
28 MAX_BUFFER_SIZE = 500; |
|
29 |
|
30 var BufferedService_prototype = { |
|
31 /* |
|
32 * Buffered service for traces |
|
33 */ |
|
34 // url: "", |
|
35 // buffer: [], |
|
36 // isReady: false, |
|
37 // timer: null, |
|
38 // failureCount: 0, |
|
39 |
|
40 /* Flush buffer */ |
|
41 flush: function() { |
|
42 // FIXME: add mutex on this.buffer |
|
43 if (! this.isReady) |
|
44 { |
|
45 if (window.console) window.console.log("Sync service not ready"); |
|
46 } else if (this.failureCount > MAX_FAILURE_COUNT) |
|
47 { |
|
48 // Disable synchronisation |
|
49 this.set_sync_mode('none'); |
|
50 } else if (this.buffer.length) { |
|
51 var temp = this.buffer; |
|
52 this.buffer = []; |
|
53 |
|
54 if (this.mode == 'GET') |
|
55 { |
|
56 // GET mode: do some data mangline. We mark the |
|
57 // "compressed" nature of the generated JSON by |
|
58 // prefixing it with c |
|
59 var data = 'c' + JSON.stringify(temp.map(function (o) { return o.toCompactJSON(); })); |
|
60 // Swap " (very frequent, which will be |
|
61 // serialized into %22) and ; (rather rare), this |
|
62 // saves some bytes |
|
63 data = data.replace(/[;"#]/g, function(s){ return s == ';' ? '"' : ( s == '"' ? ';' : '%23'); }); |
|
64 // FIXME: check data length (< 2K is safe) |
|
65 var request=$('<img />').error( function() { this.failureCount += 1; }) |
|
66 .load( function() { this.failureCount = 0; }) |
|
67 .attr('src', this.url + 'trace/?data=' + data); |
|
68 } |
|
69 else |
|
70 { |
|
71 $.ajax({ url: this.url + 'trace/', |
|
72 type: 'POST', |
|
73 contentType: 'application/json', |
|
74 data: JSON.stringify(temp.map(function (o) { return o.toJSON(); })), |
|
75 processData: false, |
|
76 // Type of the returned data. |
|
77 dataType: "html", |
|
78 error: function(jqXHR, textStatus, errorThrown) { |
|
79 if (window.console) window.console.log("Error when sending buffer:", textStatus); |
|
80 this.failureCount += 1; |
|
81 }, |
|
82 success: function(data, textStatus, jqXHR) { |
|
83 // Reset failureCount to 0 as soon as there is 1 valid answer |
|
84 this.failureCount = 0; |
|
85 } |
|
86 }); |
|
87 } |
|
88 } |
|
89 }, |
|
90 |
|
91 /* Sync mode: delayed, sync (immediate sync), none (no |
|
92 * synchronisation with server, the trace has to be explicitly saved |
|
93 * if needed */ |
|
94 set_sync_mode: function(mode, default_subject) { |
|
95 this.sync_mode = mode; |
|
96 if (! this.isReady && mode !== "none") |
|
97 this.init(default_subject); |
|
98 if (mode == 'delayed') { |
|
99 this.start_timer(); |
|
100 } else { |
|
101 this.stop_timer(); |
|
102 } |
|
103 }, |
|
104 |
|
105 /* Enqueue an obsel */ |
|
106 enqueue: function(obsel) { |
|
107 if (this.buffer.length > MAX_BUFFER_SIZE) |
|
108 { |
|
109 obsel = new Obsel('ktbsFullBuffer', this.buffer[0].begin, |
|
110 this.buffer[this.buffer.length - 1].end, this.buffer[0].subject); |
|
111 obsel.trace = this.buffer[0].trace; |
|
112 this.buffer = []; |
|
113 } |
|
114 this.buffer.push(obsel); |
|
115 if (this.sync_mode === 'sync') { |
|
116 // Immediate sync of the obsel. |
|
117 this.flush(); |
|
118 } |
|
119 }, |
|
120 |
|
121 start_timer: function() { |
|
122 var self = this; |
|
123 if (this.timer === null) { |
|
124 this.timer = window.setInterval(function() { |
|
125 self.flush(); |
|
126 }, this.timeOut); |
|
127 } |
|
128 }, |
|
129 |
|
130 stop_timer: function() { |
|
131 if (this.timer !== null) { |
|
132 window.clearInterval(this.timer); |
|
133 this.timer = null; |
|
134 } |
|
135 }, |
|
136 |
|
137 /* |
|
138 * Initialize the sync service |
|
139 */ |
|
140 init: function(default_subject) { |
|
141 var self = this; |
|
142 if (this.isReady) |
|
143 /* Already initialized */ |
|
144 return; |
|
145 if (typeof default_subject === 'undefined') |
|
146 default_subject = 'anonymous'; |
|
147 if (this.mode == 'GET') |
|
148 { |
|
149 var request=$('<img/>').attr('src', this.url + 'login?userinfo={"default_subject": "' + default_subject + '"}'); |
|
150 // Do not wait for the return, assume it is |
|
151 // initialized. This assumption will not work anymore |
|
152 // if login returns some necessary information |
|
153 this.isReady = true; |
|
154 } |
|
155 else |
|
156 { |
|
157 $.ajax({ url: this.url + 'login', |
|
158 type: 'POST', |
|
159 data: 'userinfo={"default_subject":"' + default_subject + '"}', |
|
160 success: function(data, textStatus, jqXHR) { |
|
161 self.isReady = true; |
|
162 if (self.buffer.length) { |
|
163 self.flush(); |
|
164 } |
|
165 } |
|
166 }); |
|
167 } |
|
168 } |
|
169 }; |
|
170 var BufferedService = function(url, mode) { |
|
171 this.url = url; |
|
172 this.buffer = []; |
|
173 this.isReady = false; |
|
174 this.timer = null; |
|
175 this.failureCount = 0; |
|
176 // sync_mode is either "none", "sync" or "buffered" |
|
177 this.sync_mode = "none"; |
|
178 /* mode can be either POST or GET */ |
|
179 if (mode == 'POST' || mode == 'GET') |
|
180 this.mode = mode; |
|
181 else |
|
182 this.mode = 'POST'; |
|
183 /* Flush buffer every timeOut ms if the sync_mode is delayed */ |
|
184 this.timeOut = 2000; |
|
185 }; |
|
186 BufferedService.prototype = BufferedService_prototype; |
|
187 |
|
188 var Trace_prototype = { |
|
189 /* FIXME: We could/should use a sorted list such as |
|
190 http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html |
|
191 to speed up queries based on time */ |
|
192 obsels: [], |
|
193 /* Trace URI */ |
|
194 uri: "", |
|
195 default_subject: "", |
|
196 /* baseuri is used as the base URI to resolve relative |
|
197 * attribute-type names in obsels. Strictly speaking, this |
|
198 * should rather be expressed as a reference to model, or |
|
199 * more generically, as a qname/URI dict */ |
|
200 baseuri: "", |
|
201 /* Mapping of obsel type or property name to a compact |
|
202 * representation (shorthands). |
|
203 */ |
|
204 shorthands: null, |
|
205 syncservice: null, |
|
206 |
|
207 /* Define the trace URI */ |
|
208 set_uri: function(uri) { |
|
209 this.uri = uri; |
|
210 }, |
|
211 |
|
212 /* Sync mode: delayed, sync (immediate sync), none (no |
|
213 * synchronisation with server, the trace has to be explicitly saved |
|
214 * if needed */ |
|
215 set_sync_mode: function(mode) { |
|
216 if (this.syncservice !== null) { |
|
217 this.syncservice.set_sync_mode(mode, this.default_subject); |
|
218 } |
|
219 }, |
|
220 |
|
221 /* |
|
222 * Return a list of the obsels of this trace matching the parameters |
|
223 */ |
|
224 list_obsels: function(_begin, _end, _reverse) { |
|
225 var res; |
|
226 if (typeof _begin !== 'undefined' || typeof _end !== 'undefined') { |
|
227 /* |
|
228 * Not optimized yet. |
|
229 */ |
|
230 res = []; |
|
231 var l = this.obsels.length; |
|
232 for (var i = 0; i < l; i++) { |
|
233 var o = this.obsels[i]; |
|
234 if ((typeof _begin !== 'undefined' && o.begin > _begin) && (typeof _end !== 'undefined' && o.end < _end)) { |
|
235 res.push(o); |
|
236 } |
|
237 } |
|
238 } |
|
239 |
|
240 if (typeof _reverse !== 'undefined') { |
|
241 if (res !== undefined) { |
|
242 /* Should reverse the whole list. Make a copy. */ |
|
243 res = this.obsels.slice(0); |
|
244 } |
|
245 res.sort(function(a, b) { return b.begin - a.begin; }); |
|
246 return res; |
|
247 } |
|
248 |
|
249 if (res === undefined) { |
|
250 res = this.obsels; |
|
251 } |
|
252 return res; |
|
253 |
|
254 }, |
|
255 |
|
256 /* |
|
257 * Return the obsel of this trace identified by the URI, or undefined |
|
258 */ |
|
259 get_obsel: function(id) { |
|
260 for (var i = 0; i < this.obsels.length; i++) { |
|
261 /* FIXME: should check against variations of id/uri, take this.baseuri into account */ |
|
262 if (this.obsels[i].uri === id) { |
|
263 return this.obsels[i]; |
|
264 } |
|
265 } |
|
266 return undefined; |
|
267 }, |
|
268 |
|
269 set_default_subject: function(subject) { |
|
270 // FIXME: if we call this method after the sync_service |
|
271 // init method, then the default_subject will not be |
|
272 // consistent anymore. Maybe we should then call init() again? |
|
273 this.default_subject = subject; |
|
274 }, |
|
275 |
|
276 get_default_subject: function() { |
|
277 return this.default_subject; |
|
278 }, |
|
279 |
|
280 /* (type:ObselType, begin:int, end:int?, subject:str?, attributes:[AttributeType=>any]?) */ |
|
281 /* Create a new obsel and add it to the trace */ |
|
282 create_obsel: function(type, begin, end, subject, _attributes) { |
|
283 var o = new Obsel(type, begin, end, subject); |
|
284 if (typeof _attributes !== 'undefined') { |
|
285 o.attributes = _attributes; |
|
286 } |
|
287 o.trace = this; |
|
288 this.obsels.push(o); |
|
289 if (this.syncservice !== null) |
|
290 this.syncservice.enqueue(o); |
|
291 }, |
|
292 |
|
293 /* Helper methods */ |
|
294 |
|
295 /* Create a new obsel with the given attributes */ |
|
296 trace: function(type, _attributes, _begin, _end, _subject) { |
|
297 var t = (new Date()).getTime(); |
|
298 if (typeof begin === 'undefined') { |
|
299 _begin = t; |
|
300 } |
|
301 if (typeof end === 'undefined') { |
|
302 _end = _begin; |
|
303 } |
|
304 if (typeof subject === 'undefined') { |
|
305 _subject = this.default_subject; |
|
306 } |
|
307 if (typeof _attributes === 'undefined') { |
|
308 _attributes = {}; |
|
309 } |
|
310 return this.create_obsel(type, _begin, _end, _subject, _attributes); |
|
311 } |
|
312 }; |
|
313 |
|
314 var Trace = function(uri, requestmode) { |
|
315 /* FIXME: We could/should use a sorted list such as |
|
316 http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html |
|
317 to speed up queries based on time */ |
|
318 this.obsels = []; |
|
319 /* Trace URI */ |
|
320 if (uri === undefined) |
|
321 uri = ""; |
|
322 this.uri = uri; |
|
323 this.sync_mode = "none"; |
|
324 this.default_subject = ""; |
|
325 this.shorthands = {}; |
|
326 /* baseuri is used a the base URI to resolve relative attribute names in obsels */ |
|
327 this.baseuri = ""; |
|
328 |
|
329 this.syncservice = new BufferedService(uri, requestmode); |
|
330 $(window).unload( function () { |
|
331 if (this.syncservice && this.sync_mode !== 'none') { |
|
332 this.syncservice.flush(); |
|
333 this.syncservice.stop_timer(); |
|
334 } |
|
335 }); |
|
336 }; |
|
337 Trace.prototype = Trace_prototype; |
|
338 |
|
339 var Obsel_prototype = { |
|
340 /* The following attributes are here for documentation |
|
341 * purposes. They MUST be defined in the constructor |
|
342 * function. */ |
|
343 trace: undefined, |
|
344 type: undefined, |
|
345 begin: undefined, |
|
346 end: undefined, |
|
347 subject: undefined, |
|
348 /* Dictionary indexed by ObselType URIs */ |
|
349 attributes: {}, |
|
350 |
|
351 /* Method definitions */ |
|
352 get_trace: function() { |
|
353 return this.trace; |
|
354 }, |
|
355 |
|
356 get_obsel_type: function() { |
|
357 return this.type; |
|
358 }, |
|
359 get_begin: function() { |
|
360 return this.begin; |
|
361 }, |
|
362 get_end: function() { |
|
363 return this.end; |
|
364 }, |
|
365 get_subject: function() { |
|
366 return this.subject; |
|
367 }, |
|
368 |
|
369 list_attribute_types: function() { |
|
370 var result = []; |
|
371 for (var prop in this.attributes) { |
|
372 if (this.attributes.hasOwnProperty(prop)) |
|
373 result.push(prop); |
|
374 } |
|
375 /* FIXME: we return URIs here instead of AttributeType elements */ |
|
376 return result; |
|
377 }, |
|
378 |
|
379 list_relation_types: function() { |
|
380 /* FIXME: not implemented yet */ |
|
381 }, |
|
382 |
|
383 list_related_obsels: function (rt) { |
|
384 /* FIXME: not implemented yet */ |
|
385 }, |
|
386 list_inverse_relation_types: function () { |
|
387 /* FIXME: not implemented yet */ |
|
388 }, |
|
389 list_relating_obsels: function (rt) { |
|
390 /* FIXME: not implemented yet */ |
|
391 }, |
|
392 /* |
|
393 * Return the value of the given attribute type for this obsel |
|
394 */ |
|
395 get_attribute_value: function(at) { |
|
396 if (typeof at === "string") |
|
397 /* It is a URI */ |
|
398 return this.attributes[at]; |
|
399 else |
|
400 /* FIXME: check that at is instance of AttributeType */ |
|
401 return this.attributes[at.uri]; |
|
402 }, |
|
403 |
|
404 |
|
405 /* obsel modification (trace amendment) */ |
|
406 |
|
407 set_attribute_value: function(at, value) { |
|
408 if (typeof at === "string") |
|
409 /* It is a URI */ |
|
410 this.attributes[at] = value; |
|
411 /* FIXME: check that at is instance of AttributeType */ |
|
412 else |
|
413 this.attributes[at.uri] = value; |
|
414 }, |
|
415 |
|
416 del_attribute_value: function(at) { |
|
417 if (typeof at === "string") |
|
418 /* It is a URI */ |
|
419 delete this.attributes[at]; |
|
420 /* FIXME: check that at is instance of AttributeType */ |
|
421 else |
|
422 delete this.attributes[at.uri]; |
|
423 }, |
|
424 |
|
425 add_related_obsel: function(rt, value) { |
|
426 /* FIXME: not implemented yet */ |
|
427 }, |
|
428 |
|
429 del_related_obsel: function(rt, value) { |
|
430 /* FIXME: not implemented yet */ |
|
431 }, |
|
432 |
|
433 /* |
|
434 * Return a JSON representation of the obsel |
|
435 */ |
|
436 toJSON: function() { |
|
437 var r = { |
|
438 "@id": this.id, |
|
439 "@type": this.type, |
|
440 "begin": this.begin, |
|
441 "end": this.end, |
|
442 "subject": this.subject |
|
443 }; |
|
444 for (var prop in this.attributes) { |
|
445 if (this.attributes.hasOwnProperty(prop)) |
|
446 r[prop] = this.attributes[prop]; |
|
447 } |
|
448 return r; |
|
449 }, |
|
450 |
|
451 /* |
|
452 * Return a compact JSON representation of the obsel. |
|
453 * Use predefined + custom shorthands for types/properties |
|
454 */ |
|
455 toCompactJSON: function() { |
|
456 var r = { |
|
457 "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type), |
|
458 "@b": this.begin, |
|
459 }; |
|
460 // Transmit subject only if different from default_subject |
|
461 if (this.subject !== this.trace.default_subject) |
|
462 r["@s"] = this.subject; |
|
463 |
|
464 // Store duration (to save some bytes) and only if it is non-null |
|
465 if (this.begin !== this.end) |
|
466 r["@d"] = this.end - this.begin; |
|
467 |
|
468 // Store id only if != "" |
|
469 if (this.id !== "") |
|
470 r["@i"] = this.id; |
|
471 |
|
472 for (var prop in this.attributes) { |
|
473 if (this.attributes.hasOwnProperty(prop)) |
|
474 { |
|
475 var v = this.attributes[prop]; |
|
476 r[prop] = this.trace.shorthands.hasOwnProperty(v) ? this.trace.shorthands[v] : v; |
|
477 } |
|
478 } |
|
479 return r; |
|
480 }, |
|
481 |
|
482 toJSONstring: function() { |
|
483 return JSON.stringify(this.toJSON()); |
|
484 } |
|
485 }; |
|
486 |
|
487 var Obsel = function(type, begin, end, subject, attributes) { |
|
488 this.trace = undefined; |
|
489 this.uri = ""; |
|
490 this.id = ""; |
|
491 this.type = type; |
|
492 this.begin = begin; |
|
493 this.end = end; |
|
494 this.subject = subject; |
|
495 /* Is the obsel synched with the server ? */ |
|
496 this.sync_status = false; |
|
497 /* Dictionary indexed by ObselType URIs */ |
|
498 this.attributes = {}; |
|
499 }; |
|
500 Obsel.prototype = Obsel_prototype; |
|
501 |
|
502 var TraceManager_prototype = { |
|
503 traces: [], |
|
504 /* |
|
505 * Return the trace with id name |
|
506 * If it was not registered, return undefined. |
|
507 */ |
|
508 get_trace: function(name) { |
|
509 return this.traces[name]; |
|
510 }, |
|
511 |
|
512 /* |
|
513 * Explicitly create and initialize a new trace with the given name. |
|
514 * The optional uri parameter allows to initialize the trace URI. |
|
515 * |
|
516 * If another existed with the same name before, then it is replaced by a new one. |
|
517 */ |
|
518 init_trace: function(name, params) |
|
519 { |
|
520 if (window.console) window.console.log("init_trace", params); |
|
521 url = params.url ? params.url : ""; |
|
522 requestmode = params.requestmode ? params.requestmode : "POST"; |
|
523 syncmode = params.syncmode ? params.syncmode : "none"; |
|
524 default_subject = params.default_subject ? params.default_subject : "default"; |
|
525 var t = new Trace(url, requestmode); |
|
526 t.set_default_subject(default_subject); |
|
527 t.set_sync_mode(syncmode); |
|
528 this.traces[name] = t; |
|
529 return t; |
|
530 } |
|
531 }; |
|
532 |
|
533 var TraceManager = function() { |
|
534 this.traces = {}; |
|
535 }; |
|
536 TraceManager.prototype = TraceManager_prototype; |
|
537 |
|
538 var tracemanager = new TraceManager(); |
|
539 return tracemanager; |
|
540 })(jQuery); |