2021-09-07 08:44:45 -05:00
import { DataSourceSettings } from '@grafana/data' ;
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' ;
2020-03-04 10:17:02 -06:00
interface Labels {
[ label : string ] : string [ ] ;
}
interface Series {
[ label : string ] : string ;
}
interface SeriesForSelector {
[ selector : string ] : Series [ ] ;
}
export function makeMockLokiDatasource ( labelsAndValues : Labels , series? : SeriesForSelector ) : LokiDatasource {
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/ ;
2019-12-03 02:40:22 -06:00
2022-04-28 07:28:57 -05:00
const lokiLabelsEndpoint = 'labels' ;
2021-09-07 08:44:45 -05:00
const rangeMock = {
start : 1560153109000 ,
end : 1560163909000 ,
2021-03-02 09:58:14 -06:00
} ;
2019-12-03 02:40:22 -06:00
2019-09-12 03:02:49 -05:00
const labels = Object . keys ( labelsAndValues ) ;
return {
2021-03-02 09:58:14 -06:00
getTimeRangeParams : ( ) = > rangeMock ,
2020-03-04 10:17:02 -06:00
metadataRequest : ( url : string , params ? : { [ key : string ] : string } ) = > {
2020-04-22 06:59:06 -05:00
if ( url === lokiLabelsEndpoint ) {
2020-03-04 10:17:02 -06:00
return labels ;
2019-09-12 03:02:49 -05:00
} else {
2020-03-04 10:17:02 -06:00
const labelsMatch = url . match ( lokiLabelsAndValuesEndpointRegex ) ;
const seriesMatch = url . match ( lokiSeriesEndpointRegex ) ;
2020-04-22 06:59:06 -05:00
if ( labelsMatch ) {
2020-03-04 10:17:02 -06:00
return labelsAndValues [ labelsMatch [ 1 ] ] || [ ] ;
2020-07-08 04:05:20 -05:00
} else if ( seriesMatch && series && params ) {
2021-09-07 08:44:45 -05:00
return series [ params [ 'match[]' ] ] || [ ] ;
2020-03-04 10:17:02 -06:00
} else {
throw new Error ( ` Unexpected url error, ${ url } ` ) ;
2019-09-12 03:02:49 -05:00
}
}
} ,
2022-02-03 04:40:19 -06:00
interpolateString : ( string : string ) = > string ,
2019-09-12 03:02:49 -05:00
} as any ;
}
2019-10-25 09:43:20 -05:00
export function createDefaultConfigOptions ( ) : DataSourceSettings < LokiOptions > {
2022-07-20 02:25:09 -05:00
return getMockDataSource < LokiOptions > ( {
jsonData : { maxLines : '531' } ,
2019-10-25 09:43:20 -05:00
} ) ;
}