All files / ext.wikilambda.edit/mixins eventLogUtils.js

88.23% Statements 15/17
66.66% Branches 10/15
100% Functions 4/4
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                  30x   30x                   61x 61x                     55x 55x 208x 175x     55x                             49x 49x                               20x   15x   4x   1x                  
/**
 * WikiLambda Vue editor: Event logging utils mixin
 * Mixin with util functions to handle component errors
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../Constants.js' );
 
module.exports = exports = {
	methods: {
		/**
		 * Dispatches the event with name and payload data
		 * using the mw.eventLog service
		 *
		 * @param {string} name
		 * @param {Object} data
		 */
		dispatchEvent: function ( name, data ) {
			Eif ( mw.eventLog ) {
				mw.eventLog.dispatch( name, data );
			}
		},
 
		/**
		 * Create a new object by removing properties with null or undefined values from the original object
		 *
		 * @param {Object} original
		 * @return {Object}
		 */
		removeNullUndefined: function ( original ) {
			const result = {};
			for ( const key in original ) {
				if ( original[ key ] !== null && original[ key ] !== undefined ) {
					result[ key ] = original[ key ];
				}
			}
			return result;
		},
 
		// T350497 Update the WikiLambda instrumentation to use core interaction events
		/**
		 * Submit an interaction event using Metrics Platform
		 *
		 * Since the schema specifies each property to be either string or Boolean, we defensively remove
		 * properties with null or undefined values. (Otherwise, a null or undefined property would cause
		 * the event to be dropped from the stream.)
		 *
		 * @param {string} action
		 * @param {Object} interactionData
		 */
		submitInteraction: function ( action, interactionData ) {
			Eif ( mw.eventLog ) {
				mw.eventLog.submitInteraction(
					'mediawiki.product_metrics.wikifunctions_ui',
					'/analytics/mediawiki/product_metrics/wikilambda/ui_actions/1.0.0',
					action,
					this.removeNullUndefined( interactionData ) );
			}
		},
		/**
		 * Returns the event namespace string for particular
		 * actions and important types, else returns generic one
		 *
		 * @param {string|null} type
		 * @param {string} action defaults to 'edit'
		 * @return {string}
		 */
		getNamespace: function ( type = null, action = 'edit' ) {
			switch ( type ) {
				case Constants.Z_FUNCTION:
					return `${ action }Function`;
				case Constants.Z_IMPLEMENTATION:
					return `${ action }Implementation`;
				case Constants.Z_TESTER:
					return `${ action }Tester`;
				case Constants.Z_TYPE:
					return `${ action }Type`;
				default:
					return `${ action }ZObject`;
			}
		}
	}
};