2023-02-09 11:27:02 -06:00
import {
DataFrame ,
2023-04-18 05:04:51 -05:00
DataFrameType ,
2023-02-09 11:27:02 -06:00
DataSourceInstanceSettings ,
DataSourceSettings ,
FieldType ,
PluginType ,
toUtc ,
} from '@grafana/data' ;
2022-08-26 02:26:48 -05:00
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 = {
2022-10-06 09:35:30 -05:00
timeRange : jest.fn ( ) . mockReturnValue ( {
2022-08-26 02:26:48 -05:00
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
}
2023-02-09 11:27:02 -06:00
2023-02-16 05:55:31 -06:00
export function getMockFrames() {
const logFrameA : DataFrame = {
refId : 'A' ,
fields : [
{
name : 'Time' ,
type : FieldType . time ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 3 , 4 ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'Line' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 'line1' , 'line2' ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'labels' ,
type : FieldType . other ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [
2023-02-16 05:55:31 -06:00
{
label : 'value' ,
} ,
{
otherLabel : 'other value' ,
} ,
2023-04-20 09:59:18 -05:00
] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'tsNs' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ '3000000' , '4000000' ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'id' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 'id1' , 'id2' ] ,
2023-02-16 05:55:31 -06:00
} ,
] ,
2023-02-20 07:55:56 -06:00
meta : {
2023-04-18 05:04:51 -05:00
custom : {
frameType : 'LabeledTimeValues' ,
} ,
2023-02-20 07:55:56 -06:00
stats : [
2023-03-03 03:58:28 -06:00
{ displayName : 'Summary: total bytes processed' , unit : 'decbytes' , value : 11 } ,
2023-02-20 07:55:56 -06:00
{ displayName : 'Ingester: total reached' , value : 1 } ,
] ,
} ,
2023-02-16 05:55:31 -06:00
length : 2 ,
} ;
2023-02-09 11:27:02 -06:00
2023-02-16 05:55:31 -06:00
const logFrameB : DataFrame = {
refId : 'A' ,
fields : [
{
name : 'Time' ,
type : FieldType . time ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 1 , 2 ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'Line' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 'line3' , 'line4' ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'labels' ,
type : FieldType . other ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [
2023-02-16 05:55:31 -06:00
{
otherLabel : 'other value' ,
} ,
2023-04-20 09:59:18 -05:00
] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'tsNs' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ '1000000' , '2000000' ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'id' ,
type : FieldType . string ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 'id3' , 'id4' ] ,
2023-02-16 05:55:31 -06:00
} ,
] ,
meta : {
2023-04-18 05:04:51 -05:00
custom : {
frameType : 'LabeledTimeValues' ,
} ,
2023-02-20 07:55:56 -06:00
stats : [
2023-03-03 03:58:28 -06:00
{ displayName : 'Summary: total bytes processed' , unit : 'decbytes' , value : 22 } ,
2023-02-20 07:55:56 -06:00
{ displayName : 'Ingester: total reached' , value : 2 } ,
] ,
2023-02-09 11:27:02 -06:00
} ,
2023-02-16 05:55:31 -06:00
length : 2 ,
} ;
2023-02-09 11:27:02 -06:00
2023-02-16 05:55:31 -06:00
const metricFrameA : DataFrame = {
refId : 'A' ,
fields : [
{
name : 'Time' ,
type : FieldType . time ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 3000000 , 4000000 ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'Value' ,
type : FieldType . number ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 5 , 4 ] ,
2023-04-18 05:04:51 -05:00
labels : {
level : 'debug' ,
} ,
2023-02-16 05:55:31 -06:00
} ,
] ,
meta : {
2023-04-18 05:04:51 -05:00
type : DataFrameType . TimeSeriesMulti ,
2023-02-20 07:55:56 -06:00
stats : [
{ displayName : 'Ingester: total reached' , value : 1 } ,
2023-03-03 03:58:28 -06:00
{ displayName : 'Summary: total bytes processed' , unit : 'decbytes' , value : 11 } ,
2023-02-20 07:55:56 -06:00
] ,
2023-02-09 11:27:02 -06:00
} ,
2023-02-16 05:55:31 -06:00
length : 2 ,
} ;
2023-02-09 11:27:02 -06:00
2023-02-16 05:55:31 -06:00
const metricFrameB : DataFrame = {
refId : 'A' ,
fields : [
{
name : 'Time' ,
type : FieldType . time ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 1000000 , 2000000 ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'Value' ,
type : FieldType . number ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 6 , 7 ] ,
2023-04-18 05:04:51 -05:00
labels : {
level : 'debug' ,
} ,
2023-02-16 05:55:31 -06:00
} ,
] ,
meta : {
2023-04-18 05:04:51 -05:00
type : DataFrameType . TimeSeriesMulti ,
2023-02-20 07:55:56 -06:00
stats : [
{ displayName : 'Ingester: total reached' , value : 2 } ,
2023-03-03 03:58:28 -06:00
{ displayName : 'Summary: total bytes processed' , unit : 'decbytes' , value : 22 } ,
2023-02-20 07:55:56 -06:00
] ,
2023-02-09 11:27:02 -06:00
} ,
2023-02-16 05:55:31 -06:00
length : 2 ,
} ;
const metricFrameC : DataFrame = {
refId : 'A' ,
name : 'some-time-series' ,
fields : [
{
name : 'Time' ,
type : FieldType . time ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 3000000 , 4000000 ] ,
2023-02-16 05:55:31 -06:00
} ,
{
name : 'Value' ,
type : FieldType . number ,
config : { } ,
2023-04-20 09:59:18 -05:00
values : [ 6 , 7 ] ,
2023-04-18 05:04:51 -05:00
labels : {
level : 'error' ,
} ,
2023-02-16 05:55:31 -06:00
} ,
] ,
meta : {
2023-04-18 05:04:51 -05:00
type : DataFrameType . TimeSeriesMulti ,
2023-02-20 07:55:56 -06:00
stats : [
{ displayName : 'Ingester: total reached' , value : 2 } ,
2023-03-03 03:58:28 -06:00
{ displayName : 'Summary: total bytes processed' , unit : 'decbytes' , value : 33 } ,
2023-02-20 07:55:56 -06:00
] ,
2023-02-09 11:27:02 -06:00
} ,
2023-02-16 05:55:31 -06:00
length : 2 ,
} ;
return {
logFrameA ,
logFrameB ,
metricFrameA ,
metricFrameB ,
metricFrameC ,
} ;
}