コード例 #1
0
        public InstanceResponse GetByUsername(string username, string connectionString)
        {
            var result = new InstanceResponse
            {
                Condition = false, Message = string.Empty, Instances = new List <Instance>(), Instance = new Instance()
            };

            try
            {
                using (IDbConnection dbConnection = new SqlConnection(connectionString))
                {
                    dbConnection.Open();
                    var sql        = $"SELECT * FROM [app].[Instance] WHERE [Username] = '{username}'";
                    var data       = dbConnection.Query <Instance>(sql);
                    var enumerable = data.ToList();
                    if (!enumerable.Any())
                    {
                        result.Message = "No instances found.";
                        return(result);
                    }

                    result.Condition = true;
                    result.Instance  = enumerable.FirstOrDefault();
                    result.Instances.AddRange(enumerable);
                    return(result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                result.Message = e.InnerException.Message;
                return(result);
            }
        }
コード例 #2
0
        // TODO: Ability to CloneTo a local instance to a remote node
        // TODO: Ability to CloneFrom a remote instance to the local node
        // This will require handling Clone requests through the master node
        public InstanceResponse CloneInstance(InstanceRequest request)
        {
            var instanceId = request.Id;

            // Get a copy of the instance data from the Data module
            var instance = _data.Get <StitchInstance>(instanceId);

            if (instance == null)
            {
                _log.LogError("Could not clone instance {0}, instance does not exist.", instanceId);
                return(InstanceResponse.Failure(request));
            }

            // Update the model to be fresh
            instance.Id           = null;
            instance.StoreVersion = 0;

            // Insert the fresh version to the Data module
            instance = _data.Insert(instance);
            if (instance == null)
            {
                _log.LogError("Could not clone instance {0}, data could not be saved.", instanceId);
                return(InstanceResponse.Failure(request));
            }

            // Report success
            _log.LogInformation("Instance {0} cloned to {1}", instanceId, instance.Id);
            _notifier.StitchCreated(instance);
            return(InstanceResponse.Success(request, instance));
        }
コード例 #3
0
 static TRequestType FromResponse <TRequestType>(InstanceResponse response) where TRequestType : Api.Models.Instance, new() => new TRequestType
 {
     Id   = response.Id,
     Path = response.Path,
     AutoUpdateInterval = response.AutoUpdateInterval,
     ChatBotLimit       = response.ChatBotLimit,
     ConfigurationType  = response.ConfigurationType,
     Name   = response.Name,
     Online = response.Online
 };
コード例 #4
0
        public async Task <IActionResult> GetJupyterNotebookUrlAsync(string id)
        {
            string response = string.Empty;
            string _jUrl    = string.Empty;
            string message  = "Notebook is up and running successfully";
            //We needs to populate Current User from request
            string   CURRENT_USER    = string.Empty;
            string   ResourcePath    = Helpers.Common.FilePathHelper.GetFilePathById(id, codeResponse);
            FileInfo resourceInfo    = new System.IO.FileInfo(ResourcePath);
            string   notebookDir     = resourceInfo.Directory.ToString();
            string   notebookLinkURL = string.Empty;

            var obj = new
            {
                base_url     = "/",
                NotebookDir  = $"{notebookDir}",
                ResourcePath = $"{ResourcePath}"
            };

            try
            {
                var             portRegex = new Regex(@"(?<![^/]/[^/]*):\d+");//to remove port number
                JupyterNotebook JNBTool   = this.jupyterClient.GetJupyterNotebookTool();
                await System.Threading.Tasks.Task.FromResult(0);

                ITask JupyterNoteBookTask = JNBTool.FindTask(ResourcePath);
                if (JupyterNoteBookTask.IsEmpty())
                {
                    JNBTool.StartTaskAsync((int)TaskTypes.Start, ResourcePath, (JObject)JObject.FromObject(obj));
                }
                notebookLinkURL = JNBTool.GetResourceLink(ResourcePath);
                // notebookLinkURL = @"C:\myCode\Project\ZMOD\code\1_SVM\1_SVM.ipynb";
                //
                // JupyterNotebook nb = JupyterNotebook.Find(ResourcePath);
                var objJNBInst = new InstanceResponse()
                {
                    Id   = id,
                    Name = $"{id}.ipynb",
                    Type = "JNB"
                           // Properties = new List<InstanceProperty>(){ new InstanceProperty{ key="port", value=nb.Info.Port}}
                };
                InstancePayload.Create(objJNBInst);
                notebookLinkURL = notebookLinkURL.Replace(@"//", @"/");
                //
                return(Ok(new { user = CURRENT_USER, id = id, message = message, url = notebookLinkURL.Replace(notebookLinkURL.Substring(0, notebookLinkURL.IndexOf("jnb")), "") }));
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(BadRequest(new { user = CURRENT_USER, id = id, message = message }));
            }
        }
コード例 #5
0
ファイル: ZMKDockerCmdHelper.cs プロジェクト: vadgama/MLW
        public static InstanceResponse GetNonDockerZMK()
        {
            InstanceResponse newRecord = new InstanceResponse()
            {
                Id        = "zmk1",
                Name      = "ZMK1",
                Type      = "ZMK",
                Processes = ""
            };


            return(newRecord);
        }
コード例 #6
0
        public static IList <InstanceResponse> GetAllRunningZMK()
        {
            string cmd    = "docker ps | awk '{if (NR!=1) {print}}' | awk '{print $1, $2, $NF}' | grep 'zmod_zmk'";
            string output = "";
            IList <InstanceResponse> runningZMK = new List <InstanceResponse>();

            //
            try
            {
                output = cmd.Bash();
                var oArr = output.Split('\n');
                foreach (var line in oArr)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        var lineArr = line.Split(' ');
                        List <InstanceProperty> _props = new List <InstanceProperty>();
                        _props.Add(new InstanceProperty {
                            key = "Container Id", value = lineArr[0]
                        });
                        _props.Add(new InstanceProperty {
                            key = "Image", value = lineArr[1]
                        });
                        InstanceResponse newRecord = new InstanceResponse()
                        {
                            Id         = lineArr[0],
                            Name       = lineArr[2],
                            Type       = "ZMK",
                            Processes  = "",
                            Properties = _props
                        };

                        runningZMK.Add(newRecord);
                    }
                }
            }
            catch (Exception ex)
            {
                //do nothing
                string error = ex.StackTrace;
            }

            return(runningZMK.OrderBy(i => i.Name).ToList());
        }
