Query option to override use trends option, fixes #1442

This commit is contained in:
Alexander Zobnin
2022-12-29 14:07:02 +01:00
parent 1cee6f0ae3
commit cf6b19e189
6 changed files with 39 additions and 15 deletions

View File

@@ -75,9 +75,10 @@ type QueryFilter struct {
// QueryOptions model // QueryOptions model
type QueryOptions struct { type QueryOptions struct {
ShowDisabledItems bool `json:"showDisabledItems"` ShowDisabledItems bool `json:"showDisabledItems"`
DisableDataAlignment bool `json:"disableDataAlignment"` DisableDataAlignment bool `json:"disableDataAlignment"`
UseZabbixValueMapping bool `json:"useZabbixValueMapping"` UseZabbixValueMapping bool `json:"useZabbixValueMapping"`
UseTrends string `json:"useTrends"`
} }
// QueryOptions model // QueryOptions model

View File

@@ -108,7 +108,7 @@ func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context
func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, query *QueryModel, series []*timeseries.TimeSeriesData, DBPostProcessing bool) ([]*data.Frame, error) { func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, query *QueryModel, series []*timeseries.TimeSeriesData, DBPostProcessing bool) ([]*data.Frame, error) {
consolidateBy := ds.getConsolidateBy(query) consolidateBy := ds.getConsolidateBy(query)
useTrend := ds.isUseTrend(query.TimeRange) useTrend := ds.isUseTrend(query.TimeRange, query)
// Sort trend data (in some cases Zabbix API returns it unsorted) // Sort trend data (in some cases Zabbix API returns it unsorted)
if useTrend { if useTrend {
@@ -199,7 +199,7 @@ func (ds *ZabbixDatasourceInstance) getConsolidateBy(query *QueryModel) string {
func (ds *ZabbixDatasourceInstance) getHistotyOrTrend(ctx context.Context, query *QueryModel, items []*zabbix.Item, trendValueType string) (zabbix.History, error) { func (ds *ZabbixDatasourceInstance) getHistotyOrTrend(ctx context.Context, query *QueryModel, items []*zabbix.Item, trendValueType string) (zabbix.History, error) {
timeRange := query.TimeRange timeRange := query.TimeRange
useTrend := ds.isUseTrend(timeRange) useTrend := ds.isUseTrend(timeRange, query)
if useTrend { if useTrend {
result, err := ds.zabbix.GetTrend(ctx, items, timeRange) result, err := ds.zabbix.GetTrend(ctx, items, timeRange)
@@ -212,8 +212,8 @@ func (ds *ZabbixDatasourceInstance) getHistotyOrTrend(ctx context.Context, query
return ds.zabbix.GetHistory(ctx, items, timeRange) return ds.zabbix.GetHistory(ctx, items, timeRange)
} }
func (ds *ZabbixDatasourceInstance) isUseTrend(timeRange backend.TimeRange) bool { func (ds *ZabbixDatasourceInstance) isUseTrend(timeRange backend.TimeRange, query *QueryModel) bool {
if !ds.Settings.Trends { if query.Options.UseTrends == "false" {
return false return false
} }
@@ -223,8 +223,7 @@ func (ds *ZabbixDatasourceInstance) isUseTrend(timeRange backend.TimeRange) bool
toSec := timeRange.To.Unix() toSec := timeRange.To.Unix()
rangeSec := float64(toSec - fromSec) rangeSec := float64(toSec - fromSec)
if (fromSec < time.Now().Add(-trendsFrom).Unix()) || (rangeSec > trendsRange.Seconds()) { trendTimeRange := (fromSec < time.Now().Add(-trendsFrom).Unix()) || (rangeSec > trendsRange.Seconds())
return true useTrendsToggle := query.Options.UseTrends == "true" || ds.Settings.Trends
} return trendTimeRange && useTrendsToggle
return false
} }

View File

@@ -69,6 +69,7 @@ const getDefaultQuery: () => Partial<ZabbixMetricsQuery> = () => ({
skipEmptyValues: false, skipEmptyValues: false,
disableDataAlignment: false, disableDataAlignment: false,
useZabbixValueMapping: false, useZabbixValueMapping: false,
useTrends: 'default',
}, },
table: { table: {
skipEmptyValues: false, skipEmptyValues: false,
@@ -103,7 +104,9 @@ export interface ZabbixQueryEditorProps
extends QueryEditorProps<ZabbixDatasource, ZabbixMetricsQuery, ZabbixDSOptions> {} extends QueryEditorProps<ZabbixDatasource, ZabbixMetricsQuery, ZabbixDSOptions> {}
export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: ZabbixQueryEditorProps) => { export const QueryEditor = ({ query, datasource, onChange, onRunQuery }: ZabbixQueryEditorProps) => {
query = { ...getDefaultQuery(), ...query }; const queryDefaults = getDefaultQuery();
query = { ...queryDefaults, ...query };
query.options = { ...queryDefaults.options, ...query.options };
const { queryType } = query; const { queryType } = query;
if (queryType === c.MODE_PROBLEMS || queryType === c.MODE_TRIGGERS) { if (queryType === c.MODE_PROBLEMS || queryType === c.MODE_TRIGGERS) {
const defaults = getProblemsQueryDefaults(); const defaults = getProblemsQueryDefaults();

View File

@@ -26,6 +26,12 @@ const sortOptions: Array<SelectableValue<string>> = [
{ label: 'Severity', value: 'severity' }, { label: 'Severity', value: 'severity' },
]; ];
const trendsOptions: Array<SelectableValue<string>> = [
{ label: 'Default', value: 'default' },
{ label: 'True', value: 'true' },
{ label: 'False', value: 'false' },
];
interface Props { interface Props {
queryType: string; queryType: string;
queryOptions: ZabbixQueryOptions; queryOptions: ZabbixQueryOptions;
@@ -93,6 +99,15 @@ export const QueryOptionsEditor = ({ queryType, queryOptions, onChange }: Props)
const renderMetricOptions = () => { const renderMetricOptions = () => {
return ( return (
<> <>
<InlineField label="Trends" labelWidth={24}>
<Select
isSearchable={false}
width={16}
value={queryOptions.useTrends}
options={trendsOptions}
onChange={onPropChange('useTrends')}
/>
</InlineField>
<InlineField label="Show disabled items" labelWidth={24}> <InlineField label="Show disabled items" labelWidth={24}>
<InlineSwitch <InlineSwitch
value={queryOptions.showDisabledItems} value={queryOptions.showDisabledItems}

View File

@@ -279,7 +279,7 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
request.scopedVars = Object.assign({}, request.scopedVars, utils.getRangeScopedVars(request.range)); request.scopedVars = Object.assign({}, request.scopedVars, utils.getRangeScopedVars(request.range));
this.replaceTargetVariables(target, request); this.replaceTargetVariables(target, request);
const timeRange = this.buildTimeRange(request, target); const timeRange = this.buildTimeRange(request, target);
const useTrends = this.isUseTrends(timeRange); const useTrends = this.isUseTrends(timeRange, target);
if (!target.queryType || target.queryType === c.MODE_METRICS) { if (!target.queryType || target.queryType === c.MODE_METRICS) {
return this.queryNumericData(target, timeRange, useTrends, request); return this.queryNumericData(target, timeRange, useTrends, request);
@@ -857,11 +857,16 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
}); });
} }
isUseTrends(timeRange) { isUseTrends(timeRange, target: ZabbixMetricsQuery) {
if (target.options.useTrends === 'false') {
return false;
}
const [timeFrom, timeTo] = timeRange; const [timeFrom, timeTo] = timeRange;
const useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000); const useTrendsFrom = Math.ceil(dateMath.parse('now-' + this.trendsFrom) / 1000);
const useTrendsRange = Math.ceil(utils.parseInterval(this.trendsRange) / 1000); const useTrendsRange = Math.ceil(utils.parseInterval(this.trendsRange) / 1000);
const useTrends = this.trends && (timeFrom < useTrendsFrom || timeTo - timeFrom > useTrendsRange); const useTrendsToggle = target.options.useTrends === 'true';
const useTrends =
(useTrendsToggle || this.trends) && (timeFrom < useTrendsFrom || timeTo - timeFrom > useTrendsRange);
return useTrends; return useTrends;
} }

View File

@@ -67,6 +67,7 @@ export interface ZabbixQueryOptions {
skipEmptyValues?: boolean; skipEmptyValues?: boolean;
disableDataAlignment?: boolean; disableDataAlignment?: boolean;
useZabbixValueMapping?: boolean; useZabbixValueMapping?: boolean;
useTrends?: 'default' | 'true' | 'false';
// Problems options // Problems options
minSeverity?: number; minSeverity?: number;
sortProblems?: string; sortProblems?: string;