|
|
@ -1,5 +1,5 @@ |
|
|
|
import { getJsonDataSync, writeJsonDataSync } from '../services/json' |
|
|
|
import { getJsonDataSync, writeJsonDataSync } from '../services/json' |
|
|
|
import { ANOMALIES_PATH } from '../config' |
|
|
|
import { ANALYTIC_UNITS_PATH } from '../config' |
|
|
|
|
|
|
|
|
|
|
|
import * as crypto from 'crypto'; |
|
|
|
import * as crypto from 'crypto'; |
|
|
|
|
|
|
|
|
|
|
@ -20,71 +20,57 @@ export type Metric = { |
|
|
|
targets: string[] |
|
|
|
targets: string[] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type AnalyticUnitId = string; |
|
|
|
|
|
|
|
|
|
|
|
export type AnalyticUnit = { |
|
|
|
export type AnalyticUnit = { |
|
|
|
|
|
|
|
id?: AnalyticUnitId, |
|
|
|
name: string, |
|
|
|
name: string, |
|
|
|
|
|
|
|
|
|
|
|
panelUrl: string, |
|
|
|
panelUrl: string, |
|
|
|
|
|
|
|
type: string, |
|
|
|
pattern: string, |
|
|
|
|
|
|
|
metric: Metric, |
|
|
|
metric: Metric, |
|
|
|
datasource: Datasource |
|
|
|
datasource: Datasource |
|
|
|
status: string, |
|
|
|
status: string, |
|
|
|
error?: string, |
|
|
|
error?: string, |
|
|
|
|
|
|
|
|
|
|
|
lastPredictionTime: number, |
|
|
|
lastPredictionTime: number, |
|
|
|
nextId: number |
|
|
|
nextId: number |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export type AnalyticUnitId = string; |
|
|
|
export function createItem(item: AnalyticUnit): AnalyticUnitId { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createItem(item: AnalyticUnit): AnalyticUnitId { |
|
|
|
|
|
|
|
const hashString = item.name + (new Date()).toString(); |
|
|
|
const hashString = item.name + (new Date()).toString(); |
|
|
|
const newId: AnalyticUnitId = crypto.createHash('md5').update(hashString).digest('hex'); |
|
|
|
const newId: AnalyticUnitId = crypto.createHash('md5').update(hashString).digest('hex'); |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${newId}.json`); |
|
|
|
let filename = path.join(ANALYTIC_UNITS_PATH, `${newId}.json`); |
|
|
|
if(fs.existsSync(filename)) { |
|
|
|
if(fs.existsSync(filename)) { |
|
|
|
return null; |
|
|
|
throw new Error(`Can't create item with id ${newId}`); |
|
|
|
} |
|
|
|
} |
|
|
|
save(newId, item); |
|
|
|
save(newId, item); |
|
|
|
|
|
|
|
item.id = newId; |
|
|
|
return newId; |
|
|
|
return newId; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function removeItem(key: AnalyticUnitId) { |
|
|
|
export function removeItem(id: AnalyticUnitId) { |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${key}.json`); |
|
|
|
let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`); |
|
|
|
fs.unlinkSync(filename); |
|
|
|
fs.unlinkSync(filename); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function save(predictorId: AnalyticUnitId, anomaly: AnalyticUnit) { |
|
|
|
export function save(id: AnalyticUnitId, unit: AnalyticUnit) { |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`); |
|
|
|
return writeJsonDataSync(filename, anomaly); |
|
|
|
return writeJsonDataSync(filename, unit); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function loadById(predictorId: AnalyticUnitId): AnalyticUnit { |
|
|
|
// TODO: make async
|
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
export function loadById(id: AnalyticUnitId): AnalyticUnit { |
|
|
|
|
|
|
|
let filename = path.join(ANALYTIC_UNITS_PATH, `${id}.json`); |
|
|
|
if(!fs.existsSync(filename)) { |
|
|
|
if(!fs.existsSync(filename)) { |
|
|
|
return null; |
|
|
|
throw new Error(`Can't find Analytic Unit with id ${id}`); |
|
|
|
} |
|
|
|
} |
|
|
|
return getJsonDataSync(filename); |
|
|
|
return getJsonDataSync(filename); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function saveAnomalyTypeInfo(info) { |
|
|
|
export function getAnomalyTypeInfo(name) { |
|
|
|
console.log('Saving'); |
|
|
|
return getJsonDataSync(path.join(ANALYTIC_UNITS_PATH, `${name}.json`)); |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${info.name}.json`); |
|
|
|
|
|
|
|
if(info.next_id === undefined) { |
|
|
|
|
|
|
|
info.next_id = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if(info.last_prediction_time === undefined) { |
|
|
|
|
|
|
|
info.last_prediction_time = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return writeJsonDataSync(filename, info); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getAnomalyTypeInfo(name) { |
|
|
|
|
|
|
|
return getJsonDataSync(path.join(ANOMALIES_PATH, `${name}.json`)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function setAnomalyStatus(predictorId: AnalyticUnitId, status: string, error?: string) { |
|
|
|
export function setAnomalyStatus(predictorId: AnalyticUnitId, status: string, error?: string) { |
|
|
|
let info = loadById(predictorId); |
|
|
|
let info = loadById(predictorId); |
|
|
|
info.status = status; |
|
|
|
info.status = status; |
|
|
|
if(error !== undefined) { |
|
|
|
if(error !== undefined) { |
|
|
@ -95,13 +81,8 @@ function setAnomalyStatus(predictorId: AnalyticUnitId, status: string, error?: s |
|
|
|
save(predictorId, info); |
|
|
|
save(predictorId, info); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function setAnomalyPredictionTime(predictorId: AnalyticUnitId, lastPredictionTime: number) { |
|
|
|
export function setPredictionTime(id: AnalyticUnitId, time: number) { |
|
|
|
let info = loadById(predictorId); |
|
|
|
let info = loadById(id); |
|
|
|
info.lastPredictionTime = lastPredictionTime; |
|
|
|
info.lastPredictionTime = time; |
|
|
|
save(predictorId, info); |
|
|
|
save(id, info); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export { |
|
|
|
|
|
|
|
save, loadById, createItem, removeItem, saveAnomalyTypeInfo, |
|
|
|
|
|
|
|
getAnomalyTypeInfo, setAnomalyStatus, setAnomalyPredictionTime |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|