Ejemplo n.º 1
0
        public async Task EditReportElement(ReportElementDto reportElementDTO)
        {
            ReportElement reportElement = mapper.Map <ReportElementDto, ReportElement>(reportElementDTO);
            await unitOfWork.ReportElementRepo.Update(reportElement);

            unitOfWork.Save();
        }
Ejemplo n.º 2
0
        public async Task <ReportElementDto> GetWordCloudById(int ReportElementId)
        {
            ReportElement reportElement = await unitOfWork.ReportElementRepo.GetById(ReportElementId);

            if (reportElement == null)
            {
                return(new ReportElementDto {
                    Id = ReportElementId, IsCorrect = false
                });
            }
            DateTime date = new DateTime(1970, 1, 1, 0, 0, 0);

            if (reportElement.Hours != 0)
            {
                date = DateTime.Now.AddHours(-(int)reportElement.Hours);
            }
            IEnumerable <History> histories = await unitOfWork.HistoryRepo.GetHistoriesBySensorIdAndDate(reportElement.SensorId.Value, date);

            if (!histories.Any())
            {
                return new ReportElementDto {
                           Id = ReportElementId, IsCorrect = false
                }
            }
            ;

            ReportElementDto wordCloud = mapper.Map <ReportElement, ReportElementDto>(reportElement);

            wordCloud.Values = new List <dynamic>();

            foreach (History history in histories)
            {
                switch (wordCloud.MeasurementType)
                {
                case MeasurementType.Int when history.IntValue.HasValue:
                    wordCloud.Values.Add(history.IntValue);
                    break;

                case MeasurementType.Double when history.DoubleValue.HasValue:
                    wordCloud.Values.Add(history.DoubleValue);
                    break;

                case MeasurementType.Bool when history.BoolValue.HasValue:
                    wordCloud.Values.Add(history.BoolValue);
                    break;

                case MeasurementType.String when !String.IsNullOrEmpty(history.StringValue):
                    wordCloud.Values.Add(history.StringValue);
                    break;
                }
            }
            if (!wordCloud.Values.Any())
            {
                return new ReportElementDto {
                           Id = ReportElementId, IsCorrect = false
                }
            }
            ;
            return(wordCloud);
        }
        public async Task <IViewComponentResult> InvokeAsync(int reportElementId)
        {
            ReportElementDto columnRangeDTO = await _reportElementManager.GetColumnRangeById(reportElementId);

            ReportElementViewModel model = _mapper.Map <ReportElementDto, ReportElementViewModel>(columnRangeDTO);

            return(View(model));
        }
        public async Task <IViewComponentResult> InvokeAsync(int reportElementId)
        {
            ReportElementDto wordCloud = await _reportElementManager.GetWordCloudById(reportElementId);

            ReportElementViewModel result = _mapper.Map <ReportElementDto, ReportElementViewModel>(wordCloud);

            return(View("Wordcloud", result));
        }
Ejemplo n.º 5
0
        public async Task <ReportElementDto> GetOnOffById(int ReportElementId)
        {
            ReportElement reportElement = await unitOfWork.ReportElementRepo.GetById(ReportElementId);

            ReportElementDto onOff = mapper.Map <ReportElement, ReportElementDto>(reportElement);

            return(onOff);
        }
Ejemplo n.º 6
0
        public async Task <IViewComponentResult> InvokeAsync(int reportElementId)
        {
            ReportElementDto onOff = await _reportElementManager.GetOnOffById(reportElementId);

            ReportElementViewModel result = _mapper.Map <ReportElementDto, ReportElementViewModel>(onOff);

            return(View("OnOff", result));
        }
        public async Task <IActionResult> EditReportElement(EditReportElementViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            ReportElementDto reportElement = _mapper.Map <EditReportElementViewModel, ReportElementDto>(model);
            await _reportElementManager.EditReportElement(reportElement);

            return(RedirectToAction("Detail", "Dashboard", new { id = model.DashboardId }));
        }
