/// <summary> /// Calls the azure ml asynchronous. /// </summary> /// <param name="requrestData">The requrest data.</param> /// <param name="apiUrl">The API URL.</param> /// <param name="auth">The authentication.</param> /// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns> public static async Task <AnomalyServiceResponse> CallAzureMLAsync(AnomalyServiceRequest requrestData, string apiUrl, string auth) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", auth); client.BaseAddress = new Uri(apiUrl); try { HttpResponseMessage response = client.PostAsJsonAsync("", requrestData).Result; if (response.IsSuccessStatusCode) { string result = await response.Content.ReadAsStringAsync(); AnomalyServiceResponse responseReceived = JsonConvert.DeserializeObject <AnomalyServiceResponse>(result); return(responseReceived); } else { Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode)); // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure Console.WriteLine(response.Headers.ToString()); string responseContent = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseContent); return(null); } } catch (Exception e) { throw; } } }
private static void AddAnomalyToDatabase(SqlConnection sqlConnection, SqlTransaction sqlTransaction, AnomalyServiceResponse anomalyServiceResponseModel, string piServerName, DateTime anomalyDetectionStartTime, DateTime anomalyDetectionEndTime, AlertService alertService) { List <List <string> > filteredAnomalies = anomalyServiceResponseModel.Results.Output1.Value.Values.Where(x => Convert.ToDouble(x.ElementAt(8)) < ConfigurationSettings.AnomalyThreshold).ToList(); string anomalyoutputInsertQuery = "INSERT INTO AnomalyOutput(PowerScout, Temperature,Timestamp,Visibility,days,Breaker_details,kW_System,ScoredLabels,ScoredProbabilities, PiServername) VALUES (@PowerScout,@Temperature,@Timestamp,@Visibility,@days,@Breaker_details,@kW_System,@ScoredLabels,@ScoredProbabilities, @PiServerName)"; foreach (var rowData in filteredAnomalies) { using (SqlCommand cmd = new SqlCommand(anomalyoutputInsertQuery, sqlConnection, sqlTransaction)) { cmd.Parameters.AddWithValue("@PowerScout", rowData.ElementAt(0).ToString()); cmd.Parameters.AddWithValue("@Temperature", Convert.ToDouble(rowData.ElementAt(1))); cmd.Parameters.AddWithValue("@Timestamp", Convert.ToDateTime(rowData.ElementAt(2), CultureInfo.InvariantCulture).ToString(Constants.DATE_TIME_FORMAT)); cmd.Parameters.AddWithValue("@Visibility", Convert.ToDouble(rowData.ElementAt(3))); cmd.Parameters.AddWithValue("@days", rowData.ElementAt(4).ToString()); cmd.Parameters.AddWithValue("@Breaker_details", rowData.ElementAt(5).ToString()); cmd.Parameters.AddWithValue("@kW_System", Convert.ToDouble(rowData.ElementAt(6))); cmd.Parameters.AddWithValue("@ScoredLabels", Convert.ToDouble(rowData.ElementAt(7))); cmd.Parameters.AddWithValue("@ScoredProbabilities", Convert.ToDouble(rowData.ElementAt(8))); cmd.Parameters.AddWithValue("@PiServerName", piServerName); cmd.ExecuteNonQuery(); } } if (filteredAnomalies.Count > 0) { string anomalyAlertMessage = string.Format(Constants.ANOMALY_ALERT_MESSAGE, filteredAnomalies.Count, anomalyDetectionStartTime, anomalyDetectionEndTime); var alert = new Alert { AlertType = Constants.ALERT_TYPE_ANOMALY, Description = anomalyAlertMessage, TimeStamp = anomalyDetectionStartTime, PiServerName = piServerName }; alertService.AddNewAlert(sqlConnection, sqlTransaction, alert); } Console.WriteLine("AnomalyDetectionJob RowInserted : Created alert and anomaly for PiServer - {0}.", piServerName); }