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;
}
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;
}
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;
}
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
// Create the filter
const apiFilter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
.withClause(new API.Clause(
new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
API.Operators.AND));
// Simulate persisting and reloading
const serialized = JSON.stringify(apiFilter);
const deserialized = JSON.parse(serialized);
const cloned = API.Filter.fromJson(deserialized);
// Now try to map it to an ui filter
const uiFilter = mapping.getUiFilter(cloned);
expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
});
});
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
// Create the filter
const apiFilter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
.withClause(new API.Clause(
new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
API.Operators.AND));
// Simulate persisting and reloading
const serialized = JSON.stringify(apiFilter);
const deserialized = JSON.parse(serialized);
const cloned = API.Filter.fromJson(deserialized);
// Now try to map it to an ui filter
const uiFilter = mapping.getUiFilter(cloned);
expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
});
});
it('should substitute scoped variables', () => {
// The filter with variables
const filter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("key", API.Comparators.EQ, "$variable1"), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("key2", API.Comparators.EQ, "Hello this is my [[variable1]]"), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("key3", API.Comparators.EQ, "value3"), API.Operators.AND));
// The scoped variables
const options = {
scopedVars: {
"variable1": {value: "dummy-value"}
}
};
const substitutedFilter = ctx.datasource.buildQuery(filter, options);
// Verify
expect(substitutedFilter.clauses[0].restriction.value).to.equal("dummy-value");
expect(substitutedFilter.clauses[1].restriction.value).to.equal("Hello this is my dummy-value");
expect(substitutedFilter.clauses[2].restriction.value).to.equal("value3");
});
it('should substitude $range_from and $range_to accordingly', () => {
// The input filter
const filter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("key", API.Comparators.EQ, "$range_from"), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("key2", API.Comparators.EQ, "$range_to"), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("key3", API.Comparators.EQ, "[[range_from]]"), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("key4", API.Comparators.EQ, "[[range_to]]"), API.Operators.AND));
// Options
const options = {
targets: [filter],
range: {
from: ctx.range_from,
to: ctx.range_to,
},
scopedVars: {}
};
// Build query and verify
const substitutedFilter = ctx.datasource.buildQuery(filter, options);
expect(substitutedFilter.clauses[0].restriction.value).to.equal(ctx.range_from);
.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);
}
it('should work with nested clauses', function (done) {
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
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));
expect(uiFilter.getQueryString()).to.eql("select all alarms where location = 'Stuttgart' or (severity >= 'WARNING' and severity <= 'MAJOR')");
// 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()
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
// Create the filter
const apiFilter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
.withClause(new API.Clause(
new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
API.Operators.AND));
// Simulate persisting and reloading
const serialized = JSON.stringify(apiFilter);
const deserialized = JSON.parse(serialized);
const cloned = API.Filter.fromJson(deserialized);
// Now try to map it to an ui filter
const uiFilter = mapping.getUiFilter(cloned);
expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
});
});