Пример #1
0
        private AlertRules LoadNetwork(AlertRules rule)
        {
            var nics = HardwareService.GetAllNics();
            rule.NicDict = new Dictionary<string, int>();
            foreach (var nic in nics)
            {
                rule.NicDict.Add(nic.Key, nic.Value);
            }
            rule.Nics = new SelectList(rule.NicDict, "Value", "Key", 0);

            return rule;
        }
Пример #2
0
        static AlertMapperInitialise()
        {
            // View to DTO
            Mapper.AddMap<AlertSettingsViewModel, AlertSettingsDto>(x =>
            {
                //var settings = new AlertSettingsDto
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new AlertRulesDto().InjectFrom(c)).Cast<AlertRulesDto>()
                //        .ToList()
                //};

                var settings = new AlertSettingsDto();
                settings.InjectFrom(x);
                foreach (var a in x.AlertRules)
                {
                    var newA = new AlertRulesDto();
                    newA.InjectFrom(a);
                    newA.AlertType = (AlertTypeDto)(int)a.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return settings;
            });

            // DTO to Entity
            Mapper.AddMap<AlertSettingsDto, AlertSettings>(x =>
            {
                //var settings = new AlertSettings
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new DataAccessLayer.Models.Settings.AlertRules().InjectFrom(c)).Cast<DataAccessLayer.Models.Settings.AlertRules> ()
                //        .ToList()
                //};

                var settings = new AlertSettings { AlertRules = new List<DataAccessLayer.Models.Settings.AlertRules>()};
                settings.InjectFrom(x);
                foreach (var a in x.AlertRules)
                {
                    var newA = new DataAccessLayer.Models.Settings.AlertRules();
                    newA.InjectFrom(a);
                    newA.AlertType = (DataAccessLayer.Models.Settings.AlertType)(int)a.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return settings;
            });

            // Entity to DTO
            Mapper.AddMap<AlertSettings, AlertSettingsDto>(x =>
            {
                var settings = new AlertSettingsDto { AlertRules = new List<AlertRulesDto>() };
                settings.InjectFrom(x);
                //foreach (var entityAr in x.AlertRules)
                //{
                //    settings.AlertRules.Add((AlertRulesDto)new AlertRulesDto().InjectFrom(entityAr));
                //}
                foreach (var entityAr in x.AlertRules)
                {
                    var newA = new AlertRulesDto();
                    newA.InjectFrom(entityAr);
                    newA.AlertType = (AlertTypeDto)(int)entityAr.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return settings;
            });

            // DTO to View
            Mapper.AddMap<AlertSettingsDto, AlertSettingsViewModel>(x =>
            {
                //var settings = new AlertSettingsViewModel
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new AlertRules().InjectFrom(c)).Cast<AlertRules>()
                //        .ToList()
                //};

                var settings = new AlertSettingsViewModel();
                settings.InjectFrom(x);
                foreach (var entityAr in x.AlertRules)
                {
                    var newA = new AlertRules();
                    newA.InjectFrom(entityAr);
                    newA.AlertType = (AlertType)(int)entityAr.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return settings;
            });

            // View to DTO
            Mapper.AddMap<AlertRules, AlertRulesDto>(x =>
            {
                var dto = new AlertRulesDto();
                dto.InjectFrom(x);
                dto.AlertType = (AlertTypeDto)(int)x.AlertType;
                return dto;
            });

            // DTO to Entity
            Mapper.AddMap<AlertRulesDto, DataAccessLayer.Models.Settings.AlertRules>(x =>
            {
                var dto = new DataAccessLayer.Models.Settings.AlertRules();
                dto.InjectFrom(x);
                dto.AlertType = (DataAccessLayer.Models.Settings.AlertType)(int)x.AlertType;
                return dto;
            });

            // Entity to DTO
            Mapper.AddMap<DataAccessLayer.Models.Settings.AlertRules, AlertRulesDto>(x =>
            {
                var dto = new AlertRulesDto();
                dto.InjectFrom(x);
                dto.AlertType = (AlertTypeDto)(int)x.AlertType;
                return dto;
            });

            // DTO to View
            Mapper.AddMap<AlertRulesDto, AlertRules>(x =>
            {
                var dto = new AlertRules();
                dto.InjectFrom(x);
                dto.AlertType = (AlertType)(int)x.AlertType;
                return dto;
            });
        }
Пример #3
0
        public ActionResult UpdateAlert(AlertRules model)
        {
            if (!model.IsValid)
            {
                switch (model.AlertType)
                {
                    case AlertType.Cpu:
                        return PartialView("CpuAlertModal", model);
                    case AlertType.Network:
                        model = LoadNetwork(model);
                        return PartialView("NetworkAlertModal", model);
                    case AlertType.Hdd:
                        model = LoadHdd(model);
                        return PartialView("DriveAlertModal", model);
                    default:
                        Logger.Fatal("ArgumentOutOfRangeException on AlertType: {0}", model.AlertType);
                        throw new ArgumentOutOfRangeException();
                }
            }
            var currentSettings = AlertSettingsService.GetSettings();

            var maxRecord = 1;

            if (currentSettings.AlertRules.Count > 0)
            {
                maxRecord = currentSettings.AlertRules.Max(x => x.Id);
            }

            var match = currentSettings.AlertRules.FirstOrDefault(x => x.Id == model.Id);
            if (match == null)
            {
                // We don't yet have any rules so create one
                var dtoRule = Mapper.Map<AlertRules, AlertRulesDto>(model);
                dtoRule.Id = ++maxRecord;

                currentSettings.AlertRules.Add(dtoRule);
                Logger.Trace("Saving new rule with id {0}", dtoRule.Id);
                var result = AlertSettingsService.SaveSettings(currentSettings);
                if (result)
                {
                    return Json(new { Result = "True" }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                // We have rules already so let's modify the existing rules, remove the existing and add the new
                var dtoModel = new AlertRulesDto();
                dtoModel = Mapper.Map<AlertRulesDto>(model);

                currentSettings.AlertRules.Remove(match);
                currentSettings.AlertRules.Add(dtoModel);
                var result = AlertSettingsService.SaveSettings(currentSettings);
                if (result)
                {
                    return Json(new { Result = "True" }, JsonRequestBehavior.AllowGet);
                }
            }

            return View("Error");
        }
Пример #4
0
        private AlertRules LoadHdd(AlertRules rule)
        {
            var drives = HardwareService.GetDrives();
            rule.Drives = new List<DriveAlertViewModel>();

            foreach (var drive in drives.Where(drive => drive.IsReady))
            {
                var d = new DriveAlertViewModel { DriveId = drive.DriveId, DriveVolumeLabel = drive.Name };
                rule.Drives.Add(d);
            }

            return rule;
        }
Пример #5
0
        public ActionResult UpdateAlert(int id)
        {
            var settings = AlertSettingsService.GetSettings();
            var selected = settings.AlertRules.FirstOrDefault(x => x.Id == id);
            var vm = new AlertRules();

            if (selected != null)
            {
                vm = Mapper.Map<AlertRules>(selected);
            }

            switch (vm.AlertType)
            {
                case AlertType.Cpu:
                    return PartialView("CpuAlertModal", vm);
                case AlertType.Network:
                    vm = LoadNetwork(vm);
                    return PartialView("NetworkAlertModal", vm);
                case AlertType.Hdd:
                    vm = LoadHdd(vm);
                    return PartialView("DriveAlertModal", vm);
                default:
                    return View("Error");
            }
        }
Пример #6
0
        public ActionResult OpenModal(AlertType alertType)
        {
            var model = new AlertRules { AlertType = alertType };
            var settings = AlertSettingsService.GetSettings();
            switch (model.AlertType)
            {
                case AlertType.Cpu:
                    if (settings.AlertRules.Any(x => x.AlertType == AlertTypeDto.Cpu))
                    {
                        return PartialView("AlertExistsModal", model.AlertType);
                    }
                    return PartialView("CpuAlertModal", model);

                case AlertType.Network:
                    model = LoadNetwork(model);
                    return PartialView("NetworkAlertModal", model);

                case AlertType.Hdd:
                    if (settings.AlertRules.Any(x => x.AlertType == AlertTypeDto.Hdd))
                    {
                        return PartialView("AlertExistsModal", model.AlertType);
                    }
                    model = LoadHdd(model);
                    return PartialView("DriveAlertModal", model);

                default:
                    Logger.Fatal("ArgumentOutOfRangeException on AlertType: {0}", model.AlertType);
                    throw new ArgumentOutOfRangeException();
            }
        }
Пример #7
0
        static AlertMapperInitialise()
        {
            // View to DTO
            Mapper.AddMap <AlertSettingsViewModel, AlertSettingsDto>(x =>
            {
                //var settings = new AlertSettingsDto
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new AlertRulesDto().InjectFrom(c)).Cast<AlertRulesDto>()
                //        .ToList()
                //};

                var settings = new AlertSettingsDto();
                settings.InjectFrom(x);
                foreach (var a in x.AlertRules)
                {
                    var newA = new AlertRulesDto();
                    newA.InjectFrom(a);
                    newA.AlertType = (AlertTypeDto)(int)a.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return(settings);
            });

            // DTO to Entity
            Mapper.AddMap <AlertSettingsDto, AlertSettings>(x =>
            {
                //var settings = new AlertSettings
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new DataAccessLayer.Models.Settings.AlertRules().InjectFrom(c)).Cast<DataAccessLayer.Models.Settings.AlertRules> ()
                //        .ToList()
                //};

                var settings = new AlertSettings {
                    AlertRules = new List <DataAccessLayer.Models.Settings.AlertRules>()
                };
                settings.InjectFrom(x);
                foreach (var a in x.AlertRules)
                {
                    var newA = new DataAccessLayer.Models.Settings.AlertRules();
                    newA.InjectFrom(a);
                    newA.AlertType = (DataAccessLayer.Models.Settings.AlertType)(int) a.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return(settings);
            });

            // Entity to DTO
            Mapper.AddMap <AlertSettings, AlertSettingsDto>(x =>
            {
                var settings = new AlertSettingsDto {
                    AlertRules = new List <AlertRulesDto>()
                };
                settings.InjectFrom(x);
                //foreach (var entityAr in x.AlertRules)
                //{
                //    settings.AlertRules.Add((AlertRulesDto)new AlertRulesDto().InjectFrom(entityAr));
                //}
                foreach (var entityAr in x.AlertRules)
                {
                    var newA = new AlertRulesDto();
                    newA.InjectFrom(entityAr);
                    newA.AlertType = (AlertTypeDto)(int)entityAr.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return(settings);
            });


            // DTO to View
            Mapper.AddMap <AlertSettingsDto, AlertSettingsViewModel>(x =>
            {
                //var settings = new AlertSettingsViewModel
                //{
                //    AlertRules = x.AlertRules
                //        .Select(c => new AlertRules().InjectFrom(c)).Cast<AlertRules>()
                //        .ToList()
                //};

                var settings = new AlertSettingsViewModel();
                settings.InjectFrom(x);
                foreach (var entityAr in x.AlertRules)
                {
                    var newA = new AlertRules();
                    newA.InjectFrom(entityAr);
                    newA.AlertType = (AlertType)(int)entityAr.AlertType;
                    settings.AlertRules.Add(newA);
                }

                return(settings);
            });


            // View to DTO
            Mapper.AddMap <AlertRules, AlertRulesDto>(x =>
            {
                var dto = new AlertRulesDto();
                dto.InjectFrom(x);
                dto.AlertType = (AlertTypeDto)(int)x.AlertType;
                return(dto);
            });

            // DTO to Entity
            Mapper.AddMap <AlertRulesDto, DataAccessLayer.Models.Settings.AlertRules>(x =>
            {
                var dto = new DataAccessLayer.Models.Settings.AlertRules();
                dto.InjectFrom(x);
                dto.AlertType = (DataAccessLayer.Models.Settings.AlertType)(int) x.AlertType;
                return(dto);
            });


            // Entity to DTO
            Mapper.AddMap <DataAccessLayer.Models.Settings.AlertRules, AlertRulesDto>(x =>
            {
                var dto = new AlertRulesDto();
                dto.InjectFrom(x);
                dto.AlertType = (AlertTypeDto)(int)x.AlertType;
                return(dto);
            });

            // DTO to View
            Mapper.AddMap <AlertRulesDto, AlertRules>(x =>
            {
                var dto = new AlertRules();
                dto.InjectFrom(x);
                dto.AlertType = (AlertType)(int)x.AlertType;
                return(dto);
            });
        }
        public void UpdateNewNetworkAlertPost()
        {
            ExpectedDto.AlertRules[0].AlertType = AlertTypeDto.Network;
            Settings.Setup(x => x.GetSettings()).Returns(ExpectedDto);
            Settings.Setup(x => x.SaveSettings(It.IsAny<AlertSettingsDto>())).Returns(true).Verifiable();

            CreateController();

            var model = new AlertRules { Id = int.MaxValue, ThresholdTime = 90, Percentage = 92229, Enabled = true, AlertType = AlertType.Network, NicId = 22 };

            _controller.UpdateAlert(model);
            Settings.Verify(x => x.SaveSettings(It.Is<AlertSettingsDto>(c => c.AlertRules.Any(y => y.Percentage == 92229))), Times.Once);
            Settings.Verify(x => x.SaveSettings(It.Is<AlertSettingsDto>(c => c.AlertRules.Any(y => y.ThresholdTime == 90))), Times.Once);

            Settings.Verify(x => x.SaveSettings(It.Is<AlertSettingsDto>(c => c.AlertRules.Any(y => y.AlertType == AlertTypeDto.Network))), Times.Once);
            Settings.Verify(x => x.SaveSettings(It.Is<AlertSettingsDto>(c => c.AlertRules.Any(y => y.NicId == 22))), Times.Once);
            Settings.Verify(x => x.SaveSettings(It.Is<AlertSettingsDto>(c => c.AlertRules.Any(y => y.Enabled))), Times.Once);
        }
        public void UpdateAlertPostInvalidModel()
        {
            ExpectedDto.AlertRules[0].AlertType = AlertTypeDto.Cpu;
            Settings.Setup(x => x.GetSettings()).Returns(ExpectedDto);
            Settings.Setup(x => x.SaveSettings(It.IsAny<AlertSettingsDto>())).Returns(true).Verifiable();

            CreateController();

            var model = new AlertRules { Id = int.MaxValue, Enabled = true, AlertType = AlertType.Cpu, };

            var result = (PartialViewResult)_controller.UpdateAlert(model);
            var resultModel = (AlertRules)result.Model;

            Assert.That(resultModel.Errors.ContainsKey(AlertRules.PercentageErrorKey), Is.True);
            Assert.That(resultModel.Errors.ContainsKey(AlertRules.ThresholdErrorKey), Is.True);

            Settings.Verify(x => x.SaveSettings(It.IsAny<AlertSettingsDto>()), Times.Never);
            Settings.Verify(x => x.GetSettings(), Times.Never);
            Settings.Verify(x => x.Delete(It.IsAny<AlertSettingsDto>()), Times.Never);
        }