jenkins-bot has submitted this change and it was merged.

Change subject: Add experimental function to log events using the sendBeacon API
......................................................................


Add experimental function to log events using the sendBeacon API

https://www.mediawiki.org/wiki/Extension:EventLogging/sendBeacon
https://etherpad.wikimedia.org/p/sendBeacon-2014-10-03

Bug: 42815
Change-Id: I022b13cfd74a691e116348b7bfb8c6f8698b2b41
---
M modules/ext.eventLogging.core.js
1 file changed, 63 insertions(+), 4 deletions(-)

Approvals:
  Mattflaschen: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/ext.eventLogging.core.js b/modules/ext.eventLogging.core.js
index 0eab52e..9e44559 100644
--- a/modules/ext.eventLogging.core.js
+++ b/modules/ext.eventLogging.core.js
@@ -241,10 +241,29 @@
                        };
                },
 
+               /**
+                * Constructs the EventLogging URI based on the base URI and the
+                * encoded and stringified data.
+                *
+                * @method getEventLoggingUri
+                * @param {Object} data Payload to send
+                * @return {String|Boolean} The URI to log the event.
+                *  false if the baseUri doesn't exist.
+                */
+               getEventLoggingUri: function ( data ) {
+                       var baseUri = mw.config.get( 'wgEventLoggingBaseUri' );
+                       if ( !baseUri ) {
+                               return false;
+                       } else {
+                               return baseUri + '?' + encodeURIComponent( 
JSON.stringify( data ) ) + ';';
+                       }
+               },
 
                /**
                 * Encodes a JavaScript object as percent-encoded JSON and
-                * pushes it to the server using a GET request.
+                * pushes it to the server using a GET request. This request
+                * is sent by requesting an image with the source as the
+                * required EventLogging URI.
                 *
                 * @method dispatch
                 * @param {Object} data Payload to send.
@@ -252,10 +271,10 @@
                 */
                dispatch: function ( data ) {
                        var beacon = document.createElement( 'img' ),
-                               baseUri = mw.config.get( 
'wgEventLoggingBaseUri' ),
+                               uri = self.getEventLoggingUri( data ),
                                deferred = $.Deferred();
 
-                       if ( !baseUri ) {
+                       if ( !uri ) {
                                deferred.rejectWith( data, [ data ] );
                                return deferred.promise();
                        }
@@ -266,10 +285,50 @@
                                deferred.resolveWith( data, [ data ] );
                        } );
 
-                       beacon.src = baseUri + '?' + encodeURIComponent( 
JSON.stringify( data ) ) + ';';
+                       beacon.src = uri;
                        return deferred.promise();
                },
 
+               /*
+                * Asynchronously initiate logging for event data. The method 
returns a
+                * promise that indicates whether the method successfully 
queued the data.
+                * Even if it is queued for delivery, there is no guarantee it 
will ever be
+                * delivered. Currently, the only backend is sendBeacon, so if 
that is not
+                * available, the promise will be rejected.
+                * NOTE: This is an experimental method.
+                *
+                * @method logPersistentEvent
+                * @experimental
+                * @param {String} schemaName Canonical schema name.
+                * @param {Object} eventInstance Event instance.
+                * @return {jQuery.Promise} jQuery Promise object for the 
logging call. The promise would
+                *  have been resolved or rejected before the function returns. 
The first argument passed
+                *  to the callback is the data itself and the second one is a 
string with the status.
+                *  Even when the deferred is resolved it only means that the 
data has been queued to be sent.
+                */
+               logPersistentEvent: function ( schemaName, eventInstance ) {
+                       var data = self.prepare( schemaName, eventInstance ),
+                               uri = self.getEventLoggingUri( data ),
+                               deferred = $.Deferred();
+
+                       if ( !uri ) {
+                               deferred.rejectWith( data, [ data, 
'no-base-uri' ] );
+                               return deferred.promise();
+                       }
+
+                       if ( navigator.sendBeacon === undefined ) {
+                               deferred.rejectWith( data, [ data, 
'no-browser-support' ] );
+                               return deferred.promise();
+                       }
+
+                       if ( navigator.sendBeacon( uri ) ) {
+                               deferred.resolveWith( data, [ data, 'queued' ] 
);
+                       } else {
+                               deferred.rejectWith( data, [ data, 
'could-not-queue' ]  );
+                       }
+
+                       return deferred.promise();
+               },
 
                /**
                 * Construct and transmit to a remote server a record of some 
event

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I022b13cfd74a691e116348b7bfb8c6f8698b2b41
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Prtksxna <[email protected]>
Gerrit-Reviewer: Halfak <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Nuria <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>
Gerrit-Reviewer: Prtksxna <[email protected]>
Gerrit-Reviewer: Robmoen <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to