B2G/gecko/dom/network/src/NetworkStatsDB.jsm
/*
   * This function check that stats are saved in the database following the sample rate.
   * In this way is easier to find elements when stats are requested.
   */
This function check that stats are saved in the database following the sample rate.
In this way is easier to find elements when stats are requested.
This function check that stats are saved in the database following the sample rate . In this way is easier to find elements when stats are requested .
function _processSamplesDiff(txn, store, lastSampleCursor, newSample) {
    let lastSample = lastSampleCursor.value;
    let diff = (newSample.timestamp - lastSample.timestamp) / SAMPLE_RATE;
    if (diff % 1) {
      txn.abort();
      throw new Error("Error processing samples");
    }
    if (DEBUG) {
      debug("New: " + newSample.timestamp + " - Last: " + lastSample.timestamp + " - diff: " + diff);
    }
    let rxDiff = newSample.rxTotalBytes - lastSample.rxTotalBytes;
    let txDiff = newSample.txTotalBytes - lastSample.txTotalBytes;
    if (rxDiff < 0 || txDiff < 0) {
      rxDiff = newSample.rxTotalBytes;
      txDiff = newSample.txTotalBytes;
    }
    newSample.rxBytes = rxDiff;
    newSample.txBytes = txDiff;
    if (diff == 1) {
      this._saveStats(txn, store, newSample);
      return;
    }
    if (diff > 1) {
      if (diff > VALUES_MAX_LENGTH) {
        diff = VALUES_MAX_LENGTH;
      }
      let data = [];
      for (let i = diff - 2; i >= 0; i--) {
        let time = newSample.timestamp - SAMPLE_RATE * (i + 1);
        let sample = {connectionType: newSample.connectionType,
                      timestamp:      time,
                      rxBytes:        0,
                      txBytes:        0,
                      rxTotalBytes:   lastSample.rxTotalBytes,
                      txTotalBytes:   lastSample.txTotalBytes};
        data.push(sample);
      }
      data.push(newSample);
      this._saveStats(txn, store, data);
      return;
    }
    if (diff == 0 || diff < 0) {
      lastSample.rxBytes += rxDiff;
      lastSample.txBytes += txDiff;
      lastSample.rxTotalBytes = newSample.rxTotalBytes;
      lastSample.txTotalBytes = newSample.txTotalBytes;
      if (DEBUG) {
        debug("Update: " + JSON.stringify(lastSample));
      }
      let req = lastSampleCursor.update(lastSample);
    }
  }
function _processSamplesDiff ( txn , store , lastSampleCursor , newSample ) { let lastSample = lastSampleCursor . value ; let diff = ( newSample . timestamp - lastSample . timestamp ) / SAMPLE_RATE ; if ( diff % 1 ) { txn . abort ( ) ; throw new Error ( "Error processing samples" ) ; } if ( DEBUG ) { debug ( "New: " + newSample . timestamp + " - Last: " + lastSample . timestamp + " - diff: " + diff ) ; } let rxDiff = newSample . rxTotalBytes - lastSample . rxTotalBytes ; let txDiff = newSample . txTotalBytes - lastSample . txTotalBytes ; if ( rxDiff < 0 || txDiff < 0 ) { rxDiff = newSample . rxTotalBytes ; txDiff = newSample . txTotalBytes ; } newSample . rxBytes = rxDiff ; newSample . txBytes = txDiff ; if ( diff == 1 ) { this . _saveStats ( txn , store , newSample ) ; return ; } if ( diff > 1 ) { if ( diff > VALUES_MAX_LENGTH ) { diff = VALUES_MAX_LENGTH ; } let data = [ ] ; for ( let i = diff - 2 ; i >= 0 ; i -- ) { let time = newSample . timestamp - SAMPLE_RATE * ( i + 1 ) ; let sample = { connectionType : newSample . connectionType , timestamp : time , rxBytes : 0 , txBytes : 0 , rxTotalBytes : lastSample . rxTotalBytes , txTotalBytes : lastSample . txTotalBytes } ; data . push ( sample ) ; } data . push ( newSample ) ; this . _saveStats ( txn , store , data ) ; return ; } if ( diff == 0 || diff < 0 ) { lastSample . rxBytes += rxDiff ; lastSample . txBytes += txDiff ; lastSample . rxTotalBytes = newSample . rxTotalBytes ; lastSample . txTotalBytes = newSample . txTotalBytes ; if ( DEBUG ) { debug ( "Update: " + JSON . stringify ( lastSample ) ) ; } let req = lastSampleCursor . update ( lastSample ) ; } }
This function check that stats are saved in the database following the sample rate.
This function check that stats are saved in the database following the sample rate .
// Get difference between last and new sample. // diff is decimal, so some error happened because samples are stored as a multiple // of SAMPLE_RATE // New element. // Some samples lost. Device off during one or more samplerate periods. // Time or timezone changed // Add lost samples with 0 bytes and the actual one. // New element received before samplerate period. // It means that device has been restarted (or clock / timezone change). // Update element. // If diff < 0, clock or timezone changed back. Place data in the last sample.