public IComponent Export()
        {
            using (_logger.Scope("Exporting active entities to component."))
            {
                var rootComponent = _factory.Create();

                // Apply the active entities to the component tree.
                var activeEvents = _table
                                   .GetActiveEntities <EventEntity>()
                                   .ToList()
                                   .Where(e =>
                                          _table
                                          .GetChildEntities <MessageEntity, EventEntity>(e)
                                          .ToList()
                                          .Any())
                                   .ToList();

                _logger.LogInformation("Found {EventCount} active events with messages.", activeEvents.Count);

                var activeIncidentGroups = activeEvents
                                           .SelectMany(e =>
                                                       _table
                                                       .GetChildEntities <IncidentGroupEntity, EventEntity>(e)
                                                       .Where(i => i.IsActive)
                                                       .ToList())
                                           .ToList();

                _logger.LogInformation("Found {GroupCount} active incident groups linked to active events with messages.", activeIncidentGroups.Count);

                var activeEntities = activeIncidentGroups
                                     .Concat <IComponentAffectingEntity>(activeEvents)
                                     // Only apply entities with a non-Up status.
                                     .Where(e => e.AffectedComponentStatus != (int)ComponentStatus.Up)
                                     // If multiple events are affecting a single region, the event with the highest severity should affect the component.
                                     .GroupBy(e => e.AffectedComponentPath)
                                     .Select(g => g.OrderByDescending(e => e.AffectedComponentStatus).First())
                                     .ToList();

                _logger.LogInformation("Active entities affect {PathCount} distinct subcomponents.", activeEntities.Count);
                foreach (var activeEntity in activeEntities)
                {
                    using (_logger.Scope("Applying active entity affecting {AffectedComponentPath} of severity {AffectedComponentStatus} at {StartTime} to root component",
                                         activeEntity.AffectedComponentPath, (ComponentStatus)activeEntity.AffectedComponentStatus, activeEntity.StartTime))
                    {
                        var currentComponent = rootComponent.GetByPath(activeEntity.AffectedComponentPath);

                        if (currentComponent == null)
                        {
                            throw new InvalidOperationException($"Couldn't find component with path {activeEntity.AffectedComponentPath} corresponding to active entities.");
                        }

                        currentComponent.Status = (ComponentStatus)activeEntity.AffectedComponentStatus;
                    }
                }

                return(rootComponent);
            }
        }
 public async Task UpdateAllAsync(DateTime cursor)
 {
     using (_logger.Scope("Updating active events."))
     {
         var activeEvents = _table.GetActiveEntities <EventEntity>().ToList();
         _logger.LogInformation("Updating {ActiveEventsCount} active events.", activeEvents.Count());
         foreach (var activeEvent in activeEvents)
         {
             await _updater.UpdateAsync(activeEvent, cursor);
         }
     }
 }