Exemple #1
0
        public void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success)
        {
            if (success && AzPredictor._azAccountCommands.Contains(commandLine.Trim().Split().FirstOrDefault()))
            {
                // The context only changes when the user executes the corresponding command successfully.
                _azContext?.UpdateContext();
            }

            if (!_parsedCommandLineHistory.TryRemove(commandLine, out var parsedResult))
            {
                // We should already parsed the last command in OnCommandLineAccepted which we don't need to do again now.
                // Just in case that wasn't correct or that's missing, we clear the _parsedCommandLineHistory and parse it now.
                // On possible reason it's missing is because we're still initializing in the task and don't handle
                // OnCommandLineAccepted.
                _parsedCommandLineHistory.Clear();
                parsedResult = GetAstAndMaskedCommandLine(commandLine);
            }

            if (parsedResult.IsSupported && _surveyHelper?.ShouldPromptSurvey() == true)
            {
                _surveyHelper.PromptSurvey();
            }

            _telemetryClient.OnHistory(new HistoryTelemetryData(client, parsedResult.MaskedCommandLine ?? AzPredictorConstants.CommandPlaceholder, success));
            _commandLineExecutedCompletion?.SetResult();
        }
 public void OnCommandLineAccepted(PredictionClient client, IReadOnlyList <string> history)
 {
     foreach (string item in history)
     {
         History.Add($"{client.Name}-{item}");
     }
 }
Exemple #3
0
        /// <inhericdoc />
        public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken)
        {
            var localSuggestionSessionId = _suggestionSessionId++;

            if (!_isInitialized || _settings.SuggestionCount.Value <= 0)
            {
                return(CreateResult(null));
            }

            Exception             exception   = null;
            CommandLineSuggestion suggestions = null;

            try
            {
                var localCancellationToken = Settings.ContinueOnTimeout ? CancellationToken.None : cancellationToken;

                suggestions = _service.GetSuggestion(context, _settings.SuggestionCount.Value, _settings.MaxAllowedCommandDuplicate.Value, localCancellationToken);

                var returnedValue = suggestions?.PredictiveSuggestions?.ToList();
                return(CreateResult(returnedValue));
            }
            catch (Exception e) when(!(e is OperationCanceledException))
            {
                exception = e;
                return(CreateResult(null));
            }
            finally
            {
                _telemetryClient.OnGetSuggestion(new GetSuggestionTelemetryData(client, localSuggestionSessionId, context.InputAst,
                                                                                suggestions,
                                                                                cancellationToken.IsCancellationRequested,
                                                                                exception));
            }

            SuggestionPackage CreateResult(List <PredictiveSuggestion> suggestions)
            {
                if ((suggestions == null) || (suggestions.Count == 0))
                {
                    return(default(SuggestionPackage));
                }

                // Replace the last suggestion with "Open-AzPredictorSurvey".

                if (suggestions.Count == _settings.SuggestionCount.Value)
                {
                    suggestions[suggestions.Count - 1] = _surveySuggestions[_azContext?.Cohort ?? 0];
                }
                else
                {
                    suggestions.Add(_surveySuggestions[_azContext?.Cohort ?? 0]);
                }

                return(new SuggestionPackage(localSuggestionSessionId, suggestions));
            }
        }
Exemple #4
0
 /// <inhericdoc />
 public void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex)
 {
     if (countOrIndex > 0)
     {
         _telemetryClient.OnSuggestionDisplayed(SuggestionDisplayedTelemetryData.CreateForListView(client, session, countOrIndex));
     }
     else
     {
         _telemetryClient.OnSuggestionDisplayed(SuggestionDisplayedTelemetryData.CreateForInlineView(client, session, -countOrIndex));
     }
 }
