import { i18n } from '@osd/i18n' ; import { CoreStart } from 'opensearch-dashboards/public' ; import { TelemetryPluginConfig } from '../plugin' ; interface TelemetryServiceConstructor { config : TelemetryPluginConfig ; http : CoreStart [ 'http' ] ; notifications : CoreStart [ 'notifications' ] ; reportOptInStatusChange ? : boolean ; } export class TelemetryService { private readonly http : CoreStart [ 'http' ] ; private readonly reportOptInStatusChange : boolean ; private readonly notifications : CoreStart [ 'notifications' ] ; private readonly defaultConfig : TelemetryPluginConfig ; private updatedConfig ? : TelemetryPluginConfig ; constructor ( { config , http , notifications , reportOptInStatusChange = true , } ) { this . defaultConfig = config ; this . reportOptInStatusChange = reportOptInStatusChange ; this . notifications = notifications ; this . http = http ; } public set config ( updatedConfig ) { this . updatedConfig = updatedConfig ; } public get config ( ) { return { ... this . defaultConfig , ... this . updatedConfig } ; } public get isOptedIn ( ) { return false ; } public set isOptedIn ( optIn ) { optIn = false ; this . config = { ... this . config , optIn } ; } public get userHasSeenOptedInNotice ( ) { return this . config . telemetryNotifyUserAboutOptInDefault ; } public set userHasSeenOptedInNotice ( telemetryNotifyUserAboutOptInDefault ) { this . config = { ... this . config , telemetryNotifyUserAboutOptInDefault } ; } public getCanChangeOptInStatus = ( ) => { const allowChangingOptInStatus = this . config . allowChangingOptInStatus ; return allowChangingOptInStatus ; } ; public getOptInStatusUrl = ( ) => { const telemetryOptInStatusUrl = this . config . optInStatusUrl ; return telemetryOptInStatusUrl ; } ; public getTelemetryUrl = ( ) => { const telemetryUrl = this . config . url ; return telemetryUrl ; } ; /**
     * Returns if an user should be shown the notice about Opt-In/Out telemetry.
     * The decision is made based on whether any user has already dismissed the message or
     * the user can't actually change the settings (in which case, there's no point on bothering them)
     */ public getUserShouldSeeOptInNotice ( ) { return ( ( this . config . telemetryNotifyUserAboutOptInDefault && this . config . userCanChangeSettings ) ?? false ) ; } public get userCanChangeSettings ( ) { return this . config . userCanChangeSettings ?? false ; } public set userCanChangeSettings ( userCanChangeSettings ) { this . config = { ... this . config , userCanChangeSettings } ; } public getIsOptedIn = ( ) => { return this . isOptedIn ; } ; public fetchExample = async ( ) => { return await this . fetchTelemetry ( { unencrypted : true } ) ; } ; public fetchTelemetry = async ( { unencrypted = false } = { } ) => { return ; } ; public setOptIn = async ( optedIn ) : Promise < boolean > => { const canChangeOptInStatus = this . getCanChangeOptInStatus ( ) ; if ( ! canChangeOptInStatus ) { return false ; } try { const optInPayload = await this . http . post < string [ ] > ( '/api/telemetry/v2/optIn' , { body : JSON . stringify ( { enabled : optedIn } ) , } ) ; if ( this . reportOptInStatusChange ) { await this . reportOptInStatus ( optInPayload ) ; } this . isOptedIn = optedIn ; } catch ( err ) { this . notifications . toasts . addError ( err , { title : i18n . translate ( 'telemetry.optInErrorToastTitle' , { defaultMessage : 'Error' , } ) , toastMessage : i18n . translate ( 'telemetry.optInErrorToastText' , { defaultMessage : 'An error occurred while trying to set the usage statistics preference.' , } ) , } ) ; return false ; } return true ; } ; public setUserHasSeenNotice = async ( ) : Promise < void > => { try { await this . http . put ( '/api/telemetry/v2/userHasSeenNotice' ) ; this . userHasSeenOptedInNotice = true ; } catch ( error ) { this . notifications . toasts . addError ( error , { title : i18n . translate ( 'telemetry.optInNoticeSeenErrorTitle' , { defaultMessage : 'Error' , } ) , toastMessage : i18n . translate ( 'telemetry.optInNoticeSeenErrorToastText' , { defaultMessage : 'An error occurred dismissing the notice' , } ) , } ) ; this . userHasSeenOptedInNotice = false ; } } ; param optInPayload private reportOptInStatus = async ( optInPayload ) : Promise < void > => { const telemetryOptInStatusUrl = this . getOptInStatusUrl ( ) ; try { await fetch ( telemetryOptInStatusUrl , { method : 'POST' , headers : { 'Content-Type' : 'application/json' , } , body : JSON . stringify ( optInPayload ) , } ) ; } catch ( err ) { } } ; }