Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(property => {
if (property) {
const comparators = property.type.getComparators();
if (comparators && comparators.length > 0) {
return comparators;
}
}
console.log("No comparators found for property with id '" + propertyId + "'. Falling back to EQ.");
// This may be the case when the user entered a property, which does not exist
// therefore fallback to EQ
return [ API.Comparators.EQ ];
}).catch(this.decorateError);
}
// 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
clause.restriction = replacement;
self.substitute(clause.restriction.clauses, options);
return;
}
}
}
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;
}
it("should map from api to ui comparator", function (done) {
expect(mapping.getUiComparator(API.Comparators.EQ)).to.eql("=");
expect(mapping.getUiComparator(API.Comparators.NE)).to.eql("!=");
expect(mapping.getUiComparator(API.Comparators.GE)).to.eql(">=");
expect(mapping.getUiComparator(API.Comparators.LE)).to.eql("<=");
expect(mapping.getUiComparator(API.Comparators.GT)).to.eql(">");
expect(mapping.getUiComparator(API.Comparators.LT)).to.eql("<");
done();
});
cloneRestriction(restriction) {
const comparator = _.find(API.Comparators, function(comparator) {
return comparator.label === restriction.comparator.label;
});
return new API.Restriction(restriction.attribute, comparator, restriction.value);
}
}
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);
} else if (selected.inputType === 'text') {
if (val.length === 0) {
return undefined;
}
if (!val.startsWith('*') && !val.endsWith('*')) {
return new API.Restriction(key, comparator, '*' + val + '*');
}
}
return new API.Restriction(key, comparator, val);
}
return undefined;
};
getApiComparator(uiComparator) {
const apiComparator = _.find(API.Comparators, function(comparator) {
return comparator.matches(uiComparator);
});
if (!apiComparator) {
throw new Error("No API comparator for alias '" + uiComparator + "' found.");
}
return apiComparator;
}
}
it ('should map from api to ui filter and vice versa', function(done) {
const apiFilter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("key", API.Comparators.EQ, "value"), API.Operators.OR))
.withClause(new API.Clause(new API.Restriction("key2", API.Comparators.NE, "value2"), API.Operators.AND));
apiFilter.limit = 0;
const uiFilter = new UI.Filter(uiSegmentSrv, alarmEntity)
.withClause(new UI.Clause(uiSegmentSrv, UI.Operators.OR, new UI.RestrictionDTO("key", "=", "value")))
.withClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO("key2", "!=", "value2")));
expect(mapping.getUiFilter(apiFilter)).to.eql(uiFilter);
expect(mapping.getApiFilter(uiFilter)).to.eql(apiFilter);
done();
});
getUiComparator(apiComparator) {
const theComparator = API.Comparators[apiComparator.label];
if (theComparator !== API.Comparators.NULL
&& theComparator !== API.Comparators.NOTNULL
&& theComparator != API.LIKE
&& theComparator != API.ILIKE
&& theComparator.aliases && theComparator.aliases.length > 0) {
return theComparator.aliases[0];
}
throw new Error("No matching UI comparator found for '" + apiComparator.label + "'.");
}