Esempio n. 1
0
        public async Task <IActionResult> GetAsync()
        {
            var tasks = SchedulerPayload.Get().Select(t => new
            {
                t.Id,
                t.Name,
                t.CreatedOn,
                t.EditedOn,
                t.Type,
                t.CronExpression,
                t.StartDate,
                t.StartTimeH,
                t.StartTimeM,
                t.Recurrence,
                t.Status
            });
            await Task.FromResult(0);

            if (tasks.Count() > 0)
            {
                return(Json(tasks));
            }
            else
            {
                return(Ok(new { }));
            }
        }
Esempio n. 2
0
 public IActionResult GetRunningJobs(string id)
 {
     if (!string.IsNullOrEmpty(id))
     {
         return(Json(SchedulerPayload.Get().Where(j => j.Id == id)));
     }
     else
     {
         return(Json(SchedulerPayload.Get()));
     }
 }
Esempio n. 3
0
    public static bool AddZMKResponses(string id, object resp)
    {
        bool result = false;
        var  schObj = SchedulerPayload.Get().Where(s => s.Id == id).FirstOrDefault();

        //schObj.ZMKResponse.Add(resp);
        schObj.History.Add(resp);
        //update payload
        SchedulerPayload.Update(schObj);

        return(result);
    }
Esempio n. 4
0
 public JobsController(IWebHostEnvironment environment, IConfiguration configuration, ILogger <CodeController> log, IScheduler factory)
 {
     //update
     _environment       = environment ?? throw new ArgumentNullException(nameof(environment));
     this.Configuration = configuration;
     this.Logger        = log;
     _scheduler         = factory;
     try
     {
         InitZmodDirectory.ScanModelsDirectory();
         jobsResponse = SchedulerPayload.Get();
     }
     catch (Exception ex)
     {
         Logger.LogCritical(ex, ex.StackTrace);
     }
 }
Esempio n. 5
0
        public async Task <IActionResult> DeleteTaskAsync(string id)
        {
            string response = string.Empty;
            //delete job from scheduler
            // First we must get a reference to a scheduler
            ISchedulerFactory schfack   = new StdSchedulerFactory();
            IScheduler        scheduler = await schfack.GetScheduler();

            string filePath  = SchedulerPayload.GetById(id).Select(c => c.FilePath).FirstOrDefault();
            var    jobKey    = new JobKey(filePath);
            bool   isDeleted = await scheduler.DeleteJob(jobKey);

            //
            SchedulerPayload.Delete(id);
            response = await nnclient.DeleteRunningTask(id);

            return(Json(new { message = "Task deleted.", id = id }));
        }
Esempio n. 6
0
    public static bool AddZMKResponses(string id, string resp, string type)
    {
        bool    result = false;
        var     schObj = SchedulerPayload.Get().Where(s => s.Id == id).FirstOrDefault();
        JObject so     = JObject.Parse(resp);

        switch (type)
        {
        case "ExecuteCode":
            var objExecute = JsonConvert.DeserializeObject <ExecuteCodeResponse>(resp);
            schObj.History.Add(objExecute);
            break;

        case "Train":
            var obj = JsonConvert.DeserializeObject <TrainingResponse>(resp);
            schObj.History.Add(obj);
            break;
        }
        //update payload
        SchedulerPayload.Update(schObj);

        return(result);
    }
Esempio n. 7
0
        public async Task <IActionResult> StopJobAsync(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var filePath = SchedulerPayload.Get().Where(s => s.Id == id).Select(s => s.FilePath).FirstOrDefault();
                ISchedulerFactory schfack   = new StdSchedulerFactory();
                IScheduler        scheduler = await schfack.GetScheduler();

                await scheduler.PauseJob(new JobKey(filePath));

                var resp = new
                {
                    id      = id,
                    status  = "STOPPED",
                    message = "Scheduled Job is stopped."
                };

                return(Json(resp));
            }
            else
            {
                return(NotFound());
            }
        }