Ejemplo n.º 8
0
        public async Task <bool> CreateReportElement(ReportElementDto reportElementDto, string userId)
        {
            UserId = userId;
            var reportElements = await unitOfWork.ReportElementRepo.GetAll();

            reportElements = reportElements.Where(r => r.DashboardId == reportElementDto.DashboardId);
            if (reportElements.Any())
            {
                var  el          = reportElements.OrderByDescending(r => r.Y).First();
                var  maxElements = reportElements.Where(e => e.Y == el.Y);
                bool rightPos    = false;
                int  totalWidth  = 0;
                foreach (var element in maxElements)
                {
                    if (element.X < 5)
                    {
                        rightPos = true;
                    }
                    totalWidth += element.Width;
                }

                ReportElement reportElement = mapper.Map <ReportElementDto, ReportElement>(reportElementDto);
                reportElement.Height = 6;
                reportElement.Width  = 4;
                if (rightPos)
                {
                    reportElement.X = totalWidth;
                    reportElement.Y = el.Y;
                }
                else
                {
                    reportElement.X = 0;
                    reportElement.Y = el.Y;
                }

                await unitOfWork.ReportElementRepo.Insert(reportElement);

                unitOfWork.Save();
                return(true);
            }
            else
            {
                ReportElement reportElement = mapper.Map <ReportElementDto, ReportElement>(reportElementDto);
                reportElement.Height = 6;
                reportElement.Width  = 4;
                reportElement.X      = 0;
                reportElement.Y      = 0;
                await unitOfWork.ReportElementRepo.Insert(reportElement);

                unitOfWork.Save();
                return(true);
            }
        }
        public async Task <IActionResult> CreateReportElement(CreateReportElementViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string           userId        = _userManager.GetUserId(User);
            ReportElementDto reportElement = _mapper.Map <CreateReportElementViewModel, ReportElementDto>(model);
            await _reportElementManager.CreateReportElement(reportElement, userId);

            return(RedirectToAction("Detail", "Dashboard", new { id = model.DashboardId }));
        }
Ejemplo n.º 10
0
        public async Task <ReportElementDto> GetDataForTimeSeries(int reportElementId)
        {
            var reportElement = await unitOfWork.ReportElementRepo.GetById(reportElementId);

            if (reportElement == null)
            {
                return(null);
            }

            DateTimeOffset date = new DateTimeOffset(1970, 1, 1, 0, 0, 0,
                                                     new TimeSpan(0, 0, 0));

            if (reportElement.Hours != 0)
            {
                date = DateTimeOffset.Now.AddHours(-(int)reportElement.Hours);
            }

            var histories = await unitOfWork.HistoryRepo.GetHistoriesBySensorIdAndDate(reportElement.SensorId.Value, date);

            if (histories.Count() == 0 || histories == null)
            {
                return new ReportElementDto
                       {
                           Id         = reportElementId,
                           SensorName = reportElement.Sensor.Name,
                           IsCorrect  = false
                       }
            }
            ;

            var milliseconds = GetMilliseconds(histories).ToList();
            var values       = GetValues(histories).ToList();

            ReportElementDto data = mapper.Map <ReportElement, ReportElementDto>(reportElement);

            data.Milliseconds = milliseconds;
            data.Values       = values;

            return(data);
        }
Ejemplo n.º 11
0
        public async Task <IViewComponentResult> InvokeAsync(int reportElementId, string userid)
        {
            ReportElementDto element = await _reportElementManager.GetStatusReport(reportElementId, userid);

            return(View(_mapper.Map <ReportElementDto, ReportElementViewModel>(element)));
        }
