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

63.15% Statements 72/114
36.17% Branches 17/47
77.77% Functions 7/9
62.83% Lines 71/113

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312                        26x 26x 26x             26x                                             26x     2x                       2x 2x     2x   2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x   2x 6x 6x   6x       6x 6x                 2x                             2x   2x 2x 2x       2x   2x 2x                                                                                                               8x 4x 4x 4x 4x     4x 4x           4x     4x 4x                                                                                               8x 8x 8x 4x   4x 4x                         24x 24x 24x 22x   2x           2x 2x 2x                       4x 4x 4x 4x                
/**
 * WikiLambda Vue editor: portray mixin
 * Mixin with functions to generate HTML that portrays ZObjects,
 * starting from JSON ZObject representation.
 * This HTML is (initially) motivated by the metadata dialog, and is
 * meant to give a compact and readable presentation.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../Constants.js' );
const getValueFromCanonicalZMap = require( './schemata.js' ).methods.getValueFromCanonicalZMap;
const extractErrorStructure = require( './schemata.js' ).methods.extractErrorStructure;
 
/**
 * Map from internal key (which appears in the result envelopes returned from backend services)
 * to the internationalization identifier.  We map to an object so as to allow for future
 * inclusion of other attributes.
 */
const knownKeys = new Map( [
	[ 'errors', { i18nId: 'wikilambda-functioncall-metadata-errors-summary' } ],
	[ 'validateErrors', { i18nId: 'wikilambda-functioncall-metadata-validator-errors-summary' } ],
	[ 'actualTestResult', { i18nId: 'wikilambda-functioncall-metadata-actual-result' } ],
	[ 'expectedTestResult', { i18nId: 'wikilambda-functioncall-metadata-expected-result' } ],
	[ 'implementationId', { i18nId: 'wikilambda-functioncall-metadata-implementation-id' } ],
	[ 'implementationType', { i18nId: 'wikilambda-functioncall-metadata-implementation-type' } ],
	[ 'orchestrationStartTime', { i18nId: 'wikilambda-functioncall-metadata-orchestration-start-time' } ],
	[ 'orchestrationEndTime', { i18nId: 'wikilambda-functioncall-metadata-orchestration-end-time' } ],
	[ 'orchestrationDuration', { i18nId: 'wikilambda-functioncall-metadata-orchestration-duration' } ],
	[ 'orchestrationCpuUsage', { i18nId: 'wikilambda-functioncall-metadata-orchestration-cpu-usage' } ],
	[ 'orchestrationMemoryUsage', { i18nId: 'wikilambda-functioncall-metadata-orchestration-memory-usage' } ],
	[ 'orchestrationHostname', { i18nId: 'wikilambda-functioncall-metadata-orchestration-hostname' } ],
	[ 'evaluationStartTime', { i18nId: 'wikilambda-functioncall-metadata-evaluation-start-time' } ],
	[ 'evaluationEndTime', { i18nId: 'wikilambda-functioncall-metadata-evaluation-end-time' } ],
	[ 'evaluationDuration', { i18nId: 'wikilambda-functioncall-metadata-evaluation-duration' } ],
	[ 'evaluationCpuUsage', { i18nId: 'wikilambda-functioncall-metadata-evaluation-cpu-usage' } ],
	[ 'evaluationMemoryUsage', { i18nId: 'wikilambda-functioncall-metadata-evaluation-memory-usage' } ],
	[ 'evaluationHostname', { i18nId: 'wikilambda-functioncall-metadata-evaluation-hostname' } ],
	[ 'executionCpuUsage', { i18nId: 'wikilambda-functioncall-metadata-execution-cpu-usage' } ],
	[ 'executionMemoryUsage', { i18nId: 'wikilambda-functioncall-metadata-execution-memory-usage' } ]
] );
 