Esempio n. 8
0
        public async Task <IActionResult> GetSelectedTaskAysnc(string id)
        {
            var           taskData    = SchedulerPayload.Get().Where(s => s.Id == id).FirstOrDefault();
            var           _type       = SchedulerPayload.Get().Where(s => s.Id == id).Select(t => t.Type).FirstOrDefault();
            List <object> taskHistNew = new List <object>();

            if (!string.IsNullOrEmpty(taskData.Id))
            {
                string origid = "";
                if (id.IndexOf('-') > 0)
                {
                    origid = id.Substring(0, id.IndexOf('-'));
                }
                else
                {
                    origid = id;
                }
                var resp = await nnclient.GetRunningTaskByTaskName(origid);

                if ((JObject.Parse(resp)["runningTask"].Count() == 0) && (taskData.Id != taskData.Name))
                {
                    resp = await nnclient.GetRunningTaskByTaskName(origid.Substring(0, origid.IndexOf(taskData.Name)));
                }
                var    joResp = JsonConvert.DeserializeObject <ZMM.Models.ClientJsonObjects.ZMKCodeExecution.RootObject>(resp);
                JArray jArr   = JArray.FromObject(joResp.runningTask);
                Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ZMK RUNNING TASK: {joResp}");
                // JObject joResp = JObject.Parse(resp);
                // JArray jArr = (JArray)joResp["runningTask"];
                //
                if (taskData.Recurrence == "REPEAT")
                {
                    taskHistNew.Clear();
                    foreach (var j in taskData.History)
                    {
                        if (_type.Contains("PYTHON"))
                        {
                            ExecuteCodeResponse ecr = (ExecuteCodeResponse)j;
                            string _status          = jArr.Where(s => s["idforData"].ToString() == ecr.idforData).Select(s => s["status"].Value <string>()).FirstOrDefault();
                            ecr.status = _status;
                            taskHistNew.Add(ecr);
                        }
                        if (_type.Contains("AUTOML"))
                        {
                            AutoMLResponse obj     = (AutoMLResponse)j;
                            string         _status = jArr.Where(s => s["idforData"].ToString() == obj.idforData).Select(s => s["status"].Value <string>()).FirstOrDefault();
                            obj.status = _status;
                            taskHistNew.Add(obj);
                        }
                        if (_type.Contains("NN"))
                        {
                            TrainingResponse obj     = (TrainingResponse)j;
                            string           _status = jArr.Where(s => s["idforData"].ToString() == obj.idforData).Select(s => s["status"].Value <string>()).FirstOrDefault();
                            obj.status = _status;
                            taskHistNew.Add(obj);
                        }
                    }
                }
                else if (taskData.Recurrence == "ONE_TIME")
                {
                    taskHistNew.Clear();
                    foreach (var j in taskData.History)
                    {
                        if (j.ToString() == "ZMM.Models.ResponseMessages.TrainingResponse")
                        {
                            TrainingResponse jlist      = (TrainingResponse)j;
                            string           _idfordata = jArr.Where(s => s["idforData"].ToString() == jlist.idforData).Select(s => s["idforData"].Value <string>()).FirstOrDefault();
                            if (!string.IsNullOrEmpty(_idfordata))
                            {
                                JObject obj = JObject.FromObject(new
                                {
                                    idforData  = _idfordata,
                                    status     = jArr.Where(s => s["idforData"].ToString() == jlist.idforData).Select(s => s["status"].Value <string>()).FirstOrDefault(),
                                    executedAt = jlist.executedAt
                                });
                                taskHistNew.Add(obj);
                            }
                        }
                        else
                        {
                            var    jo         = JObject.Parse(j.ToString());
                            string _idfordata = jArr.Where(s => s["idforData"].ToString() == jo["idforData"].ToString()).Select(s => s["idforData"].Value <string>()).FirstOrDefault();
                            if (!string.IsNullOrEmpty(_idfordata))
                            {
                                JObject obj = JObject.FromObject(new
                                {
                                    idforData  = _idfordata,
                                    status     = jArr.Where(s => s["idforData"].ToString() == jo["idforData"].ToString()).Select(s => s["status"].Value <string>()).FirstOrDefault(),
                                    executedAt = jo["executedAt"].ToString()
                                });
                                taskHistNew.Add(obj);
                            }
                        }
                    }
                }
                taskData.History.Clear();
                taskData.History = taskHistNew;
                SchedulerPayload.Update(taskData);

                return(Json(SchedulerPayload.Get().Where(s => s.Id == id).FirstOrDefault()));
            }
            else
            {
                return(Ok(new { }));
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> ExecutePyAsync(string id)
        {
            string zmkResponse = "";
            string filePath    = "";
            string reqBody     = "";

            try
            {
                //read cron information from the request body
                using (var reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();
                    reqBody = body.ToString();
                }

                //
                foreach (var record in codeResponse)
                {
                    if (record.Id.ToString() == id)
                    {
                        filePath = record.FilePath;
                        break;
                    }
                }
                //
                if (!string.IsNullOrEmpty(filePath))
                {
                    zmkResponse = await pyCompileClient.ExecutePy(filePath, "");

                    var jsonObj = JsonConvert.DeserializeObject <ExecuteCodeResponse>(zmkResponse);
                    jsonObj.executedAt = DateTime.Now;
                    List <ExecuteCodeResponse> tresp = new List <ExecuteCodeResponse>();
                    tresp.Add(jsonObj);

                    JObject cronjson = JObject.Parse(reqBody);
                    if (cronjson["recurrence"].ToString() == "REPEAT")
                    {
                        //check if same job is scheduled
                        ISchedulerFactory schfack   = new StdSchedulerFactory();
                        IScheduler        scheduler = await schfack.GetScheduler();

                        var jobKey = new JobKey(filePath);
                        if (await scheduler.CheckExists(jobKey))
                        {
                            await scheduler.ResumeJob(jobKey);
                        }
                        else
                        {
                            #region create quartz job for execute code
                            ITrigger trigger = TriggerBuilder.Create()
                                               .WithIdentity($"Execute Code Job-{DateTime.Now}")
                                               .WithCronSchedule(cronjson["cronExpression"].ToString())
                                               .WithPriority(1)
                                               .Build();

                            IJobDetail job = JobBuilder.Create <ExecuteCodeJob>()
                                             .WithIdentity(filePath)
                                             .Build();

                            job.JobDataMap["id"]       = id;
                            job.JobDataMap["filePath"] = filePath;
                            job.JobDataMap["baseurl"]  = Configuration["PyServiceLocation:srvurl"];

                            await _scheduler.ScheduleJob(job, trigger);

                            //add to scheduler payload
                            SchedulerResponse schJob = new SchedulerResponse()
                            {
                                CreatedOn      = DateTime.Now.ToString(),
                                CronExpression = cronjson["cronExpression"].ToString(),
                                DateCreated    = DateTime.Now,
                                EditedOn       = DateTime.Now.ToString(),
                                FilePath       = filePath,
                                Id             = id,
                                Name           = id,
                                Type           = "PYTHON",
                                Url            = "",
                                Recurrence     = cronjson["recurrence"].ToString(),
                                StartDate      = (cronjson["startDate"] == null) ? "" : cronjson["startDate"].ToString(),
                                //cronjson["startDate"].ToString(),
                                StartTimeH = (cronjson["startTimeH"] == null) ? "" : cronjson["startTimeH"].ToString(),
                                StartTimeM = (cronjson["startTimeM"] == null) ? "" : cronjson["startTimeM"].ToString(),
                                // ZMKResponse = tresp.ToList<object>(),
                                History = tresp.ToList <object>()
                            };
                            SchedulerPayload.Create(schJob);
                            #endregion
                        }
                    }
                    else
                    {
                        //add history
                        JArray jHist = new JArray();
                        foreach (var r in tresp)
                        {
                            jHist.Add(new JObject()
                            {
                                { "idforData", r.idforData },
                                { "status", r.status },
                                { "executedAt", r.executedAt }
                            });
                        }
                        //add to scheduler payload
                        SchedulerResponse schJob = new SchedulerResponse()
                        {
                            CreatedOn      = DateTime.Now.ToString(),
                            CronExpression = "",
                            DateCreated    = DateTime.Now,
                            EditedOn       = DateTime.Now.ToString(),
                            FilePath       = filePath,
                            Id             = id,
                            Name           = id,
                            Type           = "PYTHON",
                            Url            = "",
                            Recurrence     = "ONE_TIME",
                            StartDate      = "",
                            StartTimeH     = "",
                            StartTimeM     = "",
                            // ZMKResponse = tresp.ToList<object>(),
                            Status  = "",
                            History = jHist.ToList <object>()
                        };


                        SchedulerPayload.Create(schJob);
                    }


                    return(Json(jsonObj));
                }
                else
                {
                    return(BadRequest(new { message = "File execution failed." }));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "File execution failed.", exception = ex.StackTrace }));
            }
        }
