Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
subtituteNodeRestriction(clause) {
const restriction = clause.restriction;
// Handle "node" as a special case, updating restrictions to either foreignSource+foreignId or node.id
if (restriction.attribute === 'node') {
if (restriction.value.indexOf(':') > 0) {
if (restriction.comparator.id !== API.Comparators.EQ.id) {
console.log('WARNING: Using a comparator other than EQ will probably not work as expected with a foreignSource:foreignId node criteria.');
}
const nodeCriteria = restriction.value.split(':');
const replacement = new API.NestedRestriction(
new API.Clause(new API.Restriction('node.foreignSource', restriction.comparator, nodeCriteria[0]), API.Operators.AND),
new API.Clause(new API.Restriction('node.foreignId', restriction.comparator, nodeCriteria[1]), API.Operators.AND),
);
clause.restriction = replacement;
} else if (isNumber(restriction.value)) {
clause.restriction = new API.Restriction('node.id', restriction.comparator, restriction.value);
} else if (restriction.value === '{}') {
return true;
} else {
console.log('WARNING: found a "node" criteria but it does not appear to be a node ID nor a foreignSource:foreignId tuple.',restriction);
}
}
return false;
}
buildQuery(filter, options) {
var clonedFilter = API.Filter.fromJson(filter);
// Before replacing any variables, add a global time range restriction (which is hidden to the user)
if (options && options.enforceTimeRange) {
if (!options.entity || options.entity.type === 'alarm') {
clonedFilter.withAndRestriction(
new API.NestedRestriction()
.withAndRestriction(new API.Restriction("lastEventTime", API.Comparators.GE, "$range_from"))
.withAndRestriction(new API.Restriction("lastEventTime", API.Comparators.LE, "$range_to")));
}
}
// Substitute $ or [[variable]] in the restriction value
this.substitute(clonedFilter.clauses, options);
return clonedFilter;
}
// let's try the other way around
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR') and location = 'Stuttgart'");
// let's try 2 nested restrictions
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Stuttgart'), API.Operators.OR))
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Fulda'), API.Operators.OR)), API.Operators.AND)
);
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.AND)
);
expect(uiFilter.getQueryString()).to.eql("select all alarms where (location = 'Stuttgart' or location = 'Fulda') and (severity >= 'WARNING' and severity <= 'MAJOR')");
done();
});
// Process multi-selects
if (templateVariable && templateVariable.multi) {
if (templateVariable.current.value && self.templateSrv.isAllValue(templateVariable.current.value)) {
// if we're querying "all" we just dump the clause altogether
remove.push(clause);
} else {
// annoyingly, depending on how you interact with the UI, if one value is selected it will
// *either* be an array with 1 entry, or just the raw value >:|
// so we normalize it back to just the raw value here if necessary
if (_.isArray(templateVariable.current.value) && templateVariable.current.value.length === 1) {
templateVariable.current.value = templateVariable.current.value[0];
}
// now if it's *still* an array, we chop it up into nested restrictions
if (_.isArray(templateVariable.current.value)) {
const replacement = new API.NestedRestriction();
let values = templateVariable.current.value;
if (!_.isArray(values)) {
values = [values];
}
for (const value of values) {
if (restriction.comparator.id === API.Comparators.EQ.id) {
replacement.withOrRestriction(new API.Restriction(restriction.attribute, restriction.comparator, value));
} else if (restriction.comparator.id === API.Comparators.NE.id) {
replacement.withAndRestriction(new API.Restriction(restriction.attribute, restriction.comparator, value));
} else {
throw new Error('Unable to query "' + restriction.attribute + '": multi-select values with variable substitution must be either "=" or "!="');
}
}
// we've turned a single restriction into a nested one, so re-process it as a
// collection and skip the simple replacement below
return new API.Restriction(key, comparator, '*' + val + '*');
}
}
return new API.Restriction(key, comparator, val);
}
return undefined;
};
if (selected.value) {
const values = Array.isArray(selected.value) ? selected.value : [selected.value];
let restriction;
if (values.length === 0) {
return;
} else if (values.length === 1) {
restriction = getValueRestriction(values[0]);
} else {
restriction = new API.NestedRestriction();
values.forEach(val => {
if (val) restriction.withOrRestriction(getValueRestriction(val));
});
if (!restriction.clauses || restriction.clauses.length === 0) {
restriction = undefined;
}
}
if (restriction) {
restrictions.withAndRestriction(restriction);
}
}
});
}
getApiNestedRestriction(uiQuery) {
const self = this;
const nestedRestriction = new API.NestedRestriction();
_.each(uiQuery.clauses, uiClause => {
const apiClause = new ClauseMapping(self.uiSegmentSrv, self.entity).getApiClause(uiClause);
if (apiClause !== null) {
nestedRestriction.withClause(apiClause);
}
});
return nestedRestriction;
}
}
cloneNestedRestriction(nestedRestriction) {
const self = this;
const newNestedRestriction = new API.NestedRestriction();
_.each(nestedRestriction.clauses, function(clause) {
newNestedRestriction.withClause(self.cloneClause(clause));
});
return newNestedRestriction;
}
getPanelRestrictions() {
const self = this;
const dashboard = self.datasource.dashboardSrv.getCurrent();
const filterPanel = dashboard.panels.filter(panel => panel.type === 'opennms-helm-filter-panel')[0];
const restrictions = new API.NestedRestriction();
if (filterPanel && filterPanel.columns && filterPanel.columns.length > 0) {
filterPanel.columns.forEach((column) => {
const selected = column.selected;
if (!selected) {
return;
}
let key = selected.resource;
if (selected.entityType && selected.entityType.id !== self.type) {
key = selected.entityType.id + '.' + key;
}
const comparator = API.Comparators.EQ;
const getValueRestriction = val => {
if (!self.datasource.templateSrv.isAllValue(val) && !_.isNil(val)) {
if ((selected.resource === 'categories' || selected.resource === 'category.name')) {
return new API.Restriction('category.name', comparator, val);