Example #1
0
        public IActionResult Get(bool refresh)
        {
            //
            if (refresh)
            {
                CodePayload.Clear();
                InitZmodDirectory.ScanCodeDirectory();
                codeResponse = CodePayload.Get();
            }

            //
            string jsonStr = JsonConvert.SerializeObject(codeResponse, Formatting.None);

            try
            {
                if (!string.IsNullOrEmpty(jsonStr))
                {
                    var jsonObj = JsonConvert.DeserializeObject <List <CodeResponse> >(jsonStr);
                    return(Json(jsonObj));
                }
                else
                {
                    return(Ok(new { message = "No Content", errorCode = 204, exception = "No Content" }));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "Server error.", errorCode = 404, exception = ex.StackTrace }));
            }
        }
Example #2
0
        public void TestUpdateCodePayload()
        {
            List <Property>     _prop = new List <Property>();
            List <CodeResponse> _Code = new List <CodeResponse>();

            TestCreateCodePayload();
            _Code = CodePayload.Get();
            _prop.Add(new Property {
                key = "new_prop", value = "200"
            });
            CodeResponse updateRecord = new CodeResponse()
            {
                Id         = "HelloCode",
                Name       = "HelloCode.py",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".png",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "PY",
                Url        = "http://localhost/uploads/Code/HelloCode.py",
                FilePath   = "",
                Properties = _prop
            };

            CodeResponse updated = CodePayload.Update(updateRecord);

            Assert.NotEqual(_Code[0].Properties, updateRecord.Properties);
        }
Example #3
0
        public void TestReadCodePayload()
        {
            List <CodeResponse> _Code = new List <CodeResponse>();

            _Code = CodePayload.Get();
            Assert.NotNull(_Code);
        }
Example #4
0
        public IActionResult DeleteInstancesAsync(string id)
        {
            bool   result = false;
            string type   = "";

            //check file type to delete - jupyter
            var codeResponse = CodePayload.Get();

            foreach (var item in codeResponse)
            {
                if (item.Id == id)
                {
                    type = item.Type;
                }
            }

            //check got pmml file
            if (string.IsNullOrEmpty(type))
            {
                var modalResponse = ModelPayload.Get();
                foreach (var item in modalResponse)
                {
                    if (item.Id == id)
                    {
                        type = item.Type;
                    }
                }
            }
            //check for zmk
            if (string.IsNullOrEmpty(type))
            {
                type = "ZMK";
            }

            switch (type)
            {
            case "JUPYTER_NOTEBOOK":
                result = StopJupyter(id);
                InstancePayload.Delete(id);
                break;

            case "PMML":
                result = StopTensorboard(id);
                InstancePayload.Delete(id);
                break;

            case "ZMK":
                result = ZMKDockerCmdHelper.StopZMKInstance(id);
                break;
            }
            var zmkResponse1 = InstancePayload.Get();

            return(Ok(new { user = string.Empty, id = id, type = type, message = "Instance deleted successfully.", Json = JsonConvert.SerializeObject(zmkResponse1) }));
        }
Example #5
0
        public void TestDeleteCodePayload()
        {
            List <CodeResponse> _model = new List <CodeResponse>();

            TestCreateCodePayload();
            _model = CodePayload.Get();
            Assert.NotNull(_model);
            //
            bool isDeleted = CodePayload.Delete("HelloCode");

            Assert.True(isDeleted);
            //
            _model = CodePayload.Get();
            Assert.True(_model.Count == 0);
        }
Example #6
0
 public CodeController(IHostingEnvironment environment, IConfiguration configuration, ILogger <CodeController> log, IPyJupyterServiceClient _jupyterClient, IPyCompile _pyCodeCompile)
 {
     //update
     _environment         = environment ?? throw new ArgumentNullException(nameof(environment));
     this.Configuration   = configuration;
     this.jupyterClient   = _jupyterClient;
     this.pyCompileClient = _pyCodeCompile;
     this.Logger          = log;
     this.BASEURLJUP      = Configuration["JupyterServer:srvurl"];
     try
     {
         codeResponse = CodePayload.Get();
     }
     catch (Exception ex)
     {
         Logger.LogCritical(ex, ex.StackTrace);
     }
 }
