Gilles has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/172713

Change subject: Allow to set a different sampling factor for logged-in users
......................................................................

Allow to set a different sampling factor for logged-in users

Change-Id: Ia15e837fab0126c398fde103614e31f58641f968
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/978
---
M ImageMetrics.php
M resources/ext.imageMetrics.js
M resources/ext.imageMetrics.loader.js
M tests/qunit/ext.imageMetrics.test.js
4 files changed, 30 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ImageMetrics 
refs/changes/13/172713/1

diff --git a/ImageMetrics.php b/ImageMetrics.php
index 81341eb..2898b3e 100644
--- a/ImageMetrics.php
+++ b/ImageMetrics.php
@@ -26,6 +26,9 @@
 /** @var int|bool: If set, logs once per this many requests. False if unset. 
**/
 $wgImageMetricsSamplingFactor = false;
 
+/** @var int|bool: If set, logs once per this many requests for logged-in 
users. False if unset. **/
+$wgImageMetricsLoggedinSamplingFactor = false;
+
 $wgMessagesDirs['ImageMetrics'] = __DIR__ . '/i18n';
 
 $wgHooks['EventLoggingRegisterSchemas'][] = function( array &$schemas ) {
@@ -73,8 +76,9 @@
  * @return bool
  */
 $wgHooks[ 'ResourceLoaderGetConfigVars' ][] = function ( &$vars ) {
-       global $wgImageMetricsSamplingFactor;
+       global $wgImageMetricsSamplingFactor, 
$wgImageMetricsLoggedinSamplingFactor;
        $vars[ 'wgImageMetricsSamplingFactor' ] = $wgImageMetricsSamplingFactor;
+       $vars[ 'wgImageMetricsLoggedinSamplingFactor' ] = 
$wgImageMetricsLoggedinSamplingFactor;
        return true;
 };
 
diff --git a/resources/ext.imageMetrics.js b/resources/ext.imageMetrics.js
index d544d91..a2d31be 100644
--- a/resources/ext.imageMetrics.js
+++ b/resources/ext.imageMetrics.js
@@ -20,13 +20,16 @@
        /**
         * @class ImageMetrics
         * @constructor
+        * @param {number} samplingFactor sampling factor
         * @param {Object} performance window.performance
         * @param {Object} location window.location
         * @param {Object} mwConfig mw.config
         * @param {Object} geo window.Geo
         * @param {Object} eventLog mw.eventLog
         */
-       function ImageMetrics( performance, location, mwConfig, geo, eventLog ) 
{
+       function ImageMetrics( samplingFactor, performance, location, mwConfig, 
geo, eventLog ) {
+               this.samplingFactor = samplingFactor;
+
                /** @property {Object} performance window.performance */
                this.performance = performance;
 
@@ -46,18 +49,20 @@
        /**
         * Factory function to take care of dependency injection.
         * @static
+        * @param {number} samplingFactor sampling factor
         * @return {ImageMetrics}
         */
-       ImageMetrics.create = function() {
-               return new ImageMetrics( window.performance, window.location, 
mw.config, window.Geo, mw.eventLog );
+       ImageMetrics.create = function( samplingFactor ) {
+               return new ImageMetrics( samplingFactor, window.performance, 
window.location, mw.config, window.Geo, mw.eventLog );
        };
 
        /**
         * Installs the event handler which will perform the logging.
         * @static
+        * @param {number} samplingFactor sampling factor
         */
-       ImageMetrics.install = function() {
-               var imageMetrics = ImageMetrics.create();
+       ImageMetrics.install = function( samplingFactor ) {
+               var imageMetrics = ImageMetrics.create( samplingFactor );
 
                $( window ).load( function () {
                        imageMetrics.log();
@@ -155,7 +160,7 @@
                var $file,
                        data = {};
 
-               data.samplingFactor = this.mwConfig.get( 
'wgImageMetricsSamplingFactor' );
+               data.samplingFactor = this.samplingFactor;
 
                data.isHttps = this.location.protocol === 'https:';
 
diff --git a/resources/ext.imageMetrics.loader.js 
b/resources/ext.imageMetrics.loader.js
index 942433e..e6ae00c 100644
--- a/resources/ext.imageMetrics.loader.js
+++ b/resources/ext.imageMetrics.loader.js
@@ -8,13 +8,19 @@
 ( function ( mw, $ ) {
        'use strict';
 
+       var factor = mw.config.get( 'wgImageMetricsSamplingFactor', false ),
+               loggedinFactor = mw.config.get( 
'wgImageMetricsLoggedinSamplingFactor', false );
+
+       if ( !mw.user.isAnon() && loggedinFactor ) {
+               factor = loggedinFactor;
+       }
+
        /**
         * Makes a random decision (based on the sampling factor configuration 
setting) whether the current
         * request should be logged.
         * @return {boolean}
         */
        function isInSample() {
-               var factor = mw.config.get( 'wgImageMetricsSamplingFactor', 
false );
                if ( !$.isNumeric( factor ) || factor < 1 ) {
                        return false;
                }
@@ -23,7 +29,7 @@
 
        if ( isInSample() ) {
                mw.loader.using( 'ext.imageMetrics', function () {
-                       mw.ImageMetrics.install();
+                       mw.ImageMetrics.install( factor );
                } );
        }
 } ( mediaWiki, jQuery ) );
diff --git a/tests/qunit/ext.imageMetrics.test.js 
b/tests/qunit/ext.imageMetrics.test.js
index cfe3538..82e799a 100644
--- a/tests/qunit/ext.imageMetrics.test.js
+++ b/tests/qunit/ext.imageMetrics.test.js
@@ -8,7 +8,7 @@
                        logEvent = sandbox.stub(),
                        config = new mw.Map( options.config || {} );
 
-               imageMetrics = new mw.ImageMetrics( options.performance || {}, 
options.location || {}, config,
+               imageMetrics = new mw.ImageMetrics( options.samplingFactor, 
options.performance || {}, options.location || {}, config,
                        options.geo || {}, { logEvent: logEvent } );
 
                options.logEvent = logEvent;
@@ -23,7 +23,8 @@
        QUnit.test( 'Minimal logging scenario', 12, function ( assert ) {
                var data,
                        options = {
-                               config: { wgImageMetricsSamplingFactor: 1 }
+                               samplingFactor: 1,
+                               config: {}
                        },
                        imageMetrics = createImageMetrics( this.sandbox, 
options );
                $( '#qunit-fixture' ).append( '<div id="file"><img 
alt="Foo.jpg" /></div>' );
@@ -53,7 +54,7 @@
        QUnit.test( 'Geo logging', 1, function ( assert ) {
                var data,
                        options = {
-                               config: { wgImageMetricsSamplingFactor: 1 },
+                               samplingFactor: 1,
                                geo: { country: 'London' }
                        },
                        imageMetrics = createImageMetrics( this.sandbox, 
options );
@@ -71,7 +72,7 @@
                var data,
                        options = {
                                performance: { navigation: { type: 0 } },
-                               config: { wgImageMetricsSamplingFactor: 1 }
+                               samplingFactor: 1
                        },
                        imageMetrics = createImageMetrics( this.sandbox, 
options );
                $( '#qunit-fixture' ).append( '<div id="file"><img 
alt="Foo.jpg" /></div>' );
@@ -107,7 +108,7 @@
        QUnit.test( 'No image, no logging', 1, function ( assert ) {
                var options = {
                                performance: { navigation: { type: 0 } },
-                               config: { wgImageMetricsSamplingFactor: 1 }
+                               samplingFactor: 1
                        },
                        imageMetrics = createImageMetrics( this.sandbox, 
options );
                $( '#qunit-fixture' ).append( '<div id="file"></div>' );

-- 
To view, visit https://gerrit.wikimedia.org/r/172713
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia15e837fab0126c398fde103614e31f58641f968
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ImageMetrics
Gerrit-Branch: master
Gerrit-Owner: Gilles <gdu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to