From f1df24d02f49d9ea1ebbb5f4a3f8937576c58c30 Mon Sep 17 00:00:00 2001 From: Yevhen Mohylevskyy Date: Mon, 25 Jan 2021 15:12:48 -0800 Subject: [PATCH 1/3] add local parameter map saving to a local file --- .../Az.Tools.Predictor/AzPredictorSettings.json | 2 +- .../Az.Tools.Predictor/ParameterValuePredictor.cs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json index 50e37963ef68..9200a4d20921 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json @@ -1,5 +1,5 @@ { "maxAllowedCommandDuplicate": 1, - "serviceUri": "https://app.aladdin.microsoft.com/api/v2.0", + "serviceUri": "https://app.aladdindev.microsoft.com/api/v2.0", "suggestionCount": 7 } diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs index 9d2d3246a8c4..e6fa6722c76b 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs @@ -20,6 +20,8 @@ sealed class ParameterValuePredictor { private readonly ConcurrentDictionary _localParameterValues = new ConcurrentDictionary(); + private string paramValueHistoryFilePath = ""; + private readonly Dictionary> _command_param_to_resource_map; public ParameterValuePredictor() @@ -28,6 +30,16 @@ public ParameterValuePredictor() var directory = fileInfo.DirectoryName; var mappingFilePath = Path.Join(directory, "command_param_to_resource_map.json"); _command_param_to_resource_map = JsonSerializer.Deserialize>>(File.ReadAllText(mappingFilePath), JsonUtilities.DefaultSerializerOptions); + + String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + string[] paths = new string[] { path, "Microsoft", "Windows", "PowerShell", "PSReadLine", "paramValueHistory.json" }; + paramValueHistoryFilePath = System.IO.Path.Combine(paths); + //Console.WriteLine(filePath); + if (System.IO.File.Exists(paramValueHistoryFilePath)) + { + _localParameterValues = JsonSerializer.Deserialize>(File.ReadAllText(paramValueHistoryFilePath), JsonUtilities.DefaultSerializerOptions); + } + } /// @@ -119,6 +131,8 @@ private void ExtractLocalParameters(System.Collections.ObjectModel.ReadOnlyColle var key = _command_param_to_resource_map[commandNoun][parameterName]; var parameterValue = command[i].ToString(); this._localParameterValues.AddOrUpdate(key, parameterValue, (k, v) => parameterValue); + String localParameterValuesJson = JsonSerializer.Serialize>(_localParameterValues, JsonUtilities.DefaultSerializerOptions); + System.IO.File.WriteAllText(paramValueHistoryFilePath, localParameterValuesJson); } } } From ed292f1ea834ae637cbe348dcadb55732df9e919 Mon Sep 17 00:00:00 2001 From: Yevhen Mohylevskyy Date: Wed, 27 Jan 2021 15:15:55 -0800 Subject: [PATCH 2/3] fix local parameter map saving --- .../ParameterValuePredictor.cs | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs index e6fa6722c76b..4acbb1a3c492 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterValuePredictor.cs @@ -8,8 +8,8 @@ using System.Collections.Generic; using System.Linq; using System.Management.Automation.Language; - - +using System.Threading.Tasks; +using System.Threading; namespace Microsoft.Azure.PowerShell.Tools.AzPredictor { @@ -20,25 +20,47 @@ sealed class ParameterValuePredictor { private readonly ConcurrentDictionary _localParameterValues = new ConcurrentDictionary(); - private string paramValueHistoryFilePath = ""; + private System.Threading.Mutex _mutex = new System.Threading.Mutex(false, "paramValueHistoryFile_update"); + + private readonly Dictionary> _commandPparamToResourceMap; + + private string _paramValueHistoryFilePath = ""; + private CancellationTokenSource _cancellationTokenSource; - private readonly Dictionary> _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 = JsonSerializer.Deserialize>>(File.ReadAllText(mappingFilePath), JsonUtilities.DefaultSerializerOptions); + _commandPparamToResourceMap = JsonSerializer.Deserialize>>(File.ReadAllText(mappingFilePath), JsonUtilities.DefaultSerializerOptions); String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); - string[] paths = new string[] { path, "Microsoft", "Windows", "PowerShell", "PSReadLine", "paramValueHistory.json" }; - paramValueHistoryFilePath = System.IO.Path.Combine(paths); - //Console.WriteLine(filePath); - if (System.IO.File.Exists(paramValueHistoryFilePath)) + string[] paths = new string[] { path, "Microsoft", "Windows", "PowerShell", "AzPredictor", "paramValueHistory.json" }; + _paramValueHistoryFilePath = System.IO.Path.Combine(paths); + Directory.CreateDirectory(Path.GetDirectoryName(_paramValueHistoryFilePath)); + + Task.Run(() => { - _localParameterValues = JsonSerializer.Deserialize>(File.ReadAllText(paramValueHistoryFilePath), JsonUtilities.DefaultSerializerOptions); - } + if (System.IO.File.Exists(_paramValueHistoryFilePath)) + { + _mutex.WaitOne(); + try + { + var localParameterValues = JsonSerializer.Deserialize>(File.ReadAllText(_paramValueHistoryFilePath), JsonUtilities.DefaultSerializerOptions); + foreach (var v in localParameterValues) + { + _localParameterValues.AddOrUpdate(v.Key, key => v.Value, (key, oldValue) => oldValue); + } + } + finally + { + _mutex.ReleaseMutex(); + } + } + + + }); } @@ -67,12 +89,12 @@ public void ProcessHistoryCommand(CommandAst command) /// The parameter value from the history command. Null if that is not available. public string GetParameterValueFromAzCommand(string commandNoun, string parameterName) { - if (_command_param_to_resource_map.ContainsKey(commandNoun)) + if (_commandPparamToResourceMap.ContainsKey(commandNoun)) { parameterName = parameterName.ToLower(); - if (_command_param_to_resource_map[commandNoun].ContainsKey(parameterName)) + if (_commandPparamToResourceMap[commandNoun].ContainsKey(parameterName)) { - var key = _command_param_to_resource_map[commandNoun][parameterName]; + var key = _commandPparamToResourceMap[commandNoun][parameterName]; if (_localParameterValues.TryGetValue(key, out var value)) { return value; @@ -124,15 +146,32 @@ private void ExtractLocalParameters(System.Collections.ObjectModel.ReadOnlyColle if (command[i - 1] is CommandParameterAst parameterAst && command[i] is StringConstantExpressionAst) { var parameterName = command[i - 1].ToString().ToLower().Trim('-'); - if (_command_param_to_resource_map.ContainsKey(commandNoun)) + if (_commandPparamToResourceMap.ContainsKey(commandNoun)) { - if (_command_param_to_resource_map[commandNoun].ContainsKey(parameterName)) + if (_commandPparamToResourceMap[commandNoun].ContainsKey(parameterName)) { - var key = _command_param_to_resource_map[commandNoun][parameterName]; + _cancellationTokenSource?.Cancel(); + _cancellationTokenSource = new CancellationTokenSource(); + var key = _commandPparamToResourceMap[commandNoun][parameterName]; var parameterValue = command[i].ToString(); - this._localParameterValues.AddOrUpdate(key, parameterValue, (k, v) => parameterValue); - String localParameterValuesJson = JsonSerializer.Serialize>(_localParameterValues, JsonUtilities.DefaultSerializerOptions); - System.IO.File.WriteAllText(paramValueHistoryFilePath, localParameterValuesJson); + Task.Run(() => + { + this._localParameterValues.AddOrUpdate(key, parameterValue, (k, v) => parameterValue); + if (_cancellationTokenSource.IsCancellationRequested) + { + throw new OperationCanceledException(); + } + String localParameterValuesJson = JsonSerializer.Serialize>(_localParameterValues, JsonUtilities.DefaultSerializerOptions); + _mutex.WaitOne(); + try + { + System.IO.File.WriteAllText(_paramValueHistoryFilePath, localParameterValuesJson); + } + finally + { + _mutex.ReleaseMutex(); + } + }); } } } From 0333bf752addccbda2ee529fe143dc9b5a4b597b Mon Sep 17 00:00:00 2001 From: Yevhen Mohylevskyy Date: Wed, 27 Jan 2021 15:54:56 -0800 Subject: [PATCH 3/3] revere serviceUri change in AzPredictorSettings --- .../Az.Tools.Predictor/AzPredictorSettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json index 9200a4d20921..50e37963ef68 100644 --- a/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorSettings.json @@ -1,5 +1,5 @@ { "maxAllowedCommandDuplicate": 1, - "serviceUri": "https://app.aladdindev.microsoft.com/api/v2.0", + "serviceUri": "https://app.aladdin.microsoft.com/api/v2.0", "suggestionCount": 7 }