Example #7
0
 public CodeController(IWebHostEnvironment environment, IConfiguration configuration, ILogger <CodeController> log, IPyJupyterServiceClient _jupyterClient, IPyCompile _pyCodeCompile, IScheduler factory)
 {
     //update
     _environment         = environment ?? throw new ArgumentNullException(nameof(environment));
     this.Configuration   = configuration;
     this.jupyterClient   = _jupyterClient;
     this.pyCompileClient = _pyCodeCompile;
     _scheduler           = factory;
     this.Logger          = log;
     this.BASEURLJUP      = Configuration["JupyterServer:srvurl"];
     try
     {
         codeResponse = CodePayload.Get().Where(c => c.Name.Contains("-checkpoint.ipynb") == false).ToList <CodeResponse>();
     }
     catch (Exception ex)
     {
         Logger.LogCritical(ex, ex.StackTrace);
     }
 }
Example #8
0
        public async Task <IActionResult> PauseScheduledJob(string id)
        {
            string response = string.Empty;
            string filePath = Helpers.Common.FilePathHelper.GetFilePathById(id, CodePayload.Get());

            ISchedulerFactory schfack   = new StdSchedulerFactory();
            IScheduler        scheduler = await schfack.GetScheduler();

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

            var resp = new
            {
                id             = id,
                taskName       = id,
                createdOn      = "",
                type           = "Code",
                cronExpression = "0 * * ? * *",
                status         = "STOPPED"
            };

            return(Ok(resp));
        }
Example #9
0
        public async Task <IActionResult> Post(List <IFormFile> file)
        {
            List <CodeResponse> response         = new List <CodeResponse>();
            List <CodeResponse> existingCodeData = new List <CodeResponse>();
            long   size = file.Sum(f => f.Length);
            bool   IsFileExists = false;
            string type, dirFullpath, nbPath, _url = string.Empty;
            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var formFile in file)
            {
                dirFullpath = DirectoryHelper.GetCodeDirectoryPath();
                if (formFile.Length > 0)
                {
                    //check if the file with the same name exists
                    existingCodeData = CodePayload.Get();
                    if (existingCodeData.Count > 0)
                    {
                        //
                        foreach (var record in existingCodeData)
                        {
                            if (record.Name == formFile.FileName)
                            {
                                IsFileExists = true;
                            }
                        }
                    }
                    existingCodeData.Clear();
                    //
                    if (!IsFileExists)
                    {
                        var fileExt = System.IO.Path.GetExtension(formFile.FileName).Substring(1);
                        if (fileExt.ToLower() == "ipynb")
                        {
                            dirFullpath = dirFullpath + formFile.FileName.Replace(".ipynb", "/");
                            _url        = DirectoryHelper.GetCodeUrl(formFile.FileName.Replace(".ipynb", string.Empty) + "/" + formFile.FileName);
                        }
                        else
                        {
                            _url = DirectoryHelper.GetCodeUrl(formFile.FileName);
                        }
                        nbPath = dirFullpath + formFile.FileName;

                        //check if folder path exists...if not then create folder
                        if (!Directory.Exists(dirFullpath))
                        {
                            Directory.CreateDirectory(dirFullpath);
                        }
                        // upload file start
                        using (var fileStream = new FileStream(Path.Combine(dirFullpath, formFile.FileName), FileMode.Create))
                        {
                            //check file allowed extensions
                            if (!extensions.Contains(fileExt.ToString().ToLower()))
                            {
                                return(BadRequest("File type not allowed"));
                            }

                            //file copy
                            await formFile.CopyToAsync(fileStream);

                            // upload file edn and start creating payload
                            switch (fileExt.ToLower())
                            {
                            case "py":
                                type = "PYTHON";
                                break;

                            case "ipynb":
                                type = "JUPYTER_NOTEBOOK";
                                break;

                            default:
                                type = "UNRECOGNIZED";
                                break;
                            }

                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = formFile.FileName.Replace("." + fileExt, ""),
                                Name        = formFile.FileName,
                                User        = "",
                                Created_on  = DateTime.Now.ToString(),
                                Edited_on   = DateTime.Now.ToString(),
                                Extension   = fileExt,
                                MimeType    = formFile.ContentType,
                                Size        = formFile.Length,
                                Type        = type,
                                Url         = _url,
                                FilePath    = nbPath,
                                DateCreated = DateTime.Now
                            };
                            CodePayload.Create(newRecord);
                            response.Add(newRecord);
                        }
                    }
                }
                IsFileExists = false;
            }

            if (response.Count > 0)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest("File already exists."));
            }
        }
