From a92a4a1d2b1744e87ca64d13ca9ac13df03d4cf3 Mon Sep 17 00:00:00 2001 From: Praven Kuttappan <55455725+praveenkuttappan@users.noreply.github.com> Date: Fri, 8 Dec 2023 17:34:23 -0500 Subject: [PATCH] =?UTF-8?q?Optimize=20swagger=20diff=20calculation=20and?= =?UTF-8?q?=20avoid=20cleanup=20of=20approved=20PR=20re=E2=80=A6=20(#7415)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Optimize swagger diff calculation and avoid cleanup of approved PR reviews --- .../LinesWithDiffBackgroundHostedService.cs | 13 ++++++++---- .../Managers/APIRevisionsManager.cs | 21 ++++++++++--------- .../APIViewWeb/Managers/PullRequestManager.cs | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/dotnet/APIView/APIViewWeb/HostedServices/LinesWithDiffBackgroundHostedService.cs b/src/dotnet/APIView/APIViewWeb/HostedServices/LinesWithDiffBackgroundHostedService.cs index 6b7b2f71e47..4138d4cfa4d 100644 --- a/src/dotnet/APIView/APIViewWeb/HostedServices/LinesWithDiffBackgroundHostedService.cs +++ b/src/dotnet/APIView/APIViewWeb/HostedServices/LinesWithDiffBackgroundHostedService.cs @@ -10,6 +10,7 @@ using Microsoft.ApplicationInsights.DataContracts; using System.Collections.Generic; using APIViewWeb.LeanModels; +using System.Linq; namespace APIViewWeb.HostedServices { @@ -41,16 +42,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { var reviews = await _reviewManager.GetReviewsAsync(language: "Swagger"); - + reviews = reviews.OrderBy(r => r.CreatedOn).Reverse(); + int index = 1; + int total = reviews.Count(); foreach (var review in reviews) { - _telemetryClient.TrackTrace($"Computing Line Number of Sections with Diff for Review {review.Id}"); + _telemetryClient.TrackTrace($"Computing Line Number of Sections with Diff for Review {review.Id}, processing {index}/{total}."); var apiRevisions = await _apiRevisionManager.GetAPIRevisionsAsync(reviewId: review.Id); - + var processedRevisions = new HashSet(); foreach (var apiRevision in apiRevisions) { - await _apiRevisionManager.GetLineNumbersOfHeadingsOfSectionsWithDiff(reviewId: review.Id, apiRevision: apiRevision, apiRevisions: apiRevisions); + processedRevisions.Add(apiRevision.Id); + await _apiRevisionManager.GetLineNumbersOfHeadingsOfSectionsWithDiff(reviewId: review.Id, apiRevision: apiRevision, apiRevisions: apiRevisions.Where(r => !processedRevisions.Contains(r.Id))); } + index++; } } catch (Exception ex) diff --git a/src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs b/src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs index 07e4aaca3f0..334e4e5a0d8 100644 --- a/src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs +++ b/src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs @@ -273,24 +273,25 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP { apiRevisions = await _apiRevisionsRepository.GetAPIRevisionsAsync(reviewId); } - var RevisionACodeFile = await _codeFileRepository.GetCodeFileAsync(apiRevision, false); + var RevisionACodeFile = await _codeFileRepository.GetCodeFileAsync(apiRevision, true); var RevisionAHtmlLines = RevisionACodeFile.Render(false); var RevisionATextLines = RevisionACodeFile.RenderText(false); - foreach (var rev in apiRevisions) + var latestFewRevisions = apiRevisions.Count() > 10? apiRevisions.OrderBy(r => r.CreatedOn).Reverse().Take(10) : apiRevisions; + foreach (var rev in latestFewRevisions) { if (rev.Id != apiRevision.Id) { var lineNumbersForHeadingOfSectionWithDiff = new HashSet(); - var RevisionBCodeFile = await _codeFileRepository.GetCodeFileAsync(rev, false); + var RevisionBCodeFile = await _codeFileRepository.GetCodeFileAsync(rev, true); var RevisionBHtmlLines = RevisionBCodeFile.RenderReadOnly(false); var RevisionBTextLines = RevisionBCodeFile.RenderText(false); - // Compute diff before: apiRevision -> after: exisitngAPIRevision + // Compute diff before: apiRevision -> after: existing APIRevision var diffLines = InlineDiff.Compute(before: RevisionATextLines, after: RevisionBTextLines, beforeResults: RevisionAHtmlLines, afterResults: RevisionBHtmlLines); - foreach (var diffLine in diffLines) + Parallel.ForEach(diffLines, diffLine => { if (diffLine.Kind == DiffLineKind.Unchanged && diffLine.Line.SectionKey != null && diffLine.OtherLine.SectionKey != null) { @@ -304,7 +305,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP lineNumbersForHeadingOfSectionWithDiff.Add((int)diffLine.Line.LineNumber); } } - } + }); if (apiRevision.HeadingsOfSectionsWithDiff.ContainsKey(rev.Id)) { @@ -316,10 +317,10 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP } await _apiRevisionsRepository.UpsertAPIRevisionAsync(apiRevision); - // Compute diff before: exisitngAPIRevision -> after: apiRevision + // Compute diff before: existing APIRevision -> after: apiRevision diffLines = InlineDiff.Compute(before: RevisionBTextLines, after: RevisionATextLines, beforeResults: RevisionBHtmlLines, afterResults: RevisionAHtmlLines); - foreach (var diffLine in diffLines) + Parallel.ForEach(diffLines, diffLine => { if (diffLine.Kind == DiffLineKind.Unchanged && diffLine.Line.SectionKey != null && diffLine.OtherLine.SectionKey != null) { @@ -333,7 +334,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP lineNumbersForHeadingOfSectionWithDiff.Add((int)diffLine.Line.LineNumber); } } - } + }); if (rev.HeadingsOfSectionsWithDiff.ContainsKey(apiRevision.Id)) { @@ -350,7 +351,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP } /// - /// Computed the diff for hidden (colapsible) API sections + /// Computed the diff for hidden (collapsible) API sections /// /// /// diff --git a/src/dotnet/APIView/APIViewWeb/Managers/PullRequestManager.cs b/src/dotnet/APIView/APIViewWeb/Managers/PullRequestManager.cs index d89ce835a30..bc5554ae12e 100644 --- a/src/dotnet/APIView/APIViewWeb/Managers/PullRequestManager.cs +++ b/src/dotnet/APIView/APIViewWeb/Managers/PullRequestManager.cs @@ -184,7 +184,7 @@ private async Task ClosePullRequestAPIRevision(PullRequestModel pullRequestModel if (!String.IsNullOrEmpty(pullRequestModel.APIRevisionId)) { var apiRevision = await _apiRevisionsRepository.GetAPIRevisionAsync(pullRequestModel.APIRevisionId); - if (apiRevision != null) + if (apiRevision != null && !apiRevision.Approvers.Any()) { await _apiRevisionManager.SoftDeleteAPIRevisionAsync(userName: "azure-sdk", apiRevision: apiRevision, notes: "Deleted by PullRequest CleanUp Automation"); }