web/res/js/tracemanager.js
changeset 623 5b7d7ab6baff
parent 583 f655dafb741e
child 1304 10974bff4dae
equal deleted inserted replaced
622:7e5174fe9816 623:5b7d7ab6baff
     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,
    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;
   432           */
   454           */
   433          toCompactJSON: function() {
   455          toCompactJSON: function() {
   434              var r = {
   456              var r = {
   435                  "@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),
   436                  "@b": this.begin,
   458                  "@b": this.begin,
   437                  "@s": this.subject
       
   438              };
   459              };
       
   460              // Transmit subject only if different from default_subject
       
   461              if (this.subject !== this.trace.default_subject)
       
   462                  r["@s"] = this.subject;
       
   463 
   439              // 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
   440              if (this.begin !== this.end)
   465              if (this.begin !== this.end)
   441                  r["@d"] = this.end - this.begin;
   466                  r["@d"] = this.end - this.begin;
   442 
   467 
   443              // Store id only if != ""
   468              // Store id only if != ""
   496              url = params.url ? params.url : "";
   521              url = params.url ? params.url : "";
   497              requestmode = params.requestmode ? params.requestmode : "POST";
   522              requestmode = params.requestmode ? params.requestmode : "POST";
   498              syncmode = params.syncmode ? params.syncmode : "none";
   523              syncmode = params.syncmode ? params.syncmode : "none";
   499              default_subject = params.default_subject ? params.default_subject : "default";
   524              default_subject = params.default_subject ? params.default_subject : "default";
   500              var t = new Trace(url, requestmode);
   525              var t = new Trace(url, requestmode);
       
   526              t.set_default_subject(default_subject);
   501              t.set_sync_mode(syncmode);
   527              t.set_sync_mode(syncmode);
   502              t.set_default_subject(default_subject);
       
   503              this.traces[name] = t;
   528              this.traces[name] = t;
   504              return t;
   529              return t;
   505          }
   530          }
   506      };
   531      };
   507 
   532 
   510      };
   535      };
   511      TraceManager.prototype = TraceManager_prototype;
   536      TraceManager.prototype = TraceManager_prototype;
   512 
   537 
   513      var tracemanager  = new TraceManager();
   538      var tracemanager  = new TraceManager();
   514      return tracemanager;
   539      return tracemanager;
   515  };
   540  })(jQuery);