jenkins-bot has submitted this change and it was merged.
Change subject: Add Schema class
......................................................................
Add Schema class
Based on Schema class from MobileFrontend. The features offered by the class
are particularly useful for RelatedArticles and other extensions that
try not to depend on MobileFrontend. The class allows inheriting classes
to log events based on a sampling rate if sampling is enabled.
Bug: T117140
Change-Id: Iea00d534371353c3ae5c06c74a08aa10cb60047b
---
M EventLogging.php
A modules/ext.eventLogging.Schema.js
M modules/ext.eventLogging.core.js
A modules/ext.eventLogging.init.js
4 files changed, 125 insertions(+), 1 deletion(-)
Approvals:
Krinkle: Looks good to me, but someone else must approve
Jdlrobson: Looks good to me, approved
Gergő Tisza: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/EventLogging.php b/EventLogging.php
index 854ac5f..76b46bb 100644
--- a/EventLogging.php
+++ b/EventLogging.php
@@ -180,6 +180,12 @@
);
// Modules
+$wgResourceModules[ 'ext.eventLogging.init' ] = array(
+ 'scripts' => 'modules/ext.eventLogging.init.js',
+ 'localBasePath' => __DIR__,
+ 'remoteExtPath' => 'EventLogging',
+ 'targets' => array( 'desktop', 'mobile' ),
+);
$wgResourceModules[ 'ext.eventLogging' ] = array(
'scripts' => 'modules/ext.eventLogging.core.js',
@@ -187,6 +193,7 @@
'remoteExtPath' => 'EventLogging',
'dependencies' => array(
'json',
+ 'ext.eventLogging.init'
),
'targets' => array( 'desktop', 'mobile' ),
);
@@ -212,6 +219,17 @@
'position' => 'top',
);
+$wgResourceModules[ 'ext.eventLogging.Schema' ] = array(
+ 'scripts' => 'modules/ext.eventLogging.Schema.js',
+ 'localBasePath' => __DIR__,
+ 'remoteExtPath' => 'EventLogging',
+ 'dependencies' => array(
+ 'mediawiki.user',
+ 'ext.eventLogging.init'
+ ),
+ 'targets' => array( 'desktop', 'mobile' ),
+);
+
// Hooks
$wgExtensionFunctions[] = 'EventLoggingHooks::onSetup';
diff --git a/modules/ext.eventLogging.Schema.js
b/modules/ext.eventLogging.Schema.js
new file mode 100644
index 0000000..de045b1
--- /dev/null
+++ b/modules/ext.eventLogging.Schema.js
@@ -0,0 +1,95 @@
+( function ( mw, $ ) {
+ 'use strict';
+
+ // 2^32 - 1
+ var MAX_INT32_UNSIGNED = 4294967295;
+
+ /**
+ * The class allows inheriting classes to log events based on a sampling
+ * rate if sampling is enabled.
+ *
+ * How to use:
+ *
+ * var mySchema = new mw.eventLog.Schema( 'Name', 0.01, { skin:
'minerva' } );
+ * // Log the following event at the default sampling rate of 0.01.
+ * mySchema.log( { 'action': 'viewed' } );
+ * // Log the following event at the sampling rate of 0.2.
+ * mySchema.log( { 'action': 'clicked' }, 0.2 );
+ *
+ * @class mw.eventLog.Schema
+ * @constructor
+ * @param {string} name Schema name to log to.
+ * @param {number} samplingRate The rate at which sampling is performed.
+ * The values are between 0 and 1 inclusive.
+ * @param {Object} [defaults] A set of defaults to log to the schema.
Once
+ * these defaults are set the values will be logged along with any
additional
+ * fields that are passed to the log method.
+ */
+ function Schema( name, samplingRate, defaults ) {
+ var randomNumber;
+
+ if ( !name ) {
+ throw new Error( 'name is required' );
+ }
+ // Theoretically samplingRate can be 0
+ if ( samplingRate === undefined ) {
+ throw new Error( 'samplingRate is required' );
+ }
+
+ this.name = name;
+ this.samplingRate = samplingRate;
+ this.defaults = defaults || {};
+
+ // Get the first 32-bit integer from the random session ID and
+ // scale it down to [0, 1] range.
+ randomNumber = parseInt(
+ mw.user.generateRandomSessionId().slice( 0, 8 ),
+ 16
+ ) / MAX_INT32_UNSIGNED;
+
+ /**
+ * Random number that is used for determining whether the user
is in the sample
+ *
+ * @return {number} number Between 0 and 1 inclusive
+ */
+ this.getRandomNumber = function () {
+ return randomNumber;
+ };
+ }
+
+ /**
+ * Whether the user is bucketed. Returns true the randomly generated
number
+ * during initialization is smaller than the samplingRate.
+ *
+ * @param {number} samplingRate Number between 0 and 1 inclusive
+ * @return {boolean}
+ */
+ Schema.prototype.isUserInBucket = function ( samplingRate ) {
+ return this.getRandomNumber() <= samplingRate;
+ };
+
+ /**
+ * Log an event via the EventLogging subscriber. If the schema uses
different
+ * sampling rates for different events, samplingRate argument can also
be
+ * passed which will determine whether to log the event. Otherwise,
+ * the prototype samplingRate will be used.
+ *
+ * @param {Object} data Data to log
+ * @param {number} [samplingRate] number between 0 and 1.
+ * If not passed this.samplingRate will be used.
+ */
+ Schema.prototype.log = function ( data, samplingRate ) {
+ var self = this;
+
+ samplingRate = ( samplingRate !== undefined ) ? samplingRate :
this.samplingRate;
+
+ if ( this.isUserInBucket( samplingRate ) ) {
+ mw.loader.using( [ 'ext.eventLogging', 'schema.' +
this.name ], function () {
+ mw.eventLog.logEvent( self.name, $.extend( {},
self.defaults, data ) );
+ } );
+ }
+ };
+
+ mw.eventLog.Schema = Schema;
+
+}( mediaWiki, jQuery ) );
diff --git a/modules/ext.eventLogging.core.js b/modules/ext.eventLogging.core.js
index 81cef1d..352a73b 100644
--- a/modules/ext.eventLogging.core.js
+++ b/modules/ext.eventLogging.core.js
@@ -27,7 +27,7 @@
* @class mw.eventLog
* @singleton
*/
- self = mw.eventLog = {
+ self = {
/**
* Schema registry. Schemas that have been declared explicitly
via
@@ -298,4 +298,6 @@
mw.log.error( error );
} );
+ $.extend( mw.eventLog, self );
+
}( mediaWiki, jQuery ) );
diff --git a/modules/ext.eventLogging.init.js b/modules/ext.eventLogging.init.js
new file mode 100644
index 0000000..8b72e3e
--- /dev/null
+++ b/modules/ext.eventLogging.init.js
@@ -0,0 +1,9 @@
+( function ( mw ) {
+ 'use strict';
+
+ /**
+ * @class mw.eventLog
+ * @singleton
+ */
+ mw.eventLog = {};
+}( mediaWiki ) );
--
To view, visit https://gerrit.wikimedia.org/r/250057
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iea00d534371353c3ae5c06c74a08aa10cb60047b
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Bmansurov <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Gergő Tisza <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Nuria <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits