Bug 966244 - Alarms are not updated after clearStats. r=gene, r=jshih
☠☠ backed out by b1b0772bba22 ☠ ☠
authorAlbert Crespell <acperez@tid.es>
Tue, 11 Feb 2014 12:35:28 +0100
changeset 168138 93a64959289de32ebc2f2187a0140d7abade7843
parent 168137 a21db9070b6012d74b140e7e94a13be873561b87
child 168139 b0c83e05dfef6d98bbad444373a313b28e1ef6d2
push id26197
push userryanvm@gmail.com
push dateTue, 11 Feb 2014 19:57:30 +0000
treeherdermozilla-central@802d87c77e76 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene, jshih
bugs966244
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 966244 - Alarms are not updated after clearStats. r=gene, r=jshih
dom/network/src/NetworkStatsDB.jsm
dom/network/src/NetworkStatsService.jsm
--- a/dom/network/src/NetworkStatsDB.jsm
+++ b/dom/network/src/NetworkStatsDB.jsm
@@ -571,17 +571,17 @@ NetworkStatsDB.prototype = {
           lastSample.txBytes = 0;
           self._saveStats(aTxn, aStore, lastSample);
         }
       };
     };
   },
 
   clearInterfaceStats: function clearInterfaceStats(aNetwork, aResultCb) {
-    let network = [aNetwork.id, aNetwork.type];
+    let network = [aNetwork.network.id, aNetwork.network.type];
     let self = this;
 
     // Clear and save an empty sample to keep sync with system counters
     this.dbNewTxn(STATS_STORE_NAME, "readwrite", function(aTxn, aStore) {
       let sample = null;
       let request = aStore.index("network").openCursor(network, "prev");
       request.onsuccess = function onsuccess(event) {
         let cursor = event.target.result;
@@ -604,17 +604,17 @@ NetworkStatsDB.prototype = {
           sample.rxBytes = 0;
           sample.txBytes = 0;
           sample.rxTotalBytes = 0;
           sample.txTotalBytes = 0;
 
           self._saveStats(aTxn, aStore, sample);
         }
       };
-    }, aResultCb);
+    }, this._resetAlarms.bind(this, aNetwork.networkId, aResultCb));
   },
 
   clearStats: function clearStats(aNetworks, aResultCb) {
     let index = 0;
     let stats = [];
     let self = this;
 
     let callback = function(aError, aResult) {
@@ -858,31 +858,33 @@ NetworkStatsDB.prototype = {
       };
     }, aResultCb);
   },
 
   alarmToRecord: function alarmToRecord(aAlarm) {
     let record = { networkId: aAlarm.networkId,
                    absoluteThreshold: aAlarm.absoluteThreshold,
                    relativeThreshold: aAlarm.relativeThreshold,
+                   startTime: aAlarm.startTime,
                    data: aAlarm.data,
                    manifestURL: aAlarm.manifestURL,
                    pageURL: aAlarm.pageURL };
 
     if (aAlarm.id) {
       record.id = aAlarm.id;
     }
 
     return record;
   },
 
   recordToAlarm: function recordToalarm(aRecord) {
     let alarm = { networkId: aRecord.networkId,
                   absoluteThreshold: aRecord.absoluteThreshold,
                   relativeThreshold: aRecord.relativeThreshold,
+                  startTime: aRecord.startTime,
                   data: aRecord.data,
                   manifestURL: aRecord.manifestURL,
                   pageURL: aRecord.pageURL };
 
     if (aRecord.id) {
       alarm.id = aRecord.id;
     }
 
@@ -999,10 +1001,34 @@ NetworkStatsDB.prototype = {
 
         if (!aNetworkId || cursor.value.networkId == aNetworkId) {
           txn.result.push(self.recordToAlarm(cursor.value));
         }
 
         cursor.continue();
       }
     }, aResultCb);
+  },
+
+  _resetAlarms: function _resetAlarms(aNetworkId, aResultCb) {
+    this.dbNewTxn(ALARMS_STORE_NAME, "readwrite", function(txn, store) {
+      if (DEBUG) {
+        debug("Reset alarms for network " + aNetworkId);
+      }
+
+      let lowerFilter = [aNetworkId, 0];
+      let upperFilter = [aNetworkId, ""];
+      let range = IDBKeyRange.bound(lowerFilter, upperFilter);
+
+      store.index("alarm").openCursor(range).onsuccess = function onsuccess(event) {
+        let cursor = event.target.result;
+        if (cursor) {
+          if (cursor.value.startTime) {
+            cursor.value.relativeThreshold = cursor.value.threshold;
+            cursor.update(cursor.value);
+          }
+          cursor.continue();
+          return;
+        }
+      };
+    }, aResultCb);
   }
 };
--- a/dom/network/src/NetworkStatsService.jsm
+++ b/dom/network/src/NetworkStatsService.jsm
@@ -460,34 +460,43 @@ this.NetworkStatsService = {
 
     this.validateNetwork(network, function onValidateNetwork(aNetId) {
       if (!aNetId) {
         mm.sendAsyncMessage("NetworkStats:Clear:Return",
                             { id: msg.id, error: "Invalid connectionType", result: null });
         return;
       }
 
+      network = {network: network, networkId: aNetId};
       self._db.clearInterfaceStats(network, function onDBCleared(aError, aResult) {
+          self._updateCurrentAlarm(aNetId);
           mm.sendAsyncMessage("NetworkStats:Clear:Return",
                               { id: msg.id, error: aError, result: aResult });
       });
     });
   },
 
   clearDB: function clearDB(mm, msg) {
     let self = this;
     this._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
       if (aError) {
         mm.sendAsyncMessage("NetworkStats:ClearAll:Return",
                             { id: msg.id, error: aError, result: aResult });
         return;
       }
 
       let networks = aResult;
+      networks.forEach(function(network, index) {
+        networks[index] = {network: network, networkId: self.getNetworkId(network.id, network.type)};
+      }, self);
+
       self._db.clearStats(networks, function onDBCleared(aError, aResult) {
+        networks.forEach(function(network, index) {
+          self._updateCurrentAlarm(network.networkId);
+        }, self);
         mm.sendAsyncMessage("NetworkStats:ClearAll:Return",
                             { id: msg.id, error: aError, result: aResult });
       });
     });
   },
 
   updateAllStats: function updateAllStats(aCallback) {
     // Update |cachedStats|.