From 171b13149e1fb6edabff533aa8d3b93bb520452a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Fri, 16 May 2014 18:17:52 +0000 Subject: [PATCH] Add sampling to unsampled event logging Has issues: - needs dependency injection - needs to be DRY - should not load mw.eventLogging when we are not in sample Due to urgency this will be fixed in another commit. Change-Id: I0df067a619109a7c945f82c8d33fa2e621217f0b Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/619 --- MultimediaViewer.php | 11 +++++++++++ MultimediaViewerHooks.php | 5 +++-- resources/mmv/mmv.DurationLogger.js | 14 +++++++++++++- resources/mmv/mmv.logger.js | 11 ++++++++++- tests/qunit/mmv/mmv.DurationLogger.test.js | 1 + 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/MultimediaViewer.php b/MultimediaViewer.php index 69521437c..a1f9f3cd2 100644 --- a/MultimediaViewer.php +++ b/MultimediaViewer.php @@ -25,6 +25,17 @@ if ( !isset( $wgNetworkPerformanceSamplingFactor ) ) { $wgNetworkPerformanceSamplingFactor = false; } +if ( !isset( $wgMediaViewerSamplingFactor ) ) { + /** + * If set, records user activity and loading times. A value of 1000 means there will be an + * 1:1000 chance to enable logging on page load; for that page, either all or none of the + * events will be logged. + * False if unset. + * @var int|bool + */ + $wgMediaViewerSamplingFactor = false; +} + if ( !isset( $wgMediaViewerIsInBeta ) ) { /** @var bool: If set, Media Viewer will try to use BetaFeatures. False if unset. **/ $wgMediaViewerIsInBeta = false; diff --git a/MultimediaViewerHooks.php b/MultimediaViewerHooks.php index a19b954dc..72274214f 100644 --- a/MultimediaViewerHooks.php +++ b/MultimediaViewerHooks.php @@ -144,8 +144,8 @@ class MultimediaViewerHooks { * @return bool */ public static function resourceLoaderGetConfigVars( &$vars ) { - global $wgAPIPropModules, $wgNetworkPerformanceSamplingFactor, $wgMediaViewerIsInBeta, - $wgMediaViewerUseThumbnailGuessing, $wgMediaViewerShowSurvey; + global $wgAPIPropModules, $wgNetworkPerformanceSamplingFactor, $wgMediaViewerSamplingFactor, + $wgMediaViewerIsInBeta, $wgMediaViewerUseThumbnailGuessing, $wgMediaViewerShowSurvey; $vars['wgMultimediaViewer'] = array( 'infoLink' => self::$infoLink, 'discussionLink' => self::$discussionLink, @@ -153,6 +153,7 @@ class MultimediaViewerHooks { 'globalUsageAvailable' => isset( $wgAPIPropModules['globalusage'] ), 'useThumbnailGuessing' => (bool)$wgMediaViewerUseThumbnailGuessing, 'showSurvey' => (bool)$wgMediaViewerShowSurvey, + 'samplingFactor' => $wgMediaViewerSamplingFactor, ); $vars['wgNetworkPerformanceSamplingFactor'] = $wgNetworkPerformanceSamplingFactor; $vars['wgMediaViewer'] = true; diff --git a/resources/mmv/mmv.DurationLogger.js b/resources/mmv/mmv.DurationLogger.js index 5a75abe01..0905ad50d 100644 --- a/resources/mmv/mmv.DurationLogger.js +++ b/resources/mmv/mmv.DurationLogger.js @@ -98,7 +98,10 @@ e.country = self.Geo.country; } - self.eventLog.logEvent( self.schema, e ); + if ( self.isInSample() ) { + self.eventLog.logEvent( self.schema, e ); + } + mw.log( message ); } ); } @@ -138,5 +141,14 @@ return waitForEventLog; }; + L.isInSample = function () { + var factor = mw.config.get( 'wgMultimediaViewer' ).samplingFactor; + + if ( !$.isNumeric( factor ) || factor < 1 ) { + return false; + } + return Math.floor( Math.random() * factor ) === 0; + }; + mw.mmv.durationLogger = new DurationLogger(); }( mediaWiki, jQuery ) ); \ No newline at end of file diff --git a/resources/mmv/mmv.logger.js b/resources/mmv/mmv.logger.js index 4a9d18a34..2f1213822 100644 --- a/resources/mmv/mmv.logger.js +++ b/resources/mmv/mmv.logger.js @@ -75,7 +75,7 @@ mw.log( translatedAction ); - if ( mw.eventLog && !skipEventLog ) { + if ( mw.eventLog && !skipEventLog && this.isInSample() ) { return mw.eventLog.logEvent( 'MediaViewer', { version: '1.1', action: action @@ -85,5 +85,14 @@ return $.Deferred().resolve(); }; + L.isInSample = function () { + var factor = mw.config.get( 'wgMultimediaViewer' ).samplingFactor; + + if ( !$.isNumeric( factor ) || factor < 1 ) { + return false; + } + return Math.floor( Math.random() * factor ) === 0; + }; + mw.mmv.logger = new Logger(); }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/mmv/mmv.DurationLogger.test.js b/tests/qunit/mmv/mmv.DurationLogger.test.js index 629f57a39..41e3ca17c 100755 --- a/tests/qunit/mmv/mmv.DurationLogger.test.js +++ b/tests/qunit/mmv/mmv.DurationLogger.test.js @@ -2,6 +2,7 @@ QUnit.module( 'mmv.DurationLogger', QUnit.newMwEnvironment({ setup: function () { this.clock = this.sandbox.useFakeTimers(); + mw.config.get( 'wgMultimediaViewer' ).samplingFactor = 1; } } ) );