1import { fromString } from '../../graphite/configuration/parseLokiLabelMappings';
2import fromGraphiteQueries from './fromGraphite';
3import { GraphiteQuery } from '../../graphite/types';
4import { GraphiteDatasource } from '../../graphite/datasource';
5
6describe('importing from Graphite queries', () => {
7  let graphiteDatasourceMock: GraphiteDatasource;
8
9  function mockSettings(stringMappings: string[]) {
10    graphiteDatasourceMock = ({
11      getImportQueryConfiguration: () => ({
12        loki: {
13          mappings: stringMappings.map(fromString),
14        },
15      }),
16      createFuncInstance: (name: string) => ({
17        name,
18        params: [],
19        def: {
20          name,
21          params: [{ multiple: true }],
22        },
23        updateText: () => {},
24      }),
25    } as any) as GraphiteDatasource;
26  }
27
28  function mockGraphiteQuery(raw: string): GraphiteQuery {
29    return {
30      refId: 'A',
31      target: raw,
32    };
33  }
34
35  beforeEach(() => {});
36
37  it('test matching mappings', () => {
38    mockSettings(['servers.(cluster).(server).*']);
39    const lokiQueries = fromGraphiteQueries(
40      [
41        // metrics: captured
42        mockGraphiteQuery('interpolate(alias(servers.west.001.cpu,1,2))'),
43        mockGraphiteQuery('interpolate(alias(servers.east.001.request.POST.200,1,2))'),
44        mockGraphiteQuery('interpolate(alias(servers.*.002.*,1,2))'),
45        // tags: captured
46        mockGraphiteQuery("interpolate(seriesByTag('cluster=west', 'server=002'), inf))"),
47        mockGraphiteQuery("interpolate(seriesByTag('foo=bar', 'server=002'), inf))"),
48        // regexp
49        mockGraphiteQuery('interpolate(alias(servers.eas*.{001,002}.request.POST.200,1,2))'),
50        // not captured
51        mockGraphiteQuery('interpolate(alias(test.west.001.cpu))'),
52        mockGraphiteQuery('interpolate(alias(servers.west.001))'),
53      ],
54      graphiteDatasourceMock
55    );
56
57    expect(lokiQueries).toMatchObject([
58      { refId: 'A', expr: '{cluster="west", server="001"}' },
59      { refId: 'A', expr: '{cluster="east", server="001"}' },
60      { refId: 'A', expr: '{server="002"}' },
61
62      { refId: 'A', expr: '{cluster="west", server="002"}' },
63      { refId: 'A', expr: '{foo="bar", server="002"}' },
64
65      { refId: 'A', expr: '{cluster=~"^eas.*", server=~"^(001|002)"}' },
66
67      { refId: 'A', expr: '' },
68      { refId: 'A', expr: '' },
69    ]);
70  });
71});
72