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 map saving to a local file #14018

Merged
merged 4 commits into from
Feb 5, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
using System.Collections.Generic;
using System.IO;
using System.Management.Automation.Language;
using System.Threading.Tasks;
using System.Threading;
using System.Text.Json;



namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
Expand All @@ -32,7 +32,13 @@ 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;
private System.Threading.Mutex _mutex = new System.Threading.Mutex(false, "paramValueHistoryFile_update");

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

private string _paramValueHistoryFilePath = "";
private CancellationTokenSource _cancellationTokenSource;


private ITelemetryClient _telemetryClient;

Expand All @@ -49,17 +55,46 @@ public ParameterValuePredictor(ITelemetryClient telemetryClient)

try
{
_command_param_to_resource_map = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(File.ReadAllText(mappingFilePath), JsonUtilities.DefaultSerializerOptions);
_commandParamToResourceMap = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(File.ReadAllText(mappingFilePath), JsonUtilities.DefaultSerializerOptions);
}
catch (Exception e)
{
// We don't want it to crash the module when the file doesn't exist or when it's mal-formatted.
exception = e;
}

_telemetryClient.OnLoadParameterMap(new ParameterMapTelemetryData(exception));

String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string[] paths = new string[] { path, "Microsoft", "Windows", "PowerShell", "AzPredictor", "paramValueHistory.json" };
_paramValueHistoryFilePath = System.IO.Path.Combine(paths);
Directory.CreateDirectory(Path.GetDirectoryName(_paramValueHistoryFilePath));

Task.Run(() =>
{
if (System.IO.File.Exists(_paramValueHistoryFilePath))
{
_mutex.WaitOne();
try
{
var localParameterValues = JsonSerializer.Deserialize<ConcurrentDictionary<string, string>>(File.ReadAllText(_paramValueHistoryFilePath), JsonUtilities.DefaultSerializerOptions);
foreach (var v in localParameterValues)
{
_localParameterValues.AddOrUpdate(v.Key, key => v.Value, (key, oldValue) => oldValue);
}
}
finally
{
_mutex.ReleaseMutex();
}
}


});

}



/// <summary>
/// Process the command from history
/// </summary>
Expand Down Expand Up @@ -89,7 +124,7 @@ public string GetParameterValueFromAzCommand(string commandNoun, string paramete
var key = parameterName;
Dictionary<string, string> commandNounMap = null;

if (_command_param_to_resource_map?.TryGetValue(commandNoun, out commandNounMap) == true)
if (_commandParamToResourceMap?.TryGetValue(commandNoun, out commandNounMap) == true)
{
if (commandNounMap.TryGetValue(parameterName, out var parameterNameMappedValue))
{
Expand Down Expand Up @@ -146,7 +181,7 @@ private void ExtractLocalParameters(CommandAst command)
}

Dictionary<string, string> commandNounMap = null;
_command_param_to_resource_map?.TryGetValue(commandNoun, out commandNounMap);
_commandParamToResourceMap?.TryGetValue(commandNoun, out commandNounMap);

for (int i = 1; i < command.CommandElements.Count;)
{
Expand Down Expand Up @@ -189,8 +224,26 @@ private void ExtractLocalParameters(CommandAst command)
parameterKey = mappedValue;
}
}

_localParameterValues.AddOrUpdate(parameterKey, parameterValue, (k, v) => parameterValue);
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
this._localParameterValues.AddOrUpdate(parameterKey, parameterValue, (k, v) => parameterValue);
if (_cancellationTokenSource.IsCancellationRequested)
{
throw new OperationCanceledException();
}
String localParameterValuesJson = JsonSerializer.Serialize<ConcurrentDictionary<string, string>>(_localParameterValues, JsonUtilities.DefaultSerializerOptions);
_mutex.WaitOne();
try
{
System.IO.File.WriteAllText(_paramValueHistoryFilePath, localParameterValuesJson);
}
finally
{
_mutex.ReleaseMutex();
}
});
}
}
}
Expand Down