/// <summary>
 /// Constructs a new instance of <see cref="AzPredictor"/>
 /// </summary>
 /// <param name="service">The service that provides the suggestion</param>
 /// <param name="telemetryClient">The client to collect telemetry</param>
 /// <param name="settings">The settings of the service</param>
 /// <param name="azContext">The Az context which this module runs with</param>
 public AzPredictor(IAzPredictorService service, ITelemetryClient telemetryClient, Settings settings, IAzContext azContext)
 {
     this._service         = service;
     this._telemetryClient = telemetryClient;
     this._settings        = settings;
     this._azContext       = azContext;
 }
Пример #2
0
 /// <summary>
 /// Constructs a new instance of <see cref="AzPredictor"/> for testing.
 /// </summary>
 /// <param name="service">The service that provides the suggestion.</param>
 /// <param name="telemetryClient">The client to collect telemetry.</param>
 /// <param name="settings">The settings for <see cref="AzPredictor"/>.</param>
 /// <param name="azContext">The Az context which this module runs in.</param>
 internal AzPredictor(IAzPredictorService service, ITelemetryClient telemetryClient, Settings settings, IAzContext azContext)
 {
     _service         = service;
     _telemetryClient = telemetryClient;
     _settings        = settings;
     _azContext       = azContext;
     _isInitialized   = true;
 }
Пример #3
0
        /// <summary>
        /// Constructs a new instance of <see cref="AzPredictor"/> to use in PowerShell's prediction subsystem.
        /// </summary>
        public AzPredictor()
        {
            // To make import-module fast, we'll do all the initialization in a task.
            // Slow initialization may make opening a PowerShell window slow if "Import-Module" is added to the user's profile.
            Task.Run(() =>
            {
                _settings     = Settings.GetSettings();
                var azContext = new AzContext()
                {
                    IsInternal = (_settings.SetAsInternal == true) ? true : false,
                    SurveyId   = _settings.SurveyId?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
                };

                RegisterDisposableObject(azContext);

                _azContext = azContext;

                _azContext.UpdateContext();
                _telemetryClient = new AzPredictorTelemetryClient(_azContext);
                _service         = new AzPredictorService(_settings.ServiceUri, _telemetryClient, _azContext);
                _isInitialized   = true;
            });
        }
Пример #4
0
        /// <summary>
        /// Constructs a new instance of <see cref="AzPredictor"/> to use in PowerShell's prediction subsystem.
        /// </summary>
        public AzPredictor()
        {
            _powerShellRuntime = new PowerShellRuntime();
            _surveyHelper      = new AzPredictorSurveyHelper(_powerShellRuntime);

            // To make import-module fast, we'll do all the initialization in a task.
            // Slow initialization may make opening a PowerShell window slow if "Import-Module" is added to the user's profile.
            Task.Run(() =>
            {
                _settings  = Settings.GetSettings();
                _azContext = new AzContext(_powerShellRuntime)
                {
                    IsInternal = (_settings.SetAsInternal == true) ? true : false,
                };

                _azContext.UpdateContext();
                // This will run the script in the right context.
                var _            = _azContext.PowerShellVersion;
                _telemetryClient = new AzPredictorTelemetryClient(_azContext);
                _service         = new AzPredictorService(_settings.ServiceUri, _telemetryClient, _azContext);
                _isInitialized   = true;
            });
        }
Пример #5
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));
                }
            }
        }