Пример #1
0
        /// <inhericdoc />
        public List <PredictiveSuggestion> GetSuggestion(PredictionContext context, CancellationToken cancellationToken)
        {
            if (_settings.SuggestionCount.Value <= 0)
            {
                return(new List <PredictiveSuggestion>());
            }

            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(returnedValue ?? new List <PredictiveSuggestion>());
            }
            catch (Exception e) when(!(e is OperationCanceledException))
            {
                exception = e;
                return(new List <PredictiveSuggestion>());
            }
            finally
            {
                _telemetryClient.OnGetSuggestion(new GetSuggestionTelemetryData(context.InputAst,
                                                                                suggestions,
                                                                                cancellationToken.IsCancellationRequested,
                                                                                exception));
            }
        }
Пример #2
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));
            }
        }
        /// <inhericdoc />
        public List <PredictiveSuggestion> GetSuggestion(PredictionContext context, CancellationToken cancellationToken)
        {
            var localCancellationToken = Settings.ContinueOnTimeout ? CancellationToken.None : cancellationToken;

            IEnumerable <ValueTuple <string, string, PredictionSource> > suggestions = Enumerable.Empty <ValueTuple <string, string, PredictionSource> >();
            string maskedUserInput = string.Empty;
            // This is the list of records of the source suggestion and the prediction source.
            var telemetryData = new List <ValueTuple <string, PredictionSource> >();

            try
            {
                maskedUserInput = AzPredictor.MaskCommandLine(context.InputAst.FindAll((ast) => ast is CommandAst, true).LastOrDefault() as CommandAst);

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

                localCancellationToken.ThrowIfCancellationRequested();

                var userAcceptedAndSuggestion = new Dictionary <string, string>();

                foreach (var s in suggestions)
                {
                    telemetryData.Add(ValueTuple.Create(s.Item2, s.Item3));
                    userAcceptedAndSuggestion[s.Item1] = s.Item2;
                }

                lock (_userAcceptedAndSuggestion)
                {
                    foreach (var u in userAcceptedAndSuggestion)
                    {
                        _userAcceptedAndSuggestion[u.Key] = u.Value;
                    }
                }

                localCancellationToken.ThrowIfCancellationRequested();

                var returnedValue = suggestions.Select((r, index) =>
                {
                    return(new PredictiveSuggestion(r.Item1));
                })
                                    .ToList();

                _telemetryClient.OnGetSuggestion(maskedUserInput,
                                                 telemetryData,
                                                 cancellationToken.IsCancellationRequested);

                return(returnedValue);
            }
            catch (Exception e) when(!(e is OperationCanceledException))
            {
                this._telemetryClient.OnGetSuggestionError(e);
            }

            return(new List <PredictiveSuggestion>());
        }
Пример #4
0
        /// <inhericdoc />
        public SuggestionPackage GetSuggestion(string clientId, PredictionContext context, CancellationToken cancellationToken)
        {
            var localSuggestionSessionId = _suggestionSessionId++;

            if (_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(clientId, localSuggestionSessionId, context.InputAst,
                                                                                suggestions,
                                                                                cancellationToken.IsCancellationRequested,
                                                                                exception));
            }

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

                return(new SuggestionPackage(localSuggestionSessionId, suggestions));
            }
        }