2022-08-26 02:26:48 -05:00
import { DataSourceInstanceSettings , DataSourceSettings , PluginType , toUtc } from '@grafana/data' ;
import { TemplateSrv } from '@grafana/runtime' ;
2022-04-22 08:33:13 -05:00
2022-07-20 02:25:09 -05:00
import { getMockDataSource } from '../../../features/datasources/__mocks__' ;
2019-09-12 03:02:49 -05:00
2022-04-28 07:28:57 -05:00
import { LokiDatasource } from './datasource' ;
2022-04-22 08:33:13 -05:00
import { LokiOptions } from './types' ;
2022-08-26 02:26:48 -05:00
export function createDefaultConfigOptions ( ) : DataSourceSettings < LokiOptions > {
return getMockDataSource < LokiOptions > ( {
jsonData : { maxLines : '531' } ,
} ) ;
2020-03-04 10:17:02 -06:00
}
2022-08-26 02:26:48 -05:00
const rawRange = {
from : toUtc ( '2018-04-25 10:00' ) ,
to : toUtc ( '2018-04-25 11:00' ) ,
} ;
const defaultTimeSrvMock = {
timeRange : ( ) = > ( {
from : rawRange . from ,
to : rawRange.to ,
raw : rawRange ,
} ) ,
} ;
2020-03-04 10:17:02 -06:00
2022-09-12 10:48:04 -05:00
const defaultTemplateSrvMock = {
replace : ( input : string ) = > input ,
} ;
2022-08-26 02:26:48 -05:00
export function createLokiDatasource (
2022-09-12 10:48:04 -05:00
templateSrvMock : Partial < TemplateSrv > = defaultTemplateSrvMock ,
2022-08-26 02:26:48 -05:00
settings : Partial < DataSourceInstanceSettings < LokiOptions > > = { } ,
timeSrvStub = defaultTimeSrvMock
) : LokiDatasource {
const customSettings : DataSourceInstanceSettings < LokiOptions > = {
url : 'myloggingurl' ,
id : 0 ,
uid : '' ,
type : '' ,
name : '' ,
meta : {
id : 'id' ,
name : 'name' ,
type : PluginType . datasource ,
module : '' ,
baseUrl : '' ,
info : {
author : {
name : 'Test' ,
} ,
description : '' ,
links : [ ] ,
logos : {
large : '' ,
small : '' ,
} ,
screenshots : [ ] ,
updated : '' ,
version : '' ,
} ,
} ,
2022-08-26 06:17:04 -05:00
readOnly : false ,
2022-08-26 02:26:48 -05:00
jsonData : {
maxLines : '20' ,
} ,
access : 'direct' ,
. . . settings ,
} ;
// @ts-expect-error
return new LokiDatasource ( customSettings , templateSrvMock , timeSrvStub ) ;
2020-03-04 10:17:02 -06:00
}
2022-08-26 02:26:48 -05:00
export function createMetadataRequest (
labelsAndValues : Record < string , string [ ] > ,
series? : Record < string , Array < Record < string , string > >>
) {
2022-06-02 04:30:47 -05:00
// added % to allow urlencoded labelKeys. Note, that this is not confirm with Loki, as loki does not allow specialcharacters in labelKeys, but needed for tests.
const lokiLabelsAndValuesEndpointRegex = /^label\/([%\w]*)\/values/ ;
2022-04-28 07:28:57 -05:00
const lokiSeriesEndpointRegex = /^series/ ;
const lokiLabelsEndpoint = 'labels' ;
2019-09-12 03:02:49 -05:00
const labels = Object . keys ( labelsAndValues ) ;
2022-08-26 02:26:48 -05:00
return async function metadataRequestMock ( url : string , params? : Record < string , string | number > ) {
if ( url === lokiLabelsEndpoint ) {
return labels ;
} else {
const labelsMatch = url . match ( lokiLabelsAndValuesEndpointRegex ) ;
const seriesMatch = url . match ( lokiSeriesEndpointRegex ) ;
if ( labelsMatch ) {
return labelsAndValues [ labelsMatch [ 1 ] ] || [ ] ;
} else if ( seriesMatch && series && params ) {
return series [ params [ 'match[]' ] ] || [ ] ;
2019-09-12 03:02:49 -05:00
} else {
2022-08-26 02:26:48 -05:00
throw new Error ( ` Unexpected url error, ${ url } ` ) ;
2019-09-12 03:02:49 -05:00
}
2022-08-26 02:26:48 -05:00
}
} ;
2019-10-25 09:43:20 -05:00
}