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 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 FILES_DATABASE_PATH = path.join(DATA_PATH, 'files.db');
export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000');
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> {
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.analyticUnitFromObj(obj);
AnalyticUnit.create(unit);
let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.AnalyticUnit.fromObject(obj);
let id = await AnalyticUnit.create(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 AnalyticUnit = {
id?: AnalyticUnitId,
name: string,
panelUrl: string,
type: string,
metric: Metric
status: string,
error?: string,
lastPredictionTime: number
export class AnalyticUnit {
constructor(
public name: string,
public panelUrl: string,
public type: string,
public metric: Metric,
public id?: AnalyticUnitId,
public lastPredictionTime?: number,
public status?: string,
public error?: string,
) {
if(name === undefined) {
throw new Error(`Missing field "name"`);
}
export function analyticUnitFromObj(obj: any): AnalyticUnit {
if(obj === undefined) {
throw new Error('obj is undefined');
if(panelUrl === undefined) {
throw new Error(`Missing field "panelUrl"`);
}
if(obj.type === undefined) {
if(type === undefined) {
throw new Error(`Missing field "type"`);
}
if(obj.name === undefined) {
throw new Error(`Missing field "name"`);
if(metric === undefined) {
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) {
throw new Error(`Missing field "metric.targets"`);
return new AnalyticUnit(
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> {
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
*/
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> {
await db.remove(id);
await db.removeOne(id);
return;
}
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) {
return db.update(id, { status, error });
return db.updateOne(id, { status, error });
}
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';
let db = makeDBQ(Collection.METRICS);
export type Datasource = {
method: string,
data: Object,
params: Object,
type: string,
url: string
export class Metric {
constructor(public datasource: string, public targets: any[]) {
if(datasource === undefined) {
throw new Error('datasource is undefined');
}
if(targets === undefined) {
throw new Error('targets is undefined');
}
if(targets.length === 0) {
throw new Error('targets is empty');
}
export type MetricId = string;
export type Metric = {
id?: MetricId,
datasource: Datasource,
targets: string[]
}
export function metricFromObj(obj: any): Metric {
public toObject() {
return {
datasource: obj.datasource,
targets: obj.targets
datasource: this.datasource,
targets: this.targets
};
}
// export async function saveTargets(targets: string[]) {
// let metrics = [];
// for (let target of targets) {
// metrics.push(create(target));
// }
// return metrics;
// }
export async function create(metric: Metric): Promise<MetricId> {
return metric.id = await db.insert(metric);
static fromObject(obj: any): Metric {
if(obj === undefined) {
throw new Error('obj is undefined');
}
return new Metric(
obj.datasource,
obj.targets
);
}
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';
let db = makeDBQ(Collection.SEGMENTS);
import * as _ from 'lodash';
type SegmentId = string;
type Segment = {
id?: SegmentId,
from: number,
to: number,
labeled: boolean
export class Segment {
constructor(
public from: number,
public to: number,
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) {
return
static fromObject(obj: any): Segment {
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';
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
*/
export type DBQ = {
insert: (document: object) => string,
insertOne: (document: 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,
remove: (query: string | object) => number
removeOne: (query: string | object) => number
}
export function makeDBQ(collection: Collection): DBQ {
return {
insert: dbInsert.bind(null, collection),
insertOne: dbInsert.bind(null, collection),
insertMany: dbInsertMany.bind(null, collection),
update: dbUpdate.bind(null, collection),
updateOne: dbUpdate.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
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 });

Loading…
Cancel
Save