diff --git a/.changeset/four-pandas-glow.md b/.changeset/four-pandas-glow.md new file mode 100644 index 0000000..37dcf62 --- /dev/null +++ b/.changeset/four-pandas-glow.md @@ -0,0 +1,5 @@ +--- +'grafana-zabbix': patch +--- + +Variables: Fix item tag filtering in Items type diff --git a/src/datasource/datasource.ts b/src/datasource/datasource.ts index e4c793f..9bf0e5e 100644 --- a/src/datasource/datasource.ts +++ b/src/datasource/datasource.ts @@ -852,7 +852,7 @@ export class ZabbixDatasource extends DataSourceApi { it('should return items', (done) => { const tests = [ - { query: '*.*.*.*', expect: ['/.*/', '/.*/', '', null, '/.*/'] }, - { query: '.*.*.*', expect: ['', '/.*/', '', null, '/.*/'] }, - { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', null, '/.*/'] }, - { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', null, '/.*/'] }, + { query: '*.*.*.*', expect: ['/.*/', '/.*/', '', undefined, '/.*/'] }, + { query: '.*.*.*', expect: ['', '/.*/', '', undefined, '/.*/'] }, + { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', undefined, '/.*/'] }, + { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', undefined, '/.*/'] }, ]; for (const test of tests) { @@ -318,5 +319,128 @@ describe('ZabbixDatasource', () => { expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/'); done(); }); + + describe('When invoking metricFindQuery()', () => { + beforeEach(() => { + ctx.ds.replaceTemplateVars = (str) => str; + ctx.ds.zabbix = { + getGroups: jest.fn().mockReturnValue(Promise.resolve([{ name: 'Group1' }, { name: 'Group2' }])), + getHosts: jest.fn().mockReturnValue(Promise.resolve([{ name: 'Host1' }, { name: 'Host2' }])), + getApps: jest.fn().mockReturnValue(Promise.resolve([{ name: 'App1' }, { name: 'App2' }])), + getItems: jest.fn().mockReturnValue(Promise.resolve([{ name: 'Item1' }, { name: 'Item2' }])), + getItemTags: jest.fn().mockReturnValue(Promise.resolve([{ name: 'Tag1' }, { name: 'Tag2' }])), + getItemValues: jest.fn().mockReturnValue(Promise.resolve([{ name: 'Value1' }, { name: 'Value2' }])), + }; + }); + + it('should return groups when queryType is Group', async () => { + const query = { queryType: VariableQueryTypes.Group, group: 'GroupFilter' }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(ctx.ds.zabbix.getGroups).toHaveBeenCalledWith('GroupFilter'); + expect(result).toEqual([ + { text: 'Group1', expandable: false }, + { text: 'Group2', expandable: false }, + ]); + }); + + it('should return hosts when queryType is Host', async () => { + const query = { queryType: VariableQueryTypes.Host, group: 'GroupFilter', host: 'HostFilter' }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(ctx.ds.zabbix.getHosts).toHaveBeenCalledWith('GroupFilter', 'HostFilter'); + expect(result).toEqual([ + { text: 'Host1', expandable: false }, + { text: 'Host2', expandable: false }, + ]); + }); + + it('should return applications when queryType is Application', async () => { + const query = { + queryType: VariableQueryTypes.Application, + group: 'GroupFilter', + host: 'HostFilter', + application: 'AppFilter', + }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(ctx.ds.zabbix.getApps).toHaveBeenCalledWith('GroupFilter', 'HostFilter', 'AppFilter'); + expect(result).toEqual([ + { text: 'App1', expandable: false }, + { text: 'App2', expandable: false }, + ]); + }); + + it('should return items when queryType is Item', async () => { + const query = { + queryType: VariableQueryTypes.Item, + group: 'GroupFilter', + host: 'HostFilter', + application: 'AppFilter', + itemTag: 'TagFilter', + item: 'ItemFilter', + }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(ctx.ds.zabbix.getItems).toHaveBeenCalledWith( + 'GroupFilter', + 'HostFilter', + 'AppFilter', + 'TagFilter', + 'ItemFilter' + ); + expect(result).toEqual([ + { text: 'Item1', expandable: false }, + { text: 'Item2', expandable: false }, + ]); + }); + + it('should return item tags when queryType is ItemTag', async () => { + const query = { + queryType: VariableQueryTypes.ItemTag, + group: 'GroupFilter', + host: 'HostFilter', + itemTag: 'TagFilter', + }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(ctx.ds.zabbix.getItemTags).toHaveBeenCalledWith('GroupFilter', 'HostFilter', 'TagFilter'); + expect(result).toEqual([ + { text: 'Tag1', expandable: false }, + { text: 'Tag2', expandable: false }, + ]); + }); + + it('should return item values when queryType is ItemValues', async () => { + const query = { + queryType: VariableQueryTypes.ItemValues, + group: 'GroupFilter', + host: 'HostFilter', + application: 'AppFilter', + item: 'ItemFilter', + }; + const options = { range: { from: 'now-1h', to: 'now' } }; + const result = await ctx.ds.metricFindQuery(query, options); + expect(ctx.ds.zabbix.getItemValues).toHaveBeenCalledWith( + 'GroupFilter', + 'HostFilter', + 'AppFilter', + 'ItemFilter', + { + range: options.range, + } + ); + expect(result).toEqual([ + { text: 'Value1', expandable: false }, + { text: 'Value2', expandable: false }, + ]); + }); + + it('should return an empty array for an unknown queryType', async () => { + const query = { queryType: 'UnknownType' }; + const result = await ctx.ds.metricFindQuery(query, {}); + expect(result).toEqual([]); + }); + + it('should return an empty array for an empty query', async () => { + const result = await ctx.ds.metricFindQuery('', {}); + expect(result).toEqual([]); + }); + }); }); });