import { Guid } from './../Utilities/Guid' ; import { Injectable } from '@angular/core' ; import { IAppInsights , IConfig , SeverityLevel } from '../models/app-insights' ; declare var appInsights ; function AiDefined ( checkFunctionName ? ) { checkFunctionName = typeof checkFunctionName !== 'undefined' ? checkFunctionName : true ; return ( _ , functionName , descriptor ) => { const originalMethod = descriptor . value ; descriptor . value = function ( ... args ) { if ( typeof appInsights !== 'undefined' && ( typeof appInsights [ functionName ] !== 'undefined' || ! checkFunctionName ) ) { return originalMethod . apply ( this , args ) ; } else { return null ; } } ; return descriptor ; } ; } function run < T > ( action ) { if ( typeof appInsights !== 'undefined' ) { return action ( ) ; } else { return ( ) => { } ; } } @ Injectable ( ) export class AiService implements IAppInsights { config : IConfig = run ( ( ) => appInsights . config ) ; context : any = run ( ( ) => appInsights . context ) ; queue : ( ( ) => void ) [ ] = run ( ( ) => appInsights . queue ) ; private _traceStartTimes : { [ name ] : number ; } = { } ; /**
     * Sets the sessionId for all the current events.
     */ @ AiDefined ( false ) setSessionId ( sessionId , count ? ) { count = typeof count !== 'undefined' ? count : 0 ; if ( appInsights . context ) { appInsights . context . addTelemetryInitializer ( envelope => { if ( envelope && envelope . tags ) { envelope . tags [ 'ai.session.id' ] = sessionId ; } } ) ; } else if ( count < 5 ) { setTimeout ( ( ) => this . setSessionId ( sessionId , count + 1 ) , 2000 ) ; } } /**
     * Starts timing how long the user views a page or other item. Call this when the page opens.
     * This method doesn't send any telemetry. Call  stopTrackTelemetry to log the page when it closes.
     *  param name @ AiDefined ( ) startTrackPage ( name ? ) { return appInsights . startTrackPage ( name ) ; } /**
     * Logs how long a page or other item was visible, after  startTrackPage . Call this when the page closes.
     *  param name param url param properties param measurements @ AiDefined ( ) stopTrackPage ( name ? , url ? , properties ? , measurements ? ) { return appInsights . stopTrackPage ( name , url , properties , measurements ) ; } param name param url param properties param measurements param duration @ AiDefined ( ) trackPageView ( name ? , url ? , properties ? , measurements ? , _ ? ) { return appInsights . trackPageView ( name , url , properties , measurements ) ; } /**
     * Start timing an extended event. Call  stopTrackEvent to log the event when it ends.
     *  param name @ AiDefined ( ) startTrackEvent ( name ) { return appInsights . startTrackEvent ( name ) ; } /**
     * Log an extended event that you started timing with  startTrackEvent .
     *  param name param properties param measurements @ AiDefined ( ) stopTrackEvent ( name , properties ? , measurements ? ) { return appInsights . stopTrackEvent ( name , properties , measurements ) ; } returns startTrace ( ) { const traceKey = Guid . newTinyGuid ( ) ; this . _traceStartTimes [ traceKey ] = Date . now ( ) ; return traceKey ; } /**
     * Log an extended event that you started timing with  startTrace .
     *  param traceKey param properties param measurements stopTrace ( name , traceKey , properties ? , measurements ? ) { const eventStart = this . _traceStartTimes [ traceKey ] ; if ( eventStart ) { delete this . _traceStartTimes [ traceKey ] ; const duration = Date . now ( ) - eventStart ; properties = ! ! properties ? properties : { } ; properties [ 'duration' ] = duration + '' ; this . trackEvent ( name , properties , measurements ) ; } } param name param properties param measurements @ AiDefined ( ) trackEvent ( name , properties ? , measurements ? ) { const data = this . _getTrackingData ( properties ) ; return appInsights . trackEvent ( name , data , measurements ) ; } param name param expired trackLinkClick ( _ , __ , ___ ? , ____ ? ) { } param id param method param absoluteUrl param pathName param totalTime param success param resultCode @ AiDefined ( ) trackDependency ( id , method , absoluteUrl , pathName , totalTime , success , resultCode ) { return appInsights . trackDependency ( id , method , absoluteUrl , pathName , totalTime , success , resultCode ) ; } param exception param properties param measurements param severityLevel trackException ( exception , handledAt ? , properties ? , measurements ? , _ ? ) { console . error ( exception ) ; if ( typeof appInsights !== 'undefined' && appInsights . trackException ) { return appInsights . trackException ( exception , handledAt , properties , measurements ) ; } } param name param average param sampleCount param min param max @ AiDefined ( ) trackMetric ( name , average , sampleCount ? , min ? , max ? , properties ? ) { return appInsights . trackMetric ( name , average , sampleCount , min , max , properties ) ; } param message param properties @ AiDefined ( ) trackTrace ( message , properties ? ) { return appInsights . trackTrace ( message , properties ) ; } /**
     * Immediately send all queued telemetry.
     */ @ AiDefined ( ) flush ( ) { return appInsights . flush ( ) ; } /**
     * Clears the authenticated user id and the account id from the user context.
     */ @ AiDefined ( ) clearAuthenticatedUserContext ( ) { return appInsights . clearAuthenticatedUserContext ( ) ; } @ AiDefined ( ) downloadAndSetup ( config ) { return appInsights . downloadAndSetup ( config ) ; } param { string } message param { string } url param { number } lineNumber param { number } columnNumber param { Error } error @ AiDefined ( ) _onerror ( message , url , lineNumber , columnNumber , error ) { return appInsights . _onerror ( message , url , lineNumber , columnNumber , error ) ; } private _getTrackingData ( data ) { const properties = data ? typeof data === 'object' ? data : { message : data } : { } ; const identifiers = window . appsvc ? JSON . stringify ( { hostName : window . appsvc . env && window . appsvc . env . hostName , appName : window . appsvc . env && window . appsvc . env . appName , version : window . appsvc . version , resourceId : window . appsvc . resourceId , feature : window . appsvc . feature , frameId : window . appsvc . frameId } ) : '' ; return { identifiers , ... properties , } ; } }