public async Task LoadTestCaseHistoryCollectionAsync()
        {
            try
            {
                var testCaseHistoryDtoCollection = new List <TestCaseHistoryDto>();

                var testCasesHistoryFilePath   = GetTestCasesHistoryFileNamePath();
                var testCaseHistoryFileContent = string.Empty;
                if (_fileProvider.Exists(testCasesHistoryFilePath))
                {
                    testCaseHistoryFileContent = await _fileProvider.ReadAllTextAsync(testCasesHistoryFilePath).ConfigureAwait(false);
                }

                if (!string.IsNullOrEmpty(testCaseHistoryFileContent))
                {
                    testCaseHistoryDtoCollection = _jsonSerializer.Deserialize <List <TestCaseHistoryDto> >(testCaseHistoryFileContent);
                }

                if (testCaseHistoryDtoCollection.Any())
                {
                    foreach (var testCaseHistoryDto in testCaseHistoryDtoCollection)
                    {
                        var testCaseHistory        = Mapper.Map <TestCaseHistory>(testCaseHistoryDto);
                        var createdTestCaseHistory = await _meissaRepository.InsertWithSaveAsync(testCaseHistory).ConfigureAwait(false);

                        if (testCaseHistoryDto.Durations.Any())
                        {
                            foreach (var currentDuration in testCaseHistoryDto.Durations)
                            {
                                var testCaseHistoryEntry = new TestCaseHistoryEntry
                                {
                                    TestCaseHistoryId = createdTestCaseHistory.TestCaseHistoryId,
                                    AvgDuration       = currentDuration,
                                };

                                await _meissaRepository.InsertWithSaveAsync(testCaseHistoryEntry).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            _backgroundTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                using (var scope = _backgroundTaskQueue.CreateScope())
                {
                    var meissaRepository = scope.ServiceProvider.GetRequiredService <MeissaRepository>();
                    var logger           = scope.ServiceProvider.GetRequiredService <ILogger <TestCaseRunsController> >();

                    try
                    {
                        var existingTestCasesHistory = meissaRepository.GetAllQuery <TestCaseHistory>().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                        var testCaseHistoryEntries   = meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                        foreach (var testCaseRun in testCaseRuns)
                        {
                            if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                            {
                                var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                                // Creates the new test case history entry for the current run.
                                if (existingTestCaseHistory != null)
                                {
                                    var testCaseHistoryEntry = new TestCaseHistoryEntry
                                    {
                                        AvgDuration       = testCaseRun.Duration,
                                        TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                                    };
                                    meissaRepository.Insert(testCaseHistoryEntry);

                                    // Get all previous runs for the test and add to the list the new entry.
                                    var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                                    allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                                }

                                // Calculate the new average duration for the current tests based on the new entry.
                                double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                                var newAverageDuration         = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                                // Update the test case history info.
                                if (existingTestCaseHistory != null)
                                {
                                    existingTestCaseHistory.AvgDuration     = newAverageDuration;
                                    existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                                    meissaRepository.Update(existingTestCaseHistory);
                                }
                            }
                            else
                            {
                                // If no entries exist, we create the history test case and a new history entry.
                                var testCaseHistoryDto = new TestCaseHistory()
                                {
                                    FullName        = testCaseRun.FullName,
                                    LastUpdatedTime = DateTime.Now,
                                    AvgDuration     = testCaseRun.Duration,
                                };
                                testCaseHistoryDto = await meissaRepository.InsertWithSaveAsync(testCaseHistoryDto);

                                var testCaseHistoryEntry = new TestCaseHistoryEntry
                                {
                                    AvgDuration       = testCaseRun.Duration,
                                    TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                                };
                                meissaRepository.Insert(testCaseHistoryEntry);
                            }
                        }

                        await meissaRepository.SaveAsync();
                    }
                    catch (Exception ex)
                    {
                        logger.LogCritical("Exception while updating test cases execution history.", ex);
                    }
                }
            });

            return(Ok());
        }
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            try
            {
                Debug.WriteLine("##### Start UpdateTestCaseExecutionHistory");
                DateTime startTime = DateTime.Now;
                var      existingTestCasesHistory = _meissaRepository.GetAllQuery <TestCaseHistory>().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                var      testCaseHistoryEntries   = _meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                foreach (var testCaseRun in testCaseRuns)
                {
                    if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                    {
                        var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                        // Creates the new test case history entry for the current run.
                        if (existingTestCaseHistory != null)
                        {
                            var testCaseHistoryEntry = new TestCaseHistoryEntry
                            {
                                AvgDuration       = testCaseRun.Duration,
                                TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                            };
                            _meissaRepository.Insert(testCaseHistoryEntry);

                            // Get all previous runs for the test and add to the list the new entry.
                            var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                            allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                        }

                        // Calculate the new average duration for the current tests based on the new entry.
                        double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                        var    newAverageDuration      = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                        // Update the test case history info.
                        if (existingTestCaseHistory != null)
                        {
                            existingTestCaseHistory.AvgDuration     = newAverageDuration;
                            existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                            _meissaRepository.Update(existingTestCaseHistory);
                        }
                    }
                    else
                    {
                        // If no entries exist, we create the history test case and a new history entry.
                        var testCaseHistoryDto = new TestCaseHistory()
                        {
                            FullName        = testCaseRun.FullName,
                            LastUpdatedTime = DateTime.Now,
                            AvgDuration     = testCaseRun.Duration,
                        };
                        testCaseHistoryDto = await _meissaRepository.InsertWithSaveAsync(testCaseHistoryDto);

                        var testCaseHistoryEntry = new TestCaseHistoryEntry
                        {
                            AvgDuration       = testCaseRun.Duration,
                            TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                        };
                        _meissaRepository.Insert(testCaseHistoryEntry);
                    }
                }

                await _meissaRepository.SaveAsync();

                DateTime endTime = DateTime.Now;
                Debug.WriteLine($"##### End UpdateTestCaseExecutionHistory for {endTime - startTime}");

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while updating test cases execution history.", ex);
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> UpdateTestCaseExecutionHistoryAsync([FromBody] List <TestCaseRun> testCaseRuns)
        {
            try
            {
                var existingTestCasesHistory = _meissaRepository.GetAllQuery <TestCaseHistory>().AsEnumerable().Where(x => testCaseRuns.Any(y => y.FullName.Equals(x.FullName))).ToList();
                var testCaseHistoryEntries   = _meissaRepository.GetAllQuery <TestCaseHistoryEntry>();
                foreach (var testCaseRun in testCaseRuns)
                {
                    if (existingTestCasesHistory.Any(x => x.FullName.Equals(testCaseRun.FullName)))
                    {
                        var existingTestCaseHistory = existingTestCasesHistory.FirstOrDefault(x => x.FullName.Equals(testCaseRun.FullName));

                        // Creates the new test case history entry for the current run.
                        if (existingTestCaseHistory != null)
                        {
                            var testCaseHistoryEntry = new TestCaseHistoryEntry
                            {
                                AvgDuration       = testCaseRun.Duration,
                                TestCaseHistoryId = existingTestCaseHistory.TestCaseHistoryId,
                            };
                            _meissaRepository.Insert(testCaseHistoryEntry);

                            // Get all previous runs for the test and add to the list the new entry.
                            var allCurrentTestCaseHistoryEntries = testCaseHistoryEntries.Where(x => x.TestCaseHistoryId.Equals(existingTestCaseHistory.TestCaseHistoryId)).ToList();
                            allCurrentTestCaseHistoryEntries.Add(testCaseHistoryEntry);
                        }

                        // Calculate the new average duration for the current tests based on the new entry.
                        double newAverageDurationTicks = testCaseHistoryEntries.Average(x => x.AvgDuration.Ticks);
                        var    newAverageDuration      = new TimeSpan(Convert.ToInt64(newAverageDurationTicks));

                        // Update the test case history info.
                        if (existingTestCaseHistory != null)
                        {
                            existingTestCaseHistory.AvgDuration     = newAverageDuration;
                            existingTestCaseHistory.LastUpdatedTime = DateTime.Now;

                            _meissaRepository.Update(existingTestCaseHistory);
                        }
                    }
                    else
                    {
                        // If no entries exist, we create the history test case and a new history entry.
                        var testCaseHistoryDto = new TestCaseHistory()
                        {
                            FullName        = testCaseRun.FullName,
                            LastUpdatedTime = DateTime.Now,
                            AvgDuration     = testCaseRun.Duration,
                        };
                        testCaseHistoryDto = await _meissaRepository.InsertWithSaveAsync(testCaseHistoryDto).ConfigureAwait(false);

                        var testCaseHistoryEntry = new TestCaseHistoryEntry
                        {
                            AvgDuration       = testCaseRun.Duration,
                            TestCaseHistoryId = testCaseHistoryDto.TestCaseHistoryId,
                        };
                        _meissaRepository.Insert(testCaseHistoryEntry);
                    }
                }

                await _meissaRepository.SaveAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while updating test cases execution history. {ex.Message} {ex.InnerException?.StackTrace} {ex.InnerException?.Message}", ex);
            }

            return(Ok());
        }