1 /* |
1 /* |
2 * Modelled Trace API |
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 * |
3 */ |
19 */ |
4 IriSP.TraceManager = function($) { |
20 /* FIXME: properly use require.js feature. This will do for debugging in the meantime */ |
|
21 window.tracemanager = (function($) { |
5 // If there are more than MAX_FAILURE_COUNT synchronisation |
22 // If there are more than MAX_FAILURE_COUNT synchronisation |
6 // failures, then disable synchronisation |
23 // failures, then disable synchronisation |
7 MAX_FAILURE_COUNT = 20; |
24 MAX_FAILURE_COUNT = 20; |
8 |
25 |
9 // If there are more than MAX_BUFFER_SIZE obsels in the buffer, |
26 // If there are more than MAX_BUFFER_SIZE obsels in the buffer, |
41 // prefixing it with c |
58 // prefixing it with c |
42 var data = 'c' + JSON.stringify(temp.map(function (o) { return o.toCompactJSON(); })); |
59 var data = 'c' + JSON.stringify(temp.map(function (o) { return o.toCompactJSON(); })); |
43 // Swap " (very frequent, which will be |
60 // Swap " (very frequent, which will be |
44 // serialized into %22) and ; (rather rare), this |
61 // serialized into %22) and ; (rather rare), this |
45 // saves some bytes |
62 // saves some bytes |
46 data = data.replace(/[;"]/g, function(s){ return s == ';' ? '"' : ';'; }).replace(/#/g, '%23'); |
63 data = data.replace(/[;"#]/g, function(s){ return s == ';' ? '"' : ( s == '"' ? ';' : '%23'); }); |
47 // FIXME: check data length (< 2K is safe) |
64 // FIXME: check data length (< 2K is safe) |
48 var request=$('<img />').error( function() { this.failureCount += 1; }) |
65 var request=$('<img />').error( function() { this.failureCount += 1; }) |
49 .load( function() { this.failureCount = 0; }) |
66 .load( function() { this.failureCount = 0; }) |
50 .attr('src', this.url + 'trace/?data=' + data); |
67 .attr('src', this.url + 'trace/?data=' + data); |
51 } |
68 } |
72 }, |
89 }, |
73 |
90 |
74 /* Sync mode: delayed, sync (immediate sync), none (no |
91 /* Sync mode: delayed, sync (immediate sync), none (no |
75 * synchronisation with server, the trace has to be explicitly saved |
92 * synchronisation with server, the trace has to be explicitly saved |
76 * if needed */ |
93 * if needed */ |
77 set_sync_mode: function(mode) { |
94 set_sync_mode: function(mode, default_subject) { |
78 this.sync_mode = mode; |
95 this.sync_mode = mode; |
79 if (! this.isReady && mode !== "none") |
96 if (! this.isReady && mode !== "none") |
80 this.init(); |
97 this.init(default_subject); |
81 if (mode == 'delayed') { |
98 if (mode == 'delayed') { |
82 this.start_timer(); |
99 this.start_timer(); |
83 } else { |
100 } else { |
84 this.stop_timer(); |
101 this.stop_timer(); |
85 } |
102 } |
118 }, |
135 }, |
119 |
136 |
120 /* |
137 /* |
121 * Initialize the sync service |
138 * Initialize the sync service |
122 */ |
139 */ |
123 init: function() { |
140 init: function(default_subject) { |
124 var self = this; |
141 var self = this; |
125 if (this.isReady) |
142 if (this.isReady) |
126 /* Already initialized */ |
143 /* Already initialized */ |
127 return; |
144 return; |
|
145 if (typeof default_subject === 'undefined') |
|
146 default_subject = 'anonymous'; |
128 if (this.mode == 'GET') |
147 if (this.mode == 'GET') |
129 { |
148 { |
130 var request=$('<img/>').attr('src', this.url + 'login?userinfo={"name":"ktbs4js"}'); |
149 var request=$('<img/>').attr('src', this.url + 'login?userinfo={"default_subject": "' + default_subject + '"}'); |
131 // Do not wait for the return, assume it is |
150 // Do not wait for the return, assume it is |
132 // initialized. This assumption will not work anymore |
151 // initialized. This assumption will not work anymore |
133 // if login returns some necessary information |
152 // if login returns some necessary information |
134 this.isReady = true; |
153 this.isReady = true; |
135 } |
154 } |
136 else |
155 else |
137 { |
156 { |
138 $.ajax({ url: this.url + 'login', |
157 $.ajax({ url: this.url + 'login', |
139 type: 'POST', |
158 type: 'POST', |
140 data: 'userinfo={"name":"ktbs4js"}', |
159 data: 'userinfo={"default_subject":"' + default_subject + '"}', |
141 success: function(data, textStatus, jqXHR) { |
160 success: function(data, textStatus, jqXHR) { |
142 self.isReady = true; |
161 self.isReady = true; |
143 if (self.buffer.length) { |
162 if (self.buffer.length) { |
144 self.flush(); |
163 self.flush(); |
145 } |
164 } |
193 /* Sync mode: delayed, sync (immediate sync), none (no |
212 /* Sync mode: delayed, sync (immediate sync), none (no |
194 * synchronisation with server, the trace has to be explicitly saved |
213 * synchronisation with server, the trace has to be explicitly saved |
195 * if needed */ |
214 * if needed */ |
196 set_sync_mode: function(mode) { |
215 set_sync_mode: function(mode) { |
197 if (this.syncservice !== null) { |
216 if (this.syncservice !== null) { |
198 this.syncservice.set_sync_mode(mode); |
217 this.syncservice.set_sync_mode(mode, this.default_subject); |
199 } |
218 } |
200 }, |
219 }, |
201 |
220 |
202 /* |
221 /* |
203 * Return a list of the obsels of this trace matching the parameters |
222 * Return a list of the obsels of this trace matching the parameters |
246 } |
265 } |
247 return undefined; |
266 return undefined; |
248 }, |
267 }, |
249 |
268 |
250 set_default_subject: function(subject) { |
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? |
251 this.default_subject = subject; |
273 this.default_subject = subject; |
252 }, |
274 }, |
253 |
275 |
254 get_default_subject: function() { |
276 get_default_subject: function() { |
255 return this.default_subject; |
277 return this.default_subject; |
430 * Return a compact JSON representation of the obsel. |
452 * Return a compact JSON representation of the obsel. |
431 * Use predefined + custom shorthands for types/properties |
453 * Use predefined + custom shorthands for types/properties |
432 */ |
454 */ |
433 toCompactJSON: function() { |
455 toCompactJSON: function() { |
434 var r = { |
456 var r = { |
435 "@i": this.id, |
|
436 "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type), |
457 "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type), |
437 "@b": this.begin, |
458 "@b": this.begin, |
438 "@s": this.subject |
|
439 }; |
459 }; |
|
460 // Transmit subject only if different from default_subject |
|
461 if (this.subject !== this.trace.default_subject) |
|
462 r["@s"] = this.subject; |
|
463 |
440 // Store duration (to save some bytes) and only if it is non-null |
464 // Store duration (to save some bytes) and only if it is non-null |
441 if (this.begin !== this.end) |
465 if (this.begin !== this.end) |
442 r["@d"] = this.end - this.begin; |
466 r["@d"] = this.end - this.begin; |
443 |
467 |
|
468 // Store id only if != "" |
|
469 if (this.id !== "") |
|
470 r["@i"] = this.id; |
|
471 |
444 for (var prop in this.attributes) { |
472 for (var prop in this.attributes) { |
445 if (this.hasOwnProperty(prop)) |
473 if (this.attributes.hasOwnProperty(prop)) |
446 { |
474 { |
447 var v = this.attributes[prop]; |
475 var v = this.attributes[prop]; |
448 r[prop] = this.trace.shorthands.hasOwnProperty(v) ? this.trace.shorthands[v] : v; |
476 r[prop] = this.trace.shorthands.hasOwnProperty(v) ? this.trace.shorthands[v] : v; |
449 } |
477 } |
450 } |
478 } |
493 url = params.url ? params.url : ""; |
521 url = params.url ? params.url : ""; |
494 requestmode = params.requestmode ? params.requestmode : "POST"; |
522 requestmode = params.requestmode ? params.requestmode : "POST"; |
495 syncmode = params.syncmode ? params.syncmode : "none"; |
523 syncmode = params.syncmode ? params.syncmode : "none"; |
496 default_subject = params.default_subject ? params.default_subject : "default"; |
524 default_subject = params.default_subject ? params.default_subject : "default"; |
497 var t = new Trace(url, requestmode); |
525 var t = new Trace(url, requestmode); |
|
526 t.set_default_subject(default_subject); |
498 t.set_sync_mode(syncmode); |
527 t.set_sync_mode(syncmode); |
499 t.set_default_subject(default_subject); |
|
500 this.traces[name] = t; |
528 this.traces[name] = t; |
501 return t; |
529 return t; |
502 } |
530 } |
503 }; |
531 }; |
504 |
532 |