Skip to content

Commit

Permalink
Optimize swagger diff calculation and avoid cleanup of approved PR re… (
Browse files Browse the repository at this point in the history
#7415)

* Optimize swagger diff calculation and avoid cleanup of approved PR reviews
  • Loading branch information
praveenkuttappan authored Dec 8, 2023
1 parent 1fc8e89 commit a92a4a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.ApplicationInsights.DataContracts;
using System.Collections.Generic;
using APIViewWeb.LeanModels;
using System.Linq;

namespace APIViewWeb.HostedServices
{
Expand Down Expand Up @@ -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<string>();
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)
Expand Down
21 changes: 11 additions & 10 deletions src/dotnet/APIView/APIViewWeb/Managers/APIRevisionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>();
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)
{
Expand All @@ -304,7 +305,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP
lineNumbersForHeadingOfSectionWithDiff.Add((int)diffLine.Line.LineNumber);
}
}
}
});

if (apiRevision.HeadingsOfSectionsWithDiff.ContainsKey(rev.Id))
{
Expand All @@ -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)
{
Expand All @@ -333,7 +334,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP
lineNumbersForHeadingOfSectionWithDiff.Add((int)diffLine.Line.LineNumber);
}
}
}
});

if (rev.HeadingsOfSectionsWithDiff.ContainsKey(apiRevision.Id))
{
Expand All @@ -350,7 +351,7 @@ public async Task GetLineNumbersOfHeadingsOfSectionsWithDiff(string reviewId, AP
}

/// <summary>
/// Computed the diff for hidden (colapsible) API sections
/// Computed the diff for hidden (collapsible) API sections
/// </summary>
/// <param name="before"></param>
/// <param name="after"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit a92a4a1

Please sign in to comment.