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.")); } }
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()); }