import React, { useEffect } from 'react'; import { QueryEditorProps, SelectableValue } from '@grafana/data'; import { InlineField, InlineFieldRow, Select } from '@grafana/ui'; import * as c from '../constants'; import * as migrations from '../migrations'; import { ZabbixDatasource } from '../datasource'; import { ShowProblemTypes, ZabbixDSOptions, ZabbixMetricsQuery, ZabbixQueryOptions } from '../types'; import { MetricsQueryEditor } from './QueryEditor/MetricsQueryEditor'; import { QueryFunctionsEditor } from './QueryEditor/QueryFunctionsEditor'; import { QueryOptionsEditor } from './QueryEditor/QueryOptionsEditor'; import { TextMetricsQueryEditor } from './QueryEditor/TextMetricsQueryEditor'; import { ProblemsQueryEditor } from './QueryEditor/ProblemsQueryEditor'; import { ItemIdQueryEditor } from './QueryEditor/ItemIdQueryEditor'; import { ServicesQueryEditor } from './QueryEditor/ServicesQueryEditor'; import { TriggersQueryEditor } from './QueryEditor/TriggersQueryEditor'; const zabbixQueryTypeOptions: Array> = [ { value: c.MODE_METRICS, label: 'Metrics', description: 'Query numeric metrics', }, { value: c.MODE_TEXT, label: 'Text', description: 'Query text data', }, { value: c.MODE_ITSERVICE, label: 'Services', description: 'Query services SLA', }, { value: c.MODE_ITEMID, label: 'Item Id', description: 'Query metrics by item ids', }, { value: c.MODE_TRIGGERS, label: 'Triggers', description: 'Query triggers data', }, { value: c.MODE_PROBLEMS, label: 'Problems', description: 'Query problems', }, ]; const getDefaultQuery: () => Partial = () => ({ queryType: c.MODE_METRICS, group: { filter: '' }, host: { filter: '' }, application: { filter: '' }, itemTag: { filter: '' }, item: { filter: '' }, functions: [], triggers: { count: true, minSeverity: 3, acknowledged: 2, }, trigger: { filter: '' }, tags: { filter: '' }, proxy: { filter: '' }, textFilter: '', options: { showDisabledItems: false, skipEmptyValues: false, disableDataAlignment: false, useZabbixValueMapping: false, useTrends: 'default', }, table: { skipEmptyValues: false, }, }); function getSLAQueryDefaults(): Partial { return { itServiceFilter: '', slaFilter: '', slaProperty: 'sla', slaInterval: 'none', }; } function getProblemsQueryDefaults(): Partial { return { showProblems: ShowProblemTypes.Problems, options: { minSeverity: 0, sortProblems: 'default', acknowledged: 2, hostsInMaintenance: false, hostProxy: false, limit: c.DEFAULT_ZABBIX_PROBLEMS_LIMIT, useTimeRange: false, }, }; } export interface ZabbixQueryEditorProps extends QueryEditorProps {} export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: ZabbixQueryEditorProps) => { const queryDefaults = getDefaultQuery(); query = { ...queryDefaults, ...query }; query.options = { ...queryDefaults.options, ...query.options }; const { queryType } = query; if (queryType === c.MODE_PROBLEMS || queryType === c.MODE_TRIGGERS) { const defaults = getProblemsQueryDefaults(); query = { ...defaults, ...query }; query.options = { ...defaults.options, ...query.options }; } if (queryType === c.MODE_ITSERVICE) { query = { ...getSLAQueryDefaults(), ...query }; } // Migrate query on load useEffect(() => { const migratedQuery = migrations.migrate(query); onChange(migratedQuery); }, []); const onPropChange = (prop: string) => { return (option: SelectableValue) => { if (option.value !== null) { onChangeInternal({ ...query, [prop]: option.value }); } }; }; const onChangeInternal = (query: ZabbixMetricsQuery) => { onChange(query); onRunQuery(); }; const onOptionsChange = (options: ZabbixQueryOptions) => { onChangeInternal({ ...query, options }); }; const renderMetricsEditor = () => { return ( <> ); }; const renderItemIdsEditor = () => { return ( <> ); }; const renderTextMetricsEditor = () => { return ( <> {/* */} ); }; const renderITServicesEditor = () => { return ( <> ); }; const renderProblemsEditor = () => { return ; }; const renderTriggersEditor = () => { return ; }; return ( <>