refactor: use constants instead magic values

This commit is contained in:
Alexander Zobnin
2017-06-08 13:53:02 +03:00
parent 3eb5dc49e6
commit fe455113d5
12 changed files with 158 additions and 46 deletions

View File

@@ -0,0 +1,16 @@
// Editor modes
export const MODE_METRICS = 0;
export const MODE_TEXT = 2;
export const MODE_ITSERVICE = 1;
// Triggers severity
export const SEV_NOT_CLASSIFIED = 0;
export const SEV_INFORMATION = 1;
export const SEV_WARNING = 2;
export const SEV_AVERAGE = 3;
export const SEV_HIGH = 4;
export const SEV_DISASTER = 5;
export const SHOW_ALL_TRIGGERS = [0, 1];
export const SHOW_ALL_EVENTS = [0, 1];
export const SHOW_OK_EVENTS = 1;

View File

@@ -3,6 +3,7 @@ import * as dateMath from 'app/core/utils/datemath';
import * as utils from './utils';
import * as migrations from './migrations';
import * as metricFunctions from './metricFunctions';
import * as c from './constants';
import dataProcessor from './dataProcessor';
import responseHandler from './responseHandler';
import './zabbix.js';
@@ -40,7 +41,7 @@ class ZabbixAPIDatasource {
// Alerting options
this.alertingEnabled = instanceSettings.jsonData.alerting;
this.addThresholds = instanceSettings.jsonData.addThresholds;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || 2;
this.alertingMinSeverity = instanceSettings.jsonData.alertingMinSeverity || c.SEV_WARNING;
this.zabbix = new Zabbix(this.url, this.username, this.password, this.basicAuth, this.withCredentials, this.cacheTTL);
@@ -92,7 +93,7 @@ class ZabbixAPIDatasource {
let useTrends = this.isUseTrends([timeFrom, timeTo]);
// Metrics or Text query mode
if (target.mode !== 1) {
if (target.mode !== c.MODE_ITSERVICE) {
// Migrate old targets
target = migrations.migrate(target);
@@ -101,15 +102,15 @@ class ZabbixAPIDatasource {
return [];
}
if (!target.mode || target.mode === 0) {
if (!target.mode || target.mode === c.MODE_METRICS) {
return this.queryNumericData(target, timeFrom, timeTo, useTrends);
} else if (target.mode === 2) {
} else if (target.mode === c.MODE_TEXT) {
return this.queryTextData(target, timeFrom, timeTo);
}
}
// IT services mode
else if (target.mode === 1) {
else if (target.mode === c.MODE_ITSERVICE) {
// Don't show undefined and hidden targets
if (target.hide || !target.itservice || !target.slaProperty) {
return [];
@@ -342,10 +343,10 @@ class ZabbixAPIDatasource {
var timeFrom = Math.ceil(dateMath.parse(options.rangeRaw.from) / 1000);
var timeTo = Math.ceil(dateMath.parse(options.rangeRaw.to) / 1000);
var annotation = options.annotation;
var showOkEvents = annotation.showOkEvents ? [0, 1] : 1;
var showOkEvents = annotation.showOkEvents ? c.SHOW_ALL_EVENTS : c.SHOW_OK_EVENTS;
// Show all triggers
var showTriggers = [0, 1];
var showTriggers = c.SHOW_ALL_TRIGGERS;
var getTriggers = this.zabbix
.getTriggers(this.replaceTemplateVars(annotation.group, {}),

View File

@@ -1,6 +1,7 @@
import {QueryCtrl} from 'app/plugins/sdk';
import angular from 'angular';
import _ from 'lodash';
import * as c from './constants';
import * as utils from './utils';
import * as metricFunctions from './metricFunctions';
import * as migrations from './migrations';
@@ -22,9 +23,9 @@ export class ZabbixQueryController extends QueryCtrl {
this.templateSrv = templateSrv;
this.editorModes = {
0: {value: 'num', text: 'Metrics', mode: 0},
1: {value: 'itservice', text: 'IT Services', mode: 1},
2: {value: 'text', text: 'Text', mode: 2}
0: {value: 'num', text: 'Metrics', mode: c.MODE_METRICS},
1: {value: 'itservice', text: 'IT Services', mode: c.MODE_ITSERVICE},
2: {value: 'text', text: 'Text', mode: c.MODE_TEXT}
};
// Map functions for bs-typeahead
@@ -56,7 +57,7 @@ export class ZabbixQueryController extends QueryCtrl {
// Load default values
var targetDefaults = {
mode: 0,
mode: c.MODE_METRICS,
group: { filter: "" },
host: { filter: "" },
application: { filter: "" },
@@ -73,8 +74,8 @@ export class ZabbixQueryController extends QueryCtrl {
return metricFunctions.createFuncInstance(func.def, func.params);
});
if (target.mode === 0 ||
target.mode === 2) {
if (target.mode === c.MODE_METRICS ||
target.mode === c.MODE_TEXT) {
this.downsampleFunctionList = [
{name: "avg", value: "avg"},
@@ -86,7 +87,7 @@ export class ZabbixQueryController extends QueryCtrl {
this.initFilters();
}
else if (target.mode === 1) {
else if (target.mode === c.MODE_ITSERVICE) {
this.slaPropertyList = [
{name: "Status", property: "status"},
{name: "SLA", property: "sla"},