From 95f91d5963dfa7874641633e068d65f9690b261c Mon Sep 17 00:00:00 2001 From: rozetko Date: Tue, 16 Apr 2019 21:28:04 +0300 Subject: [PATCH] Sort analytic units by creation date / name #514 (#581) * Add sorting query to dbFindMany * Add timestampData option to db init * Use sorting query in AnalyticUnit.findMany * enum SortingOrder --- server/src/models/analytic_unit_model.ts | 7 +++++-- server/src/services/data_service.ts | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 27a087f..9b83d07 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -1,4 +1,4 @@ -import { Collection, makeDBQ } from '../services/data_service'; +import { Collection, makeDBQ, SortingOrder } from '../services/data_service'; import { Metric } from 'grafana-datasource-kit'; @@ -174,7 +174,10 @@ export async function findById(id: AnalyticUnitId): Promise { } export async function findMany(query: FindManyQuery): Promise { - let analyticUnits = await db.findMany(query); + const analyticUnits = await db.findMany(query, { + createdAt: SortingOrder.ASCENDING, + name: SortingOrder.ASCENDING + }); if(analyticUnits === null) { return []; } diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index 71eddda..197d273 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -6,6 +6,7 @@ import * as fs from 'fs'; export enum Collection { ANALYTIC_UNITS, ANALYTIC_UNIT_CACHES, SEGMENTS, THRESHOLD }; +export enum SortingOrder { ASCENDING = 1, DESCENDING = -1 }; /** * Class which helps to make queries to your collection @@ -14,7 +15,7 @@ export enum Collection { ANALYTIC_UNITS, ANALYTIC_UNIT_CACHES, SEGMENTS, THRESHO */ export type DBQ = { findOne: (query: string | object) => Promise, - findMany: (query: string[] | object) => Promise, + findMany: (query: string[] | object, sortQuery?: object) => Promise, insertOne: (document: object) => Promise, insertMany: (documents: object[]) => Promise, updateOne: (query: string | object, updateQuery: any) => Promise, @@ -142,13 +143,13 @@ let dbFindOne = (collection: Collection, query: string | object) => { }); } -let dbFindMany = (collection: Collection, query: string[] | object) => { +let dbFindMany = (collection: Collection, query: string[] | object, sortQuery: object = {}) => { if(isEmptyArray(query)) { return Promise.resolve([]); } query = wrapIdsToQuery(query); return new Promise((resolve, reject) => { - db.get(collection).find(query, (err, docs: any[]) => { + db.get(collection).find(query).sort(sortQuery).exec((err, docs: any[]) => { if(err) { reject(err); } else { @@ -208,7 +209,7 @@ function checkDataFolders(): void { checkDataFolders(); // TODO: it's better if models request db which we create if it`s needed -db.set(Collection.ANALYTIC_UNITS, new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true })); +db.set(Collection.ANALYTIC_UNITS, new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true, timestampData: true })); db.set(Collection.ANALYTIC_UNIT_CACHES, new nedb({ filename: config.ANALYTIC_UNIT_CACHES_DATABASE_PATH, autoload: true })); db.set(Collection.SEGMENTS, new nedb({ filename: config.SEGMENTS_DATABASE_PATH, autoload: true })); db.set(Collection.THRESHOLD, new nedb({ filename: config.THRESHOLD_DATABASE_PATH, autoload: true }));