import { BackendSrv } from 'grafana/app/core/services/backend_srv' ; import { DEFAULT_LOUDML_RP , MODEL_TYPE_LIST , DEFAULT_MODEL , DEFAULT_FEATURE , DEFAULT_START_OPTIONS , MIN_INTERVAL_SECOND , MIN_INTERVAL_UNIT , MAX_INTERVAL_SECOND , MAX_INTERVAL_UNIT , MIN_SPAN , MAX_SPAN , DEFAULT_ANOMALY_TYPE , ANOMALY_HOOK_NAME , ANOMALY_HOOK , } from './types' ; export default class LoudMLAPI { private url : string ; constructor ( instanceSettings , private backendSrv ) { this . url = instanceSettings . url ; } async get ( url , params ? ) { return this . _query ( 'GET' , url , params ) ; } private async _query ( method , url , data ? , data_as_params ? ) { method = method . toUpperCase ( ) ; let options = { method , url : this . url + url , headers : { } , } ; if ( method === 'GET' || method === 'DELETE' || data_as_params ) { options . params = data ; } else { options . data = data ; options . headers [ 'Content-Type' ] = 'application/json' ; } const response = await this . backendSrv . datasourceRequest ( options ) ; const responseData = response . data ; return responseData ; } createAndGetBucket = async ( database , retentionPolicy , measurement , source ) => { const { host , port } = this . splitAddr ( 'http://localhost:8086' , 8086 ) ; const bucketName = [ database , retentionPolicy , measurement ] . join ( '_' ) ; const settings = { type : source . type , name : bucketName , retention_policy : retentionPolicy , database , measurement , addr : `${ host }:${ port }` , username : source . username , password : source . password , } ; await this . _query ( 'POST' , '/buckets' , settings ) ; const response = await this . _query ( 'GET' , `/buckets/${ bucketName }` ) ; return response [ 0 ] ; } ; splitAddr = ( url , port ) => { const re = /(https?:)?(\/\/)?([\w\.]*):?(\d*)?/ ; const res = re . exec ( url ) ; return { host : res [ 3 ] , port : res [ 4 ] || port , } ; } ; createModel = async ( model ) => { return this . _query ( 'POST' , '/models' , model ) ; } ; patchModel = async ( name , model ) => { return this . _query ( 'PATCH' , `/models/${ name }` , model ) ; } ; getModel = async ( name ) => { return this . _query ( 'GET' , `/models/${ name }` ) ; } ; deleteModel = async ( name ) => { return this . _query ( 'DELETE' , `/models/${ name }` ) ; } ; createHook = ( hook , bucket ) => { const h = { ... hook } ; h . config . bucket = bucket ; return h ; } ; createModelHook = async ( name , hook ) => { return this . _query ( 'POST' , `/models/${ name }/hooks` , hook ) ; } ; trainAndStartModel = async ( name , from , to , output_bucket ) => { const params = { ... DEFAULT_START_OPTIONS , from , to , output_bucket : output_bucket , } ; return this . _query ( 'POST' , `/models/${ name }/_train` , params , true ) ; } ; forecastModel = async ( name , data , output_bucket ) => { const { from , to } = data . timeRange . raw ; const params = { from , to , save_output_data : true , output_bucket : output_bucket , bg : true , } ; return this . _query ( 'POST' , `/models/${ name }/_forecast` , params , true ) ; } ; trainModel = async ( name , data , output_bucket ) => { const { lower , upper } = this . convertTimeRange ( data . timeRange ) ; return await this . trainAndStartModel ( name , lower , upper , output_bucket ) ; } ; startModel = async ( name , output_bucket ) => { const params = { ... DEFAULT_START_OPTIONS , output_bucket : output_bucket , } ; return this . _query ( 'POST' , `/models/${ name }/_start` , params , true ) ; } ; stopModel = async ( name ) => { const params = { } ; return this . _query ( 'POST' , `/models/${ name }/_stop` , params , true ) ; } ; scheduleJob = async ( job ) => { let params = { ... job , } ; window . console . log ( params ) ; params . every . count = parseInt ( params . every . count , 10 ) || 1 ; if ( ! params . params ) { delete params . params ; } else { params . params = JSON . parse ( params . params ) ; } if ( ! params . json ) { delete params . json ; } else { params . json = JSON . parse ( params . json ) ; } if ( ! params . every . at ) { delete params . every . at ; } delete params [ '$$hashKey' ] ; delete params . ok ; delete params . error ; delete params . status_code ; delete params . last_run_timestamp ; window . console . log ( params ) ; return this . _query ( 'POST' , `/scheduled_jobs` , params ) ; } ; deleteJob = async ( name ) => { return this . _query ( 'DELETE' , `/scheduled_jobs/${ name }` ) ; } ; convertTimeRange = timeRange => { const { from , to } = timeRange . raw ; return { lower : from , upper : to , } ; } ; }