public ActionResult WormyApplications(int?top, Guid?componentTypeId, ReportPeriod?period) { var model = new WormyApplicationsModel() { Top = top ?? 10, ComponentTypeId = componentTypeId }; if (period == null) { period = ReportPeriod.Day; } model.PeriodRange = ReportPeriodHelper.GetRange(period.Value); model.Load(CurrentUser.AccountId, CurrentAccountDbContext); return(View(model)); }
public void LoadData(Guid accountId, AccountDbContext accountDbContext) { if (ComponentId.HasValue) { Component = accountDbContext.GetComponentRepository().GetById(ComponentId.Value); } var periodRange = ReportPeriodHelper.GetRange(Period); ToTime = periodRange.To; FromTime = periodRange.From; // вычисляем интервалы if (Period == ReportPeriod.Hour) { TimeStep = TimeSpan.FromMinutes(5); } else if (Period == ReportPeriod.Day) { TimeStep = TimeSpan.FromHours(1); } else if (Period == ReportPeriod.Week) { TimeStep = TimeSpan.FromHours(4); } else if (Period == ReportPeriod.Month) { TimeStep = TimeSpan.FromDays(1); } else { throw new Exception("Неизвестное значение Period: " + Period); } // создаем интервалы статистики var items = new List <SimpleBarCharModel.DataItem>(); var date = FromTime; int itemIndex = 0; while (date < ToTime) { var item = new SimpleBarCharModel.DataItem() { Index = itemIndex, FromTime = date, ToTime = date + TimeStep }; date = date + TimeStep; itemIndex++; items.Add(item); } // загружаем события var events = accountDbContext.GetEventRepository().GetErrorsByPeriod(FromTime, ToTime); if (ComponentId.HasValue) { events = events.Where(x => x.OwnerId == ComponentId); } if (ComponentTypeId.HasValue) { var componentIdArray = accountDbContext.GetComponentRepository() .QueryAll() .Where(x => x.ComponentTypeId == ComponentTypeId.Value) .Select(x => x.Id) .ToArray(); events = events.Where(x => componentIdArray.Contains(x.OwnerId)); } if (Mode == ShowMode.NotProcessed) { events = events.Where(t => t.EventType.DefectId == null); } var eventsArray = events.OrderBy(x => x.StartDate).ToArray(); // собираем статистику var intervals = new List <Event> [items.Count]; foreach (var item in items) { intervals[item.Index] = new List <Event>(); } foreach (var eventObj in eventsArray) { bool hasInterval = false; foreach (var timeInterval in items) { // если интервал левее события if (timeInterval.ToTime <= eventObj.StartDate) { continue; } // если интервал правее события if (timeInterval.FromTime > eventObj.EndDate) { break; } hasInterval = true; intervals[timeInterval.Index].Add(eventObj); } if (!hasInterval) { MvcApplication.ComponentControl.AddApplicationError( "Не удалось найти интервал статистики для события"); } } foreach (var item in items) { var intervalEvents = intervals[item.Index]; item.Value = intervalEvents.GroupBy(x => x.EventTypeId).Count(); } BarCharModel = new SimpleBarCharModel(items.ToArray()); // EventTypeDatas EventTypeDatas = new List <EventTypeData>(); var eventTypeGroups = eventsArray.GroupBy(x => x.EventTypeId); foreach (var eventTypeGroup in eventTypeGroups) { Guid eventTypeId = eventTypeGroup.Key; var eventTypeData = new EventTypeData(); var eventType = accountDbContext.EventTypes.Find(eventTypeId); if (eventType == null) { MvcApplication.ComponentControl.AddApplicationError( "Не удалось найти тип события " + eventTypeId); continue; } eventTypeData.EventType = eventType; eventTypeData.Events = eventTypeGroup.ToArray(); EventTypeDatas.Add(eventTypeData); eventTypeData.InitIntervals(FromTime, ToTime); } // сортируем типы if (Order == SortOrder.ByCount) { EventTypeDatas = EventTypeDatas.OrderByDescending(x => x.SausageModel.Count).ToList(); } else if (Order == SortOrder.ByDuration) { EventTypeDatas = EventTypeDatas.OrderByDescending(x => x.SausageModel.Coverage).ToList(); } }