예제 #1
0
        private async Task<(bool, object, object, IScanModel)> GetScanIds(ScanInstanceModel model)
        {
            bool allowSave = true;
            object nodeId = await this.Db.Nodes.GetIdBy(model.NodeAddress);//NodeAddress is indexed and unique.
            if (nodeId == null)
                throw new InvalidOperationException($"There is no node item for '{model.NodeAddress}' in the database.");


            object scanResourceId = await this.Db.ScanResources.GetScanResourceIdBy(model.ResourceName);//ScanResource.Name is indexed and unique
            if (scanResourceId == null)
                throw new InvalidOperationException($"There is no ScanResource item which's name is '{model.ResourceName}' in the database.");


            IScanModel scanModel = await this.Db.Scans.GetBy(model.Asset, scanResourceId);//Scan.Asset and Scan.ScanResourceId are indexed and unique
            if (scanModel == null)
                throw new InvalidOperationException($"There is no Scan item which's name is '{model.ResourceName}' in the database.");

            if (String.IsNullOrEmpty(model.FailedReason) && String.IsNullOrEmpty(model.Result))//Hem hata olmamış hem de null değer gelmiş.
            {
                allowSave = scanModel.SaveNullResult;
            }

            if (allowSave)
            {
                allowSave = scanModel.LastAssignedNodeId.Equals(nodeId);//offline gibi durumlarda atanan node' den gelmediyse kayıt etme.
                if (!allowSave)
                {
                   _=Utility.CreateLogger(nameof(ScanWorks), nameof(GetScanIds)).Code(786).Warning($"Last-Scan-Ids are not match. Orginal is {scanModel.LastAssignedNodeId} but incoming is {nodeId}").SaveAsync();
                }
            }

            return (allowSave, nodeId, scanResourceId, scanModel);
        }
예제 #2
0
        internal async Task<int> SaveScanInstance(ScanInstanceModel model)
        {
            int ret = 0;
            if (model.IsModelValid())
            {
                IScanInstanceModel entity = this.Db.ModelFactory.CreateScanInstanceModel();
                entity.CopyPropertiesFrom(model);

                (bool allowSave, object nodeId, object scanResourceId, IScanModel scan) = await this.GetScanIds(model);

                if (allowSave)
                {
                    entity.NodeId = nodeId;
                    entity.ScanId = scan.ScanId;

                    await this.Db.ScanInstances.Save(entity);
                    await this.CheckMaxInstance(scan);
                    ret = 1;

                    ConsoleObserver.Instance.Notify(nameof(ScanWorks) + "_" + nameof(SaveScanInstance), "A scan result has been got and saved.", model);
                }
                else
                {
                    ConsoleObserver.Instance.Notify(nameof(ScanWorks) + "_" + nameof(SaveScanInstance), "A scan result has been got but the value was null or empty.", model);
                }
            }

            return ret;
        }
예제 #3
0
            public async Task <int> SaveScanInstanceOrEditResult(ScanInstanceModel model)
            {
                try
                {
                    return(await RestClient.Instance.PostAsync <int>($"{KamajiScanActions.SaveScanInstanceOrEditResult}", model));
                }
                catch (Exception ex)
                {
                    await OfflineData.From(model, nameof(SaveScanInstanceOrEditResult), ex).SaveAsync();

                    return(-1);
                }
            }
예제 #4
0
        public Task <IActionResult> GetScanInstanceListBy(string resourceName, string asset, bool alsoGetParent, bool alsoGetChilds)
        {
            return(this.ResultAsync(async() =>
            {
                IEnumerable <ScanInstanceModel> ret = null;
                if (!String.IsNullOrEmpty(resourceName) && !String.IsNullOrEmpty(asset))
                {
                    List <object> scanIdList = new List <object>();
                    IScanModel scan = await new ScanWorks(this.Db).GetScanBy(resourceName, asset);
                    scanIdList.Add(scan.ScanId);

                    if (scan.ParentId != null && alsoGetParent)
                    {
                        scanIdList.Add(scan.ParentId);
                        // IScanModel parent = await this.Db.Scans.GetBy(scan.ParentId);
                        //if (null != parent)
                        //  scanIdList.Add(parent.ParentId);
                    }

                    if (alsoGetChilds)
                    {
                        var childList = await this.Db.Scans.GetRecursivelyChildList(scan.ScanId);
                        if (!childList.IsEmptyList())
                        {
                            scanIdList.AddRange(childList.Select(p => p.ScanId));
                        }
                    }

                    var temp = await this.Db.ScanInstances.GetListBy(scanIdList);
                    if (null != temp)
                    {
                        ret = temp.Select(i =>
                        {
                            ScanInstanceModel item = new ScanInstanceModel();
                            item.CopyPropertiesFrom(i);
                            return item;
                        });
                    }
                }

                return ret;
            }));
        }
예제 #5
0
        private async Task HandleOfflineData(string nodeAddress)
        {
            ScanWorks scan            = new ScanWorks(this.Db);
            var       offlineDataList = await NodesClient.Instance.GetOfflineData(nodeAddress);

            if (!offlineDataList.IsEmptyList())
            {
                foreach (var offline in offlineDataList)
                {
                    if (!String.IsNullOrEmpty(offline.Operation) && !String.IsNullOrEmpty(offline.Json))
                    {
                        try
                        {
                            switch (offline.Operation)
                            {
                            case "SaveScanInstance":
                                ScanInstanceModel sim = JsonConvert.DeserializeObject <ScanInstanceModel>(offline.Json);
                                await scan.SaveScanInstance(sim);

                                break;

                            case "SaveScanInstanceOrEditResult":
                                ScanInstanceModel sim2 = JsonConvert.DeserializeObject <ScanInstanceModel>(offline.Json);
                                await scan.SaveScanInstanceOrEditResult(sim2);

                                break;

                            case "EditScan":    //Burası kuyruğa eklendiği için iptal edildçi.
                                //ScanModel sm = JsonConvert.DeserializeObject<ScanModel>(offline.Json);
                                //  await scan.EditScan(sm);
                                break;

                            case "AddChildScans":
                                AddChildScansModel asm = JsonConvert.DeserializeObject <AddChildScansModel>(offline.Json);
                                await scan.AddChildScans(asm);

                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            var logger = Utility.CreateLogger(nameof(HandleOfflineData), nameof(HandleOfflineData)).Code(468);
                            await logger.Error(ex.FindRoot() ?? ex).SaveAsync();

                            ConsoleObserver.Instance.Notify(nameof(HandleOfflineData), nameof(HandleOfflineData), ex);
                        }
                    }

                    try
                    {
                        if (offline.Id > 0)
                        {
                            await NodesClient.Instance.DeleteOfflineData(nodeAddress, offline.Id);
                        }
                    }
                    catch (Exception ex)
                    {
                        var logger = Utility.CreateLogger(nameof(HandleOfflineData), nameof(HandleOfflineData)).Code(467);
                        await logger.Error(ex.FindRoot() ?? ex).SaveAsync();

                        ConsoleObserver.Instance.Notify(nameof(HandleOfflineData), nameof(HandleOfflineData), ex);
                    }
                }
            }
        }
예제 #6
0
 public Task <IActionResult> SaveScanInstanceOrEditResult(ScanInstanceModel model)
 => this.ResultAsync(() => new ScanWorks(this.Db).SaveScanInstanceOrEditResult(model));     //edit by ScanId
예제 #7
0
 public Task <IActionResult> SaveScanInstance(ScanInstanceModel model)
 => this.ResultAsync(() => new ScanWorks(this.Db).SaveScanInstance(model));