Browse Source

Remove deasync lib #829 (#915)

* make dataservice a class

* fix dataservice usage

* rm deasync

* fix

* fix

Co-authored-by: corpglory-dev <dev@corpglory.com>
pull/1/head
rozetko 5 years ago committed by GitHub
parent
commit
0e940115a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      server/package.json
  2. 4
      server/src/models/analytic_unit_cache_model.ts
  3. 4
      server/src/models/analytic_units/db.ts
  4. 5
      server/src/models/detection_model.ts
  5. 6
      server/src/models/segment_model.ts
  6. 10
      server/src/services/data_service/db_connector/mongodb_connector.ts
  7. 11
      server/src/services/data_service/db_connector/nedb_connector.ts
  8. 100
      server/src/services/data_service/index.ts
  9. 12
      server/src/services/data_service/migrations.ts

1
server/package.json

@ -37,7 +37,6 @@
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0", "babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1", "babel-preset-es2015": "^6.24.1",
"deasync": "^0.1.15",
"es6-promise": "^4.2.4", "es6-promise": "^4.2.4",
"event-stream": "3.3.4", "event-stream": "3.3.4",
"file-loader": "^1.1.11", "file-loader": "^1.1.11",

4
server/src/models/analytic_unit_cache_model.ts

@ -1,11 +1,11 @@
import { AnalyticUnitId, AnalyticUnit } from './analytic_units'; import { AnalyticUnitId, AnalyticUnit } from './analytic_units';
import { Collection } from '../services/data_service/collection'; import { Collection } from '../services/data_service/collection';
import { makeDBQ } from '../services/data_service'; import { DataService } from '../services/data_service';
import * as _ from 'lodash'; import * as _ from 'lodash';
const db = makeDBQ(Collection.ANALYTIC_UNIT_CACHES); const db = DataService.getInstance().makeDBQ(Collection.ANALYTIC_UNIT_CACHES);
// TODO: count milliseconds in index from dataset // TODO: count milliseconds in index from dataset
const MILLISECONDS_IN_INDEX = 60000; const MILLISECONDS_IN_INDEX = 60000;

4
server/src/models/analytic_units/db.ts

@ -2,14 +2,14 @@ import { createAnalyticUnitFromObject } from './utils';
import { AnalyticUnit } from './analytic_unit_model'; import { AnalyticUnit } from './analytic_unit_model';
import { AnalyticUnitId, FindManyQuery } from './types'; import { AnalyticUnitId, FindManyQuery } from './types';
import { Collection } from '../../services/data_service/collection'; import { Collection } from '../../services/data_service/collection';
import { makeDBQ, SortingOrder } from '../../services/data_service'; import { DataService, SortingOrder } from '../../services/data_service';
import { Metric } from 'grafana-datasource-kit'; import { Metric } from 'grafana-datasource-kit';
import * as _ from 'lodash'; import * as _ from 'lodash';
const db = makeDBQ(Collection.ANALYTIC_UNITS); const db = DataService.getInstance().makeDBQ(Collection.ANALYTIC_UNITS);
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit | null> { export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit | null> {
let obj = await db.findOne(id); let obj = await db.findOne(id);

5
server/src/models/detection_model.ts

@ -1,10 +1,11 @@
import { AnalyticUnitId } from './analytic_units'; import { AnalyticUnitId } from './analytic_units';
import { Collection } from '../services/data_service/collection'; import { Collection } from '../services/data_service/collection';
import { makeDBQ } from '../services/data_service'; import { DataService } from '../services/data_service';
import * as _ from 'lodash'; import * as _ from 'lodash';
let db = makeDBQ(Collection.DETECTION_SPANS);
const db = DataService.getInstance().makeDBQ(Collection.DETECTION_SPANS);
export enum DetectionStatus { export enum DetectionStatus {
READY = 'READY', READY = 'READY',

6
server/src/models/segment_model.ts

@ -2,12 +2,12 @@ import { AnalyticUnitId } from './analytic_units';
import * as AnalyticUnit from '../models/analytic_units'; import * as AnalyticUnit from '../models/analytic_units';
import * as AnalyticUnitCache from '../models/analytic_unit_cache_model'; import * as AnalyticUnitCache from '../models/analytic_unit_cache_model';
import { Collection } from '../services/data_service/collection'; import { Collection } from '../services/data_service/collection';
import { makeDBQ } from '../services/data_service'; import { DataService } from '../services/data_service';
import * as _ from 'lodash'; import * as _ from 'lodash';
let db = makeDBQ(Collection.SEGMENTS);
const db = DataService.getInstance().makeDBQ(Collection.SEGMENTS);
export type SegmentId = string; export type SegmentId = string;
@ -117,7 +117,7 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis
export async function findByAnalyticUnitIds(analyticUnitIds: AnalyticUnitId[]): Promise<any[]> { export async function findByAnalyticUnitIds(analyticUnitIds: AnalyticUnitId[]): Promise<any[]> {
const segments = await db.findMany({ analyticUnitId: { $in: analyticUnitIds } }); const segments = await db.findMany({ analyticUnitId: { $in: analyticUnitIds } });
if(segments === null) { if(segments === null) {
return []; return [];
} }

10
server/src/services/data_service/db_connector/mongodb_connector.ts

@ -8,9 +8,6 @@ import * as mongodb from 'mongodb';
export class MongodbConnector implements DbConnector { export class MongodbConnector implements DbConnector {
private static _instance: MongodbConnector; private static _instance: MongodbConnector;
private _db = new Map<Collection, dbCollection>();
private static COLLECTION_TO_NAME_MAPPING = new Map<Collection, string>([ private static COLLECTION_TO_NAME_MAPPING = new Map<Collection, string>([
[Collection.ANALYTIC_UNITS, 'analytic_units'], [Collection.ANALYTIC_UNITS, 'analytic_units'],
[Collection.ANALYTIC_UNIT_CACHES, 'analytic_unit_caches'], [Collection.ANALYTIC_UNIT_CACHES, 'analytic_unit_caches'],
@ -20,9 +17,14 @@ export class MongodbConnector implements DbConnector {
[Collection.DB_META, 'db_meta'] [Collection.DB_META, 'db_meta']
]); ]);
private _db = new Map<Collection, dbCollection>();
private _client: mongodb.MongoClient; private _client: mongodb.MongoClient;
private constructor() { } private constructor() {
if(MongodbConnector._instance !== undefined) {
throw new Error(`Can't create 2nd instance of singleton MongodbConnector class`);
}
}
async init(): Promise<void> { async init(): Promise<void> {
const dbConfig = config.HASTIC_DB_CONFIG; const dbConfig = config.HASTIC_DB_CONFIG;

11
server/src/services/data_service/db_connector/nedb_connector.ts

@ -28,9 +28,6 @@ function checkDataFolders(): void {
export class NedbConnector implements DbConnector { export class NedbConnector implements DbConnector {
private static _instance: NedbConnector; private static _instance: NedbConnector;
private _db = new Map<Collection, dbCollection>();
private static COLLECTION_TO_CONFIG_MAPPING = new Map<Collection, NedbCollectionConfig>([ private static COLLECTION_TO_CONFIG_MAPPING = new Map<Collection, NedbCollectionConfig>([
[Collection.ANALYTIC_UNITS, { filename: config.ANALYTIC_UNITS_DATABASE_PATH, timestampData: true }], [Collection.ANALYTIC_UNITS, { filename: config.ANALYTIC_UNITS_DATABASE_PATH, timestampData: true }],
[Collection.ANALYTIC_UNIT_CACHES, { filename: config.ANALYTIC_UNIT_CACHES_DATABASE_PATH }], [Collection.ANALYTIC_UNIT_CACHES, { filename: config.ANALYTIC_UNIT_CACHES_DATABASE_PATH }],
@ -40,7 +37,13 @@ export class NedbConnector implements DbConnector {
[Collection.DB_META, { filename: config.DB_META_PATH }], [Collection.DB_META, { filename: config.DB_META_PATH }],
]); ]);
constructor() { } private _db = new Map<Collection, dbCollection>();
private constructor() {
if(NedbConnector._instance !== undefined) {
throw new Error(`Can't create 2nd instance of singleton MongodbConnector class`);
}
}
async init(): Promise<void> { async init(): Promise<void> {
checkDataFolders(); checkDataFolders();

100
server/src/services/data_service/index.ts

@ -3,8 +3,6 @@ import { getDbQueryWrapper, dbCollection } from '../data_layer';
import { DbConnector } from './db_connector'; import { DbConnector } from './db_connector';
import { DbConnectorFactory } from './db_connector/factory'; import { DbConnectorFactory } from './db_connector/factory';
import * as deasync from 'deasync';
export enum SortingOrder { ASCENDING = 1, DESCENDING = -1 }; export enum SortingOrder { ASCENDING = 1, DESCENDING = -1 };
@ -24,38 +22,78 @@ export type DBQ = {
removeMany: (query: string[] | object) => Promise<number> removeMany: (query: string[] | object) => Promise<number>
} }
const queryWrapper = getDbQueryWrapper(); export class DataService {
let db: Map<Collection, dbCollection>; private static _instance: DataService;
private _queryWrapper = getDbQueryWrapper();
function dbCollectionFromCollection(collection: Collection): dbCollection { private constructor() {
let dbCollection = db.get(collection); if(DataService._instance !== undefined) {
if(dbCollection === undefined) { throw new Error(`Can't create 2nd instance of singleton class`);
throw new Error('Can`t find collection ' + collection); }
} }
return dbCollection;
}
export function makeDBQ(collection: Collection): DBQ { public static getInstance(): DataService {
return { if(DataService._instance === undefined) {
findOne: queryWrapper.dbFindOne.bind(null, dbCollectionFromCollection(collection)), DataService._instance = new DataService();
findMany: queryWrapper.dbFindMany.bind(null, dbCollectionFromCollection(collection)), }
insertOne: queryWrapper.dbInsertOne.bind(null, dbCollectionFromCollection(collection)), return DataService._instance;
insertMany: queryWrapper.dbInsertMany.bind(null, dbCollectionFromCollection(collection)),
updateOne: queryWrapper.dbUpdateOne.bind(null, dbCollectionFromCollection(collection)),
updateMany: queryWrapper.dbUpdateMany.bind(null, dbCollectionFromCollection(collection)),
removeOne: queryWrapper.dbRemoveOne.bind(null, dbCollectionFromCollection(collection)),
removeMany: queryWrapper.dbRemoveMany.bind(null, dbCollectionFromCollection(collection))
} }
}
public makeDBQ(collection: Collection): DBQ {
return {
findOne: async (query: object | string) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbFindOne(dbCollection, query);
},
findMany: async (query: object | string[], sortQuery: object) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbFindMany(dbCollection, query, sortQuery);
},
insertOne: async (doc: object) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbInsertOne(dbCollection, doc);
},
insertMany: async (docs: object[]) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbInsertMany(dbCollection, docs);
},
updateOne: async(query: object | string, updateQuery: object) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbUpdateOne(dbCollection, query, updateQuery);
},
updateMany: async (query: object | string[], updateQuery: object) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbUpdateMany(dbCollection, query, updateQuery);
},
removeOne: async (query: string | object) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbRemoveOne(dbCollection, query);
},
removeMany: async (query: object | string[]) => {
const dbCollection = await this._getDbCollectionFromCollection(collection);
return this._queryWrapper.dbRemoveMany(dbCollection, query);
}
};
}
let done = false; private async _getConnector(): Promise<DbConnector> {
DbConnectorFactory.getDbConnector().then((connector: DbConnector) => { try {
done = true; const connector = await DbConnectorFactory.getDbConnector();
db = connector.db; return connector;
}).catch((err) => { } catch (err) {
console.log(`data service got error while connect to data base ${err}`); console.log(`data service got an error while connecting to database: ${err}`);
//TODO: choose best practice for error handling throw err;
throw err; }
}); }
deasync.loopWhile(() => !done);
private async _getDbCollectionFromCollection(collection: Collection): Promise<dbCollection> {
const connector = await this._getConnector();
const db = connector.db;
let dbCollection = db.get(collection);
if(dbCollection === undefined) {
throw new Error('Can`t find collection ' + collection);
}
return dbCollection;
}
}

12
server/src/services/data_service/migrations.ts

@ -8,15 +8,15 @@
*/ */
import { Collection } from './collection'; import { Collection } from './collection';
import { makeDBQ } from './index'; import { DataService } from './index';
import * as _ from 'lodash'; import * as _ from 'lodash';
const metaDB = makeDBQ(Collection.DB_META); const metaDB = DataService.getInstance().makeDBQ(Collection.DB_META);
const analyticUnitsDB = makeDBQ(Collection.ANALYTIC_UNITS); const analyticUnitsDB = DataService.getInstance().makeDBQ(Collection.ANALYTIC_UNITS);
const analyticUnitCachesDB = makeDBQ(Collection.ANALYTIC_UNIT_CACHES); const analyticUnitCachesDB = DataService.getInstance().makeDBQ(Collection.ANALYTIC_UNIT_CACHES);
const thresholdsDB = makeDBQ(Collection.THRESHOLD); const thresholdsDB = DataService.getInstance().makeDBQ(Collection.THRESHOLD);
const DB_META_ID = '000000000000000000000001'; //24 symbols for mongodb const DB_META_ID = '000000000000000000000001'; //24 symbols for mongodb
@ -124,7 +124,7 @@ async function integrateThresholdsIntoAnalyticUnits() {
async function addDetectorTypes() { async function addDetectorTypes() {
const analyticUnits = await analyticUnitsDB.findMany({ detectorType: { $exists: false } }); const analyticUnits = await analyticUnitsDB.findMany({ detectorType: { $exists: false } });
const promises = analyticUnits.map(analyticUnit => const promises = analyticUnits.map(analyticUnit =>
analyticUnitsDB.updateOne(analyticUnit._id, { detectorType: getDetectorByType(analyticUnit.type) }) analyticUnitsDB.updateOne(analyticUnit._id, { detectorType: getDetectorByType(analyticUnit.type) })
); );

Loading…
Cancel
Save