Ejemplo n.º 12
0
        protected override void Initialize()
        {
            base.Initialize();
            _mockHistoryManager = new Mock <IHistoryManager>();

            _manager = new ReportElementManager(
                _mockHistoryManager.Object,
                mockUnitOfWork.Object,
                mockMapper.Object);

            _reportElement = new ReportElement
            {
                Id     = 1,
                Hours  = Domain.Core.Model.Enums.ReportElementHours.Hour168,
                Sensor = new Sensor()
                {
                    Id = 1, Name = "Sensor1"
                },
                SensorId = 1
            };

            reportElements = new List <ReportElement>()
            {
                new ReportElement {
                    Id     = 1,
                    Hours  = Domain.Core.Model.Enums.ReportElementHours.Hour168,
                    Sensor = new Sensor()
                    {
                        Id = 1, Name = "Sensor1"
                    },
                    Dashboard = new Dashboard()
                    {
                        Id = 1, Name = "Dashboard1"
                    },
                    SensorId = 1
                },
                new ReportElement {
                    Id     = 2,
                    Hours  = Domain.Core.Model.Enums.ReportElementHours.Hour168,
                    Sensor = new Sensor()
                    {
                        Id = 2, Name = "Sensor2"
                    },
                    Dashboard = new Dashboard()
                    {
                        Id = 1, Name = "Dashboard1"
                    },
                    Type     = ReportElementType.Wordcloud,
                    SensorId = 2
                }
            };

            histories = new List <History>()
            {
                new History {
                    Id     = 1,
                    Date   = DateTimeOffset.Now.AddDays(-(int)_reportElement.Hours),
                    Sensor = new Sensor()
                    {
                        Id = 5, Name = "Sensor5", SensorType = new SensorType()
                        {
                            MeasurementType = Domain.Core.Model.Enums.MeasurementType.Bool
                        }
                    },
                    BoolValue = true
                },
                new History {
                    Id     = 2,
                    Date   = new DateTimeOffset(),
                    Sensor = new Sensor()
                    {
                        Id = 1, Name = "Sensor1", SensorType = new SensorType()
                        {
                            MeasurementType = Domain.Core.Model.Enums.MeasurementType.Int
                        }
                    },
                    IntValue = 2
                },
                new History {
                    Id     = 3,
                    Date   = new DateTimeOffset(),
                    Sensor = new Sensor()
                    {
                        Id = 2, Name = "Sensor2", SensorType = new SensorType()
                        {
                            MeasurementType = Domain.Core.Model.Enums.MeasurementType.Int
                        }
                    },
                    IntValue = 3
                },
                new History {
                    Id     = 4,
                    Date   = DateTimeOffset.Now.AddDays(-(int)_reportElement.Hours),
                    Sensor = new Sensor()
                    {
                        Id = 3, Name = "Sensor3", SensorType = new SensorType()
                        {
                            MeasurementType = Domain.Core.Model.Enums.MeasurementType.Int
                        }
                    },
                    IntValue = 3
                }
            };

            mockUnitOfWork.Setup(u => u.ReportElementRepo
                                 .GetById(It.IsAny <int>()))
            .Returns((int i) =>
                     Task.FromResult(reportElements.Where(x => x.Id == i).FirstOrDefault()));

            mockUnitOfWork.Setup(u => u.HistoryRepo
                                 .GetHistoriesBySensorIdAndDate(It.IsAny <int>(), It.IsAny <DateTimeOffset>()))
            .Returns((int i, DateTimeOffset date) =>
                     Task.FromResult(histories.Where(x => x.Id == i && x.Date == date)));

            mockUnitOfWork.Setup(u => u.HistoryRepo
                                 .GetAvgSensorsValuesPerDays(It.IsAny <int>(), It.IsAny <DateTime>(), It.IsAny <DateTime>()))
            .Returns(Task.FromResult <IEnumerable <AvgSensorValuePerDay> >(new List <AvgSensorValuePerDay> {
                _avgSensorValuePerDay
            }));

            _reportElementDto = new ReportElementDto
            {
                Id            = 1,
                Hours         = Domain.Core.Model.Enums.ReportElementHours.Hour168,
                DashboardId   = 1,
                SensorId      = 1,
                Type          = ReportElementType.Wordcloud,
                IsActive      = true,
                IsLocked      = true,
                DashboardName = "DashboardTest"
            };

            _gaugeDto = new GaugeDto
            {
                Id              = 1,
                Hours           = Domain.Core.Model.Enums.ReportElementHours.Hour168,
                SensorId        = 1,
                SensorName      = "Sensor1",
                MeasurementName = "*C"
            };

            _historyDto = new HistoryDto
            {
                Id       = 3,
                Date     = new DateTimeOffset(),
                IntValue = 3
            };

            _avgSensorValuePerDay = new AvgSensorValuePerDay()
            {
                WeekDay  = DateTime.Now,
                AvgValue = 3
            };

            _boolValuePercentagePerHour = new BoolValuePercentagePerHour()
            {
                DayDate        = DateTime.Now,
                HourTime       = DateTime.Now.Hour,
                TrueCount      = 1,
                TrueFalseCount = 2,
                TruePercentage = 50
            };
        }
Ejemplo n.º 13
0
        public async Task <ReportElementDto> GetStatusReport(int ReportElementId, string userid)
        {
            UserId = userid;
            ReportElement reportElement = await unitOfWork.ReportElementRepo.GetById(ReportElementId);

            if (reportElement == null)
            {
                return new ReportElementDto {
                           IsCorrect = false, Message = "Invalid report element"
                }
            }
            ;

            IEnumerable <Sensor> sensors = await unitOfWork.SensorRepo.GetAllSensorsByUserId(UserId);

            ReportElementDto statusReport = new ReportElementDto
            {
                Dates  = new List <string>(),
                Values = new List <dynamic>()
            };

            foreach (Sensor sensor in sensors)
            {
                History history = unitOfWork.HistoryRepo.GetLastHistoryBySensorId(sensor.Id);

                if (history == null)
                {
                    continue;
                }
                dynamic value = null;
                switch (sensor.SensorType.MeasurementType)
                {
                case MeasurementType.Int:
                    value = history.IntValue.GetValueOrDefault();
                    break;

                case MeasurementType.Bool:
                    value = history.BoolValue.GetValueOrDefault();
                    if (value == true)
                    {
                        value = "Active";
                    }
                    else
                    {
                        value = "Inactive";
                    }

                    break;

                case MeasurementType.Double:
                    value = Math.Round(history.DoubleValue.GetValueOrDefault(), 2);
                    break;

                case MeasurementType.String:
                    value = history.StringValue;
                    break;

                default:
                    return(new ReportElementDto {
                        Id = ReportElementId, IsCorrect = false, Message = "Incorrect sensor type for this element"
                    });
                }
                statusReport.Dates.Add(sensor.Name);
                statusReport.Values.Add(value);
            }

            return(statusReport);
        }
