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

82.19% Statements 60/73
100% Branches 7/7
75% Functions 3/4
82.19% Lines 60/73

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 747x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 22x 22x 92x 78x 78x 92x 22x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 22x 22x 22x 22x 22x 22x 22x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x                           7x 7x  
/**
 * 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: {
		/**
		 * 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 ) {
			if ( 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`;
			}
		}
	}
};