Skip to content

Commit 4aedaa4

Browse files
authored
Interface for recommendation service (#373)
* Interface for recommendation service * Make Process abstract
1 parent 24150dd commit 4aedaa4

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/Common/AzurePSCmdlet.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
5656
// Using Ansi Code to control font color(97(Bold White)) and background color(0;120;212(RGB))
5757
private static readonly string ansiCodePrefix = "\u001b[97;48;2;0;120;212m";
5858

59-
// using '[k' for erase in line. '[0m' to ending ansi code
59+
// using '[k' for erase in line. '[0m' to ending ansi code
6060
private static readonly string ansiCodeSuffix = "\u001b[K\u001b[0m";
6161

6262
protected AzurePSDataCollectionProfile _dataCollectionProfile
@@ -386,8 +386,10 @@ private void WriteBreakingChangeOrPreviewMessage()
386386
/// </summary>
387387
protected override void EndProcessing()
388388
{
389-
if (MetricHelper.IsCalledByUser()
390-
&& SurveyHelper.GetInstance().ShouldPromptAzSurvey()
389+
WriteEndProcessingRecommendation();
390+
391+
if (MetricHelper.IsCalledByUser()
392+
&& SurveyHelper.GetInstance().ShouldPromptAzSurvey()
391393
&& (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)
392394
&& !configManager.GetConfigValue<bool>(ConfigKeysForCommon.EnableInterceptSurvey).Equals(false)))
393395
{
@@ -413,6 +415,13 @@ protected override void EndProcessing()
413415
base.EndProcessing();
414416
}
415417

418+
private void WriteEndProcessingRecommendation()
419+
{
420+
if (AzureSession.Instance.TryGetComponent<IEndProcessingRecommendationService>(nameof(IEndProcessingRecommendationService), out var service))
421+
{
422+
service.Process(this, MyInvocation, _qosEvent);
423+
}
424+
}
416425

417426
protected string CurrentPath()
418427
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Management.Automation;
16+
17+
namespace Microsoft.WindowsAzure.Commands.Utilities.Common
18+
{
19+
/// <summary>
20+
/// A service to give recommendations based on the input of the user.
21+
/// For example we could recommend a region which costs less.
22+
/// </summary>
23+
public abstract class IEndProcessingRecommendationService
24+
{
25+
/// <summary>
26+
/// Process the cmdlet execution and display recommendation messages if any.
27+
/// </summary>
28+
/// <param name="azurePSCmdlet">Cmdlet instance, for writing messages.</param>
29+
/// <param name="myInvocation">Contains info about cmdlet, module, parameters.</param>
30+
/// <param name="_qosEvent">To record successful recommendation.</param>
31+
public abstract void Process(AzurePSCmdlet azurePSCmdlet, InvocationInfo myInvocation, AzurePSQoSEvent _qosEvent);
32+
}
33+
}

src/Common/MetricHelper.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public string HashMacAddress
119119
try
120120
{
121121
var macAddress = _networkHelper.GetMACAddress();
122-
_hashMacAddress = string.IsNullOrWhiteSpace(macAddress)
122+
_hashMacAddress = string.IsNullOrWhiteSpace(macAddress)
123123
? null : GenerateSha256HashString(macAddress)?.Replace("-", string.Empty)?.ToLowerInvariant();
124124
}
125125
catch
@@ -255,7 +255,7 @@ private void LogExceptionEvent(AzurePSQoSEvent qos)
255255
Dictionary<string, string> eventProperties = new Dictionary<string, string>();
256256
LoadTelemetryClientContext(qos, client.Context);
257257
PopulatePropertiesFromQos(qos, eventProperties);
258-
// qos.Exception contains exception message which may contain Users specific data.
258+
// qos.Exception contains exception message which may contain Users specific data.
259259
// We should not collect users specific data.
260260
eventProperties.Add("Message", "Message removed due to PII.");
261261
eventProperties.Add("StackTrace", qos.Exception.StackTrace);
@@ -401,7 +401,7 @@ private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string,
401401
? qos.Exception.Data[AzurePSErrorDataKeys.ErrorKindKey].ToString()
402402
: null;
403403
cloudErrorCode = (string)qos.Exception.Data[AzurePSErrorDataKeys.CloudErrorCodeKey];
404-
// For the time being, we consider ResourceNotFound and ResourceGroupNotFound as user's input error.
404+
// For the time being, we consider ResourceNotFound and ResourceGroupNotFound as user's input error.
405405
// We are considering if ResourceNotFound should be false positive error.
406406
if (("ResourceNotFound".Equals(cloudErrorCode) || "ResourceGroupNotFound".Equals(cloudErrorCode))
407407
&& existingErrorKind != ErrorKind.FalseError)
@@ -413,7 +413,7 @@ private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string,
413413
StringBuilder sb = new StringBuilder();
414414
foreach (var key in qos.Exception.Data?.Keys)
415415
{
416-
if (AzurePSErrorDataKeys.IsKeyPredefined(key.ToString())
416+
if (AzurePSErrorDataKeys.IsKeyPredefined(key.ToString())
417417
&& !AzurePSErrorDataKeys.HttpStatusCode.Equals(key))
418418
{
419419
if (sb.Length > 0)
@@ -481,8 +481,8 @@ private static string ConvertFrameToString(System.Diagnostics.StackFrame frame)
481481

482482
public bool IsMetricTermAccepted()
483483
{
484-
return _profile != null
485-
&& _profile.EnableAzureDataCollection.HasValue
484+
return _profile != null
485+
&& _profile.EnableAzureDataCollection.HasValue
486486
&& _profile.EnableAzureDataCollection.Value;
487487
}
488488

@@ -604,6 +604,12 @@ public class AzurePSQoSEvent
604604
public string TenantId { get; set; }
605605
public bool SurveyPrompted { get; set; }
606606

607+
/// <summary>
608+
/// Appear in certain resource creation commands like New-AzVM. See RegionalRecommender (PS repo).
609+
/// Represent the recommended region if we do have recommendation.
610+
/// </summary>
611+
public string DisplayRegionIdentified { get; set; }
612+
607613
public string ParameterSetName { get; set; }
608614
public string InvocationName { get; set; }
609615
public Dictionary<string, string> CustomProperties { get; private set; }
@@ -637,8 +643,8 @@ public void FinishQosEvent()
637643
public override string ToString()
638644
{
639645
StringBuilder sb = new StringBuilder("AzureQoSEvent: ");
640-
if (ShowTelemetry)
641-
{
646+
if (ShowTelemetry)
647+
{
642648
foreach(PropertyDescriptor descriptor in TypeDescriptor.GetProperties((this)))
643649
{
644650
string name = descriptor.Name;
@@ -652,7 +658,7 @@ public override string ToString()
652658
{
653659
sb = sb.Append($" Module: {ModuleName}:{ModuleVersion}; CommandName: {CommandName}; PSVersion: {PSVersion}");
654660
}
655-
661+
656662
sb.Append($"; IsSuccess: {IsSuccess}; Duration: {Duration}");
657663

658664
if (Exception != null)

0 commit comments

Comments
 (0)