[Backend] Merge 'master' changes into the backend (#875)
* CI: fix shellcheck issues (#789) Signed-off-by: Mario Trangoni <mjtrangoni@gmail.com> * annotations: fix options in grafana 6.x, fix #813 * fix function editor in Grafana 6.4, closes #810 * add typings for grafana packages * Add $__range_series variable for calculating function over the whole series, #531 * fix tests * Don't set alert styles for react panels, fix #823 * docs: add range variables * docs: percentile reference * fix codespell * update packages (build with node 12) * update circleci node image to 12 * fix test configuration (babel) * Fix 817 (#851) * problems: update panel schema * update packages (build with node 12) * problems: use datasource from target * problems: fix query editor after schema update * problems: fix list layout * update circleci node image to 12 * fix tests * build(deps-dev): bump lodash from 4.17.10 to 4.17.13 (#852) Bumps [lodash](https://github.com/lodash/lodash) from 4.17.10 to 4.17.13. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.10...4.17.13) Signed-off-by: dependabot[bot] <support@github.com> * fix packages security alerts * problems: fix tags adding and removal * fix adding func from typeahead, closes #468 * update change log * bump plugin version to 3.10.5 * problems: fix tag removal (list layout) * Fix percentile() function, closes #862 (#863) Like the other aggregation functions, the datapoints need to be sorted in time before calling groupBy_perf(). * Update copyright, happy New Year! * fix not acknowledged problem color with a message (#858) * fix not acknowledged problem color with a message * fix not acknowledged problem color with a message, closes #857 * Variable query editor (#856) * refactor: convert module to typescript * refactor: covert utils to typescript * variable query editor WIP * variable editor: fix type error after grafana/ui update * variable editor: use FormLabel from grafana/ui * variable editor: refactor * variable editor: input validation and highlights * variable editor: fix tests * variable query: fix backward compatibility with empty queries * fix linter errors * variable editor: fix variable replacement in queries * Fixes for backend Co-authored-by: Mario Trangoni <mario@mariotrangoni.de> Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Mark Reibert <mreibert@netskope.com> Co-authored-by: memfiz <arnis.civciss@gmail.com>
This commit is contained in:
committed by
Alexander Zobnin
parent
fcfb237004
commit
92e77617cf
@@ -47,7 +47,7 @@ angular
|
||||
}
|
||||
|
||||
$scope.$apply(function() {
|
||||
$scope.addFunction(funcDef);
|
||||
$scope.ctrl.addFunction(funcDef);
|
||||
});
|
||||
|
||||
$input.trigger('blur');
|
||||
|
||||
157
src/datasource-zabbix/components/VariableQueryEditor.tsx
Normal file
157
src/datasource-zabbix/components/VariableQueryEditor.tsx
Normal file
@@ -0,0 +1,157 @@
|
||||
import React, { PureComponent } from 'react';
|
||||
import { parseLegacyVariableQuery } from '../utils';
|
||||
import { Select, Input, AsyncSelect, FormLabel } from '@grafana/ui';
|
||||
import { SelectableValue } from '@grafana/data';
|
||||
import { VariableQuery, VariableQueryTypes, VariableQueryProps, VariableQueryData } from '../types';
|
||||
import { ZabbixInput } from './ZabbixInput';
|
||||
|
||||
export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> {
|
||||
queryTypes: Array<SelectableValue<VariableQueryTypes>> = [
|
||||
{ value: VariableQueryTypes.Group, label: 'Group'},
|
||||
{ value: VariableQueryTypes.Host, label: 'Host' },
|
||||
{ value: VariableQueryTypes.Application, label: 'Application' },
|
||||
{ value: VariableQueryTypes.Item, label: 'Item' },
|
||||
];
|
||||
|
||||
defaults: VariableQueryData = {
|
||||
selectedQueryType: { value: VariableQueryTypes.Group, label: 'Group' },
|
||||
queryType: VariableQueryTypes.Group,
|
||||
group: '/.*/',
|
||||
host: '',
|
||||
application: '',
|
||||
item: '',
|
||||
};
|
||||
|
||||
constructor(props: VariableQueryProps) {
|
||||
super(props);
|
||||
|
||||
if (this.props.query && typeof this.props.query === 'string') {
|
||||
// Backward compatibility
|
||||
const query = parseLegacyVariableQuery(this.props.query);
|
||||
const selectedQueryType = this.getSelectedQueryType(query.queryType);
|
||||
this.state = {
|
||||
selectedQueryType,
|
||||
legacyQuery: this.props.query,
|
||||
...query
|
||||
};
|
||||
} else if (this.props.query) {
|
||||
const query = (this.props.query as VariableQuery);
|
||||
const selectedQueryType = this.getSelectedQueryType(query.queryType);
|
||||
this.state = {
|
||||
...this.defaults,
|
||||
...query,
|
||||
selectedQueryType,
|
||||
};
|
||||
} else {
|
||||
this.state = this.defaults;
|
||||
}
|
||||
}
|
||||
|
||||
getSelectedQueryType(queryType: VariableQueryTypes) {
|
||||
return this.queryTypes.find(q => q.value === queryType);
|
||||
}
|
||||
|
||||
handleQueryUpdate = (evt: React.ChangeEvent<HTMLInputElement>, prop: string) => {
|
||||
const value = evt.currentTarget.value;
|
||||
this.setState((prevState: VariableQueryData) => {
|
||||
const newQuery = {
|
||||
...prevState,
|
||||
};
|
||||
newQuery[prop] = value;
|
||||
|
||||
return {
|
||||
...newQuery,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
handleQueryChange = () => {
|
||||
const { queryType, group, host, application, item } = this.state;
|
||||
const queryModel = { queryType, group, host, application, item };
|
||||
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
|
||||
}
|
||||
|
||||
handleQueryTypeChange = (selectedItem: SelectableValue<VariableQueryTypes>) => {
|
||||
this.setState({
|
||||
...this.state,
|
||||
selectedQueryType: selectedItem,
|
||||
queryType: selectedItem.value,
|
||||
});
|
||||
|
||||
const { group, host, application, item } = this.state;
|
||||
const queryType = selectedItem.value;
|
||||
const queryModel = { queryType, group, host, application, item };
|
||||
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { selectedQueryType, legacyQuery, group, host, application, item } = this.state;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="gf-form max-width-21">
|
||||
<FormLabel width={10}>Query Type</FormLabel>
|
||||
<Select
|
||||
width={11}
|
||||
value={selectedQueryType}
|
||||
options={this.queryTypes}
|
||||
onChange={this.handleQueryTypeChange}
|
||||
/>
|
||||
</div>
|
||||
<div className="gf-form-inline">
|
||||
<div className="gf-form max-width-30">
|
||||
<FormLabel width={10}>Group</FormLabel>
|
||||
<ZabbixInput
|
||||
value={group}
|
||||
onChange={evt => this.handleQueryUpdate(evt, 'group')}
|
||||
onBlur={this.handleQueryChange}
|
||||
/>
|
||||
</div>
|
||||
{selectedQueryType.value !== VariableQueryTypes.Group &&
|
||||
<div className="gf-form max-width-30">
|
||||
<FormLabel width={10}>Host</FormLabel>
|
||||
<ZabbixInput
|
||||
value={host}
|
||||
onChange={evt => this.handleQueryUpdate(evt, 'host')}
|
||||
onBlur={this.handleQueryChange}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
{(selectedQueryType.value === VariableQueryTypes.Application ||
|
||||
selectedQueryType.value === VariableQueryTypes.Item) &&
|
||||
<div className="gf-form-inline">
|
||||
<div className="gf-form max-width-30">
|
||||
<FormLabel width={10}>Application</FormLabel>
|
||||
<ZabbixInput
|
||||
value={application}
|
||||
onChange={evt => this.handleQueryUpdate(evt, 'application')}
|
||||
onBlur={this.handleQueryChange}
|
||||
/>
|
||||
</div>
|
||||
{selectedQueryType.value === VariableQueryTypes.Item &&
|
||||
<div className="gf-form max-width-30">
|
||||
<FormLabel width={10}>Item</FormLabel>
|
||||
<ZabbixInput
|
||||
value={item}
|
||||
onChange={evt => this.handleQueryUpdate(evt, 'item')}
|
||||
onBlur={this.handleQueryChange}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
{legacyQuery &&
|
||||
<div className="gf-form">
|
||||
<FormLabel width={10} tooltip="Original query string, read-only">Legacy Query</FormLabel>
|
||||
<Input
|
||||
value={legacyQuery}
|
||||
readOnly={true}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
70
src/datasource-zabbix/components/ZabbixInput.tsx
Normal file
70
src/datasource-zabbix/components/ZabbixInput.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import React, { FC } from 'react';
|
||||
import { css, cx } from 'emotion';
|
||||
import { Themeable, withTheme, Input, EventsWithValidation, ValidationEvents } from '@grafana/ui';
|
||||
import { GrafanaTheme } from '@grafana/data';
|
||||
import { isRegex, variableRegex } from '../utils';
|
||||
|
||||
const variablePattern = RegExp(`^${variableRegex.source}`);
|
||||
|
||||
const getStyles = (theme: GrafanaTheme) => ({
|
||||
inputRegex: css`
|
||||
color: ${theme.colors.orange}
|
||||
`,
|
||||
inputVariable: css`
|
||||
color: ${theme.colors.variable}
|
||||
`,
|
||||
});
|
||||
|
||||
const zabbixInputValidationEvents: ValidationEvents = {
|
||||
[EventsWithValidation.onBlur]: [
|
||||
{
|
||||
rule: value => {
|
||||
if (!value) {
|
||||
return true;
|
||||
}
|
||||
if (value.length > 1 && value[0] === '/') {
|
||||
if (value[value.length - 1] !== '/') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
errorMessage: 'Not a valid regex',
|
||||
},
|
||||
{
|
||||
rule: value => {
|
||||
if (value === '*') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
errorMessage: 'Wildcards not supported. Use /.*/ instead',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
interface Props extends React.ComponentProps<typeof Input>, Themeable {
|
||||
}
|
||||
|
||||
const UnthemedZabbixInput: FC<Props> = ({ theme, value, ref, validationEvents, ...restProps }) => {
|
||||
const styles = getStyles(theme);
|
||||
|
||||
let inputClass;
|
||||
if (variablePattern.test(value as string)) {
|
||||
inputClass = styles.inputVariable;
|
||||
}
|
||||
if (isRegex(value)) {
|
||||
inputClass = styles.inputRegex;
|
||||
}
|
||||
|
||||
return (
|
||||
<Input
|
||||
className={inputClass}
|
||||
value={value}
|
||||
validationEvents={zabbixInputValidationEvents}
|
||||
{...restProps}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export const ZabbixInput = withTheme(UnthemedZabbixInput);
|
||||
@@ -121,9 +121,11 @@ function aggregateWrapper(groupByCallback, interval, datapoints) {
|
||||
}
|
||||
|
||||
function percentile(interval, n, datapoints) {
|
||||
var flattenedPoints = ts.flattenDatapoints(datapoints);
|
||||
var groupByCallback = _.partial(PERCENTILE, n);
|
||||
return groupBy(flattenedPoints, interval, groupByCallback);
|
||||
const flattenedPoints = ts.flattenDatapoints(datapoints);
|
||||
// groupBy_perf works with sorted series only
|
||||
const sortedPoints = ts.sortByTime(flattenedPoints);
|
||||
let groupByCallback = _.partial(PERCENTILE, n);
|
||||
return groupBy(sortedPoints, interval, groupByCallback);
|
||||
}
|
||||
|
||||
function timeShift(interval, range) {
|
||||
|
||||
@@ -12,10 +12,11 @@ import { ZabbixAPIError } from './zabbix/connectors/zabbix_api/zabbixAPICore';
|
||||
import {
|
||||
DataSourceApi,
|
||||
// DataSourceInstanceSettings,
|
||||
} from '@grafana/ui';
|
||||
} from '@grafana/data';
|
||||
// import { BackendSrv, DataSourceSrv } from '@grafana/runtime';
|
||||
// import { ZabbixAlertingService } from './zabbixAlerting.service';
|
||||
// import { ZabbixConnectionTestQuery, ZabbixConnectionInfo, TemplateSrv, TSDBResponse } from './types';
|
||||
import { VariableQueryTypes } from './types';
|
||||
|
||||
const DEFAULT_ZABBIX_VERSION = 3;
|
||||
|
||||
@@ -495,42 +496,41 @@ export class ZabbixDatasource extends DataSourceApi {
|
||||
* of metrics in "{metric1,metcic2,...,metricN}" format.
|
||||
*/
|
||||
metricFindQuery(query) {
|
||||
let result;
|
||||
let parts = [];
|
||||
let resultPromise;
|
||||
let queryModel = _.cloneDeep(query);
|
||||
|
||||
// Split query. Query structure: group.host.app.item
|
||||
_.each(utils.splitTemplateQuery(query), part => {
|
||||
part = this.replaceTemplateVars(part, {});
|
||||
|
||||
// Replace wildcard to regex
|
||||
if (part === '*') {
|
||||
part = '/.*/';
|
||||
}
|
||||
parts.push(part);
|
||||
});
|
||||
let template = _.zipObject(['group', 'host', 'app', 'item'], parts);
|
||||
|
||||
// Get items
|
||||
if (parts.length === 4) {
|
||||
// Search for all items, even it's not belong to any application
|
||||
if (template.app === '/.*/') {
|
||||
template.app = '';
|
||||
}
|
||||
result = this.zabbix.getItems(template.group, template.host, template.app, template.item);
|
||||
} else if (parts.length === 3) {
|
||||
// Get applications
|
||||
result = this.zabbix.getApps(template.group, template.host, template.app);
|
||||
} else if (parts.length === 2) {
|
||||
// Get hosts
|
||||
result = this.zabbix.getHosts(template.group, template.host);
|
||||
} else if (parts.length === 1) {
|
||||
// Get groups
|
||||
result = this.zabbix.getGroups(template.group);
|
||||
} else {
|
||||
result = Promise.resolve([]);
|
||||
if (!query) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
return result.then(metrics => {
|
||||
if (typeof query === 'string') {
|
||||
// Backward compatibility
|
||||
queryModel = utils.parseLegacyVariableQuery(query);
|
||||
}
|
||||
|
||||
for (const prop of ['group', 'host', 'application', 'item']) {
|
||||
queryModel[prop] = this.replaceTemplateVars(queryModel[prop], {});
|
||||
}
|
||||
|
||||
switch (queryModel.queryType) {
|
||||
case VariableQueryTypes.Group:
|
||||
resultPromise = this.zabbix.getGroups(queryModel.group);
|
||||
break;
|
||||
case VariableQueryTypes.Host:
|
||||
resultPromise = this.zabbix.getHosts(queryModel.group, queryModel.host);
|
||||
break;
|
||||
case VariableQueryTypes.Application:
|
||||
resultPromise = this.zabbix.getApps(queryModel.group, queryModel.host, queryModel.application);
|
||||
break;
|
||||
case VariableQueryTypes.Item:
|
||||
resultPromise = this.zabbix.getItems(queryModel.group, queryModel.host, queryModel.application, queryModel.item);
|
||||
break;
|
||||
default:
|
||||
resultPromise = Promise.resolve([]);
|
||||
break;
|
||||
}
|
||||
|
||||
return resultPromise.then(metrics => {
|
||||
return _.map(metrics, formatMetric);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,15 +2,18 @@ import { loadPluginCss } from '@grafana/runtime';
|
||||
import { ZabbixDatasource } from './datasource';
|
||||
import { ZabbixQueryController } from './query.controller';
|
||||
import { ZabbixDSConfigController } from './config.controller';
|
||||
import { ZabbixVariableQueryEditor } from './components/VariableQueryEditor';
|
||||
import './zabbixAlerting.service.js';
|
||||
import './add-metric-function.directive';
|
||||
import './metric-function-editor.directive';
|
||||
|
||||
class ZabbixQueryOptionsController {}
|
||||
ZabbixQueryOptionsController.templateUrl = 'datasource-zabbix/partials/query.options.html';
|
||||
class ZabbixQueryOptionsController {
|
||||
static templateUrl = 'datasource-zabbix/partials/query.options.html';
|
||||
}
|
||||
|
||||
class ZabbixAnnotationsQueryController {}
|
||||
ZabbixAnnotationsQueryController.templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
||||
class ZabbixAnnotationsQueryController {
|
||||
static templateUrl = 'datasource-zabbix/partials/annotations.editor.html';
|
||||
}
|
||||
|
||||
ZabbixQueryController.templateUrl = 'datasource-zabbix/partials/query.editor.html';
|
||||
ZabbixDSConfigController.templateUrl = 'datasource-zabbix/partials/config.html';
|
||||
@@ -25,5 +28,6 @@ export {
|
||||
ZabbixDSConfigController as ConfigCtrl,
|
||||
ZabbixQueryController as QueryCtrl,
|
||||
ZabbixQueryOptionsController as QueryOptionsCtrl,
|
||||
ZabbixAnnotationsQueryController as AnnotationsQueryCtrl
|
||||
ZabbixAnnotationsQueryController as AnnotationsQueryCtrl,
|
||||
ZabbixVariableQueryEditor as VariableQueryEditor,
|
||||
};
|
||||
@@ -231,7 +231,7 @@ describe('ZabbixDatasource', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('When invoking metricFindQuery()', () => {
|
||||
describe('When invoking metricFindQuery() with legacy query', () => {
|
||||
beforeEach(() => {
|
||||
ctx.ds.replaceTemplateVars = (str) => str;
|
||||
ctx.ds.zabbix = {
|
||||
@@ -245,7 +245,6 @@ describe('ZabbixDatasource', () => {
|
||||
it('should return groups', (done) => {
|
||||
const tests = [
|
||||
{query: '*', expect: '/.*/'},
|
||||
{query: '', expect: ''},
|
||||
{query: 'Backend', expect: 'Backend'},
|
||||
{query: 'Back*', expect: 'Back*'},
|
||||
];
|
||||
@@ -258,6 +257,16 @@ describe('ZabbixDatasource', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('should return empty list for empty query', (done) => {
|
||||
ctx.ds.metricFindQuery('').then(result => {
|
||||
expect(ctx.ds.zabbix.getGroups).toBeCalledTimes(0);
|
||||
ctx.ds.zabbix.getGroups.mockClear();
|
||||
|
||||
expect(result).toEqual([]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return hosts', (done) => {
|
||||
const tests = [
|
||||
{query: '*.*', expect: ['/.*/', '/.*/']},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DataQuery } from '@grafana/ui';
|
||||
import { SelectableValue, DataQuery } from "@grafana/data";
|
||||
|
||||
export interface ZabbixConnectionInfo {
|
||||
zabbixVersion: string;
|
||||
@@ -81,5 +81,34 @@ interface QueryResult {
|
||||
|
||||
export interface TSDBResponse {
|
||||
results: { [key: string]: QueryResult };
|
||||
message: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface VariableQueryProps {
|
||||
query: LegacyVariableQuery;
|
||||
onChange: (query: VariableQuery, definition: string) => void;
|
||||
datasource: any;
|
||||
templateSrv: any;
|
||||
}
|
||||
|
||||
export interface VariableQueryData extends VariableQuery {
|
||||
selectedQueryType: SelectableValue<VariableQueryTypes>;
|
||||
legacyQuery?: string;
|
||||
}
|
||||
|
||||
export interface VariableQuery {
|
||||
queryType: VariableQueryTypes;
|
||||
group?: string;
|
||||
host?: string;
|
||||
application?: string;
|
||||
item?: string;
|
||||
}
|
||||
|
||||
export type LegacyVariableQuery = VariableQuery | string;
|
||||
|
||||
export enum VariableQueryTypes {
|
||||
Group = 'group',
|
||||
Host = 'host',
|
||||
Application = 'application',
|
||||
Item = 'item',
|
||||
}
|
||||
|
||||
@@ -2,6 +2,15 @@ import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
import kbn from 'grafana/app/core/utils/kbn';
|
||||
import * as c from './constants';
|
||||
import { VariableQuery, VariableQueryTypes } from './types';
|
||||
|
||||
/*
|
||||
* This regex matches 3 types of variable reference with an optional format specifier
|
||||
* \$(\w+) $var1
|
||||
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
|
||||
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
|
||||
*/
|
||||
export const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?:\.([^:^\}]+))?(?::(\w+))?}/g;
|
||||
|
||||
/**
|
||||
* Expand Zabbix item name
|
||||
@@ -14,8 +23,8 @@ export function expandItemName(name, key) {
|
||||
|
||||
// extract params from key:
|
||||
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
|
||||
let key_params_str = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']'));
|
||||
let key_params = splitKeyParams(key_params_str);
|
||||
const key_params_str = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']'));
|
||||
const key_params = splitKeyParams(key_params_str);
|
||||
|
||||
// replace item parameters
|
||||
for (let i = key_params.length; i >= 1; i--) {
|
||||
@@ -34,10 +43,10 @@ export function expandItems(items) {
|
||||
}
|
||||
|
||||
function splitKeyParams(paramStr) {
|
||||
let params = [];
|
||||
const params = [];
|
||||
let quoted = false;
|
||||
let in_array = false;
|
||||
let split_symbol = ',';
|
||||
const split_symbol = ',';
|
||||
let param = '';
|
||||
|
||||
_.forEach(paramStr, symbol => {
|
||||
@@ -71,9 +80,9 @@ export function containsMacro(itemName) {
|
||||
|
||||
export function replaceMacro(item, macros) {
|
||||
let itemName = item.name;
|
||||
let item_macros = itemName.match(MACRO_PATTERN);
|
||||
const item_macros = itemName.match(MACRO_PATTERN);
|
||||
_.forEach(item_macros, macro => {
|
||||
let host_macros = _.filter(macros, m => {
|
||||
const host_macros = _.filter(macros, m => {
|
||||
if (m.hostid) {
|
||||
return m.hostid === item.hostid;
|
||||
} else {
|
||||
@@ -82,10 +91,10 @@ export function replaceMacro(item, macros) {
|
||||
}
|
||||
});
|
||||
|
||||
let macro_def = _.find(host_macros, { macro: macro });
|
||||
const macro_def = _.find(host_macros, { macro: macro });
|
||||
if (macro_def && macro_def.value) {
|
||||
let macro_value = macro_def.value;
|
||||
let macro_regex = new RegExp(escapeMacro(macro));
|
||||
const macro_value = macro_def.value;
|
||||
const macro_regex = new RegExp(escapeMacro(macro));
|
||||
itemName = itemName.replace(macro_regex, macro_value);
|
||||
}
|
||||
});
|
||||
@@ -98,17 +107,62 @@ function escapeMacro(macro) {
|
||||
return macro;
|
||||
}
|
||||
|
||||
export function parseLegacyVariableQuery(query: string): VariableQuery {
|
||||
let queryType: VariableQueryTypes;
|
||||
const parts = [];
|
||||
|
||||
// Split query. Query structure: group.host.app.item
|
||||
_.each(splitTemplateQuery(query), part => {
|
||||
// Replace wildcard to regex
|
||||
if (part === '*') {
|
||||
part = '/.*/';
|
||||
}
|
||||
parts.push(part);
|
||||
});
|
||||
const template = _.zipObject(['group', 'host', 'app', 'item'], parts);
|
||||
|
||||
if (parts.length === 4 && template.app === '/.*/') {
|
||||
// Search for all items, even it's not belong to any application
|
||||
template.app = '';
|
||||
}
|
||||
|
||||
switch (parts.length) {
|
||||
case 1:
|
||||
queryType = VariableQueryTypes.Group;
|
||||
break;
|
||||
case 2:
|
||||
queryType = VariableQueryTypes.Host;
|
||||
break;
|
||||
case 3:
|
||||
queryType = VariableQueryTypes.Application;
|
||||
break;
|
||||
case 4:
|
||||
queryType = VariableQueryTypes.Item;
|
||||
break;
|
||||
}
|
||||
|
||||
const variableQuery: VariableQuery = {
|
||||
queryType,
|
||||
group: template.group || '',
|
||||
host: template.host || '',
|
||||
application: template.app || '',
|
||||
item: template.item || '',
|
||||
};
|
||||
|
||||
return variableQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split template query to parts of zabbix entities
|
||||
* group.host.app.item -> [group, host, app, item]
|
||||
* {group}{host.com} -> [group, host.com]
|
||||
*/
|
||||
export function splitTemplateQuery(query) {
|
||||
let splitPattern = /\{[^\{\}]*\}|\{\/.*\/\}/g;
|
||||
const splitPattern = /\{[^\{\}]*\}|\{\/.*\/\}/g;
|
||||
let split;
|
||||
|
||||
if (isContainsBraces(query)) {
|
||||
let result = query.match(splitPattern);
|
||||
const result = query.match(splitPattern);
|
||||
split = _.map(result, part => {
|
||||
return _.trim(part, '{}');
|
||||
});
|
||||
@@ -120,7 +174,7 @@ export function splitTemplateQuery(query) {
|
||||
}
|
||||
|
||||
function isContainsBraces(query) {
|
||||
let bracesPattern = /^\{.+\}$/;
|
||||
const bracesPattern = /^\{.+\}$/;
|
||||
return bracesPattern.test(query);
|
||||
}
|
||||
|
||||
@@ -132,9 +186,9 @@ export function isRegex(str) {
|
||||
}
|
||||
|
||||
export function isTemplateVariable(str, templateVariables) {
|
||||
var variablePattern = /^\$\w+/;
|
||||
const variablePattern = /^\$\w+/;
|
||||
if (variablePattern.test(str)) {
|
||||
var variables = _.map(templateVariables, variable => {
|
||||
const variables = _.map(templateVariables, variable => {
|
||||
return '$' + variable.name;
|
||||
});
|
||||
return _.includes(variables, str);
|
||||
@@ -156,9 +210,9 @@ export function getRangeScopedVars(range) {
|
||||
}
|
||||
|
||||
export function buildRegex(str) {
|
||||
var matches = str.match(regexPattern);
|
||||
var pattern = matches[1];
|
||||
var flags = matches[2] !== "" ? matches[2] : undefined;
|
||||
const matches = str.match(regexPattern);
|
||||
const pattern = matches[1];
|
||||
const flags = matches[2] !== "" ? matches[2] : undefined;
|
||||
return new RegExp(pattern, flags);
|
||||
}
|
||||
|
||||
@@ -169,18 +223,18 @@ export function escapeRegex(value) {
|
||||
}
|
||||
|
||||
export function parseInterval(interval) {
|
||||
var intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
|
||||
var momentInterval = intervalPattern.exec(interval);
|
||||
const intervalPattern = /(^[\d]+)(y|M|w|d|h|m|s)/g;
|
||||
const momentInterval: any[] = intervalPattern.exec(interval);
|
||||
return moment.duration(Number(momentInterval[1]), momentInterval[2]).valueOf();
|
||||
}
|
||||
|
||||
export function parseTimeShiftInterval(interval) {
|
||||
let intervalPattern = /^([\+\-]*)([\d]+)(y|M|w|d|h|m|s)/g;
|
||||
let momentInterval = intervalPattern.exec(interval);
|
||||
let duration = 0;
|
||||
const intervalPattern = /^([\+\-]*)([\d]+)(y|M|w|d|h|m|s)/g;
|
||||
const momentInterval: any[] = intervalPattern.exec(interval);
|
||||
let duration: any = 0;
|
||||
|
||||
if (momentInterval[1] === '+') {
|
||||
duration = 0 - moment.duration(Number(momentInterval[2]), momentInterval[3]).valueOf();
|
||||
duration = 0 - (moment.duration(Number(momentInterval[2]), momentInterval[3]).valueOf() as any);
|
||||
} else {
|
||||
duration = moment.duration(Number(momentInterval[2]), momentInterval[3]).valueOf();
|
||||
}
|
||||
@@ -196,13 +250,13 @@ export function parseTimeShiftInterval(interval) {
|
||||
*/
|
||||
export function formatAcknowledges(acknowledges) {
|
||||
if (acknowledges.length) {
|
||||
var formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
|
||||
let formatted_acknowledges = '<br><br>Acknowledges:<br><table><tr><td><b>Time</b></td>'
|
||||
+ '<td><b>User</b></td><td><b>Comments</b></td></tr>';
|
||||
_.each(_.map(acknowledges, function (ack) {
|
||||
var timestamp = moment.unix(ack.clock);
|
||||
_.each(_.map(acknowledges, ack => {
|
||||
const timestamp = moment.unix(ack.clock);
|
||||
return '<tr><td><i>' + timestamp.format("DD MMM YYYY HH:mm:ss") + '</i></td><td>' + ack.alias
|
||||
+ ' (' + ack.name + ' ' + ack.surname + ')' + '</td><td>' + ack.message + '</td></tr>';
|
||||
}), function (ack) {
|
||||
}), ack => {
|
||||
formatted_acknowledges = formatted_acknowledges.concat(ack);
|
||||
});
|
||||
formatted_acknowledges = formatted_acknowledges.concat('</table>');
|
||||
@@ -213,8 +267,8 @@ export function formatAcknowledges(acknowledges) {
|
||||
}
|
||||
|
||||
export function convertToZabbixAPIUrl(url) {
|
||||
var zabbixAPIUrlPattern = /.*api_jsonrpc.php$/;
|
||||
var trimSlashPattern = /(.*?)[\/]*$/;
|
||||
const zabbixAPIUrlPattern = /.*api_jsonrpc.php$/;
|
||||
const trimSlashPattern = /(.*?)[\/]*$/;
|
||||
if (url.match(zabbixAPIUrlPattern)) {
|
||||
return url;
|
||||
} else {
|
||||
@@ -247,7 +301,7 @@ export function callOnce(func, promiseKeeper) {
|
||||
*/
|
||||
export function sequence(funcsArray) {
|
||||
return function(result) {
|
||||
for (var i = 0; i < funcsArray.length; i++) {
|
||||
for (let i = 0; i < funcsArray.length; i++) {
|
||||
result = funcsArray[i].call(this, result);
|
||||
}
|
||||
return result;
|
||||
@@ -292,5 +346,5 @@ export function getArrayDepth(a, level = 0) {
|
||||
|
||||
// Fix for backward compatibility with lodash 2.4
|
||||
if (!_.includes) {
|
||||
_.includes = _.contains;
|
||||
_.includes = (_ as any).contains;
|
||||
}
|
||||
Reference in New Issue
Block a user