Exemple #5
0
        /// <inhericdoc />
        public bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback)
        {
            if (!_isInitialized)
            {
                return(false);
            }

            switch (feedback)
            {
            case PredictorFeedbackKind.SuggestionDisplayed:
            case PredictorFeedbackKind.SuggestionAccepted:
            case PredictorFeedbackKind.CommandLineAccepted:
            case PredictorFeedbackKind.CommandLineExecuted:
                return(true);

            default:
                return(false);
            }
        }
        public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken)
        {
            if (_delay)
            {
                // The delay is exaggerated to make the test reliable.
                // xUnit must spin up a lot tasks, which makes the test unreliable when the time difference between 'delay' and 'timeout' is small.
                Thread.Sleep(2000);
            }

            // You can get the user input from the AST.
            var userInput = context.InputAst.Extent.Text;
            var entries   = new List <PredictiveSuggestion>
            {
                new PredictiveSuggestion($"'{userInput}' from '{client.Name}' - TEST-1 from {Name}"),
                new PredictiveSuggestion($"'{userInput}' from '{client.Name}' - TeSt-2 from {Name}"),
            };

            return(new SuggestionPackage(56, entries));
        }
        public async Task <string> GetImageTags(Stream imageStream)
        {
            var result = await PredictionClient.PredictImageAsync(ProjectId, imageStream, IterationId);

            string resultStr = string.Empty;

            foreach (PredictionModel pred in result.Predictions)
            {
                if (pred.Probability >= 0.85)
                {
                    resultStr += pred.TagName + " ";
                }
            }

            if (resultStr == string.Empty)
            {
                resultStr = "Unable to classify image.";
            }

            return(resultStr);
        }
Exemple #8
0
 /// <inhericdoc />
 public void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion)
 {
     _telemetryClient.OnSuggestionAccepted(new SuggestionAcceptedTelemetryData(client, session, acceptedSuggestion));
 }