module.exports = exports = {
	methods: {
		maybeStringify: function ( value ) {
			return typeof value === 'string' ? value : JSON.stringify( value );
		},
		/**
		 * Given a function-call metadata map (produced by WikiFunctions backend services),
		 * produce a string that portrays the metadata map content in a compact,
		 * readable style, using raw HTML markup
		 *
		 * @param {Object} zMap a Z883/Typed map, in canonical form
		 * @param {Array} labels
		 * @return {string}
		 */
		portrayMetadataMap: function ( zMap, labels ) {
			var keysUsed = [];
			let html = '<span><ul>';
			// First, portray the known/expected metadata keys and their values
			// If there's no 'errors' value, keyAndZErrorSummary will display 'None'
			html = html + this.keyAndZErrorSummary( zMap, 'errors', labels, keysUsed );
			// With 'validateErrors', we don't want to display 'None', so check here if there's a value
			Iif ( getValueFromCanonicalZMap( zMap, 'validateErrors' ) ) {
				html = html + this.keyAndZErrorSummary( zMap, 'validateErrors', labels, keysUsed );
			}
			html = html + this.keyAndArbitraryValue( zMap, 'expectedTestResult', keysUsed );
			html = html + this.keyAndArbitraryValue( zMap, 'actualTestResult', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'implementationType', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'implementationId', keysUsed );
			html = html + this.keyAndDatetimeValue( zMap, 'orchestrationStartTime', keysUsed );
			html = html + this.keyAndDatetimeValue( zMap, 'orchestrationEndTime', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'orchestrationDuration', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'orchestrationCpuUsage', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'orchestrationMemoryUsage', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'orchestrationHostname', keysUsed );
			html = html + this.keyAndDatetimeValue( zMap, 'evaluationStartTime', keysUsed );
			html = html + this.keyAndDatetimeValue( zMap, 'evaluationEndTime', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'evaluationDuration', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'evaluationCpuUsage', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'evaluationMemoryUsage', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'evaluationHostname', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'executionCpuUsage', keysUsed );
			html = html + this.keyAndStringValue( zMap, 'executionMemoryUsage', keysUsed );
 
			// Now portray any top-level zMap entries that weren't already used above
			const k1Array = zMap[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ];
			// Ignore first item in the canonical form array; this is a string representing the type
			for ( let i = 1; i < k1Array.length; i++ ) {
				const entry = k1Array[ i ];
				let key = entry[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ] || entry[ Constants.Z_TYPED_PAIR_TYPE1 ];
				// Allow for keys that correspond to references
				Iif ( typeof key === 'object' && key[ Constants.Z_OBJECT_TYPE ] === Constants.Z_STRING_VALUE ) {
					key = key[ Constants.Z_STRING_VALUE ];
				}
				// eslint-disable-next-line no-restricted-syntax
				if ( keysUsed.includes( key ) ) {
					continue;
				}
				const keyInfo = knownKeys.get( key );
				const displayKey = keyInfo ? this.$i18n( keyInfo.i18nId ).text() : key;
				const value = this.maybeStringify( entry[ Constants.Z_TYPED_OBJECT_ELEMENT_2 ] ||
					entry[ Constants.Z_TYPED_PAIR_TYPE2 ] );
				html = html + '<li><b>' + displayKey + ':</b> ' + value + '</li>';
			}
 
			return html + '</ul></span>';
		},
		/**
		 * Portray the given key and its zMap value, which should be a Z5 / Error object.  The
		 * resulting string is a cursory, bulleted summary of the given Z5.  Each bullet represents
		 * a suberror (instance of Z50) found in the Z5.  Bullets are indented to reflect the
		 * nesting of the sub-errors.
		 *
		 * @param {Object} zMap a Z883/Typed map, in canonical form
		 * @param {string} key
		 * @param {Array} labels
		 * @param {Array} keysUsed any keys used (or checked) here are added to this array
		 * @return {string}
		 */
		keyAndZErrorSummary: function ( zMap, key, labels, keysUsed ) {
			keysUsed.push( key );
			let suberrors;
			const error = getValueFromCanonicalZMap( zMap, key );
			if ( error === undefined ) {
				suberrors = [];
			} else E{
				suberrors = extractErrorStructure( error );
			}
			if ( suberrors.length === 0 ) {
				// Display (for English reader): "Errors: none" or "Validator errors: none"
				const i18nKeyForNoErrors = this.$i18n( 'wikilambda-functioncall-metadata-errors', 0 ).text();
				return '<li><b>' + i18nKeyForNoErrors + ':</b> ' +
					this.$i18n( 'wikilambda-functioncall-metadata-errors-none' ).text() + '</li>';
			}
			// The knownKeys keys display "Error summary: ..." or "Validator error summary: ..."
			const i18nKey = this.$i18n( knownKeys.get( key ).i18nId ).text();
			let html = '<li><b>' + i18nKey + ':</b><br>';
			html = html + this.formatZErrorSummary( suberrors, labels );
			return html + '</li>';
		},
		formatZErrorSummary: function ( suberrors, labels ) {
			let html = '<ul>';
			for ( const suberror of suberrors ) {
				html = html + '<li>' + this.messageForSuberror( suberror, labels ) + '</li>';
				if ( suberror.children !== [] ) {
					html = html + this.formatZErrorSummary( suberror.children, labels );
				}
			}
			return html + '</ul>';
		},
		/**
		 * Construct a message characterizing the given suberror.  The message returned
		 * is plain text, no HTML markup.  See schemata.js::extractErrorStructure for
		 * more about the suberror objects.
		 *
		 * @param {Object} subError
		 * @param {Array} labels
		 * @return {string}
		 */
		messageForSuberror: function ( subError, labels ) {
			let message;
			const errorType = subError.errorType;
			const errorLabel = labels[ errorType ];
			message = '[' + errorType + '/' + errorLabel + ']';
			if ( subError.explanation ) {
				message = message + ' ' + subError.explanation;
			}
			return message;
		},
 
		/**
		 * Portray the given key and its zMap value, which should be a string containing
		 * a date+time in ISO 8601 format
		 *
		 * @param {Object} zMap a Z883/Typed map, in canonical form
		 * @param {string} key
		 * @param {Array} keysUsed any keys used (or checked) here are added to this array
		 * @return {string}
		 */
		keyAndDatetimeValue: function ( zMap, key, keysUsed ) {
 
			/**
			 * Attempt to render a relative timestamp given a datetime string in ISO 8601 format
			 *
			 * @param {string} dateTimeString
			 * @return {string}
			 */
			const getDatetimeRelativeValue = function ( dateTimeString ) {
				if ( Intl.RelativeTimeFormat ) {
					const target = Date.parse( dateTimeString );
					const now = Date.now();
					const offset = now - target;
 
					let relativeTimeFormatter;
					try {
						relativeTimeFormatter = new Intl.RelativeTimeFormat( mw.config.get( 'wgUserLanguage' ) );
					} catch ( error ) {
						// Fall back to English if the MW locale isn't supported
						relativeTimeFormatter = new Intl.RelativeTimeFormat( 'en' );
					}
 
					let offsetThreshold = 1000;
 
					// If this was within the last minute, render in seconds.
					if ( offset < offsetThreshold * 60 ) {
						return relativeTimeFormatter.format(
							-Math.floor( offset / offsetThreshold ),
							'second'
						);
					}
					offsetThreshold *= 60;
 
					// If this was within the last hour, render in minutes.
					if ( offset < offsetThreshold * 60 ) {
						return relativeTimeFormatter.format(
							-Math.floor( offset / offsetThreshold ),
							'minute'
						);
					}
					offsetThreshold *= 60;
 
					// If this was within the last day, render in hours.
					if ( offset < offsetThreshold * 24 ) {
						return relativeTimeFormatter.format(
							-Math.floor( offset / offsetThreshold ),
							'hour'
						);
					}
					offsetThreshold *= 24;
 
					// If this was within the last week, render in days.
					if ( offset < offsetThreshold * 7 ) {
						return relativeTimeFormatter.format(
							-Math.floor( offset / offsetThreshold ),
							'hour'
						);
					}
					offsetThreshold *= 7;
 
					// If this was within the last four weeks, render in weeks.
					if ( offset < offsetThreshold * 4 ) {
						return relativeTimeFormatter.format(
							-Math.floor( offset / offsetThreshold ),
							'hour'
						);
					}
					offsetThreshold *= 4;
				}
 
				// Fallback for browsers without Intl
				return dateTimeString.replace( 'T', ' ' ).replace( 'Z', ' (UTC)' );
			};
 
			keysUsed.push( key );
			const value = getValueFromCanonicalZMap( zMap, key );
			if ( value === undefined ) {
				return '';
			}
			const i18nKey = this.$i18n( knownKeys.get( key ).i18nId ).text();
			return '<li><b>' + i18nKey + ':</b> ' + getDatetimeRelativeValue( value ) + '</li>';
		},
 
		/**
		 * Portray the given key and its zMap value, which should be a string
		 * that's already suitable for presentation
		 *
		 * @param {Object} zMap a Z883/Typed map, in canonical form
		 * @param {string} key
		 * @param {Array} keysUsed any keys used (or checked) here are added to this array
		 * @return {string}
		 */
		keyAndStringValue: function ( zMap, key, keysUsed ) {
			keysUsed.push( key );
			let value = getValueFromCanonicalZMap( zMap, key );
			if ( value === undefined ) {
				return '';
			}
			Iif ( typeof value === 'object' && value[ Constants.Z_OBJECT_TYPE ] === Constants.Z_STRING &&
				value[ Constants.Z_STRING_VALUE ] ) {
				// In canonical form there are cases where the string remains inside a Z6 object;
				// extract it here
				value = value[ Constants.Z_STRING_VALUE ];
			}
			value = this.maybeStringify( value );
			const i18nKey = this.$i18n( knownKeys.get( key ).i18nId ).text();
			return '<li><b>' + i18nKey + ':</b> ' + value + '</li>';
		},
		/**
		 * Portray the given key and its zMap value, which should be an arbitrary ZObject
		 * not handled by any of the more specialized methods.
		 *
		 * @param {Object} zMap a Z883/Typed map, in canonical form
		 * @param {string} key
		 * @param {Array} keysUsed any keys used (or checked) here are added to this array
		 * @return {string}
		 */
		keyAndArbitraryValue: function ( zMap, key, keysUsed ) {
			keysUsed.push( key );
			let value = getValueFromCanonicalZMap( zMap, key );
			if ( value === undefined ) {
				return '';
			}
			value = this.maybeStringify( value );
			const i18nKey = this.$i18n( knownKeys.get( key ).i18nId ).text();
			return '<li><b>' + i18nKey + ':</b> ' + value + '</li>';
		}
	}
};