Browse Source

Nedb and mongodb syntax #792 (#794)

* add segment check in pattern detector

* fix query syntax
pull/1/head
Alexandr Velikiy 5 years ago committed by Alexey Velikiy
parent
commit
ae5df87455
  1. 4
      server/src/controllers/analytics_controller.ts
  2. 19
      server/src/services/data_layer/mongodb.ts

4
server/src/controllers/analytics_controller.ts

@ -156,7 +156,7 @@ async function getQueryRange(
let segments: Segment.Segment[]; let segments: Segment.Segment[];
switch(detectorType) { switch(detectorType) {
case AnalyticUnit.DetectorType.PATTERN: case AnalyticUnit.DetectorType.PATTERN:
segments = await Segment.findMany(analyticUnitId, { $or: { labeled: true, deleted: true } }); segments = await Segment.findMany(analyticUnitId, { $or: [{ labeled: true }, { deleted: true }] });
if(segments.length === 0) { if(segments.length === 0) {
throw new Error('Need at least 1 labeled segment'); throw new Error('Need at least 1 labeled segment');
} }
@ -170,7 +170,7 @@ async function getQueryRange(
}; };
case AnalyticUnit.DetectorType.ANOMALY: case AnalyticUnit.DetectorType.ANOMALY:
segments = await Segment.findMany(analyticUnitId, { $or: { labeled: true, deleted: true } }); segments = await Segment.findMany(analyticUnitId, { $or: [{ labeled: true }, { deleted: true }] });
if(segments.length === 0) { if(segments.length === 0) {
const now = Date.now(); const now = Date.now();
return { return {

19
server/src/services/data_layer/mongodb.ts

@ -84,7 +84,6 @@ export class MongoDbQueryWrapper implements DbQueryWrapper {
if(isEmptyArray(query)) { if(isEmptyArray(query)) {
return []; return [];
} }
query = convertQueryToMongoFormat(query);
query = wrapIdsToMongoDbQuery(query); query = wrapIdsToMongoDbQuery(query);
try { try {
const docs = await collection.find(query).sort(sortQuery).toArray(); const docs = await collection.find(query).sort(sortQuery).toArray();
@ -121,21 +120,3 @@ export class MongoDbQueryWrapper implements DbQueryWrapper {
} }
} }
function convertQueryToMongoFormat(query: any): object {
if(query.$or !== undefined && typeof query.$or === 'object') {
query.$or = convertQueryFieldToMongoFormat(query.$or);
}
if(query.$and !== undefined && typeof query.$or === 'object') {
query.$and = convertQueryFieldToMongoFormat(query.$and);
}
return query;
}
function convertQueryFieldToMongoFormat(query: object): object[] {
let mongoQuery = [];
for(const key in query) {
const newObject = _.pick(query, key);
mongoQuery.push(newObject);
}
return mongoQuery;
}

Loading…
Cancel
Save