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

Add local parameter value map to Az.Predictor #13739

Merged
merged 5 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ For more information on Az Predictor, please visit the following: https://aka.ms
<ItemGroup>
<None Include="Az.Tools.Predictor.psd1" CopyToOutputDirectory="PreserveNewest" />
<None Include="AzPredictorSettings.json" CopyToOutputDirectory="PreserveNewest" />
<None Include="command_param_to_resource_map.json" CopyToOutputDirectory="PreserveNewest" />
kceiw marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
using Newtonsoft.Json;
kceiw marked this conversation as resolved.
Show resolved Hide resolved



namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
Expand All @@ -18,6 +25,16 @@ sealed class ParameterValuePredictor

private readonly ConcurrentDictionary<string, string> _localParameterValues = new ConcurrentDictionary<string, string>();

private readonly Dictionary<string, Dictionary<string, string>> _command_param_to_resource_map;

public ParameterValuePredictor()
{
var fileInfo = new FileInfo(typeof(Settings).Assembly.Location);
var directory = fileInfo.DirectoryName;
var mappingFilePath = Path.Join(directory, "command_param_to_resource_map.json");
_command_param_to_resource_map = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(File.ReadAllText(mappingFilePath));
kceiw marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Process the command from history
/// </summary>
Expand All @@ -38,16 +55,24 @@ public void ProcessHistoryCommand(CommandAst command)
/// > Get-AzVM -VMName &lt;TestVM&gt;
/// "TestVM" is predicted for Get-AzVM.
/// </summary>
/// <param name="commandName">The command noun</param>
/// <param name="parameterName">The parameter name</param>
/// <returns>The parameter value from the history command. Null if that is not available.</returns>
public string GetParameterValueFromAzCommand(string parameterName)
public string GetParameterValueFromAzCommand(string commandName, string parameterName)
{
parameterName = parameterName.TrimStart(AzPredictorConstants.ParameterIndicator);
if (_localParameterValues.TryGetValue(parameterName.ToUpper(), out var value))
var commandNoun = ParameterValuePredictor.GetAzCommandNoun(commandName); ;

if (_command_param_to_resource_map.ContainsKey(commandNoun.ToLower()))
{
return value;
if (_command_param_to_resource_map[commandNoun.ToLower()].ContainsKey(parameterName.ToLower()))
kceiw marked this conversation as resolved.
Show resolved Hide resolved
{
var key = _command_param_to_resource_map[commandNoun.ToLower()][parameterName.ToLower()];
if (_localParameterValues.TryGetValue(key, out var value))
{
return value;
}
}
}

return null;
}

Expand Down Expand Up @@ -102,11 +127,17 @@ private void ExtractLocalParameters(System.Collections.ObjectModel.ReadOnlyColle
{
if (command[i - 1] is CommandParameterAst && command[i] is StringConstantExpressionAst)
{
var parameterName = command[i - 1].ToString().TrimStart(AzPredictorConstants.ParameterIndicator);
var key = ParameterValuePredictor.GetLocalParameterKey(commandNoun, parameterName);
var parameterValue = command[i].ToString();
this._localParameterValues.AddOrUpdate(key, parameterValue, (k, v) => parameterValue);
}
var parameterName = command[i - 1].ToString().ToLower();
if (_command_param_to_resource_map.ContainsKey(commandNoun.ToLower()))
{
if (_command_param_to_resource_map[commandNoun.ToLower()].ContainsKey(parameterName))
{
var key = _command_param_to_resource_map[commandNoun.ToLower()][parameterName];
var parameterValue = command[i].ToString();
this._localParameterValues.AddOrUpdate(key, parameterValue, (k, v) => parameterValue);
}
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ private void BuildParameterValue(StringBuilder builder, Tuple<string, string> pa
_ = builder.Append(AzPredictorConstants.CommandParameterSeperator);
_ = builder.Append(parameterName);

string parameterValue = this._parameterValuePredictor?.GetParameterValueFromAzCommand(parameterName);

string commandName = builder.ToString().Split(' ')[0];
kceiw marked this conversation as resolved.
Show resolved Hide resolved
string parameterValue = this._parameterValuePredictor?.GetParameterValueFromAzCommand(commandName,parameterName);

if (string.IsNullOrWhiteSpace(parameterValue))
{
Expand Down

Large diffs are not rendered by default.