Esempio n. 10
0
        public async Task <IActionResult> CreateScoringJobAsync(string id, [FromBody] TrainingRequestParam jsonbody)
        {
            string  filePath = "";
            var     json     = JsonConvert.SerializeObject(jsonbody);
            JObject jo       = JObject.Parse(json);

            /* TODO: validation for req body */
            JObject cronjson = JObject.Parse(jo.ToString());

            filePath = cronjson["filePath"].ToString();

            /*request json for zmk */
            JObject req = JObject.Parse(jo.ToString());

            req.Remove("recurrence");
            req.Remove("cronExpression");
            req.Remove("startDate");
            req.Remove("startTimeH");
            req.Remove("startTimeM");

            /* validate if model exists in the ZMOD directory and loaded. */
            if (ModelPayload.Get().Where(m => m.Id == id).Count() == 1)
            {
                List <TrainingResponse> tresp = new List <TrainingResponse>();

                #region schedule scoring

                /* check if same job is scheduled */
                ISchedulerFactory schfack   = new StdSchedulerFactory();
                IScheduler        scheduler = await schfack.GetScheduler();

                var jobKey = new JobKey(filePath);
                if (await scheduler.CheckExists(jobKey))
                {
                    await scheduler.ResumeJob(jobKey);
                }
                else
                {
                    try
                    {
                        #region create quartz job for training model
                        ITrigger trigger = TriggerBuilder.Create()
                                           .WithIdentity($"Training Model Job-{DateTime.Now}")
                                           .WithCronSchedule(cronjson["cronExpression"].ToString())
                                           .WithPriority(1)
                                           .Build();

                        IJobDetail job = JobBuilder.Create <TrainModelJob>()
                                         .WithIdentity(filePath)
                                         .Build();

                        job.JobDataMap["id"]       = id;
                        job.JobDataMap["filePath"] = filePath;
                        job.JobDataMap["reqBody"]  = req.ToString();
                        job.JobDataMap["baseurl"]  = Configuration["PyServiceLocation:srvurl"];

                        await _scheduler.ScheduleJob(job, trigger);

                        //add to scheduler payload
                        SchedulerResponse schJob = new SchedulerResponse()
                        {
                            CreatedOn      = DateTime.Now.ToString(),
                            CronExpression = cronjson["cronExpression"].ToString(),
                            DateCreated    = DateTime.Now,
                            EditedOn       = DateTime.Now.ToString(),
                            FilePath       = filePath,
                            Id             = id,
                            Name           = id,
                            Type           = "PMML",
                            Url            = "",
                            Recurrence     = cronjson["recurrence"].ToString(),
                            StartDate      = cronjson["startDate"].ToString(),
                            StartTimeH     = (cronjson["startTimeH"].ToString() == null) ? "" : cronjson["startTimeH"].ToString(),
                            StartTimeM     = (cronjson["startTimeM"].ToString() == null) ? "" : cronjson["startTimeM"].ToString(),
                            // ZMKResponse = tresp.ToList<object>()
                        };
                        SchedulerPayload.Create(schJob);
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                #endregion

                return(Ok(new { modelId = id, modelState = "model valid", message = "training job scheduled successfully!" }));
            }
            else
            {
                return(BadRequest(new { modelId = id, modelState = "model not found", message = "model does not exists!" }));
            }
        }
Esempio n. 11
0
 public IActionResult Get(string id)
 {
     return(Json(SchedulerPayload.Get().Where(s => s.Id == id).First()));
 }