public IActionResult GetNewReadings([FromQuery] DateTime newerThan, [FromQuery] int measurementId, [FromQuery] int rowCount)
        {
            //if (!IsDeviceKeyValid())
            //{
            //    return BadRequest();
            //}

            var query = new ReadingsQuery {
                MeasurementId = measurementId, NewerThan = newerThan, PageSize = int.MaxValue
            };
            var results = _dataContext.GetReadings(query);

            return(Content(JsonConvert.SerializeObject(results), "application/json"));
        }
Ejemplo n.º 2
0
        public IActionResult Index(int id, int page = 1)
        {
            var model = new DataViewModel();

            model.Measurement = _context.Measurements
                                .Include(m => m.SensorRoles)
                                .Include(m => m.Calculators)
                                .FirstOrDefault(m => m.Id == id);

            var query = new ReadingsQuery {
                MeasurementId = id, Page = page, PageSize = 10
            };

            model.Data   = _context.GetReadingsPaged(query);
            model.Labels = _calcProvider.GetTypes()
                           .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null)
                           .Select(t => new
            {
                Label = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().DisplayLabel,
                Name  = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name
            })
                           .ToDictionary(k => k.Name, e => e.Label ?? e.Name);
            return(View(model));
        }
Ejemplo n.º 3
0
        public PagedResult <KeyValuePair <DateTime, IList <ReadingViewModel> > > GetReadingsPaged(ReadingsQuery query)
        {
            var datesPaged = Readings.Where(r => r.Measurement.Id == query.MeasurementId &&
                                            (query.FromTime == null || r.ReadingTime >= query.FromTime) &&
                                            (query.ToTime == null || r.ReadingTime <= query.ToTime) &&
                                            (query.NewerThan == null || r.ReadingTime > query.NewerThan))
                             .OrderByIf(r => r.ReadingTime, () => query.Ascending)
                             .Select(r => r.ReadingTime)
                             .Distinct()
                             .GetPaged(query.Page, query.PageSize);
            var dates = datesPaged.Results;

            // Make sure readings get SensorRole and Calculator loaded automatically
            // It's practically caching them for grouping query
            var measurement = Measurements.Include(m => m.SensorRoles)
                              .Include(m => m.Calculators)
                              .FirstOrDefault(m => m.Id == query.MeasurementId);

            var grouped = Readings.Where(r =>
                                         (r.Measurement.Id == query.MeasurementId) &&
                                         (dates == null || dates.Contains(r.ReadingTime))
                                         )
                          .OrderByIf(r => r.ReadingTime, () => query.Ascending)
                          .ToList()
                          .AsQueryable()
                          .ProjectTo <ReadingViewModel>()
                          .GroupBy(r => r.ReadingTime)
                          .Select(g => new KeyValuePair <DateTime, IList <ReadingViewModel> > (
                                      g.Key,
                                      g.OrderBy(r => r.Name).ToList()
                                      ))
                          .ToList();

            var result = new PagedResult <KeyValuePair <DateTime, IList <ReadingViewModel> > >();

            result.CurrentPage = datesPaged.CurrentPage;
            result.PageCount   = datesPaged.PageCount;
            result.PageSize    = datesPaged.PageSize;
            result.RowCount    = datesPaged.RowCount;
            result.Results     = grouped;

            return(result);
        }
Ejemplo n.º 4
0
 public IList <KeyValuePair <DateTime, IList <ReadingViewModel> > > GetReadings(ReadingsQuery query)
 {
     return(GetReadingsPaged(query).Results);
 }
Ejemplo n.º 5
0
        public async Task <IActionResult> Index(int?measurementId)
        {
            _pageContext.Title      = "Home";
            _pageContext.ActiveMenu = "Home";

            if (!User.Identity.IsAuthenticated)
            {
                return(View("IndexPublic"));
            }

            if (!User.IsInRole("Administrator") && !User.IsInRole("PowerUser"))
            {
                return(View("IndexGuest"));
            }

            var model = new HomeViewModel();

            model.Measurement = await _dataContext.Measurements
                                .Include(m => m.SensorRoles)
                                .Include(m => m.Calculators)
                                .Where(m => measurementId == null || m.Id == measurementId)
                                .OrderByDescending(m => m.IsActive)
                                .FirstOrDefaultAsync();

            if (model.Measurement == null)
            {
                return(View("IndexEmpty"));
            }

            var query = new ReadingsQuery {
                MeasurementId = model.Measurement.Id, PageSize = 10
            };

            model.Readings = _dataContext.GetReadings(query);

            query = new ReadingsQuery {
                MeasurementId = model.Measurement.Id, Ascending = true, PageSize = int.MaxValue
            };
            model.ChartData = _dataContext.GetReadings(query);

            var showOnChart = _calcProvider.GetTypes()
                              .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null)
                              .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().ShowOnChart)
                              .Select(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name)
                              .ToList();

            showOnChart.AddRange(model.Measurement.SensorRoles.Select(r => r.RoleName));
            model.CalculatorsOnChart = showOnChart.ToArray();

            var labels = _calcProvider.GetTypes()
                         .Where(t => t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>() != null)
                         .Select(t => new
            {
                Label = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().DisplayLabel,
                Name  = t.GetTypeInfo().GetCustomAttribute <CalculatorAttribute>().Name
            })
                         .ToDictionary(k => k.Name, e => e.Label ?? e.Name);

            model.Labels       = labels;
            model.Calculators  = _calcProvider.GetCalculators();
            model.Statistics   = _dataContext.GetMeasurementStats(model.Measurement.Id);
            model.Measurements = _dataContext.Measurements
                                 .OrderByDescending(m => m.IsActive)
                                 .ThenBy(m => m.Name)
                                 .Select(m => new SelectListItem
            {
                Text     = m.Name,
                Value    = m.Id.ToString(),
                Selected = (m.Id == model.Measurement.Id)
            })
                                 .ToList();
            return(View(model));
        }