public MODEL UploadModelData([FromBody] Train_Data data, [FromQuery(Name = "model_type")] string model_type) { if (!AnomalyDetection.IsSupportedMethod(model_type) || !IsValidData(data)) { HttpContext.Response.StatusCode = 400; return(null); } int amount; lock (L_LearnDetectAmount) { L_LearnDetectAmount.Count++; amount = L_LearnDetectAmount.Count; } if (amount > MaxLearnDetectAmount) { lock (L_LearnDetectAmount) { L_LearnDetectAmount.Count--; } HttpContext.Response.StatusCode = 503; return(null); } var model = adm.LearnAndAddNewModel(model_type, data, () => L_LearnDetectAmount.Decrease()); if (model == null) { HttpContext.Response.StatusCode = 500; return(null); } HttpContext.Response.StatusCode = 202; return(model); }
// try to learn new normal model and save it in correspond json file. // the learning is in new Task and the return MODEL status field is therfore "pending". // does afterFinishingLearning Action after the learning finished. public MODEL LearnAndAddNewModel(string detectoionType, Train_Data data, Action afterFinishingLearning) { MODEL model; // random new model_id Random rnd = new Random(DateTime.Now.Millisecond); int id = rnd.Next(); // lock L_NormalModels in order to add the new mode_id lock (L_NormalModels) { // check it doesn't appear already, otherwise random new id. while (L_NormalModels.ContainsKey(id) || System.IO.File.Exists(new MODEL() { model_id = id }.FileName())) { id = rnd.Next(); } model = new MODEL() { model_id = id, status = MODEL.Status_Pending, upload_time = DateTime.Now }; L_NormalModels.Add(id, model); } // start new task of learning correlative features Task.Run(() => { try { // learn noraml mode [that might take while], only if id not deleted yet var correlation = AnomalyDetection.GetNormal(data.train_data, detectoionType, () => this.IsExist(id)); if (correlation == null) { throw new Exception(); } // save it to json file bool isSuccess = IO_Util.SaveNormalModel(model.FileName(), correlation, new MODEL() { model_id = model.model_id, status = MODEL.Status_Ready, upload_time = model.upload_time }); if (!isSuccess) { throw new Exception(); } lock (L_NormalModels) { if (L_NormalModels.ContainsKey(id)) { L_NormalModels[id].status = MODEL.Status_Ready; } else { // probably we won't get here since correlaion wil be null if id was deleted from L_NormalModels, // because we sent the lambda ()=>this.IsExist(id) to AnomalyDetection.GetNormal try { System.IO.File.Delete(new MODEL() { model_id = id }.FileName()); } catch { } } } } catch { lock (L_NormalModels) { if (L_NormalModels.ContainsKey(id)) { L_NormalModels[id].status = MODEL.Status_Corrupted; } } } afterFinishingLearning(); }); // return MODEL even before the learning finished // { model_id = id, status = MODEL.Status_Pending, upload_time = DateTime.Now }; return(model); }
// check Train_Data is valid, meaning has correct field values private bool IsValidData(Train_Data data) { return(data != null && IsValidData(data.train_data)); }