|
|
@ -32,7 +32,7 @@ export type Anomaly = { |
|
|
|
next_id: number |
|
|
|
next_id: number |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export type AnomalyId = string; |
|
|
|
export type PredictorId = string; |
|
|
|
|
|
|
|
|
|
|
|
let anomaliesNameToIdMap = {}; |
|
|
|
let anomaliesNameToIdMap = {}; |
|
|
|
|
|
|
|
|
|
|
@ -49,7 +49,7 @@ function saveAnomaliesMap() { |
|
|
|
writeJsonDataSync(filename, anomaliesNameToIdMap); |
|
|
|
writeJsonDataSync(filename, anomaliesNameToIdMap); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getAnomalyIdByName(anomalyName:string) : AnomalyId { |
|
|
|
function getPredictorIdByName(anomalyName:string): PredictorId { |
|
|
|
loadAnomaliesMap(); |
|
|
|
loadAnomaliesMap(); |
|
|
|
anomalyName = anomalyName.toLowerCase(); |
|
|
|
anomalyName = anomalyName.toLowerCase(); |
|
|
|
if(anomalyName in anomaliesNameToIdMap) { |
|
|
|
if(anomalyName in anomaliesNameToIdMap) { |
|
|
@ -58,42 +58,40 @@ function getAnomalyIdByName(anomalyName:string) : AnomalyId { |
|
|
|
return anomalyName; |
|
|
|
return anomalyName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function insertAnomaly(anomaly: Anomaly) : AnomalyId { |
|
|
|
function insertAnomaly(anomaly: Anomaly): PredictorId { |
|
|
|
const hashString = anomaly.name + (new Date()).toString(); |
|
|
|
const hashString = anomaly.name + (new Date()).toString(); |
|
|
|
const anomalyId:AnomalyId = crypto.createHash('md5').update(hashString).digest('hex'); |
|
|
|
const predictorId:PredictorId = crypto.createHash('md5').update(hashString).digest('hex'); |
|
|
|
anomaliesNameToIdMap[anomaly.name] = anomalyId; |
|
|
|
anomaliesNameToIdMap[anomaly.name] = predictorId; |
|
|
|
saveAnomaliesMap(); |
|
|
|
saveAnomaliesMap(); |
|
|
|
// return anomalyId
|
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
// const anomalyId:AnomalyId = anomaly.name;
|
|
|
|
|
|
|
|
let filename = path.join(ANOMALIES_PATH, `${anomalyId}.json`); |
|
|
|
|
|
|
|
if(fs.existsSync(filename)) { |
|
|
|
if(fs.existsSync(filename)) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
saveAnomaly(anomalyId, anomaly); |
|
|
|
saveAnomaly(predictorId, anomaly); |
|
|
|
return anomalyId; |
|
|
|
return predictorId; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function removeAnomaly(anomalyId:AnomalyId) { |
|
|
|
function removeAnomaly(predictorId: PredictorId) { |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${anomalyId}.json`); |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
fs.unlinkSync(filename); |
|
|
|
fs.unlinkSync(filename); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function saveAnomaly(anomalyId: AnomalyId, anomaly: Anomaly) { |
|
|
|
function saveAnomaly(predictorId: PredictorId, anomaly: Anomaly) { |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${anomalyId}.json`); |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
return writeJsonDataSync(filename, anomaly); |
|
|
|
return writeJsonDataSync(filename, anomaly); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function loadAnomalyById(anomalyId: AnomalyId) : Anomaly { |
|
|
|
function loadAnomalyById(predictorId: PredictorId): Anomaly { |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${anomalyId}.json`); |
|
|
|
let filename = path.join(ANOMALIES_PATH, `${predictorId}.json`); |
|
|
|
if(!fs.existsSync(filename)) { |
|
|
|
if(!fs.existsSync(filename)) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
return getJsonDataSync(filename); |
|
|
|
return getJsonDataSync(filename); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function loadAnomalyByName(anomalyName: string) : Anomaly { |
|
|
|
function loadAnomalyByName(anomalyName: string): Anomaly { |
|
|
|
let anomalyId = getAnomalyIdByName(anomalyName); |
|
|
|
let predictorId = getPredictorIdByName(anomalyName); |
|
|
|
return loadAnomalyById(anomalyId); |
|
|
|
return loadAnomalyById(predictorId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function saveAnomalyTypeInfo(info) { |
|
|
|
function saveAnomalyTypeInfo(info) { |
|
|
@ -113,24 +111,24 @@ function getAnomalyTypeInfo(name) { |
|
|
|
return getJsonDataSync(path.join(ANOMALIES_PATH, `${name}.json`)); |
|
|
|
return getJsonDataSync(path.join(ANOMALIES_PATH, `${name}.json`)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function setAnomalyStatus(anomalyId:AnomalyId, status:string, error?:string) { |
|
|
|
function setAnomalyStatus(predictorId: PredictorId, status: string, error?: string) { |
|
|
|
let info = loadAnomalyById(anomalyId); |
|
|
|
let info = loadAnomalyById(predictorId); |
|
|
|
info.status = status; |
|
|
|
info.status = status; |
|
|
|
if(error !== undefined) { |
|
|
|
if(error !== undefined) { |
|
|
|
info.error = error; |
|
|
|
info.error = error; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
info.error = ''; |
|
|
|
info.error = ''; |
|
|
|
} |
|
|
|
} |
|
|
|
saveAnomaly(anomalyId, info); |
|
|
|
saveAnomaly(predictorId, info); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function setAnomalyPredictionTime(anomalyId:AnomalyId, lastPredictionTime:number) { |
|
|
|
function setAnomalyPredictionTime(predictorId: PredictorId, lastPredictionTime: number) { |
|
|
|
let info = loadAnomalyById(anomalyId); |
|
|
|
let info = loadAnomalyById(predictorId); |
|
|
|
info.last_prediction_time = lastPredictionTime; |
|
|
|
info.last_prediction_time = lastPredictionTime; |
|
|
|
saveAnomaly(anomalyId, info); |
|
|
|
saveAnomaly(predictorId, info); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export { |
|
|
|
export { |
|
|
|
saveAnomaly, loadAnomalyById, loadAnomalyByName, insertAnomaly, removeAnomaly, saveAnomalyTypeInfo, |
|
|
|
saveAnomaly, loadAnomalyById, loadAnomalyByName, insertAnomaly, removeAnomaly, saveAnomalyTypeInfo, |
|
|
|
getAnomalyTypeInfo, getAnomalyIdByName, setAnomalyStatus, setAnomalyPredictionTime |
|
|
|
getAnomalyTypeInfo, getPredictorIdByName, setAnomalyStatus, setAnomalyPredictionTime |
|
|
|
} |
|
|
|
} |
|
|
|