Exemple #9
0
        /// <inhericdoc />
        public void OnCommandLineAccepted(PredictionClient client, IReadOnlyList <string> history)
        {
            _commandLineExecutedCompletion = new TaskCompletionSource();

            if (history.Count > 0)
            {
                // We try to find the commands to request predictions for.
                // We should only have "start_of_snippet" when there are no enough Az commands for prediction.
                // We only send the requests when Az commands are changed. So we'll never add "start_of_snippet" again
                // once we have enough Az commands.
                // This is the scenario.
                // 1. New-AzResourceGroup -Name ****
                // 2. $resourceName="Test"
                // 3. $resourceLocation="westus2"
                // 4. New-AzVM -Name $resourceName -Location $resourceLocation
                //
                // If the history only contains 1, we'll add "start_of_snippet" to the request.
                // We'll replace 2 and 3 with "start_of_snippet". But if we request prediction using 2 and 3, that'll reset the
                // workflow. We want to predict only by Az commands. So we don't send the request until the command 4.
                // That's to use commands 1 and 4 to request prediction.

                bool ShouldRequestPrediction = false;

                if (_lastTwoMaskedCommands.Count == 0)
                {
                    // This is the first time we populate our record. Push the second to last command in history to the
                    // queue. If there is only one command in history, push the command placeholder.

                    if (history.Count() > 1)
                    {
                        string secondToLastLine    = history.TakeLast(AzPredictorConstants.CommandHistoryCountToProcess).First();
                        var    secondToLastCommand = GetAstAndMaskedCommandLine(secondToLastLine);
                        _lastTwoMaskedCommands.Enqueue(secondToLastCommand.IsSupported ? secondToLastCommand.MaskedCommandLine : AzPredictorConstants.CommandPlaceholder);

                        if (secondToLastCommand.IsSupported)
                        {
                            _service.RecordHistory(secondToLastCommand.Ast);
                        }
                    }
                    else
                    {
                        _lastTwoMaskedCommands.Enqueue(AzPredictorConstants.CommandPlaceholder);
                        // We only extract parameter values from the command line in _service.RecordHistory.
                        // So we don't need to do that for a placeholder.
                    }

                    ShouldRequestPrediction = true;
                }

                string lastLine               = history.Last();
                var    lastCommand            = GetAstAndMaskedCommandLine(lastLine);
                bool   isLastCommandSupported = lastCommand.IsSupported;

                _parsedCommandLineHistory.TryAdd(lastLine, lastCommand);

                if (isLastCommandSupported)
                {
                    if (_lastTwoMaskedCommands.Count == 2)
                    {
                        // There are already two commands, dequeue the oldest one.
                        _lastTwoMaskedCommands.Dequeue();
                    }

                    _lastTwoMaskedCommands.Enqueue(lastCommand.MaskedCommandLine);
                    ShouldRequestPrediction = true;

                    _service.RecordHistory(lastCommand.Ast);
                }
                else if (_lastTwoMaskedCommands.Count == 1)
                {
                    ShouldRequestPrediction = true;
                    var existingInQueue = _lastTwoMaskedCommands.Dequeue();
                    _lastTwoMaskedCommands.Enqueue(AzPredictorConstants.CommandPlaceholder);
                    _lastTwoMaskedCommands.Enqueue(existingInQueue);
                }


                if (ShouldRequestPrediction)
                {
                    // When it's called multiple times, we only need to keep the one for the latest command.

                    _predictionRequestCancellationSource?.Cancel();
                    _predictionRequestCancellationSource = new CancellationTokenSource();
                    // Need to create a new object to hold the string. They're used in a seperate thread the the contents in
                    // _lastTwoMaskedCommands may change when the method is called again.
                    var       lastTwoMaskedCommands = new List <string>(_lastTwoMaskedCommands);
                    Exception exception             = null;
                    var       hasSentHttpRequest    = false;

                    // We don't need to block on the task. It sends the HTTP request and update prediction list. That can run at the background.
                    Task.Run(async() =>
                    {
                        var localCommandLineExecutedCompletion = _commandLineExecutedCompletion;
                        var requestId = Guid.NewGuid().ToString();

                        try
                        {
                            hasSentHttpRequest = await _service.RequestPredictionsAsync(lastTwoMaskedCommands, requestId, _predictionRequestCancellationSource.Token);
                        }
                        catch (ServiceRequestException e)
                        {
                            hasSentHttpRequest = e.IsRequestSent;
                            exception          = e.InnerException;
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                        finally
                        {
                            await localCommandLineExecutedCompletion.Task;
                            _telemetryClient.RequestId = requestId;
                            _telemetryClient.OnRequestPrediction(new RequestPredictionTelemetryData(client, lastTwoMaskedCommands,
                                                                                                    hasSentHttpRequest,
                                                                                                    (exception is OperationCanceledException ? null : exception)));
                        }
                    }, _predictionRequestCancellationSource.Token);
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// Requests preditions and collects telemetry event.
        /// </summary>
        /// <param name="azPredictorService">The service to send the request.</param>
        /// <param name="telemetryClient">The telemetry client to collect the data.</param>
        /// <param name="predictionClient">The client that initiate the telemetry event.</param>
        /// <param name="commands">A list of commands.</param>
        /// <param name="telemetryWaitTask">The task to wait before we collect the telemetry data.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public static async Task RequestPredictionAndCollectTelemetryAync(IAzPredictorService azPredictorService, ITelemetryClient telemetryClient, PredictionClient predictionClient, IEnumerable <string> commands, TaskCompletionSource telemetryWaitTask, CancellationToken cancellationToken)
        {
            var       requestId          = Guid.NewGuid().ToString();
            bool?     hasSentHttpRequest = default;
            Exception exception          = null;

            try
            {
                hasSentHttpRequest = await azPredictorService.RequestPredictionsAsync(commands, requestId, cancellationToken);
            }
            catch (ServiceRequestException e)
            {
                hasSentHttpRequest = e.IsRequestSent;
                exception          = e.InnerException;
            }
            catch (Exception e) when(!(e is OperationCanceledException))
            {
                exception = e;
            }
            finally
            {
                if (telemetryWaitTask != null)
                {
                    await telemetryWaitTask.Task;
                }

                if (hasSentHttpRequest.HasValue)
                {
                    telemetryClient.RequestId = requestId;
                    telemetryClient.OnRequestPrediction(new RequestPredictionTelemetryData(predictionClient,
                                                                                           commands,
                                                                                           hasSentHttpRequest.Value,
                                                                                           exception));
                }
            }
        }
        public static void RegisterSubsystem()
        {
            try
            {
                Assert.Throws <ArgumentNullException>(
                    paramName: "proxy",
                    () => SubsystemManager.RegisterSubsystem <ICommandPredictor, MyPredictor>(null));
                Assert.Throws <ArgumentNullException>(
                    paramName: "proxy",
                    () => SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, null));
                Assert.Throws <ArgumentException>(
                    paramName: "proxy",
                    () => SubsystemManager.RegisterSubsystem((SubsystemKind)0, predictor1));

                // Register 'predictor1'
                SubsystemManager.RegisterSubsystem <ICommandPredictor, MyPredictor>(predictor1);

                // Now validate the SubsystemInfo of the 'ICommandPredictor' subsystem
                SubsystemInfo ssInfo = SubsystemManager.GetSubsystemInfo(typeof(ICommandPredictor));
                SubsystemInfo crossPlatformDscInfo = SubsystemManager.GetSubsystemInfo(typeof(ICrossPlatformDsc));
                VerifyCommandPredictorMetadata(ssInfo);
                Assert.True(ssInfo.IsRegistered);
                Assert.Single(ssInfo.Implementations);

                // Now validate the 'ImplementationInfo'
                var implInfo = ssInfo.Implementations[0];
                Assert.Equal(predictor1.Id, implInfo.Id);
                Assert.Equal(predictor1.Name, implInfo.Name);
                Assert.Equal(predictor1.Description, implInfo.Description);
                Assert.Equal(SubsystemKind.CommandPredictor, implInfo.Kind);
                Assert.Same(typeof(MyPredictor), implInfo.ImplementationType);

                // Now validate the subsystem implementation itself.
                ICommandPredictor impl = SubsystemManager.GetSubsystem <ICommandPredictor>();
                Assert.Same(impl, predictor1);
                Assert.Null(impl.FunctionsToDefine);
                Assert.Equal(SubsystemKind.CommandPredictor, impl.Kind);

                const string Client     = "SubsystemTest";
                const string Input      = "Hello world";
                var          predClient = new PredictionClient(Client, PredictionClientKind.Terminal);
                var          predCxt    = PredictionContext.Create(Input);
                var          results    = impl.GetSuggestion(predClient, predCxt, CancellationToken.None);
                Assert.Equal($"'{Input}' from '{Client}' - TEST-1 from {impl.Name}", results.SuggestionEntries[0].SuggestionText);
                Assert.Equal($"'{Input}' from '{Client}' - TeSt-2 from {impl.Name}", results.SuggestionEntries[1].SuggestionText);

                // Now validate the all-subsystem-implementation collection.
                ReadOnlyCollection <ICommandPredictor> impls = SubsystemManager.GetSubsystems <ICommandPredictor>();
                Assert.Single(impls);
                Assert.Same(predictor1, impls[0]);

                // Register 'predictor2'
                SubsystemManager.RegisterSubsystem(SubsystemKind.CommandPredictor, predictor2);

                // Now validate the SubsystemInfo of the 'ICommandPredictor' subsystem
                VerifyCommandPredictorMetadata(ssInfo);
                Assert.True(ssInfo.IsRegistered);
                Assert.Equal(2, ssInfo.Implementations.Count);

                // Now validate the new 'ImplementationInfo'
                implInfo = ssInfo.Implementations[1];
                Assert.Equal(predictor2.Id, implInfo.Id);
                Assert.Equal(predictor2.Name, implInfo.Name);
                Assert.Equal(predictor2.Description, implInfo.Description);
                Assert.Equal(SubsystemKind.CommandPredictor, implInfo.Kind);
                Assert.Same(typeof(MyPredictor), implInfo.ImplementationType);

                // Now validate the new subsystem implementation.
                impl = SubsystemManager.GetSubsystem <ICommandPredictor>();
                Assert.Same(impl, predictor2);

                // Now validate the all-subsystem-implementation collection.
                impls = SubsystemManager.GetSubsystems <ICommandPredictor>();
                Assert.Equal(2, impls.Count);
                Assert.Same(predictor1, impls[0]);
                Assert.Same(predictor2, impls[1]);
            }
            finally
            {
                SubsystemManager.UnregisterSubsystem <ICommandPredictor>(predictor1.Id);
                SubsystemManager.UnregisterSubsystem(SubsystemKind.CommandPredictor, predictor2.Id);
            }
        }
        /// <summary>
        /// Add the common properties to the telemetry event.
        /// </summary>
        private IDictionary <string, string> CreateProperties(ITelemetryData telemetryData, PredictionClient client)
        {
            var properties = TelemetryUtilities.CreateCommonProperties(this._azContext);

            properties.Add("RequestId", telemetryData.RequestId);
            properties.Add("CommandId", telemetryData.CommandId);

            if (client != null)
            {
                properties.Add("ClientId", client.Name);
                properties.Add("ClientType", client.Kind.ToString());
            }

            return(properties);
        }
 bool ICommandPredictor.CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback) => true;
 public void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success)
 {
     Results.Add($"{client.Name}-{commandLine}-{success}");
 }
 public void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion)
 {
     AcceptedSuggestions.Add($"{client.Name}-{session}-{acceptedSuggestion}");
 }
 public void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex)
 {
     DisplayedSuggestions.Add($"{client.Name}-{session}-{countOrIndex}");
 }