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