2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  {  DataSourceInstanceSettings ,  DataSourceSettings ,  PluginType ,  toUtc  }  from  '@grafana/data' ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  {  TemplateSrv  }  from  '@grafana/runtime' ;  
						 
					
						
							
								
									
										
										
										
											2022-04-22 14:33:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-07-20 09:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  {  getMockDataSource  }  from  '../../../features/datasources/__mocks__' ;  
						 
					
						
							
								
									
										
										
										
											2019-09-12 10:02:49 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-28 14:28:57 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  {  LokiDatasource  }  from  './datasource' ;  
						 
					
						
							
								
									
										
										
										
											2022-04-22 14:33:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import  {  LokiOptions  }  from  './types' ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								export  function  createDefaultConfigOptions ( ) :  DataSourceSettings < LokiOptions >  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return  getMockDataSource < LokiOptions > ( { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    jsonData :  {  maxLines :  '531'  } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-04 17:17:02 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02: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 17:17:02 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-09-12 17:48:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const  defaultTemplateSrvMock  =  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  replace :  ( input : string )  = >  input , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								export  function  createLokiDatasource (  
						 
					
						
							
								
									
										
										
										
											2022-09-12 17:48:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  templateSrvMock : Partial < TemplateSrv >  =  defaultTemplateSrvMock , 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02: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 12:17:04 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    readOnly : false , 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    jsonData :  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      maxLines :  '20' , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    access :  'direct' , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    . . . settings , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // @ts-expect-error
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return  new  LokiDatasource ( customSettings ,  templateSrvMock ,  timeSrvStub ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-04 17:17:02 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								export  function  createMetadataRequest (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  labelsAndValues : Record < string ,  string [ ] > , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  series? : Record < string ,  Array < Record < string ,  string > > > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  {  
						 
					
						
							
								
									
										
										
										
											2022-06-02 11:30:47 +02: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 14:28:57 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const  lokiSeriesEndpointRegex  =  /^series/ ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const  lokiLabelsEndpoint  =  'labels' ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-12 10:02:49 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  const  labels  =  Object . keys ( labelsAndValues ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02: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 10:02:49 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      }  else  { 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        throw  new  Error ( ` Unexpected url error,  ${ url } ` ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-12 10:02:49 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      } 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 09:26:48 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } ; 
							 
						 
					
						
							
								
									
										
										
										
											2019-10-25 16:43:20 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}