BlueCollar.Test.RepositoryTests.GetStatistics C# (CSharp) Method

GetStatistics() protected method

Get statistics tests.
protected GetStatistics ( ) : void
return void
        protected void GetStatistics()
        {
            if (this.Repository != null)
            {
                IJob job = new TestJob() { Id = Guid.NewGuid() };
                string jobData = JobSerializer.Serialize(job);
                string typeName = JobSerializer.GetTypeName(job.GetType());
                DateTime date = new DateTime(2011, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                DateTime now = DateTime.UtcNow.FloorWithSeconds();
                HistoryRecord historyRecord;

                WorkerRecord workerRecord = new WorkerRecord()
                {
                    ApplicationName = BlueCollarSection.Section.ApplicationName,
                    MachineAddress = Machine.Address,
                    MachineName = Machine.Name,
                    Name = "Test Worker",
                    QueueNames = "*",
                    Signal = WorkerSignal.Stop,
                    Status = WorkerStatus.Working,
                    Startup = WorkerStartupType.Automatic,
                    UpdatedOn = DateTime.UtcNow
                };

                this.Repository.CreateWorker(workerRecord, null);

                StatisticsRecord stats = this.Repository.GetStatistics(workerRecord.ApplicationName, date.AddDays(-1), date.AddDays(-14), date, null);
                Assert.IsNotNull(stats);

                /*
                 * Dequeued per hour by day.
                 */

                for (int i = 0; i < 14; i++)
                {
                    for (int j = 0; j < 24; j++)
                    {
                        for (int k = 0; k < 10; k++)
                        {
                            DateTime finished = date.AddDays(i).AddHours(j);

                            historyRecord = new HistoryRecord()
                            {
                                ApplicationName = workerRecord.ApplicationName,
                                Data = jobData,
                                FinishedOn = finished,
                                JobName = job.Name,
                                JobType = typeName,
                                QueuedOn = finished.AddSeconds(-1),
                                QueueName = "*",
                                StartedOn = finished.AddSeconds(-11),
                                Status = HistoryStatus.Succeeded,
                                TryNumber = 1,
                                WorkerId = workerRecord.Id.Value
                            };

                            this.Repository.CreateHistory(historyRecord, null);
                        }
                    }
                }

                stats = this.Repository.GetStatistics(workerRecord.ApplicationName, date.AddDays(13), date, date.AddDays(14), null);
                Assert.AreEqual(14, stats.JobsPerHourByDay.Count);

                for (int i = 0; i < stats.JobsPerHourByDay.Count; i++)
                {
                    Assert.AreEqual(date.AddDays(i), stats.JobsPerHourByDay[i].Date);
                    Assert.AreEqual(10, stats.JobsPerHourByDay[i].JobsPerHour);
                }

                /*
                 * Dequeued per hour by day - multiple queues.
                 */

                this.Repository.DeleteAll(BlueCollarSection.Section.ApplicationName, null);

                workerRecord.Id = null;
                this.Repository.CreateWorker(workerRecord, null);

                historyRecord = new HistoryRecord()
                {
                    ApplicationName = workerRecord.ApplicationName,
                    Data = jobData,
                    FinishedOn = now.AddHours(-1),
                    JobName = job.Name,
                    JobType = typeName,
                    QueuedOn = now.AddHours(-1),
                    QueueName = "*",
                    StartedOn = now.AddHours(-1),
                    Status = HistoryStatus.Succeeded,
                    TryNumber = 1,
                    WorkerId = workerRecord.Id.Value
                };

                this.Repository.CreateHistory(historyRecord, null);

                historyRecord = new HistoryRecord()
                {
                    ApplicationName = workerRecord.ApplicationName,
                    Data = jobData,
                    FinishedOn = now.AddHours(-1),
                    JobName = job.Name,
                    JobType = typeName,
                    QueuedOn = now.AddHours(-1),
                    QueueName = "*",
                    StartedOn = now.AddHours(-1),
                    Status = HistoryStatus.Succeeded,
                    TryNumber = 1,
                    WorkerId = workerRecord.Id.Value
                };

                this.Repository.CreateHistory(historyRecord, null);

                historyRecord = new HistoryRecord()
                {
                    ApplicationName = workerRecord.ApplicationName,
                    Data = jobData,
                    FinishedOn = now.AddHours(-1),
                    JobName = job.Name,
                    JobType = typeName,
                    QueuedOn = now.AddHours(-1),
                    QueueName = "test",
                    StartedOn = now.AddHours(-1),
                    Status = HistoryStatus.Succeeded,
                    TryNumber = 1,
                    WorkerId = workerRecord.Id.Value
                };

                this.Repository.CreateHistory(historyRecord, null);

                historyRecord = new HistoryRecord()
                {
                    ApplicationName = workerRecord.ApplicationName,
                    Data = jobData,
                    FinishedOn = now.AddHours(-1),
                    JobName = job.Name,
                    JobType = typeName,
                    QueuedOn = now.AddHours(-1),
                    QueueName = "test",
                    StartedOn = now.AddHours(-1),
                    Status = HistoryStatus.Succeeded,
                    TryNumber = 1,
                    WorkerId = workerRecord.Id.Value
                };

                this.Repository.CreateHistory(historyRecord, null);

                stats = this.Repository.GetStatistics(workerRecord.ApplicationName, now.AddDays(-1), now.AddDays(-14), now, null);
                Assert.AreEqual(2, stats.JobsPerHourByDay.Count);
                Assert.AreEqual("*", stats.JobsPerHourByDay[0].QueueName);
                Assert.AreEqual(2L, stats.JobsPerHourByDay[0].JobsPerHour);
                Assert.AreEqual("test", stats.JobsPerHourByDay[1].QueueName);
                Assert.AreEqual(2L, stats.JobsPerHourByDay[1].JobsPerHour);

                /*
                 * History status counts.
                 */

                this.Repository.DeleteAll(BlueCollarSection.Section.ApplicationName, null);

                Func<int, DateTime> getDate = (int i) =>
                {
                    if (i % 3 == 0)
                    {
                        return now.AddDays(-30);
                    }
                    else if (i % 3 == 1)
                    {
                        return now.AddDays(-10);
                    }
                    else if (i % 3 == 2)
                    {
                        return now.AddHours(-13);
                    }
                    else
                    {
                        throw new InvalidOperationException("Loop count must be a multiple of 3.");
                    }
                };

                Func<int, HistoryStatus> getStatus = (int i) =>
                {
                    if (i % 5 == 0)
                    {
                        return HistoryStatus.Succeeded;
                    }
                    else if (i % 5 == 1)
                    {
                        return HistoryStatus.Failed;
                    }
                    else if (i % 5 == 2)
                    {
                        return HistoryStatus.Canceled;
                    }
                    else if (i % 5 == 3)
                    {
                        return HistoryStatus.TimedOut;
                    }
                    else if (i % 5 == 4)
                    {
                        return HistoryStatus.Interrupted;
                    }
                    else
                    {
                        throw new InvalidOperationException("Loop count must be a multiple of 5.");
                    }
                };

                for (int i = 0; i < 150; i++)
                {
                    DateTime indexDate = getDate(i);

                    historyRecord = new HistoryRecord()
                    {
                        ApplicationName = workerRecord.ApplicationName,
                        Data = jobData,
                        FinishedOn = indexDate,
                        JobName = job.Name,
                        JobType = typeName,
                        QueuedOn = date.AddSeconds(-2),
                        QueueName = "*",
                        StartedOn = date.AddSeconds(-1),
                        Status = getStatus(i),
                        TryNumber = 1,
                        WorkerId = workerRecord.Id.Value
                    };

                    this.Repository.CreateHistory(historyRecord, null);
                }

                stats = this.Repository.GetStatistics(workerRecord.ApplicationName, now.AddHours(-24), now.AddDays(-14), now, null);
                Assert.IsNotNull(stats.HistoryStatusRecent);
                Assert.AreEqual(10, stats.HistoryStatusRecent.SucceededCount);
                Assert.AreEqual(10, stats.HistoryStatusRecent.FailedCount);
                Assert.AreEqual(10, stats.HistoryStatusRecent.CanceledCount);
                Assert.AreEqual(10, stats.HistoryStatusRecent.TimedOutCount);
                Assert.AreEqual(10, stats.HistoryStatusRecent.InterruptedCount);
                Assert.AreEqual(50, stats.HistoryStatusRecent.TotalCount);

                Assert.IsNotNull(stats.HistoryStatusDistant);
                Assert.AreEqual(20, stats.HistoryStatusDistant.SucceededCount);
                Assert.AreEqual(20, stats.HistoryStatusDistant.FailedCount);
                Assert.AreEqual(20, stats.HistoryStatusDistant.CanceledCount);
                Assert.AreEqual(20, stats.HistoryStatusDistant.TimedOutCount);
                Assert.AreEqual(20, stats.HistoryStatusDistant.InterruptedCount);
                Assert.AreEqual(100, stats.HistoryStatusDistant.TotalCount);

                stats = this.Repository.GetStatistics(workerRecord.ApplicationName, now.AddDays(-31), now.AddDays(-60), now.AddDays(-30), null);
                Assert.AreEqual(10, stats.HistoryStatusDistant.SucceededCount);
                Assert.AreEqual(10, stats.HistoryStatusDistant.FailedCount);
                Assert.AreEqual(10, stats.HistoryStatusDistant.CanceledCount);
                Assert.AreEqual(10, stats.HistoryStatusDistant.TimedOutCount);
                Assert.AreEqual(10, stats.HistoryStatusDistant.InterruptedCount);
                Assert.AreEqual(50, stats.HistoryStatusDistant.TotalCount);
            }
        }