public static IEnumerable<EventHandlerProgress> Calculate(
            Func<DbContext> createDbContext,
            Func<EventStoreDbContext> createEventStoreDbContext = null)
        {
            if (createDbContext == null)
            {
                throw new ArgumentNullException("createDbContext");
            }

            int count;

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            using (var db = createEventStoreDbContext.IfNotNull()
                                                     .Then(create => create())
                                                     .Else(() => new EventStoreDbContext()))
            {
                count = db.Events.Count();
            }

            if (count == 0)
            {
                return Enumerable.Empty<EventHandlerProgress>();
            }

            var now = Clock.Now();
            var progress = new List<EventHandlerProgress>();

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            using (var db = createDbContext())
            {
                var readModelInfos = db.Set<ReadModelInfo>().ToArray();
                
                readModelInfos
                    .ForEach(i =>
                    {
                        var eventsProcessed = i.InitialCatchupEndTime.HasValue
                            ? i.BatchTotalEvents - i.BatchRemainingEvents
                            : i.InitialCatchupEvents - i.BatchRemainingEvents;

                        long? timeTakenForProcessedEvents = null;
                        if (i.BatchStartTime.HasValue && i.InitialCatchupStartTime.HasValue)
                        {
                            timeTakenForProcessedEvents = i.InitialCatchupEndTime.HasValue
                                ? (now - i.BatchStartTime).Value.Ticks
                                : (now - i.InitialCatchupStartTime).Value.Ticks;
                        }

                        if (eventsProcessed == 0)
                        {
                            return;
                        }

                        var eventHandlerProgress = new EventHandlerProgress
                        {
                            Name = i.Name,
                            InitialCatchupEvents = i.InitialCatchupEvents,
                            TimeTakenForInitialCatchup = i.InitialCatchupStartTime.HasValue
                                ? (i.InitialCatchupEndTime.HasValue ? i.InitialCatchupEndTime : now) - i.InitialCatchupStartTime
                                : null,
                            TimeRemainingForCatchup = eventsProcessed != 0 && timeTakenForProcessedEvents.HasValue
                                ? (TimeSpan?) TimeSpan.FromTicks((long) (timeTakenForProcessedEvents*(i.BatchRemainingEvents/(decimal) eventsProcessed)))
                                : null,
                            EventsRemaining = i.BatchRemainingEvents,
                            PercentageCompleted = (1 - ((decimal) i.BatchRemainingEvents/count))*100,
                            LatencyInMilliseconds = i.LatencyInMilliseconds,
                            LastUpdated = i.LastUpdated,
                            CurrentAsOfEventId = i.CurrentAsOfEventId,
                            FailedOnEventId = i.FailedOnEventId,
                            Error = i.Error
                        };

                        progress.Add(eventHandlerProgress);
                    });
            }

            return progress;
        }
Beispiel #2
0
        public static IEnumerable <EventHandlerProgress> CalculateProgress(
            Func <DbContext> createReadModelDbContext,
            Func <EventStoreDbContext> createEventStoreDbContext = null)
        {
            if (createReadModelDbContext == null)
            {
                throw new ArgumentNullException(nameof(createReadModelDbContext));
            }

            createEventStoreDbContext = createEventStoreDbContext ??
                                        (() => Configuration.Current.EventStoreDbContext());

            int eventStoreCount;

            using (var db = createEventStoreDbContext())
            {
                eventStoreCount = db.Events.Count();
            }

            if (eventStoreCount == 0)
            {
                return(Enumerable.Empty <EventHandlerProgress>());
            }

            var now      = Clock.Now();
            var progress = new List <EventHandlerProgress>();

            ReadModelInfo[] readModelInfos;

            using (var db = createReadModelDbContext())
            {
                readModelInfos = db.Set <ReadModelInfo>().ToArray();
            }

            readModelInfos
            .ForEach(i =>
            {
                var eventsProcessed = i.InitialCatchupEndTime.HasValue
                                              ? EventsProcessedOutOfBatch(i)
                                              : EventsProcessedOutOfAllEvents(i);

                if (eventsProcessed == 0)
                {
                    return;
                }

                if (!i.BatchStartTime.HasValue)
                {
                    return;
                }

                if (!i.InitialCatchupStartTime.HasValue)
                {
                    return;
                }

                var timeTakenForProcessedEvents = i.InitialCatchupEndTime.HasValue
                                                          ? (now - i.BatchStartTime).Value
                                                          : (now - i.InitialCatchupStartTime).Value;

                var eventHandlerProgress = new EventHandlerProgress
                {
                    Name = i.Name,
                    InitialCatchupEvents       = i.InitialCatchupEvents,
                    TimeTakenForInitialCatchup = TimeTakenForInitialCatchup(i, now),
                    TimeRemainingForCatchup    = TimeRemaining(timeTakenForProcessedEvents, eventsProcessed, i.BatchRemainingEvents),
                    EventsRemaining            = i.BatchRemainingEvents,
                    PercentageCompleted        = Percent(
                        eventStoreCount - i.BatchRemainingEvents,
                        eventStoreCount),
                    LatencyInMilliseconds = i.LatencyInMilliseconds,
                    LastUpdated           = i.LastUpdated,
                    CurrentAsOfEventId    = i.CurrentAsOfEventId,
                    FailedOnEventId       = i.FailedOnEventId,
                    Error = i.Error
                };

                progress.Add(eventHandlerProgress);
            });
            return(progress);
        }
Beispiel #3
0
        public static IEnumerable <EventHandlerProgress> Calculate(
            Func <DbContext> createDbContext,
            Func <EventStoreDbContext> createEventStoreDbContext = null)
        {
            if (createDbContext == null)
            {
                throw new ArgumentNullException(nameof(createDbContext));
            }

            int count;

            using (var db = createEventStoreDbContext.IfNotNull()
                            .Then(create => create())
                            .Else(() => new EventStoreDbContext()))
            {
                count = db.Events.Count();
            }

            if (count == 0)
            {
                return(Enumerable.Empty <EventHandlerProgress>());
            }

            var now      = Clock.Now();
            var progress = new List <EventHandlerProgress>();

            ReadModelInfo[] readModelInfos;

            using (var db = createDbContext())
            {
                readModelInfos = db.Set <ReadModelInfo>().ToArray();
            }
            readModelInfos
            .ForEach(i =>
            {
                var eventsProcessed = i.InitialCatchupEndTime.HasValue
                            ? i.BatchTotalEvents - i.BatchRemainingEvents
                            : i.InitialCatchupEvents - i.BatchRemainingEvents;

                long?timeTakenForProcessedEvents = null;
                if (i.BatchStartTime.HasValue && i.InitialCatchupStartTime.HasValue)
                {
                    timeTakenForProcessedEvents = i.InitialCatchupEndTime.HasValue
                                ? (now - i.BatchStartTime).Value.Ticks
                                : (now - i.InitialCatchupStartTime).Value.Ticks;
                }

                if (eventsProcessed == 0)
                {
                    return;
                }

                var eventHandlerProgress = new EventHandlerProgress
                {
                    Name = i.Name,
                    InitialCatchupEvents       = i.InitialCatchupEvents,
                    TimeTakenForInitialCatchup = i.InitialCatchupStartTime.HasValue
                                ? (i.InitialCatchupEndTime.HasValue ? i.InitialCatchupEndTime : now) - i.InitialCatchupStartTime
                                : null,
                    TimeRemainingForCatchup = eventsProcessed != 0 && timeTakenForProcessedEvents.HasValue
                                ? (TimeSpan?)TimeSpan.FromTicks((long)(timeTakenForProcessedEvents * (i.BatchRemainingEvents / (decimal)eventsProcessed)))
                                : null,
                    EventsRemaining       = i.BatchRemainingEvents,
                    PercentageCompleted   = (1 - ((decimal)i.BatchRemainingEvents / count)) * 100,
                    LatencyInMilliseconds = i.LatencyInMilliseconds,
                    LastUpdated           = i.LastUpdated,
                    CurrentAsOfEventId    = i.CurrentAsOfEventId,
                    FailedOnEventId       = i.FailedOnEventId,
                    Error = i.Error
                };

                progress.Add(eventHandlerProgress);
            });
            return(progress);
        }
Beispiel #4
0
        public static IEnumerable <EventHandlerProgress> CalculateProgress(
            Func <DbContext> createReadModelDbContext,
            Func <EventStoreDbContext> createEventStoreDbContext = null,
            Expression <Func <ReadModelInfo, bool> > filter      = null)
        {
            if (createReadModelDbContext == null)
            {
                throw new ArgumentNullException(nameof(createReadModelDbContext));
            }

            createEventStoreDbContext = createEventStoreDbContext ??
                                        (() => Configuration.Current.EventStoreDbContext());

            int eventStoreCount;

            using (var db = createEventStoreDbContext())
            {
                eventStoreCount = db.Events.Count();
            }

            if (eventStoreCount == 0)
            {
                return(Enumerable.Empty <EventHandlerProgress>());
            }

            var progress = new List <EventHandlerProgress>();

            ReadModelInfo[] readModelInfos;

            using (var db = createReadModelDbContext())
            {
                IQueryable <ReadModelInfo> query = db.Set <ReadModelInfo>();

                if (filter != null)
                {
                    query = query.Where(filter);
                }

                readModelInfos = query.ToArray();
            }

            readModelInfos
            .ForEach(i =>
            {
                var eventsProcessedOutOfBatch = EventsProcessedOutOfBatch(i);

                if (eventsProcessedOutOfBatch == 0)
                {
                    return;
                }

                if (!i.BatchStartTime.HasValue)
                {
                    return;
                }

                if (!i.InitialCatchupStartTime.HasValue)
                {
                    return;
                }

                var eventHandlerProgress = new EventHandlerProgress(i);

                progress.Add(eventHandlerProgress);
            });

            return(progress);
        }