コード例 #7
0
        private InstanceResponse StartInstanceInternal(InstanceRequest request, PackageFile packageFile, StitchInstance instance)
        {
            if (instance == null)
            {
                return(InstanceResponse.Failure(request));
            }

            var result = _stitchInstanceManager.Start(packageFile, instance);

            _data.Save(instance);
            if (!result.Success)
            {
                _log.LogError(result.Exception, "Could not start stitch {0}", request.Id);
                return(InstanceResponse.Create(request, result.Success, instance));
            }

            _log.LogInformation("Started stitch {0} Id={1}", result.StitchInstance.GroupName, result.StitchInstance.Id);
            _notifier.StitchStarted(instance);
            return(InstanceResponse.Create(request, result.Success, instance));
        }
コード例 #8
0
        public async Task TestStart()
        {
            var example = new JobResponse
            {
                Id        = 347,
                StartedAt = DateTimeOffset.UtcNow
            };

            var inst = new InstanceResponse
            {
                Id = 4958
            };

            var mockApiClient = new Mock <IApiClient>();

            mockApiClient.Setup(x => x.Create <JobResponse>(Routes.DreamDaemon, inst.Id.Value, It.IsAny <CancellationToken>())).Returns(Task.FromResult(example));

            var client = new DreamDaemonClient(mockApiClient.Object, inst);

            var result = await client.Start(default).ConfigureAwait(false);
コード例 #9
0
        /// <summary>
        /// Delete all record from the payload
        /// </summary>
        /// <returns></returns>
        public static bool DeleteAll()
        {
            bool result = false;
            //logic to delete
            InstanceResponse _inst = new InstanceResponse();

            try
            {
                foreach (var item in GlobalStorage.InstanceStorage)
                {
                    GlobalStorage.InstanceStorage.Clear();
                }
                result = true;
            }
            catch (Exception ex)
            {
                var err = ex.InnerException;
            }

            return(result);
        }
コード例 #10
0
        public InstanceResponse DeleteStitchInstance(InstanceRequest request)
        {
            bool success    = true;
            var  instanceId = request.Id;
            var  instance   = _data.Get <StitchInstance>(instanceId);

            // Tell the Stitches module to stop the Stitch
            var stopResponse = StopInstanceInternal(request, instance);

            if (!stopResponse.IsSuccess || instance.State != InstanceStateType.Stopped)
            {
                // We will continue to delete the record from the data store. When the node is restarted, this stitch
                // will not be brought back
                _log.LogError("Instance {0} could not be stopped. Deletion of records will continue.", instanceId);
                success = false;
            }

            // Delete the record from the Data module
            bool deleted = _data.Delete <StitchInstance>(instanceId);

            if (!deleted)
            {
                _log.LogError("Instance {0} could not be deleted", instanceId);
                success = false;
            }

            // Remove resources related to the stitch, including directories.
            var removeResult = _stitchInstanceManager.RemoveInstance(instance.Id);

            if (!removeResult.Success)
            {
                _log.LogError("Could not remove resources related to Stitch Id={0}", instance.Id);
                success = false;
            }

            _notifier.StitchDeleted(instanceId, instance.GroupName);

            _log.LogInformation("Instance {0} stopped and deleted successfully", instanceId);
            return(InstanceResponse.Create(request, success, instance));
        }
コード例 #11
0
        private InstanceResponse StopInstanceInternal(InstanceRequest request, StitchInstance instance)
        {
            if (instance == null)
            {
                _log.LogError("Could not find stitch {0}", request.Id);
                return(InstanceResponse.Failure(request));
            }

            var stopResult = _stitchInstanceManager.Stop(instance);

            _data.Save(stopResult.StitchInstance);

            if (!stopResult.Success || stopResult.StitchInstance.State != InstanceStateType.Stopped)
            {
                _log.LogError("Could not stop stitch {0}", request.Id);
                return(InstanceResponse.Create(request, false));
            }

            _log.LogDebug("Stitch instance {0} Id={3} stopped", instance.GroupName, instance.Id);
            _notifier.StitchStopped(instance);

            return(InstanceResponse.Create(request, true));
        }
コード例 #12
0
        /// <summary>
        /// Delete record from the payload
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(string pid)
        {
            bool result = false;
            //logic to delete
            InstanceResponse _inst = new InstanceResponse();

            foreach (var item in GlobalStorage.InstanceStorage)
            {
                try
                {
                    if ((item.Key == pid))
                    {
                        GlobalStorage.InstanceStorage.TryRemove(pid, out _inst);
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    var err = ex.InnerException;
                }
            }
            return(result);
        }
コード例 #13
0
        public IActionResult GetRunningInstances()
        {
            string cmd    = "";
            string output = "";

            List <InstanceResponse> getAllInstances = InstancePayload.Get();

            #region GPU listing

            cmd = "gpustat --json";
            try
            {
                output = cmd.Bash();
                if (!string.IsNullOrEmpty(output))
                {
                    JObject jsonObj = JObject.Parse(output);
                    JArray  arr     = (JArray)jsonObj["gpus"];
                    int     gpuctr  = 1;
                    foreach (var a in arr)
                    {
                        JArray tmpArr = (JArray)a["processes"];
                        List <InstanceProperty> _props = new List <InstanceProperty>();

                        _props.Add(new InstanceProperty {
                            key = "temperature.gpu [celsius]", value = a["temperature.gpu"]
                        });
                        _props.Add(new InstanceProperty {
                            key = "utilization.gpu [%]", value = a["utilization.gpu"]
                        });
                        _props.Add(new InstanceProperty {
                            key = "power.draw [W]", value = a["power.draw"]
                        });
                        _props.Add(new InstanceProperty {
                            key = "enforced.power.limit [W]", value = a["enforced.power.limit"]
                        });
                        _props.Add(new InstanceProperty {
                            key = "memory.used [MB]", value = a["memory.used"]
                        });
                        _props.Add(new InstanceProperty {
                            key = "memory.total [MB]", value = a["memory.total"]
                        });

                        InstanceResponse _instance = new InstanceResponse()
                        {
                            Id         = a["uuid"].ToString(),
                            Name       = $"GPU {gpuctr}",
                            Type       = "GPU",
                            Properties = _props,
                            Processes  = tmpArr
                        };
                        getAllInstances.Add(_instance);
                        gpuctr++;
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "running instance loading failed.", exception = ex.StackTrace }));
            }
            #endregion

            #region ZMK listing
            IList <InstanceResponse> zmkList = ZMKDockerCmdHelper.GetAllRunningZMK();
            if (zmkList.Count > 0)
            {
                getAllInstances.AddRange(zmkList);
            }

            #endregion

            return(Json(getAllInstances));
        }
コード例 #14
0
        public async Task <IActionResult> PostModelTrainAsync(string id)
        {
            string  response    = string.Empty;
            string  dirFullpath = DirectoryHelper.GetDataDirectoryPath();//create new folder in data folder with the name of the file
            string  reqBody     = string.Empty;
            string  filePath    = string.Empty;
            string  dataFolder  = string.Empty;
            JObject jObjOrig    = new JObject();

            using (var reader = new StreamReader(Request.Body))
            {
                var body = reader.ReadToEnd();
                reqBody = body.ToString();
            }
            try
            {
                //get file name
                if (responseData.Count > 0)
                {
                    foreach (var record in responseData)
                    {
                        if (record.Id.ToString() == id)
                        {
                            //set the filePath
                            filePath = record.FilePath;
                            //create dir with filename in data folder
                            if (!Directory.Exists(dirFullpath + record.Name.Replace(".pmml", string.Empty)))
                            {
                                Directory.CreateDirectory(dirFullpath + record.Name.Replace(".pmml", string.Empty));
                                dataFolder = dirFullpath + record.Name.Replace(".pmml", string.Empty);
                            }
                        }
                    }
                }

                //json merge
                //Add TensorBoard Info
                if (!string.IsNullOrEmpty(reqBody))
                {
                    jObjOrig = JObject.Parse(reqBody);
                }
                if (!string.IsNullOrEmpty(filePath))
                {
                    string ResourcePath         = filePath;
                    string TensorBoardLink      = string.Empty;
                    string TensorboardLogFolder = string.Empty;

                    var obj = new
                    {
                        base_url     = "/",
                        ResourcePath = $"{ResourcePath}"
                    };
                    try
                    {
                        var             portRegex       = new Regex(@"(?<![^/]/[^/]*):\d+");//to remove port number
                        TensorBoard     TBTool          = this.tbClient.GetTensorBoardTool();
                        ZMM.Tasks.ITask TensorBoardTask = TBTool.FindTask(ResourcePath);
                        if (TensorBoardTask.IsEmpty())
                        {
                            TBTool.StartTaskAsync((int)TaskTypes.Start, ResourcePath, (JObject)JObject.FromObject(obj));
                        }
                        TensorBoardLink = TBTool.GetResourceLink(ResourcePath, out TensorboardLogFolder);
                        //
                        //TB redirection
                        string tbLink = "";
                        if (TensorBoardLink.Contains("6006"))
                        {
                            tbLink = TensorBoardLink.Replace(":6006", "/tb1");
                        }
                        else if (TensorBoardLink.Contains("6007"))
                        {
                            tbLink = TensorBoardLink.Replace(":6007", "/tb2");
                        }
                        else if (TensorBoardLink.Contains("6008"))
                        {
                            tbLink = TensorBoardLink.Replace(":6008", "/tb3");
                        }
                        //
                        jObjOrig.Add("filePath", ResourcePath);
                        jObjOrig.Add("tensorboardLogFolder", TensorboardLogFolder);
                        jObjOrig.Add("tensorboardUrl", tbLink);
                        Logger.LogInformation("PostModelTrainAsync", jObjOrig.ToString());
                        //for asset
                        // int sIdx =  TensorBoardLink.IndexOf(":6");
                        // var tbInst = new List<InstanceProperty>();
                        // tbInst.Add(new InstanceProperty(){ key = "port", value = TensorBoardLink.Substring(sIdx,6)});

                        //
                        var objJNBInst = new InstanceResponse()
                        {
                            Id   = id,
                            Name = $"{id}",
                            Type = "TB"
                        };
                        InstancePayload.Create(objJNBInst);
                        //
                    }
                    catch (Exception ex)
                    {
                        Logger.LogCritical("PostModelTrainAsync", ex.Message);
                        return(BadRequest(new { user = CURRENT_USER, id = id, message = ex.Message }));
                    }
                }
                if (!string.IsNullOrEmpty(dataFolder))
                {
                    jObjOrig.Add("dataFolder", dataFolder);
                }

                response = await nnclient.TrainModel(jObjOrig.ToString());

                return(Json(response));
            }
            catch (Exception ex)
            {
                //TO DO: ILogger
                string _ex = ex.Message;
                return(BadRequest());
            }
        }
コード例 #15
0
 /// <summary>
 /// CreateInstancePayload
 /// </summary>
 /// <param name="newRecord"></param>
 /// <returns></returns>
 public static InstanceResponse Create(InstanceResponse newRecord)
 {
     GlobalStorage.InstanceStorage.TryAdd(newRecord.Id, newRecord);
     return(newRecord);
 }