Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/filterHits.js

Issue 6337686776315904: Issue 394 - hit statistics tool data collection (Closed)
Patch Set: Created April 7, 2015, 3:15 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « defaults/prefs.js ('k') | lib/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterHits.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/filterHits.js
@@ -0,0 +1,275 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2015 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", null);
+let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null);
+
+let {Prefs} = require("prefs");
+let {Utils} = require("utils");
+let {MILLIS_IN_DAY} = require("downloader");
+let {FilterNotifier} = require("filterNotifier");
+let {DownloadableSubscription} = require("subscriptionClasses");
+
+/**
+ * This class reads filter hits statistics from SQLite database,
+ * manages them in memory and writes them back.
+ * @class
+ */
+let FilterHits = exports.FilterHits =
+{
+ /**
+ * Data that shoud be sent to the server
+ * @type Object
+ */
+ filters: Object.create(null),
+
+ /**
+ * Time since last push
+ * @type Number
+ */
kzar 2015/04/14 16:33:30 I guess we don't need to document these private va
saroyanm 2015/04/16 10:42:59 Please have a look on current discussion: http://c
kzar 2015/04/16 12:46:26 I disagree on this one, I've been told not to over
saroyanm 2015/04/16 14:08:45 I guess sometimes would be nice to have link to th
+ _lastPush: 0,
+
+ /**
+ * Indicates the timeframe between pushes
+ * @type Number
+ */
+ _pushInterval: MILLIS_IN_DAY * 7,
+
+ /**
+ * Indicates whether the data is being loaded from storage
+ * @type Boolean
+ */
+ _loading: false,
+
+ /**
+ * Indicates whether the data is being saved to storage
+ * @type Boolean
+ */
+ _saving: false,
+
+ /**
+ * Indicates whether the data is being sent to the server
+ * @type Boolean
+ */
+ _sending: false,
+
+ /**
+ * Increases the filter hit count
kzar 2015/04/14 16:33:30 Perhaps "Increases the hit count for the given fil
saroyanm 2015/04/16 10:42:59 No actually the filter in the parameter is the fil
kzar 2015/04/16 12:46:26 I think you've slightly missed my point here. We a
+ * @param {Filter} filter
+ * @param {Window} window Window that the match originated in (required to get host)
+ */
+ increaseFilterHits: function(filter, wnd)
kzar 2015/04/14 16:33:30 Maybe we should just make host the parameter here,
saroyanm 2015/04/16 10:42:59 Done.
+ {
+ let subscriptions = filter.subscriptions;
+ let inDownloadableSubscription = false;
+ for (let i = 0; i < subscriptions.length; i++)
+ {
+ if (subscriptions[i] instanceof DownloadableSubscription)
+ {
+ inDownloadableSubscription = true;
+ break;
+ }
+ }
+
+ if (!inDownloadableSubscription)
+ return;
+
+ if (!(filter.text in this.filters))
+ this.filters[filter.text] = Object.create(null);
+
+ let statFilter = this.filters[filter.text];
+ let filterType = filter.thirdParty ? "thirdParty" : "firstParty";
+
+ if (!(filterType in statFilter))
+ statFilter[filterType] = Object.create(null);
+
+ if (!("subscriptions" in statFilter))
+ statFilter.subscriptions = [];
+
+ for (let i = 0; i < subscriptions.length; i++)
+ {
+ if (subscriptions[i] instanceof DownloadableSubscription
+ && statFilter.subscriptions.indexOf(subscriptions[i].url) == -1)
+ statFilter.subscriptions.push(subscriptions[i].url);
+ }
+
+ let wndLocation = Utils.getOriginWindow(wnd).location.href;
+ let host = Utils.unwrapURL(wndLocation).host;
+
+ if (!(host in statFilter[filterType]))
+ statFilter[filterType][host] = {hits: 1, latest: filter.lastHit};
kzar 2015/04/14 16:33:30 I think we're supposed to use braces consistently
saroyanm 2015/04/16 10:42:59 I'm not sure if be honest, I can see your point, b
kzar 2015/04/16 12:46:26 I've definitely been told that the braces should b
saroyanm 2015/04/16 14:08:45 Would be nice to have some reference for future, f
+ else
+ {
+ statFilter[filterType][host].hits++;
+ statFilter[filterType][host].latest = filter.lastHit;
+ }
+ },
+
+ resetFilterHits: function()
+ {
+ this.filters = Object.create(null);
+ this.saveFilterHitsToDatabase();
+ },
+
+ sendFilterHitsToServer: function()
+ {
+ if (!Prefs.sendstats)
+ return;
+
+ let request = new XMLHttpRequest();
+ request.open("POST", Prefs.sendstats_url);
+ request.setRequestHeader("Content-Type", "application/json");
+ request.addEventListener("load", function(event)
+ {
+ FilterHits._sending = false;
+ if (request.status >= 200 && request.status < 300)
+ {
+ FilterHits._lastPush = new Date().getTime();
+ FilterHits.resetFilterHits();
+ }
+ else
kzar 2015/04/14 16:33:30 Again here with the if...else brace consistency. P
saroyanm 2015/04/16 10:42:59 Same as my reply above.
+ Cu.reportError("Could not send filter hit statistics to Adblock Plus server.");
kzar 2015/04/14 16:33:30 If sending fails and it's the server's fault / a c
saroyanm 2015/04/16 10:42:59 Yes, but it depends on filterStorage action. Her
+ }, false);
+
+ let {addonName, addonVersion, application, applicationVersion, platform, platformVersion} = require("info");
kzar 2015/04/14 16:33:30 Possible to add a newline in this list? It seems l
saroyanm 2015/04/16 10:42:59 Couldn't find any documentation that explicitly sa
kzar 2015/04/16 12:46:26 It is possible, I was just being polite. (British
saroyanm 2015/04/16 14:08:45 Ha ha :) That's funny :D
+ let data = {
+ version: 1,
+ timeSincePush: this._lastPush,
+ addonName: addonName,
+ addonVersion: addonVersion,
+ application: application,
+ applicationVersion: applicationVersion,
+ platform: platform,
+ platformVersion: platformVersion,
+ filters: this.filters
+ };
+
+ this._sending = true;
+ request.send(JSON.stringify(data));
+ },
+
+ getStorageFile: function()
+ {
+ return FileUtils.getFile("ProfD", ["adblockplus.sqlite"]);
+ },
+
+ checkCreateTable: function(connection)
+ {
+ if (!connection.tableExists("filterhits"))
+ connection.executeSimpleSQL("CREATE TABLE filterhits (id INTEGER PRIMARY KEY, filters TEXT, date INTEGER)");
+ },
+
+ /**
+ * Load Filter hits from database
+ */
+ loadFilterHitsFromDatabase: function()
+ {
+ let storageFile = this.getStorageFile();
+ if (!storageFile)
+ return;
+
+ let connection = Services.storage.openDatabase(storageFile);
+ this.checkCreateTable(connection);
+
+ let statement = connection.createStatement("SELECT * FROM filterhits");
+ if (!this._loading)
+ {
+ this._loading = true;
+ statement.executeAsync(
+ {
+ handleResult: function(results)
+ {
+ let row = results.getNextRow();
+ if (row)
+ {
+ let filters = row.getResultByName("filters");
+ let lastDate = row.getResultByName("date");
+ FilterHits.filters = JSON.parse(filters);
kzar 2015/04/14 16:33:30 We are serialising some of the data into JSON and
saroyanm 2015/04/16 10:42:59 The idea was to use Storage API which is actually
kzar 2015/04/16 12:46:26 Well it just seems like a hack to mix them both to
+ FilterHits._lastPush = lastDate;
+ }
+ },
+
+ handleError: function(error)
+ {
+ Cu.reportError(error.message);
+ },
+
+ handleCompletion: function(reason)
+ {
+ if (reason != Ci.mozIStorageStatementCallback.REASON_FINISHED)
+ Cu.reportError("Loading of filter hits canceled or aborted.");
+ FilterHits._loading = false;
+ }
+ });
+ }
+
+ connection.asyncClose();
+ },
+
+ /**
+ * Save Filter hits to database
+ */
+ saveFilterHitsToDatabase: function()
+ {
+ let now = new Date().getTime();
+ if (!this._lastPush)
+ this._lastPush = now;
+
+ if (!this._sending && now - this._lastPush > this._pushInterval)
kzar 2015/04/14 16:33:30 I think that perhaps the logic here for sending th
saroyanm 2015/04/16 10:42:59 Yes you are right this should go elsewhere, but b
kzar 2015/04/16 12:46:26 The geometrical mean calculation on the server tak
+ {
+ this.sendFilterHitsToServer();
kzar 2015/04/14 16:33:30 If we are going to perform the web request here sh
saroyanm 2015/04/16 10:42:59 Yes it doesn't because after the successful postin
kzar 2015/04/16 12:46:26 I guess we can ignore this one for now as it will
saroyanm 2015/04/16 14:08:45 Yes agree.
+ return;
+ }
+
+ let storageFile = this.getStorageFile();
+ if (!storageFile)
+ return;
+
+ let connection = Services.storage.openDatabase(storageFile);
+ this.checkCreateTable(connection);
+
+ let statement = connection.createStatement("INSERT OR REPLACE INTO filterhits (id, filters, date) VALUES(0, :filters, :date)");
+ statement.params.filters = JSON.stringify(this.filters);
+ statement.params.date = this._lastPush;
+ if (!this._saving)
+ {
+ this._saving = true;
+ statement.executeAsync(
+ {
+ handleError: function(aError)
+ {
+ Cu.reportError(aError.message);
+ },
+
+ handleCompletion: function(aReason)
+ {
+ if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED)
+ Cu.reportError("Writing of filter hits canceled or aborted.");
+ FilterHits._saving = false;
+ }
+ });
+ }
+
+ connection.asyncClose();
+ }
+};
+
+FilterNotifier.addListener(function(action)
+{
+ if (action == "load" && Prefs.sendstats)
+ FilterHits.loadFilterHitsFromDatabase();
+});
« no previous file with comments | « defaults/prefs.js ('k') | lib/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld