Browse Source

models++

pull/1/head
Alexey Velikiy 6 years ago
parent
commit
1d35992384
  1. 2
      server/src/config.ts
  2. 6
      server/src/controllers/analytics_controller.ts
  3. 90
      server/src/models/analytic_unit_model.ts
  4. 56
      server/src/models/metric_model.ts
  5. 52
      server/src/models/segment_model.ts
  6. 2
      server/src/models/task_model.ts
  7. 15
      server/src/services/data_service.ts

2
server/src/config.ts

@ -12,9 +12,7 @@ export const ANALYTICS_PATH = path.join(__dirname, '../../analytics');
export const DATA_PATH = path.join(__dirname, '../../data'); export const DATA_PATH = path.join(__dirname, '../../data');
export const ANALYTIC_UNITS_DATABASE_PATH = path.join(DATA_PATH, 'analytic_units.db'); export const ANALYTIC_UNITS_DATABASE_PATH = path.join(DATA_PATH, 'analytic_units.db');
export const METRICS_DATABASE_PATH = path.join(DATA_PATH, 'metrics.db');
export const SEGMENTS_DATABASE_PATH = path.join(DATA_PATH, 'segments.db'); export const SEGMENTS_DATABASE_PATH = path.join(DATA_PATH, 'segments.db');
export const FILES_DATABASE_PATH = path.join(DATA_PATH, 'files.db');
export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000'); export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000');
export const ZMQ_CONNECTION_STRING = getConfigField('ZMQ_CONNECTION_STRING', null); export const ZMQ_CONNECTION_STRING = getConfigField('ZMQ_CONNECTION_STRING', null);

6
server/src/controllers/analytics_controller.ts

@ -123,8 +123,8 @@ export function isAnalyticReady(): boolean {
} }
export async function createAnalyticUnitFromObject(obj: any): Promise<AnalyticUnit.AnalyticUnitId> { export async function createAnalyticUnitFromObject(obj: any): Promise<AnalyticUnit.AnalyticUnitId> {
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.analyticUnitFromObj(obj); let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.AnalyticUnit.fromObject(obj);
AnalyticUnit.create(unit); let id = await AnalyticUnit.create(unit);
// runLearning(unit); // runLearning(unit);
return return id;
} }

90
server/src/models/analytic_unit_model.ts

@ -7,54 +7,66 @@ let db = makeDBQ(Collection.ANALYTIC_UNITS);
export type AnalyticUnitId = string; export type AnalyticUnitId = string;
export type AnalyticUnit = { export class AnalyticUnit {
id?: AnalyticUnitId, constructor(
name: string, public name: string,
panelUrl: string, public panelUrl: string,
type: string, public type: string,
metric: Metric public metric: Metric,
status: string, public id?: AnalyticUnitId,
error?: string, public lastPredictionTime?: number,
lastPredictionTime: number public status?: string,
public error?: string,
) {
if(name === undefined) {
throw new Error(`Missing field "name"`);
} }
if(panelUrl === undefined) {
export function analyticUnitFromObj(obj: any): AnalyticUnit { throw new Error(`Missing field "panelUrl"`);
if(obj === undefined) {
throw new Error('obj is undefined');
} }
if(obj.type === undefined) { if(type === undefined) {
throw new Error(`Missing field "type"`); throw new Error(`Missing field "type"`);
} }
if(obj.name === undefined) { if(metric === undefined) {
throw new Error(`Missing field "name"`); throw new Error(`Missing field "metric"`);
} }
if(obj.panelUrl === undefined) {
throw new Error(`Missing field "panelUrl"`);
} }
if(obj.metric === undefined) {
throw new Error(`Missing field "datasource"`); public toObject() {
return {
_id: this.id,
name: this.name,
panelUrl: this.panelUrl,
type: this.type,
metric: this.metric.toObject(),
lastPredictionTime: this.lastPredictionTime,
status: this.status,
error: this.error
};
} }
if(obj.metric.datasource === undefined) {
throw new Error(`Missing field "metric.datasource"`); static fromObject(obj: any): AnalyticUnit {
if(obj === undefined) {
throw new Error('obj is undefined');
} }
if(obj.metric.targets === undefined) { return new AnalyticUnit(
throw new Error(`Missing field "metric.targets"`); obj.name,
obj.panelUrl,
obj.type,
Metric.fromObject(obj.metric),
obj.status,
obj.lastPredictionTime,
obj.id || obj._id,
obj.error,
);
} }
const unit: AnalyticUnit = {
name: obj.name,
panelUrl: obj.panelUrl,
type: obj.type,
metric: obj.metric,
status: 'LEARNING',
lastPredictionTime: 0
};
return unit;
} }
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> { export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
return db.findOne(id); return AnalyticUnit.fromObject(db.findOne(id));
} }
/** /**
@ -64,22 +76,22 @@ export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
* @returns unit.id * @returns unit.id
*/ */
export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> { export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> {
return unit.id = await db.insert(unit); return unit.id = await db.insertOne(unit);
} }
export async function remove(id: AnalyticUnitId): Promise<void> { export async function remove(id: AnalyticUnitId): Promise<void> {
await db.remove(id); await db.removeOne(id);
return; return;
} }
export async function update(id: AnalyticUnitId, unit: AnalyticUnit) { export async function update(id: AnalyticUnitId, unit: AnalyticUnit) {
return db.update(id, unit); return db.updateOne(id, unit);
} }
export async function setStatus(id: AnalyticUnitId, status: string, error?: string) { export async function setStatus(id: AnalyticUnitId, status: string, error?: string) {
return db.update(id, { status, error }); return db.updateOne(id, { status, error });
} }
export async function setPredictionTime(id: AnalyticUnitId, lastPredictionTime: number) { export async function setPredictionTime(id: AnalyticUnitId, lastPredictionTime: number) {
return db.update(id, { lastPredictionTime }); return db.updateOne(id, { lastPredictionTime });
} }

56
server/src/models/metric_model.ts

@ -1,46 +1,32 @@
import { Collection, makeDBQ } from '../services/data_service'; export class Metric {
constructor(public datasource: string, public targets: any[]) {
if(datasource === undefined) {
let db = makeDBQ(Collection.METRICS); throw new Error('datasource is undefined');
}
if(targets === undefined) {
export type Datasource = { throw new Error('targets is undefined');
method: string, }
data: Object, if(targets.length === 0) {
params: Object, throw new Error('targets is empty');
type: string,
url: string
} }
export type MetricId = string;
export type Metric = {
id?: MetricId,
datasource: Datasource,
targets: string[]
} }
export function metricFromObj(obj: any): Metric { public toObject() {
return { return {
datasource: obj.datasource, datasource: this.datasource,
targets: obj.targets targets: this.targets
}; };
} }
// export async function saveTargets(targets: string[]) { static fromObject(obj: any): Metric {
// let metrics = []; if(obj === undefined) {
// for (let target of targets) { throw new Error('obj is undefined');
// metrics.push(create(target)); }
// } return new Metric(
// return metrics; obj.datasource,
// } obj.targets
);
export async function create(metric: Metric): Promise<MetricId> {
return metric.id = await db.insert(metric);
} }
export async function findMetric(id: MetricId): Promise<Metric> {
return db.findOne(id);
} }

52
server/src/models/segment_model.ts

@ -2,35 +2,61 @@ import { AnalyticUnitId } from './analytic_unit_model';
import { Collection, makeDBQ } from '../services/data_service'; import { Collection, makeDBQ } from '../services/data_service';
let db = makeDBQ(Collection.SEGMENTS);
import * as _ from 'lodash';
type SegmentId = string; type SegmentId = string;
type Segment = { export class Segment {
id?: SegmentId, constructor(
from: number, public from: number,
to: number, public to: number,
labeled: boolean public labeled: boolean,
public id?: SegmentId
) {
if(from === undefined) {
throw new Error('from is undefined');
}
if(to === undefined) {
throw new Error('to is undefined');
}
if(labeled === undefined) {
throw new Error('labeled is undefined');
}
} }
let db = makeDBQ(Collection.SEGMENTS); public toObject() {
return {
_id: this.id,
from: this.from,
to: this.to,
labeled: this.labeled
};
}
export function getLabeledSegments(id: AnalyticUnitId) { static fromObject(obj: any): Segment {
return if(obj === undefined) {
throw new Error('obj is undefined');
}
return new Segment(
obj.from, obj.to,
obj.labeled, obj.id || obj._id
);
}
} }
export function getPredictedSegments(id: AnalyticUnitId) {
export function getLabeled(id: AnalyticUnitId) {
//return db.
} }
export function saveSegments(id: AnalyticUnitId, segments: Segment[]) { export function getPredicted(id: AnalyticUnitId) {
} }
export async function insertSegments(id: AnalyticUnitId, addedSegments: Segment[], labeled: boolean) { export async function create(segments: Segment[]) {
} }
export function removeSegments(idsToRemove: SegmentId[]) { export function remove(idsToRemove: SegmentId[]) {
} }

2
server/src/models/task_model.ts

@ -1,3 +1,3 @@
class Task { export class Task {
} }

15
server/src/services/data_service.ts

@ -4,7 +4,7 @@ import * as nedb from 'nedb';
import * as fs from 'fs'; import * as fs from 'fs';
export enum Collection { ANALYTIC_UNITS, METRICS, SEGMENTS }; export enum Collection { ANALYTIC_UNITS, SEGMENTS };
/** /**
@ -13,20 +13,20 @@ export enum Collection { ANALYTIC_UNITS, METRICS, SEGMENTS };
* @param { string | object } query: a key as a string or mongodb-style query * @param { string | object } query: a key as a string or mongodb-style query
*/ */
export type DBQ = { export type DBQ = {
insert: (document: object) => string, insertOne: (document: object) => string,
insertMany: (documents: object[]) => string[], insertMany: (documents: object[]) => string[],
update: (query: string | object, updateQuery: any) => void, updateOne: (query: string | object, updateQuery: any) => void,
findOne: (query: string | object) => any, findOne: (query: string | object) => any,
remove: (query: string | object) => number removeOne: (query: string | object) => number
} }
export function makeDBQ(collection: Collection): DBQ { export function makeDBQ(collection: Collection): DBQ {
return { return {
insert: dbInsert.bind(null, collection), insertOne: dbInsert.bind(null, collection),
insertMany: dbInsertMany.bind(null, collection), insertMany: dbInsertMany.bind(null, collection),
update: dbUpdate.bind(null, collection), updateOne: dbUpdate.bind(null, collection),
findOne: dbFindOne.bind(null, collection), findOne: dbFindOne.bind(null, collection),
remove: dbRemove.bind(null, collection) removeOne: dbRemove.bind(null, collection)
} }
} }
@ -120,5 +120,4 @@ checkDataFolders();
// TODO: it's better if models request db which we create if it`s needed // TODO: it's better if models request db which we create if it`s needed
db[Collection.ANALYTIC_UNITS] = new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true }); db[Collection.ANALYTIC_UNITS] = new nedb({ filename: config.ANALYTIC_UNITS_DATABASE_PATH, autoload: true });
db[Collection.METRICS] = new nedb({ filename: config.METRICS_DATABASE_PATH, autoload: true });
db[Collection.SEGMENTS] = new nedb({ filename: config.SEGMENTS_DATABASE_PATH, autoload: true }); db[Collection.SEGMENTS] = new nedb({ filename: config.SEGMENTS_DATABASE_PATH, autoload: true });

Loading…
Cancel
Save