private void UpdateStatusDisplay(DelayPrediction prediction, ForecastResult forecast) { weatherForecast.ImageUrl = forecast.ForecastIconUrl; weatherForecast.ToolTip = forecast.Condition; if (String.IsNullOrWhiteSpace(mlUrl)) { lblPrediction.Text = "(not configured)"; lblConfidence.Text = "(not configured)"; return; } if (prediction == null) { throw new Exception("Prediction did not succeed. Check the Settings for mlUrl and mlApiKey."); } if (prediction.ExpectDelays) { lblPrediction.Text = "expect delays"; } else { lblPrediction.Text = "no delays expected"; } lblConfidence.Text = $"{(prediction.Confidence * 100.0):N2}"; }
private async Task PredictDelays(DepartureQuery query, ForecastResult forecast) { if (string.IsNullOrEmpty(mlUrl)) { return; } var departureDate = DateTime.Parse(txtDepartureDate.Text); prediction = new DelayPrediction(); try { using (var client = new HttpClient()) { var predictionRequest = new PredictionRequest { OriginAirportCode = query.OriginAirportCode, Month = query.DepartureDate.Month, DayofMonth = query.DepartureDate.Day, CRSDepHour = query.DepartureDate.Hour, DayOfWeek = query.DepartureDayOfWeek, Carrier = query.Carrier, DestAirportCode = query.DestAirportCode, WindSpeed = forecast.WindSpeed, SeaLevelPressure = forecast.Pressure, HourlyPrecip = forecast.Precipitation }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", mlApiKey); client.BaseAddress = new Uri(mlUrl); var response = await client.PostAsJsonAsync("", predictionRequest).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); var token = JToken.Parse(result); var parsedResult = JsonConvert.DeserializeObject <PredictionResult>((string)token); if (parsedResult.prediction == 1) { this.prediction.ExpectDelays = true; this.prediction.Confidence = parsedResult.probability; } else if (parsedResult.prediction == 0) { this.prediction.ExpectDelays = false; this.prediction.Confidence = parsedResult.probability; } else { this.prediction = null; } } else { prediction = null; Trace.Write($"The request failed with status code: {response.StatusCode}"); // Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure Trace.Write(response.Headers.ToString()); var responseContent = await response.Content.ReadAsStringAsync(); Trace.Write(responseContent); } } } catch (Exception ex) { prediction = null; System.Diagnostics.Trace.TraceError("Failed retrieving delay prediction: " + ex.ToString()); throw; } }