/// <inheritdoc/> public virtual void RequestPredictions(IEnumerable <string> commands) { AzPredictorService.ReplaceThrottleUserIdToHeader(this._client?.DefaultRequestHeaders, this._azContext.UserId); var localCommands = string.Join(AzPredictorConstants.CommandConcatenator, commands); this._telemetryClient.OnRequestPrediction(localCommands); if (string.Equals(localCommands, this._commandForPrediction, StringComparison.Ordinal)) { // It's the same history we've already requested the prediction for last time, skip it. return; } else { this.SetPredictionCommand(localCommands); // When it's called multiple times, we only need to keep the one for the latest command. this._predictionRequestCancellationSource?.Cancel(); this._predictionRequestCancellationSource = new CancellationTokenSource(); var cancellationToken = this._predictionRequestCancellationSource.Token; // We don't need to block on the task. We send the HTTP request and update prediction list at the background. Task.Run(async() => { try { var requestContext = new PredictionRequestBody.RequestContext() { SessionId = this._telemetryClient.SessionId, CorrelationId = this._telemetryClient.CorrelationId, }; var requestBody = new PredictionRequestBody(localCommands) { Context = requestContext, }; var requestBodyString = JsonConvert.SerializeObject(requestBody); var httpResponseMessage = await _client.PostAsync(this._predictionsEndpoint, new StringContent(requestBodyString, Encoding.UTF8, "application/json"), cancellationToken); var reply = await httpResponseMessage.Content.ReadAsStringAsync(cancellationToken); var suggestionsList = JsonConvert.DeserializeObject <List <string> >(reply); this.SetSuggestionPredictor(localCommands, suggestionsList); } catch (Exception e) when(!(e is OperationCanceledException)) { this._telemetryClient.OnRequestPredictionError(localCommands, e); } }, cancellationToken); } }
/// <inheritdoc/> public virtual void RequestPredictions(IEnumerable <string> commands) { Validation.CheckArgument(commands, $"{nameof(commands)} cannot be null."); var localCommands = string.Join(AzPredictorConstants.CommandConcatenator, commands); bool postSuccess = false; Exception exception = null; bool startRequestTask = false; try { if (string.Equals(localCommands, _commandToRequestPrediction, StringComparison.Ordinal)) { // It's the same history we've already requested the prediction for last time, skip it. return; } if (commands.Any()) { SetCommandToRequestPrediction(localCommands); // When it's called multiple times, we only need to keep the one for the latest command. _predictionRequestCancellationSource?.Cancel(); _predictionRequestCancellationSource = new CancellationTokenSource(); var cancellationToken = _predictionRequestCancellationSource.Token; // We don't need to block on the task. We send the HTTP request and update prediction list at the background. startRequestTask = true; Task.Run(async() => { try { AzPredictorService.ReplaceThrottleUserIdToHeader(_client?.DefaultRequestHeaders, _azContext.UserId); var requestContext = new PredictionRequestBody.RequestContext() { SessionId = _telemetryClient.SessionId, CorrelationId = _telemetryClient.CorrelationId, }; var requestBody = new PredictionRequestBody(localCommands) { Context = requestContext, }; var requestBodyString = JsonSerializer.Serialize(requestBody, JsonUtilities.DefaultSerializerOptions); var httpResponseMessage = await _client.PostAsync(_predictionsEndpoint, new StringContent(requestBodyString, Encoding.UTF8, "application/json"), cancellationToken); postSuccess = true; httpResponseMessage.EnsureSuccessStatusCode(); var reply = await httpResponseMessage.Content.ReadAsStreamAsync(cancellationToken); var suggestionsList = await JsonSerializer.DeserializeAsync <IList <string> >(reply, JsonUtilities.DefaultSerializerOptions); SetCommandBasedPreditor(localCommands, suggestionsList); } catch (Exception e) when(!(e is OperationCanceledException)) { exception = e; } finally { _telemetryClient.OnRequestPrediction(new RequestPredictionTelemetryData(localCommands, postSuccess, exception)); } }, cancellationToken); } } catch (Exception e) { exception = e; } finally { if (!startRequestTask) { _telemetryClient.OnRequestPrediction(new RequestPredictionTelemetryData(localCommands, hasSentHttpRequest: false, exception: exception)); } } }