Ejemplo n.º 14
0
        public async Task <ReportElementDto> GetColumnRangeById(int ReportElementId)
        {
            ReportElement reportElement = await unitOfWork.ReportElementRepo.GetById(ReportElementId);

            if (reportElement == null)
            {
                return new ReportElementDto {
                           IsCorrect = false, Message = "Invalid report element"
                }
            }
            ;
            DateTime date = new DateTime(1970, 1, 1, 0, 0, 0);

            if (reportElement.Hours != 0)
            {
                date = DateTime.Now.AddHours(-(int)reportElement.Hours);
            }
            IEnumerable <History> histories = await unitOfWork.HistoryRepo.GetHistoriesBySensorIdAndDate(reportElement.SensorId.Value, date);

            if (!histories.Any())
            {
                int    hours = (int)reportElement.Hours;
                string strhours;
                if (hours == 0)
                {
                    strhours = "all days";
                }
                else if (hours == 1)
                {
                    strhours = "1 hour";
                }
                else if (hours <= 12)
                {
                    strhours = $"{hours} hours";
                }
                else if (hours / 24 == 1)
                {
                    strhours = "1 day";
                }
                else
                {
                    strhours = $"{hours / 24} days";
                }
                return(new ReportElementDto {
                    Id = ReportElementId, IsCorrect = false, Message = $"No histories in this report element per {strhours}"
                });
            }

            ReportElementDto columnRange = mapper.Map <Sensor, ReportElementDto>(reportElement.Sensor);

            columnRange.Id        = ReportElementId;
            columnRange.Hours     = reportElement.Hours;
            columnRange.Dates     = new List <string>();
            columnRange.MinValues = new List <dynamic>();
            columnRange.MaxValues = new List <dynamic>();

            IEnumerable <dynamic> values = null;

            switch (columnRange.MeasurementType)
            {
            case MeasurementType.Int:
                if ((int)reportElement.Hours == 1)
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Minute).Select(p => new
                    {
                        Min  = p.Min(g => g.IntValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.IntValue.GetValueOrDefault()),
                        Date = p.Key.ToString()
                    }).ToList();
                    break;
                }
                else if ((int)reportElement.Hours > 1 && (int)reportElement.Hours <= 24)
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Hour).Select(p => new
                    {
                        Min  = p.Min(g => g.IntValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.IntValue.GetValueOrDefault()),
                        Date = p.Key.ToString()
                    }).ToList();
                    break;
                }
                else
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Date).Select(p => new
                    {
                        Min  = p.Min(g => g.IntValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.IntValue.GetValueOrDefault()),
                        Date = p.Key.ToString("d")
                    }).ToList();
                    break;
                }

            case MeasurementType.Double:

                if ((int)reportElement.Hours == 1)
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Minute).Select(p => new
                    {
                        Min  = p.Min(g => g.DoubleValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.DoubleValue.GetValueOrDefault()),
                        Date = p.Key.ToString()
                    }).ToList();
                    break;
                }
                else if ((int)reportElement.Hours > 1 && (int)reportElement.Hours <= 24)
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Hour).Select(p => new
                    {
                        Min  = p.Min(g => g.DoubleValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.DoubleValue.GetValueOrDefault()),
                        Date = p.Key.ToString()
                    }).ToList();
                    break;
                }
                else
                {
                    values = histories.OrderBy(p => p.Date.LocalDateTime).GroupBy(p => p.Date.LocalDateTime.Date).Select(p => new
                    {
                        Min  = p.Min(g => g.DoubleValue.GetValueOrDefault()),
                        Max  = p.Max(g => g.DoubleValue.GetValueOrDefault()),
                        Date = p.Key.ToString("d")
                    }).ToList();
                    break;
                }

            default:
                return(new ReportElementDto {
                    Id = ReportElementId, IsCorrect = false, Message = "Incorrect sensor type for this element"
                });
            }

            List <dynamic> items = values.ToList();

            for (int i = 0; i < items.Count(); i++)
            {
                if (values.Count() == 1 || i == 0)
                {
                    columnRange.Dates.Add(items[i].Date);
                    columnRange.MinValues.Add(items[i].Min);
                    columnRange.MaxValues.Add(items[i].Max);
                    continue;
                }
                if (i > 0 && !items[i].Equals(items[i - 1]))
                {
                    columnRange.Dates.Add(items[i].Date);
                    columnRange.MinValues.Add(items[i].Min);
                    columnRange.MaxValues.Add(items[i].Max);
                }
            }
            return(columnRange);
        }