public ActionResult Metric_Select([DataSourceRequest] DataSourceRequest request)
        {
            IList <MetricDTO> list_metric_dto = _iMetricRepository.GetMetrics();
            DataSourceResult  result          = list_metric_dto.ToDataSourceResult(request);

            return(new JsonResult()
            {
                Data = result,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            });

            //   return Json(result, JsonRequestBehavior.AllowGet);
        }
 /// <summary>
 /// Get the metrics for a run
 /// </summary>
 /// <param name="runId"></param>
 /// <returns></returns>
 public List <Metric> GetMetrics(Guid runId)
 {
     return(metricRepository.GetMetrics(runId));
 }
        private async Task <IActionResult> GetMetrics(
            IMetricRepository metricRepository,
            DateTime start,
            DateTime?end        = null,
            TimeGroup timeGroup = TimeGroup.None)
        {
            if (TimeSpanTooLarge(start, end, timeGroup))
            {
                return(BadRequest("Time range is too large for the current resolution"));
            }

            if (end.HasValue && end.Value < start)
            {
                return(BadRequest("Time range is negative (To is smaller than From)"));
            }

            var user = await userManager.GetUserAsync(User);

            var raspberryPiId = await GetRaspberryPiId(user);

            if (!raspberryPiId.HasValue)
            {
                return(NotFound("Either the user does not exist or the user does not have a raspberry pi"));
            }

            var settings = await dbSettingsRepository.GetSettingsAsync(user.Id) ?? new Settings();

            var dbMetrics = (await metricRepository.GetMetrics(raspberryPiId.Value, start, end, settings.TimeZoneId))
                            .ToList();

            if (!dbMetrics.Any())
            {
                return(Ok(new
                {
                    QueryTimestamp = DateTime.UtcNow.AddSeconds(1).ToString("o"),
                    Timestamps = new string[0],
                    Usage = new int[0],
                    Solar = new int[0],
                    Redelivery = new int[0],
                    Intake = new int[0],
                }));
            }

            var format = "";
            List <OutMetricModel> metrics;

            switch (timeGroup)
            {
            case TimeGroup.TenSeconds:
            case TimeGroup.Minutes:
                format = "HH:mm:ss";

                metrics = dbMetrics
                          .Select(d => new OutMetricModel
                {
                    DateTime   = d.Created,
                    Gas        = d.UsageGasNow,
                    Intake     = d.UsageNow,
                    Redelivery = d.RedeliveryNow,
                    Solar      = d.SolarNow,
                    Usage      = d.UsageNow + d.SolarNow - d.RedeliveryNow
                })
                          .ToList();
                break;

            case TimeGroup.Hours:
                format = "MM-dd HH:mm";

                metrics = dbMetrics
                          .Select(d => new OutMetricModel
                {
                    DateTime   = d.Created,
                    Gas        = d.UsageGasNow,
                    Intake     = d.UsageNow,
                    Redelivery = d.RedeliveryNow,
                    Solar      = d.SolarNow,
                    Usage      = d.UsageNow + d.SolarNow - d.RedeliveryNow
                })
                          .ToList();
                break;

            case TimeGroup.Days:
                metrics = dbMetrics
                          .GroupBy(m => m.Created.Date)
                          .Select(m => new
                {
                    First         = m.First(),
                    Last          = m.Last(),
                    SolarTotalMin = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Min(a => a.SolarTotal) : 0,
                    SolarTotalMax = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Max(a => a.SolarTotal) : 0
                })
                          .SelectWithNextOrDefault((c, n) =>
                {
                    var current        = c.First;
                    var next           = n?.First ?? c.Last;
                    current.SolarTotal = c.SolarTotalMin > 0 ? c.SolarTotalMin : n?.SolarTotalMin ?? 0;
                    next.SolarTotal    = n?.SolarTotalMin > 0 ? n.SolarTotalMin : c.SolarTotalMax;

                    return(current.ToOutMetricModel(next, settings));
                })
                          .ToList();

                format = user.Settings.ShowDayName ? "yyyy-MM-dd (dddd)" : "yyyy-MM-dd";
                break;

            // case TimeGroup.Weeks:
            // break;
            case TimeGroup.Months:
                metrics = dbMetrics
                          .GroupBy(m => new
                {
                    m.Created.Date.Year,
                    m.Created.Month
                })
                          .Select(m => new
                {
                    First         = m.First(),
                    Last          = m.Last(),
                    SolarTotalMin = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Min(a => a.SolarTotal) : 0,
                    SolarTotalMax = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Max(a => a.SolarTotal) : 0
                })
                          .SelectWithNextOrDefault((c, n) =>
                {
                    var current        = c.First;
                    var next           = n?.First ?? c.Last;
                    current.SolarTotal = c.SolarTotalMin > 0 ? c.SolarTotalMin : n?.SolarTotalMin ?? 0;
                    next.SolarTotal    = n?.SolarTotalMin > 0 ? n.SolarTotalMin : c.SolarTotalMax;

                    return(current.ToOutMetricModel(next, settings));
                })
                          .ToList();

                format = "yyyy-MM";
                break;

            case TimeGroup.Years:
                metrics = dbMetrics
                          .GroupBy(m => new
                {
                    m.Created.Date.Year
                })
                          .Select(m => new
                {
                    First         = m.First(),
                    Last          = m.Last(),
                    SolarTotalMin = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Min(a => a.SolarTotal) : 0,
                    SolarTotalMax = m.Any(a => a.SolarTotal > 0) ? m.Where(a => a.SolarTotal > 0).Max(a => a.SolarTotal) : 0
                })
                          .SelectWithNextOrDefault((c, n) =>
                {
                    var current        = c.First;
                    var next           = n?.First ?? c.Last;
                    current.SolarTotal = c.SolarTotalMin > 0 ? c.SolarTotalMin : n?.SolarTotalMin ?? 0;
                    next.SolarTotal    = n?.SolarTotalMin > 0 ? n.SolarTotalMin : c.SolarTotalMax;

                    return(current.ToOutMetricModel(next, settings));
                })
                          .ToList();

                format = "yyyy";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(timeGroup), timeGroup, null);
            }

            var cultureInfo     = new System.Globalization.CultureInfo("nl-NL");
            var timestamps      = metrics.Select(m => m.DateTime.ToString(format, cultureInfo)).ToList();
            var usageList       = metrics.Select(m => m.Usage.DivideByThousand()).ToList();
            var intakeList      = metrics.Select(m => m.Intake.DivideByThousand()).ToList();
            var solarList       = metrics.Select(m => m.Solar.DivideByThousand()).ToList();
            var redeliveryList  = metrics.Select(m => m.Redelivery.DivideByThousand()).ToList();
            var gasList         = metrics.Select(m => m.Gas.DivideByThousand()).ToList();
            var usageCosts      = metrics.Select(m => m.UsageCost).ToList();
            var intakeCosts     = metrics.Select(m => m.IntakeCost).ToList();
            var redeliveryCosts = metrics.Select(m => m.RedeliveryCost).ToList();
            var gasCosts        = metrics.Select(m => m.GasCost).ToList();

            var lastDateTime = metrics
                               .OrderBy(m => m.DateTime)
                               .Select(m => m.DateTime)
                               .LastOrDefault();

            lastDateTime = lastDateTime.ConvertToUtc(settings.TimeZoneId);

            return(Ok(new
            {
                QueryTimestamp = lastDateTime.AddSeconds(1).ToString("o"),
                Timestamps = timestamps,
                Usage = usageList,
                Solar = solarList,
                Redelivery = redeliveryList,
                Intake = intakeList,
                Gas = gasList,
                UsageCosts = usageCosts,
                IntakeCosts = intakeCosts,
                RedeliveryCosts = redeliveryCosts,
                GasCosts = gasCosts
            }));
        }
        public ActionResult Get_Metric2()
        {
            IList <MetricDTO> list_metric_dto = _iMetricRepository.GetMetrics();

            return(Json(new SelectList(list_metric_dto, "Id", "Metric_Name"), JsonRequestBehavior.AllowGet));
        }
Example #5
0
        public ActionResult Get_Metric()
        {
            IList <MetricDTO> list_metric_dto = _iMetricRepository.GetMetrics();

            return(Json(list_metric_dto, JsonRequestBehavior.AllowGet));
        }