public async Task <AnomalyServiceResponse> GetAnomalyPrediction(DailyConsumptionDetail dailyConsumptionDetail, SqlConnection sqlConnection, SqlTransaction sqlTransaction, string piServerName, bool isDailyPrediction = true) { string predictionDate = dailyConsumptionDetail.Timestamp.ToString("yyyy-MM-ddTHH:mm:ssZ"); if (isDailyPrediction) { predictionDate = dailyConsumptionDetail.Timestamp.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssZ"); } AnomalyServiceRequest requestmodel = new AnomalyServiceRequest() { Inputs = new Inputs() { input1 = new Input1() { ColumnNames = new List <string> { "Id", "AMPS_SYSTEM_AVG", "Building", "Breaker_details", "Daily_electric_cost", "Daily_KWH_System", "Monthly_electric_cost", "Monthly_KWH_System", "PowerScout", "Temperature", "Timestamp", "Visibility", "kW_System", "PiServerName" }, Values = new List <List <string> > { new List <string> { "0", "0", dailyConsumptionDetail.Building, dailyConsumptionDetail.Breaker_details, "0", "0", "0", "0", dailyConsumptionDetail.PowerScout, dailyConsumptionDetail.Temperature.ToString(), predictionDate, dailyConsumptionDetail.Visibility.ToString(), "0", piServerName } } } } }; var azureMLConfigurationService = new AzureMLConfigurationService(); var predictionConfig = azureMLConfigurationService.GetPredictionConfigData(sqlConnection, sqlTransaction); return(await AzureMLClient.CallAzureMLAsync(requestmodel, predictionConfig.AzureMlDailyPredictionApiURL, predictionConfig.AzureMlDailyPredictionApiKey)); }
/// <summary> /// Detects the anomalies and generate alert. /// </summary> /// <param name="timerInfo">The timer information.</param> /// <returns> /// A <see cref="Task" /> representing the asynchronous operation. /// </returns> public static async Task ProcessAnomalyDetection([TimerTrigger("0 0 0 * * *")] TimerInfo timerInfo) { try { var alertService = new AlertService(); var meterService = new MeterService(); var requestModel = new AnomalyServiceRequest(); using (SqlConnection sqlConnection = new SqlConnection(ConfigurationSettings.DbConnectionString)) { sqlConnection.Open(); var piServers = meterService.GetPiServerList(sqlConnection); foreach (var piServer in piServers) { try { DateTime anomalyDetectionStartTime = piServer.PiServerCurrentDateTime.AddDays(-1).Date; DateTime anomalyDetectionEndTime = anomalyDetectionStartTime.AddHours(24).AddSeconds(-1); using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction()) { var query = "SELECT PowerScout, Temperature, Timestamp, Visibility, kW_System, days, Breaker_details FROM LiveData WHERE Timestamp > @startTime AND Timestamp <= @endTime and PiServerName = @PiServerName ORDER BY Timestamp"; requestModel.GlobalParameters = new GlobalParameter(); Inputs inputs = new Inputs(); Input1 input = new Input1() { ColumnNames = new List <string> { "PowerScout", "Temperature", "Timestamp", "Visibility", "kW_System", "days", "Breaker_details" } }; List <List <string> > values = new List <List <string> >(); using (SqlCommand selectDataCommand = new SqlCommand(query, sqlConnection, sqlTransaction)) { selectDataCommand.Parameters.AddWithValue("@startTime", anomalyDetectionStartTime.ToString(Constants.DATE_TIME_FORMAT)); selectDataCommand.Parameters.AddWithValue("@endTime", anomalyDetectionEndTime.ToString(Constants.DATE_TIME_FORMAT)); selectDataCommand.Parameters.AddWithValue("@PiServerName", piServer.PiServerName); using (SqlDataReader result = selectDataCommand.ExecuteReader()) { List <string> rowValues = null; while (result.Read()) { rowValues = new List <string>(); rowValues.Add(SqlTypeConverter.ToString(result["PowerScout"])); rowValues.Add(SqlTypeConverter.ToString(result["Temperature"])); rowValues.Add(SqlTypeConverter.ToDateTime(result["Timestamp"]).ToString(Constants.DATE_TIME_FORMAT)); rowValues.Add(SqlTypeConverter.ToString(result["Visibility"])); rowValues.Add(SqlTypeConverter.ToString(result["kW_System"])); rowValues.Add(SqlTypeConverter.ToString(result["days"])); rowValues.Add(SqlTypeConverter.ToString(result["Breaker_details"])); values.Add(rowValues); } result.Close(); } } if (values.Count > 0) { input.Values = values; inputs.input1 = input; requestModel.Inputs = inputs; var azureMLConfigurationService = new AzureMLConfigurationService(); var anomalyConfig = azureMLConfigurationService.GetAnomalyConfigData(sqlConnection, sqlTransaction); var responseModel = await AzureMLClient.CallAzureMLAsync(requestModel, anomalyConfig.AzureMlAnomalyDetectionApiUrl, anomalyConfig.AzureMlAnomalyDetectionApiKey); if (responseModel != null) { AddAnomalyToDatabase(sqlConnection, sqlTransaction, responseModel, piServer.PiServerName, anomalyDetectionStartTime, anomalyDetectionEndTime, alertService); } } sqlTransaction.Commit(); } } catch (Exception e) { var errorMsg = string.Format("AnomalyDetectionJob Error : Anomaly detection failed for pi server - {0}", piServer.PiServerName); Console.WriteLine(errorMsg); Console.WriteLine("Error Message - {0}", e.Message); Console.WriteLine("StackTrace - {0}", e.StackTrace); ApplicationInsightsLogger.LogException(e, new Dictionary <string, string> { { "Job Error Message", errorMsg } }); } } sqlConnection.Close(); } } catch (Exception e) { ApplicationInsightsLogger.LogException(e); throw; } }