import { ConfigApi , createApiRef , DiscoveryApi , } from '@backstage/core-plugin-api' ; import { DateTime , Duration } from 'luxon' ; const DEFAULT_PROXY_PATH = '/prometheus/api' ; export const prometheusApiRef = createApiRef < PrometheusApi > ( { id : 'plugin.prometheus.service' , } ) ; type Options = { discoveryApi : DiscoveryApi ; configApi : ConfigApi ; } ; export class PrometheusApi { private readonly discoveryApi : DiscoveryApi ; /**
     * Path to use for requests via the proxy, defaults to /prometheus/api
     */ private readonly proxyPath : string ; constructor ( options ) { this . discoveryApi = options . discoveryApi ; this . proxyPath = options . configApi . getOptionalString ( 'prometheus.proxyPath' ) || DEFAULT_PROXY_PATH ; } private async getApiUrl ( ) { const proxyUrl = await this . discoveryApi . getBaseUrl ( 'proxy' ) ; return `${ proxyUrl }${ this . proxyPath }` ; } async query ( { query = `up` , range = { hours : 1 } , step = 14 , } ) { const apiUrl = await this . getApiUrl ( ) ; const end = DateTime . now ( ) . toSeconds ( ) ; const start = DateTime . now ( ) . minus ( Duration . fromObject ( range ) ) . toSeconds ( ) ; const response = await fetch ( `${ apiUrl }/query_range?query=${ query }&start=${ start }&end=${ end }&step=${ step }` ) ; if ( ! response . ok ) { throw new Error ( `failed to fetch data, status ${ response . status }: ${ response . statusText }` ) ; } return response . json ( ) ; } async getAlerts ( ) { const apiUrl = await this . getApiUrl ( ) ; const response = await fetch ( `${ apiUrl }/rules?type=alert` ) ; if ( ! response . ok ) { throw new Error ( `failed to fetch data, status ${ response . status }: ${ response . statusText }` ) ; } return response . json ( ) ; } }