/* IBM Confidential OCO Source Materials IBM Cognos Products: authoring (C) Copyright IBM Corp. 2015, 2020 The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the U.S. Copyright Office. */ define(['q'], function(Q){ 'use strict'; var v_oRsPerformance = { M_sPerformanceLogServerHost: undefined, M_sKitVersion: 'no kit version', M_bKitVersionIsCached: false, summarizeInteractiveViewerRun: function( v_oView) { this._commonSummarize( v_oView, [ { start:'rsContextMenuActions.onSelectItem', end: 'rsContentView.render'}, { start:'rsContentView.render', end: 'rsContentView._onRSApplicationReady'}, { start:'rsContentView._onRSApplicationReady', end: 'rsContentView.onViewBeforeDraw'}, { start:'rsContentView.onViewBeforeDraw', end: 'rsContentView.onViewAfterDraw'}, { start:'rsContextMenuActions.onSelectItem', end: 'rsContentView.onViewAfterDraw'}, { start:'authoring-selectItemAndDraw-start', end: 'authoring-selectItemAndDraw-stop'} ]); }, summarizeClassicViewerRun: function ( v_oView ) { this._commonSummarize(v_oView, [ { start:'rsContextMenuActions.onSelectItem', end: 'cvContentView.render'}, { start:'cvContentView.render', end: 'cvContentView.getViewerConfiguration'}, { start:'cvContentView.getViewerConfiguration', end: 'cvContentView.onReportStatusComplete'}, { start:'cvContentView.onReportStatusComplete', end: 'cvContentView._onClassicViewerLoaded'}, { start:'rsContextMenuActions.onSelectItem', end: 'cvContentView._onClassicViewerLoaded'}, { start:'authoring-selectItemAndDraw-start', end: 'authoring-selectItemAndDraw-stop'} ]); }, _commonSummarize: function ( v_oView, v_aStartEndNames ) { // We do the non-essential work inside a try-catch because the window.performance methods // can throw exceptions when markers don't exist or have been renamed. try { if ( this.getEntriesByName('rsContextMenuActions.onSelectItem','mark').length < 1 ) { return; } v_aStartEndNames.forEach( function ( v_o ) { var v_sName = v_o.start + '->' + v_o.end; try { this.measure( v_sName, v_o.start, v_o.end ); } catch(e) { // sometimes we don't have the start or end and will get an exception // skip it } }, this); var v_aPerformanceFields= [ "navigationStart", "unloadEventStart", "unloadEventEnd", "redirectStart", "redirectEnd", "fetchStart", "domainLookupStart", "domainLookupEnd", "connectStart", "connectEnd", "requestStart", "responseStart", "responseEnd", "domLoading", "domInteractive", "domContentLoadedEventStart", "domContentLoadedEventEnd", "domComplete", "loadEventStart", "loadEventEnd"]; var v_oPerformanceOut = {timing:{}}; var iframe = v_oView.m_iframeAuthoring || v_oView.m_iframeClassicViewer; var v_oPerformanceIn = iframe.contentWindow.performance; for(var iPField=0; iPField < v_aPerformanceFields.length; ++iPField) { var v_sField = v_aPerformanceFields[iPField]; if (v_oPerformanceIn.timing[v_sField] !== undefined) { v_oPerformanceOut.timing[v_sField] = v_oPerformanceIn.timing[v_sField]; } } var v_oPerformanceSummary = { userAgent: window.navigator.userAgent, urlPath: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ":" + window.location.port : "") + window.location.pathname, reportName: v_oView.cmProperties.defaultName, date: Date.now(), measures:[], rsWindowPerformance: v_oPerformanceOut }; this.getEntriesByType('measure').forEach( function( v_oEntry ) { console.info( "rsperf: performance.measure %s %s ms", v_oEntry.name, v_oEntry.duration); v_oPerformanceSummary.measures.push({name: v_oEntry.name, duration: v_oEntry.duration} ); }); this.clearMeasures(); this.clearMarks(); if (this.M_sPerformanceLogServerHost) { // Calling Q(blah) is tagged as an error because Q() looks like // a ctor function which should be new Q(blah) var qPromiseFromValueForLint = Q; Q.all([ this.getKitVersion(v_oView.glassContext), this.getPerformanceUUID(v_oView.glassContext), qPromiseFromValueForLint(v_oPerformanceSummary), qPromiseFromValueForLint(v_oView.glassContext)]) .then( function (v_aResults) { this.sendPerformanceRecordToHost.apply(this, v_aResults); }.bind(this)) .catch( function () { console.info('rsperf: failed to get kit and uuid for logging'); }) .done( function (v_oResult) { console.info('rsperf: _commonSummarized finished'); }); } else { this.getKitVersion(v_oView.glassContext) .catch( function(err){ console.info('rsperf: error while getting kit version'); }) .done( function(version){ console.info('rsperf: kit version %s (cached: %s)', version, this.M_bKitVersionIsCached.toString()); }.bind(this)); } } catch(err) { console.warn('_commonSummarize failed with an exception:\n%s', err && err.toString() || "unknown err"); this.clearMeasures(); this.clearMarks(); } }, getKitVersion: function( v_oGlassContext ) { var deferred = Q.defer(); if ( this.M_sKitVersion ) { this.M_bKitVersionIsCached = true; deferred.resolve(this.M_sKitVersion); return deferred.promise; } v_oGlassContext.services.ajax.ajax({ url: window.location.pathname + 'cmplst.txt', type: 'GET', dataType: 'text' }) .fail( function(err){ console.warn('getKitVersion() failed '); this.M_sKitVersion = 'error cannot get kit version'; deferred.reject(this.M_sKitVersion); }.bind(this)) .done( function(resp){ var v_aMatches = resp.match(/^kit_version=\s*((?:\d+.)+\d+)\s*$/m); if (v_aMatches) { this.M_sKitVersion = v_aMatches[1]; } else { this.M_sKitVersion = 'could not find kit version'; } deferred.resolve(this.M_sKitVersion); }.bind(this)); return deferred.promise; }, getPerformanceUUID: function(v_oGlassContext) { var deferred = Q.defer(); v_oGlassContext.services.ajax.ajax({ url: this.M_sPerformanceLogServerHost + "/_uuids", type: 'GET', dataType: 'json' }) .fail(function(jqXHR, err){ deferred.reject('uuid_failed'); }) .done(function(resp){ deferred.resolve(resp.uuids[0]); }); return deferred.promise; }, sendPerformanceRecordToHost: function(v_sKit, v_sUuid, v_oRecord, v_oGlassContext) { var deferred = Q.defer(); v_oRecord.kitVersion = v_sKit; v_oRecord.kitVersionCached = this.M_bKitVersionIsCached; v_oGlassContext.services.ajax.ajax({ headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, url: this.M_sPerformanceLogServerHost + "/toddtest/" + v_sUuid, type: 'PUT', data: JSON.stringify(v_oRecord) }) .fail( function (err) { console.log('error sendPerformanceRecordToHost: %s', err); deferred.reject(err); }) .done( function (result) { console.log('succeed sendPerformanceRecordToHost: %s', result); deferred.resolve(result); }); return deferred.promise; }, }; ['mark', 'measure', 'getEntriesByName', 'getEntriesByType', 'clearMeasures', 'clearMarks' ].forEach(function(fname){ if (window.performance[fname]) { this[fname] = window.performance[fname].bind(window.performance); } else if (['getEntriesByName', 'getEntriesByType'].indexOf(fname) != -1) { this[fname] = function(){ return []; }; } else { this[fname] = function(){}; } },v_oRsPerformance); return v_oRsPerformance; });