All files / ext.wikilambda.edit/components/widgets FunctionMetadataDialog.vue

91.17% Statements 124/136
77.41% Branches 48/62
96.66% Functions 29/30
91.17% Lines 124/136

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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542    28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x                                                   47x                     11x               11x 11x               11x     11x 11x 11x 144x             57x     57x                                           114x 114x 513x   513x                               513x   437x 219x   437x     114x                           456x 456x 1311x 980x                                         980x 328x 328x       56x   272x       980x 980x       456x               1x 1x   1x 1x 1x                   54x 54x               56x               54x               54x                     164x 164x 382x 382x   2x   382x   164x                     2x 2x 2x 2x                             162x     162x 162x                         108x 108x 108x 108x                           110x                   218x 218x 218x 218x   218x 218x         218x     218x 2x   216x     216x 172x   44x     44x     44x     44x     44x     44x     44x       44x       28x   28x     28x 28x 28x     28x     28x     28x       28x     28x 28x     28x     28x     28x 28x     28x       58x 58x 58x 58x 58x             57x                       68x               57x 327x         327x     327x     327x 762x                   328x                         28x                                                                                                              
<!--
	WikiLambda Vue component for the Function Metadata Dialog.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<cdx-dialog
		class="ext-wikilambda-metadata-dialog"
		:title="$i18n( 'wikilambda-about-widget-accessible-title' ).text()"
		:open="open"
		@update:open="closeDialog"
	>
		<!-- Dialog Header -->
		<template #header>
			<div class="cdx-dialog__header--default">
				<div class="cdx-dialog__header__title-group">
					<h2 class="cdx-dialog__header__title">
						{{ $i18n( 'wikilambda-function-evaluator-result-details' ).text() }}
					</h2>
					<p v-if="headerText" class="cdx-dialog__header__subtitle">
						{{ headerText }}
					</p>
				</div>
 
				<div class="ext-wikilambda-metadata-dialog-helplink">
					<cdx-icon :icon="icons.cdxIconHelpNotice"></cdx-icon>
					<a
						:title="tooltipMetaDataHelpLink"
						:href="parsedMetaDataHelpLink"
						target="_blank"
					>{{ $i18n( 'wikilambda-helplink-button' ).text() }}</a>
					<cdx-button
						weight="quiet"
						class="cdx-dialog__header__close-button"
						@click="closeDialog"
					>
						<cdx-icon :icon="icons.cdxIconClose"></cdx-icon>
					</cdx-button>
				</div>
			</div>
		</template>
 
		<!-- Dialog Body -->
		<div class="ext-wikilambda-metadata-dialog-body">
			<cdx-accordion
				v-for="( section, sectionIndex ) in sections"
				:key="sectionIndex"
				:open="section.open"
			>
				<template #title>
					{{ section.title }}
				</template>
				<template #description>
					{{ section.description }}
				</template>
				<ul class="ext-wikilambda-metadata-dialog-content">
					<li
						v-for="( item, itemIndex ) in section.content"
						:key="'item' + itemIndex"
						class="ext-wikilambda-metadata-dialog-key"
					>
						<span class="ext-wikilambda-metadata-dialog-key-title">{{ item.title }}</span>:
						<template v-if="item.value">
							<a
								v-if="item.url"
								:href="item.url"
						I		target="_blank"
							>{{ item.value }}</a>
							<span v-else>{{ item.value }}</span>
						</template>
						<ul v-if="item.content">
							<li
								v-for="( subitem, subindex ) in item.content"
								:key="'subitem' + subindex"
								class="ext-wikilambda-metadata-dialog-subkey"
							>
								{{ subitem.title }}: {{ subitem.value }}
							</li>
						</ul>
					<I/li>
				</ul>
			</cdx-accordion>
		</div>
	</cdx-dialog>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const CdxAccordion = require( '@wikimedia/codex' ).CdxAccordion,
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxDialog = require( '@wikimedia/codex' ).CdxDialog,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	Constants = require( '../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	metadataConfig = require( '../../mixins/metadata.js' ),
	schemata = require( '../../mixins/schemata.js' ).methods,
	typeUtils = require( '../../mixins/typeUtils.js' ).methods,
	icons = require( '../../../lib/icons.json' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-metadata-dialog',
	components: {
		'cdx-accordion': CdxAccordion,
		'cdx-button': CdxButton,
		'cdx-dialog': CdxDialog,
		'cdx-icon': CdxIcon
	},
	mixins: [ metadataConfig ],
	props: {
		open: {
			type: Boolean,
			required: true,
			default: false
		},
		headerText: {
			type: String,
			default: '',
			required: false
		},
		metadata: {
			type: Object,
			required: false
		}
	},
	data: function () {
		return {
			icons: icons
		};
	},
	computed: Object.assign( mapGetters( [
		'getLabel',
		'getLabelData',
		'getUserLangCode'
	] ), {
		/**
		 * Returns the help link from the Metadata dialog
		 *
		 * @return {string}
		 */
		tooltipMetaDataHelpLink: function () {
			return this.$i18n( 'wikilambda-helplink-tooltip' ).text();
		},
		/**
		 * Returns the parsed help link from the Metadata dialog
		 *
		 * @return {string}
		 */
		parsedMetaDataHelpLink: function () {
			const unformattedLink = this.$i18n( 'wikilambda-metadata-help-link' ).text();
			return mw.internalWikiUrlencode( unformattedLink );
		},
		/**
		 * Returns the available keys present in the metadata object
		 *
		 * @return {Map|undefined}
		 */
		keyValues: function () {
			if ( !this.metadata ) {
				return undefined;
			}
			const benjamin = this.metadata[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ];
			const items = benjamin.slice( 1 );
			return new Map( items.map( ( pair ) => {
				return [
					pair[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ],
					pair[ Constants.Z_TYPED_OBJECT_ELEMENT_2 ]
				];
			} ) );
		},
		/**
		 * @return {Array}
		 */
		sections: function () {
			if ( !this.keyValues ) {
				return [];
			}
			return this.compileSections( this.metadataKeys );
		}
	} ),
	methods: {
		/**
		 * Close the dialog.
		 */E
		closeDialog: function () {
			this.$emit( 'close-dialog' );
		},
		/**
		 * Returns the compiled metadata sections. Each item in the
		 * array contains the following properties:
		 * * title: the section title
		 * * description (optional): the method name that returns the description
		 * * sections (optional): the array of subsections, or
		 * * keys (optional): the array of terminal keys
		 *
		 * @param {Array} sections
		 * @return {Array}
		 */
		compileSections: function ( sections ) {
			conEst metadata = [];
			for ( const key in sections ) {
				const value = sections[ key ];
				// Build section
				const section = {
					// possible message keys:
					// * wikilambda-functioncall-metadata-errors
					// * wikilambda-functioncall-metadata-implementation
					// * wikilambda-functioncall-metadata-duration
					// * wikilambda-functioncall-metadata-orchestration
					// * wikilambda-functioncall-metadata-evaluation
					// * wikilambda-functioncall-metadata-cpu-usage
					// * wikilambda-functioncall-metadata-memory-usage
					// * wikilambda-functioncall-metadata-hostname
					// * wikilambda-functioncall-metadata-programming-language
					title: this.$i18n( value.title ).text(),
					content: ( 'sections' in value ) ?
						this.compileSections( value.sections ) :
						this.compileKeys( value.keys ),
					open: value.open || false
				};
				// Check that section has content
				if ( section.content.length > 0 ) {
					// Compute description if needed and description function is available
					if ( ( 'description' in value ) && ( value.description in this ) ) {
						section.description = this[ value.description ]();
					}
					metadata.push( section );
				}
			}
			return metadata;
		},
		/**
		 * Returns the compiled terminal key-values for the metadata sections
		 * given an array of key specifications. Each item in the array contains
		 * the following properties:
		 * * title: the key title,
		 * * key: mapping to the key identifier from the metadata array
		 * * transform (optional): method name to transform the value
		 *
		 * @param {Array} keys
		 * @return {Array}
		 */
		compileKeys: function ( keys ) {
			const metadata = [];
			for ( const value of keys ) {
				if ( this.keyValues.has( value.key ) ) {
					const item = {
						// possible message keys:
						// * wikilambda-functioncall-metadata-errors-summary
						// * wikilambda-functioncall-metadata-validator-errors-summary
						// * wikilambda-functioncall-metadata-expected-result
						// * wikilambda-functioncall-metadata-actual-result
						// * wikilambda-functioncall-metadata-execution-debug-logs
						// * wikilambda-functioncall-metadata-implementation-name
						// * wikilambda-functioncall-metadata-implementation-id
						// * wikilambda-functioncall-metadata-implementation-type
						// * wikilambda-functioncall-metadata-duration
						// * wikilambda-functioncall-metadata-start-time
						// * wikilambda-functioncall-metadata-end-time
						// * wikilambda-functioncall-metadata-orchestration
						// * wikilambda-functioncall-metadata-evaluation
						// * wikilambda-functioncall-metadata-execution
						// * wikilambda-functioncall-metadata-programming-language-version
						title: this.$i18n( value.title ).text(),
						value: this.keyValues.get( value.key )
					};
					// Apply transformation if needed and transform function is available
					if ( ( 'transform' in value ) && ( value.transform in this ) ) {
						Econst transformed = this[ value.transform ]( item.value );
						if ( !!transformed && typeof transformed === 'object' ) {
							// transformations can return an object with more values
							// E.g. { value: 'text', url: 'http://some.link' },
							// in such case, extend the item object.
							Object.assign( item, transformed );
						} else {
							item.value = transformed;
						}
					}
					// If the value is available, add to metadata
					if ( item.value ) {
						metadata.push( item );
					}
				}
			}
			return metadata;
		},I
		/**
		 * Returns the error section summary
		 *E
		 * @return {string}
		 */
		getErrorSummary: function () {
			const error = this.keyValues.get( 'errors' );
			const suberrors = schemata.extractErrorStructure( error );
			// Return None if there are no errors
			if ( suberrors.length > 0 ) {
				const errorType = suberrors[ 0 ].errorType;
				return this.getLabel( errorType );
			}
			return this.$i18n( 'wikilambda-functioncall-metadata-errors-none' ).text();
		},
		/**
		 * Returns the implementation section summary
		 *E
		 * @return {string}
		 */
		getImplementationSummary: function () {
			const implementation = this.getImplementationLink( this.keyValues.get( 'implementationId' ) );
			return implementation ? implementation.value : '';
		},
		/**
		 * Returns the duration section summary
		 *
		 * @return {string}
		 */
		getDurationSummary: function () {
			return this.sumValuesWithUnit( [
				this.keyValues.get( 'orchestrationDuration' ),
				this.keyValues.get( 'evaluationDuration' )
			], 'ms' );
		},
		/**
		 * Returns the CPU usage section summary
		 *
		 * @return {string}
		 */
		getCpuUsageSummary: function () {
			return this.sumValuesWithUnit( [
				this.keyValues.get( 'orchestrationCpuUsage' ),
				thEis.keyValues.get( 'evaluationCpuUsage' )
			], 'ms' );
		},
		/**
		 * Returns the memory usage section summary
		 *
		 * @return {string}
		 */
		getMemoryUsageSummary: function () {
			return this.sumValuesWithUnit( [
				this.keyValues.get( 'orchestrationMemoryUsage' ),
				this.keyValues.get( 'evaluationMemoryUsage' ),
				this.keyValues.get( 'executionMemoryUsage' )
			], 'MiB' );
		},
		/**
		 * Helper function to sum an array of values which
		 * are stringified numbers followed by their unit
		 *
		 * @param {Array} values
		 * @param {string} unit
		 * @return {string}
		 */
		sumValuesWithUnit: function ( values, unit ) {
			let total = 0;
			for ( let value of values ) {
				try I{
					value = parseFloat( value.split( ' ' )[ 0 ] );
				} catch ( e ) {
					value = 0;
				}
				total += value;
			}I
			return `${ total.toPrecision( 4 ) } ${ unit }`;
		},
		/**
		 * Returns the linked label of the root error from the given
		 * key ('errors' or 'validateErrors'). Returns an object with
		 * theI properties value and url, for the template to render a link.
		 *
		 * @param {Mixed} value
		 * @return {Object | undefined}
		 */
		getErrorType: function ( value ) {
			const suberrors = schemata.extractErrorStructure( value );
			if ( suberrors.length > 0 ) {
				const errorType = suberrors[ 0 ].errorType;
				return {
					value: this.getLabel( errorType ),
					url: this.getUrl( errorType )
				};
			}
			return undefined;
		},
		/**
		 * Transform method that given a mixed type value returns
		 * a string, either by returning its Z6K1, or strigifying it
		 *
		 * @param {Mixed} value
		 * @return {string}
		 */
		getStringValue: function ( value ) {
			if ( typeof value === 'string' ) {
				return value;
			}
			if ( ( typeof value === 'object' ) && ( value[ Constants.Z_STRING_VALUE ] ) ) {
				return value[ Constants.Z_STRING_VALUE ];
			}
			return JSON.stringify( value );
		},
		/**
		 * Transform method that given the value of implementationId,
		 * returns the name of that implementation Id and its Url.
		 * If the implementationId is invalid, returns undefined.
		 *
		 * @param {Mixed} value
		 * @return {Object | undefined}
		 */
		getImplementationLink: function ( value ) {
			const zid = this.getStringValue( value );
			if ( typeUtils.isValidZidFormat( zid ) ) {
				const labelData = this.getLabelData( zid );
				return {
					value: labelData ? labelData.label : this.$i18n( 'wikilambda-editor-default-name' ).text(),
					url: this.getUrl( zid )
				};
			}
			return undefined;
		},
		/**
		 * Returns the url for a given Zid
		 *
		 * @param {string} zid
		 * @return {string}
		 */
		getUrl: function ( zid ) {
			return '/view/' + this.getUserLangCode + '/' + zid;
		},
		/**
		 * Attempts to render a relative timestamp given a
		 * datetime string in ISO 8601 format
		 *
		 * @param {string} dateTimeString
		 * @return {string}
		 */
		toRelativeTime: 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)' );
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-metadata-dialog {
	.cdx-dialog__header--default {
		align-items: flex-start;
		gap: @spacing-100;
 
		.cdx-dialog__header__title-group {
			margin-top: @spacing-12;
		}
	}
 
	.ext-wikilambda-metadata-dialog-helplink {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		gap: @spacing-25;
		margin-top: -@spacing-25;
 
		> a {
			margin-right: @spacing-25;
		}
 
		> .cdx-icon {
			color: @color-base;
		}
	}
 
	.ext-wikilambda-metadata-dialog-body {
		color: @color-base;
 
		li {
			font-size: @wl-font-size-base;
		}
	}
}
</style>