Example #10
0
        public async Task <IActionResult> Post(List <IFormFile> file)
        {
            List <CodeResponse> response         = new List <CodeResponse>();
            List <CodeResponse> existingCodeData = new List <CodeResponse>();
            long   size = file.Sum(f => f.Length);
            bool   IsFileExists = false;
            string type, dirFullpath, nbPath, _url = string.Empty;
            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var formFile in file)
            {
                dirFullpath = DirectoryHelper.GetCodeDirectoryPath();
                #region type
                var fileExt = System.IO.Path.GetExtension(formFile.FileName).Substring(1);
                // upload file edn and start creating payload
                switch (fileExt.ToLower())
                {
                case "py":
                    type = "PYTHON";
                    break;

                case "ipynb":
                    type = "JUPYTER_NOTEBOOK";
                    break;

                case "r":
                    type = "R";
                    break;

                default:
                    type = "UNRECOGNIZED";
                    break;
                }
                #endregion
                if (formFile.Length > 0)
                {
                    //check if the file with the same name exists
                    existingCodeData = CodePayload.Get();
                    if (existingCodeData.Count > 0)
                    {
                        //
                        foreach (var record in existingCodeData)
                        {
                            if (record.Name == formFile.FileName)
                            {
                                IsFileExists = true;
                            }
                        }
                    }
                    existingCodeData.Clear();
                    //
                    if (!FilePathHelper.IsFileNameValid(formFile.FileName))
                    {
                        return(BadRequest("File name not valid."));
                    }
                    if (!IsFileExists)
                    {
                        #region upload large file > 400MB
                        if (size > 40000000)
                        {
                            Console.WriteLine(">>>>>>>>>>>>>>>>>>>>UPLOADING LARGE CODE FILE................................");
                            #region add to uploading
                            //
                            FilesInProgress wip = new FilesInProgress()
                            {
                                Id           = formFile.FileName,
                                CreatedAt    = DateTime.Now,
                                Name         = formFile.FileName,
                                Type         = type,
                                Module       = "CODE",
                                UploadStatus = "INPROGRESS"
                            };

                            FilesUploadingPayload.Create(wip);

                            #endregion
                            //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 training model
                                ITrigger trigger = TriggerBuilder.Create()
                                                   .WithIdentity($"Uploading Code file-{DateTime.Now}")
                                                   .WithPriority(1)
                                                   .Build();

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

                                job.JobDataMap["id"]       = formFile.FileName.Replace($".{fileExt}", "");
                                job.JobDataMap["filePath"] = filePath;
                                await _scheduler.ScheduleJob(job, trigger);

                                #endregion
                            }
                        }
                        #endregion
                        if (fileExt.ToLower() == "ipynb")
                        {
                            dirFullpath = dirFullpath + formFile.FileName.Replace(".ipynb", "/");
                            _url        = DirectoryHelper.GetCodeUrl(formFile.FileName.Replace(".ipynb", string.Empty) + "/" + formFile.FileName);
                        }
                        else
                        {
                            _url = DirectoryHelper.GetCodeUrl(formFile.FileName);
                        }
                        nbPath = dirFullpath + formFile.FileName;

                        //check if folder path exists...if not then create folder
                        if (!Directory.Exists(dirFullpath))
                        {
                            Directory.CreateDirectory(dirFullpath);
                        }
                        // upload file start
                        using (var fileStream = new FileStream(Path.Combine(dirFullpath, formFile.FileName), FileMode.Create))
                        {
                            //check file allowed extensions
                            if (!extensions.Contains(fileExt.ToString().ToLower()))
                            {
                                return(BadRequest("File type not allowed"));
                            }

                            //file copy
                            await formFile.CopyToAsync(fileStream);

                            CodeResponse newRecord = new CodeResponse()
                            {
                                Id          = formFile.FileName.Replace("." + fileExt, ""),
                                Name        = formFile.FileName,
                                User        = "",
                                Created_on  = DateTime.Now.ToString(),
                                Edited_on   = DateTime.Now.ToString(),
                                Extension   = fileExt,
                                MimeType    = formFile.ContentType,
                                Size        = formFile.Length,
                                Type        = type,
                                Url         = _url,
                                FilePath    = nbPath,
                                DateCreated = DateTime.Now
                            };
                            CodePayload.Create(newRecord);
                            response.Add(newRecord);
                        }
                    }
                    else
                    {
                        return(Conflict(new { message = "File already exists.", error = "File already exists." }));
                    }
                }
                IsFileExists = false;
            }

            if (response.Count > 0)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest("File already exists."));
            }
        }