Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-cache-missing #2099

Merged
merged 5 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/PortingAssistant.Client.Analysis/AnalysisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ private CompatibilityCheckerResponse ProcessCompatibilityCheckerRequestByApplyin
_logger.LogInformation("local cache available. try apply it on rawCompatibilityCheckerRequest");
//apply Cache result
_cacheService.ApplyCacheToCompatibleCheckerResults(rawCompatibilityCheckerRequest, rawCompatibilityCheckerRequest.PackageWithApis.Keys.ToList(),
out nugetPackagesNeedToCheck, out packageWithApisNeedToCheck, ref packageAnalysisResultsDic, ref apiAnalysisResultsDic);
out packageWithApisNeedToCheck, ref packageAnalysisResultsDic, ref apiAnalysisResultsDic);

var compatibilityCheckerRequest = new CompatibilityCheckerRequest
{
AssessmentType = rawCompatibilityCheckerRequest.AssessmentType,
Expand Down
14 changes: 11 additions & 3 deletions src/PortingAssistant.Client.Analysis/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,12 @@ public void ValidateCacheFile(string cacheFilePath, int cacheExpirationHours = 2

public void ApplyCacheToCompatibleCheckerResults(CompatibilityCheckerRequest request,
List<Compatibility.Common.Model.PackageVersionPair> allPackages,
out HashSet<Compatibility.Common.Model.PackageVersionPair> packagesNeedToCheck,
out Dictionary<Compatibility.Common.Model.PackageVersionPair, HashSet<ApiEntity>> packageWithApisNeedToCheck,
ref Dictionary<Compatibility.Common.Model.PackageVersionPair, AnalysisResult> packageAnalysisResultsDic,
ref Dictionary<Compatibility.Common.Model.PackageVersionPair, Dictionary<string, AnalysisResult>> apiAnalysisResultsDic)
{
var targetFramework = request.TargetFramework;
packagesNeedToCheck = new HashSet<Compatibility.Common.Model.PackageVersionPair>();
var packagesOnlyNeedToCheck = new HashSet<Compatibility.Common.Model.PackageVersionPair>();
packageWithApisNeedToCheck = new Dictionary<Compatibility.Common.Model.PackageVersionPair, HashSet<ApiEntity>>();

// Load results from cache object
Expand All @@ -104,7 +103,7 @@ public void ApplyCacheToCompatibleCheckerResults(CompatibilityCheckerRequest req
}
else if (package.PackageSourceType == Compatibility.Common.Model.PackageSourceType.NUGET)
{
packagesNeedToCheck.Add(package);
packagesOnlyNeedToCheck.Add(package);
}

var apis = request.PackageWithApis[package];
Expand Down Expand Up @@ -135,6 +134,15 @@ public void ApplyCacheToCompatibleCheckerResults(CompatibilityCheckerRequest req
_logger.LogError(ex, $"fail to apply cache result on package {package}");
}
}

// merge packagesNeedToCheck with packageWithApisNeedToCheck
foreach (var p in packagesOnlyNeedToCheck)
{
if (!packageWithApisNeedToCheck.ContainsKey(p))
{
packageWithApisNeedToCheck.Add(p, new HashSet<ApiEntity>());
}
}
}

private void AddCachedApiAnalysisToResult(Compatibility.Common.Model.PackageVersionPair package, ApiEntity api, AnalysisResult cachedApiAnalysisResult,
Expand Down
2 changes: 1 addition & 1 deletion src/PortingAssistant.Client.Analysis/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ICacheService
public void ValidateCacheFile(Model.PortingAssistantConfiguration config);
public void ValidateCacheFile(string cacheFilePath, int cacheExpirationHours = 24);
public void ApplyCacheToCompatibleCheckerResults(CompatibilityCheckerRequest compatibilityCheckerRequest,
List<PackageVersionPair> packageVersionPairs, out HashSet<PackageVersionPair> nugetPackagesNeedToCheck,
List<PackageVersionPair> packageVersionPairs,
out Dictionary<PackageVersionPair, HashSet<ApiEntity>> packageWithApisNeedToCheck,
ref Dictionary<PackageVersionPair, AnalysisResult> packageAnalysisResultsDic,
ref Dictionary<PackageVersionPair, Dictionary<string, AnalysisResult>> apiAnalysisResultsDic);
Expand Down
3 changes: 2 additions & 1 deletion src/PortingAssistant.Client.Analysis/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,8 @@
"Newtonsoft.Json": "[13.0.3, )",
"NuGet.Packaging": "[6.6.0, )",
"Polly": "[7.2.3, )",
"Polly.Extensions.Http": "[3.0.0, )"
"Polly.Extensions.Http": "[3.0.0, )",
"System.CodeDom": "[4.4.0, )"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="System.CodeDom" Version="4.4.0" />
</ItemGroup>
</Project>
79 changes: 48 additions & 31 deletions src/PortingAssistant.Compatibility.Common/Utils/ApiCompatiblity.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
using Amazon.Runtime.Internal.Endpoints.StandardLibrary;
using Amazon.Runtime.Internal.Transform;
using Newtonsoft.Json;
using Newtonsoft.Json;
using NuGet.Versioning;
using PortingAssistant.Compatibility.Common.Interface;
using PortingAssistant.Compatibility.Common.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.CodeDom.Compiler;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PortingAssistant.Compatibility.Common.Utils
{
public static class ApiCompatiblity
{
private static CodeDomProvider? _codeDomProvider;
private static CodeDomProvider CodeDomProvider
{
get
{
_codeDomProvider ??= CodeDomProvider.CreateProvider("C#");
return _codeDomProvider;
}
}

private static readonly Recommendation DEFAULT_RECOMMENDATION = new Recommendation
{
RecommendedActionType = RecommendedActionType.NoRecommendation
};


public static Dictionary<ApiEntity, CompatibilityResult> IsCompatibleV2(
KeyValuePair<PackageVersionPair, HashSet<ApiEntity>> packageWithApi,
Dictionary<PackageVersionPair, PackageAnalysisResult> packageAnalysisCompatCheckerResults,
Expand Down Expand Up @@ -218,6 +220,7 @@ public static async Task<Dictionary<ApiEntity,CompatibilityResult>> PreProcessAp
Compatibility = Model.Compatibility.UNKNOWN,
CompatibleVersions = new List<string>()
};

// If necessary data to determine compatibility is missing, return unknown compatibility
if (packageCompatibilityResult == null)
{
Expand Down Expand Up @@ -453,8 +456,7 @@ public static Recommendation UpgradeStrategy(
return DEFAULT_RECOMMENDATION;
}
}



private static Recommendation FetchApiRecommendation(
string apiMethodSignature,
Task<RecommendationDetails>
Expand Down Expand Up @@ -562,8 +564,6 @@ public static Dictionary<string, int> SignatureToIndexPreProcess(PackageDetails
return indexDict;
}



public static Dictionary<string, int> SignatureToIndexPreProcess(ApiDetailsV2[] apiDetails)
{
var indexDict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
Expand All @@ -588,9 +588,7 @@ public static Dictionary<string, int> SignatureToIndexPreProcess(ApiDetailsV2[]
{
indexDict.Add(extensionSignature, i);
}

}

return indexDict;
}

Expand Down Expand Up @@ -624,6 +622,28 @@ public static Dictionary<string, int> VBSignatureToIndexPreProcess(ApiDetailsV2[
return indexDict;
}

public static string RemoveParameterName(string methodParameter)
{
const string spaceChar = " ";
var startOfParameterName = methodParameter.LastIndexOf(spaceChar);

// If there are no spaces in the method parameter, no op
if (startOfParameterName == -1)
{
return methodParameter;
}

var potentialParameterName = methodParameter.Substring(startOfParameterName);

// If potential parameter name is valid, we want to remove it
if (CodeDomProvider.IsValidIdentifier(potentialParameterName.Trim()))
{
return methodParameter.Remove(startOfParameterName);
}

// The potential parameter name was not a valid identifier, no op
return methodParameter;
}

public static string GetExtensionSignature(ApiDetails api)
{
Expand All @@ -633,13 +653,15 @@ public static string GetExtensionSignature(ApiDetails api)
{
return null;
}

var possibleExtension = api.MethodParameters[0];

var methodParameters = api.MethodParameters.Select(RemoveParameterName).ToList();

var possibleExtension = methodParameters[0];
var methodSignatureIndex = api.MethodSignature.IndexOf("(") >= 0 ? api.MethodSignature.IndexOf("(") : api.MethodSignature.Length;
var sliceMethodSignature = api.MethodSignature.Substring(0, methodSignatureIndex);
var methondNameIndex = sliceMethodSignature.LastIndexOf(api.MethodName);
var methodName = sliceMethodSignature.Substring(methondNameIndex >= 0 ? methondNameIndex : sliceMethodSignature.Length);
var methodSignature = $"{possibleExtension}.{methodName}({String.Join(", ", api.MethodParameters.Skip(1))})";
var methodNameIndex = sliceMethodSignature.LastIndexOf(api.MethodName);
var methodName = sliceMethodSignature.Substring(methodNameIndex >= 0 ? methodNameIndex : sliceMethodSignature.Length);
var methodSignature = $"{possibleExtension}.{methodName}({String.Join(", ", methodParameters.Skip(1))})";
return methodSignature;
}
catch
Expand All @@ -648,7 +670,6 @@ public static string GetExtensionSignature(ApiDetails api)
}
}


public static string GetExtensionSignature(ApiDetailsV2 api)
{
try
Expand All @@ -658,18 +679,14 @@ public static string GetExtensionSignature(ApiDetailsV2 api)
return null;
}

var newMethodParameters = new List<string>();
foreach (var param in api.methodParameters )
{
newMethodParameters.Add( param.Split(" ")[0]);
}
var methodParameters = api.methodParameters.Select(RemoveParameterName).ToList();

var possibleExtension = newMethodParameters[0];
var possibleExtension = methodParameters[0];
var methodSignatureIndex = api.methodSignature.IndexOf("(") >= 0 ? api.methodSignature.IndexOf("(") : api.methodSignature.Length;
var sliceMethodSignature = api.methodSignature.Substring(0, methodSignatureIndex);
var methondNameIndex = sliceMethodSignature.LastIndexOf(api.methodName);
var methodName = sliceMethodSignature.Substring(methondNameIndex >= 0 ? methondNameIndex : sliceMethodSignature.Length);
var methodSignature = $"{possibleExtension}.{methodName}({String.Join(", ", newMethodParameters.Skip(1))})";
var methodNameIndex = sliceMethodSignature.LastIndexOf(api.methodName);
var methodName = sliceMethodSignature.Substring(methodNameIndex >= 0 ? methodNameIndex : sliceMethodSignature.Length);
var methodSignature = $"{possibleExtension}.{methodName}({string.Join(", ", methodParameters.Skip(1))})";
return methodSignature;
}
catch
Expand Down
Loading
Loading