From 6c8bfe1404e3b0abfad0cb84e7fb5205abc10fc6 Mon Sep 17 00:00:00 2001 From: Maoliang Huang Date: Tue, 29 Sep 2020 16:31:54 -0700 Subject: [PATCH] Reduce the dependencies on Azure powershell common. - That assembly and its dependencies causes a conflict if we load this module and Az.Accounts module. Though we can work around by sepcifying Az as a dependencies on this module, that'll cause long loading time. - The reason of loading that assembly is to get the setting about Azure PowerShell data collection. We can copy part of the code to load that file, so that we don't need that assembly as the dependency. --- .../Az.Tools.Predictor.csproj | 2 - .../Az.Tools.Predictor.psd1 | 2 +- .../AzPredictorConstants.cs | 1 + .../Az.Tools.Predictor/AzPredictorService.cs | 3 +- .../AzPredictorSettings.json | 2 +- .../AzPredictorTelemetryClient.cs | 66 +------------------ .../Az.Tools.Predictor/Predictor.cs | 2 +- tools/Az.Tools.Predictor/build.proj | 10 +-- 8 files changed, 8 insertions(+), 80 deletions(-) diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj index f244d19f8d81..caa40859af4d 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj @@ -34,8 +34,6 @@ - - diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1 b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1 index b5688055982d..869d3c9af620 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1 +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1 @@ -35,7 +35,7 @@ Description = 'Microsoft Azure PowerShell Predictor: Provide prediction while us PowerShellVersion = '7.1' # Modules that must be imported into the global environment prior to importing this module -RequiredModules = @(@{ModuleName = 'Az'; ModuleVersion = '3.0.0'; }) +# RequiredModules = @() NestedModules = @("Microsoft.Azure.PowerShell.Tools.AzPredictor.dll") diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs index 2dd3051c2c78..98582e59a5b4 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs @@ -67,6 +67,7 @@ internal static class AzPredictorConstants /// /// The azure profile directory name. /// + // See AzureDirectoryName in https://github.com/Azure/azure-powershell/blob/master/src/Accounts/Authentication/Properties/Resources.resx public const string AzureProfileDirectoryName = ".Azure"; } } diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorService.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorService.cs index e6daff16adbb..06ae541cf31b 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorService.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorService.cs @@ -12,7 +12,6 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.WindowsAzure.Commands.Utilities.Common; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; @@ -195,7 +194,7 @@ public virtual void RequestPredictions(IEnumerable commands) /// public virtual void RecordHistory(IEnumerable history) { - history.ForEach((h) => this._parameterValuePredictor.ProcessHistoryCommand(h)); + history.ToList().ForEach((h) => this._parameterValuePredictor.ProcessHistoryCommand(h)); } /// diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json index e525f1c3f919..23fab2cf6a3b 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json @@ -1,3 +1,3 @@ { - "service_uri": "https://app.aladdinppe.microsoft.com/api/v1" + "service_uri": "https://app.aladdin.microsoft.com/api/v1" } diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs index 8f98cb31687b..910285360040 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs @@ -14,8 +14,7 @@ using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; -using Microsoft.Azure.Commands.Common.Authentication; -using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.PowerShell.Tools.AzPredictor.Profile; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -30,40 +29,6 @@ namespace Microsoft.Azure.PowerShell.Tools.AzPredictor /// sealed class AzPredictorTelemetryClient : ITelemetryClient { - /// - /// A simple session class that provides neccessary information to get the profile. - /// - private sealed class TelemetrySession : AzureSession - { - /// - /// Constructs a new instance of - /// - public TelemetrySession() - { - DataStore = new DiskDataStore(); - ProfileDirectory = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), - AzPredictorConstants.AzureProfileDirectoryName); - } - - /// - public override TraceLevel AuthenticationLegacyTraceLevel - { - get => TraceLevel.Off; - set { } - } - - /// - public override TraceListenerCollection AuthenticationTraceListeners => Trace.Listeners; - - /// - public override SourceLevels AuthenticationTraceSourceLevel - { - get => SourceLevels.Off; - set { } - } - } - private const string TelemetryEventPrefix = "Az.Tools.Predictor"; /// @@ -74,31 +39,6 @@ public override SourceLevels AuthenticationTraceSourceLevel private readonly TelemetryClient _telemetryClient; - private object lockObject = new object(); - private AzurePSDataCollectionProfile _cachedProfile; - - private AzurePSDataCollectionProfile DataCollectionProfile - { - get - { - if (_cachedProfile != null) - { - return _cachedProfile; - } - - lock (lockObject) - { - if (_cachedProfile == null) - { - var controller = DataCollectionController.Create(new TelemetrySession()); - _cachedProfile = controller.GetProfile(() => { }); - } - - return _cachedProfile; - } - } - } - /// /// Constructs a new instance of /// @@ -255,9 +195,7 @@ public void OnGetSuggestionError(Exception e) /// true if allowed private bool IsDataCollectionAllowed() { - if (DataCollectionProfile != null && - DataCollectionProfile.EnableAzureDataCollection.HasValue && - DataCollectionProfile.EnableAzureDataCollection.Value) + if (AzurePSDataCollectionProfile.Instance.EnableAzureDataCollection == true) { return true; } diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs index 60978f0e611e..4c613d76ae13 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs @@ -39,7 +39,7 @@ public Predictor(IList modelPredictions, ParameterValuePredictor paramet this._parameterValuePredictor = parameterValuePredictor; this._predictions = new List(); - foreach (var predictionTextRaw in modelPredictions) + foreach (var predictionTextRaw in modelPredictions ?? Enumerable.Empty()) { var predictionText = EscapePredictionText(predictionTextRaw); Ast ast = Parser.ParseInput(predictionText, out Token[] tokens, out _); diff --git a/tools/Az.Tools.Predictor/build.proj b/tools/Az.Tools.Predictor/build.proj index 23ba552c0883..11a3ab126a6b 100644 --- a/tools/Az.Tools.Predictor/build.proj +++ b/tools/Az.Tools.Predictor/build.proj @@ -22,21 +22,13 @@ - build - publish + build - - - - - - -