mocks.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { DataSourceSettings } from '@grafana/data';
  2. import { createDatasourceSettings } from '../../../features/datasources/mocks';
  3. import { LokiDatasource } from './datasource';
  4. import { LokiOptions } from './types';
  5. interface Labels {
  6. [label: string]: string[];
  7. }
  8. interface Series {
  9. [label: string]: string;
  10. }
  11. interface SeriesForSelector {
  12. [selector: string]: Series[];
  13. }
  14. export function makeMockLokiDatasource(labelsAndValues: Labels, series?: SeriesForSelector): LokiDatasource {
  15. // 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.
  16. const lokiLabelsAndValuesEndpointRegex = /^label\/([%\w]*)\/values/;
  17. const lokiSeriesEndpointRegex = /^series/;
  18. const lokiLabelsEndpoint = 'labels';
  19. const rangeMock = {
  20. start: 1560153109000,
  21. end: 1560163909000,
  22. };
  23. const labels = Object.keys(labelsAndValues);
  24. return {
  25. getTimeRangeParams: () => rangeMock,
  26. metadataRequest: (url: string, params?: { [key: string]: string }) => {
  27. if (url === lokiLabelsEndpoint) {
  28. return labels;
  29. } else {
  30. const labelsMatch = url.match(lokiLabelsAndValuesEndpointRegex);
  31. const seriesMatch = url.match(lokiSeriesEndpointRegex);
  32. if (labelsMatch) {
  33. return labelsAndValues[labelsMatch[1]] || [];
  34. } else if (seriesMatch && series && params) {
  35. return series[params['match[]']] || [];
  36. } else {
  37. throw new Error(`Unexpected url error, ${url}`);
  38. }
  39. }
  40. },
  41. interpolateString: (string: string) => string,
  42. } as any;
  43. }
  44. export function createDefaultConfigOptions(): DataSourceSettings<LokiOptions> {
  45. return createDatasourceSettings<LokiOptions>({
  46. maxLines: '531',
  47. });
  48. }