private static bool Database_Update_Interpolated_Values(Point_Measure_Fact tbl_point_measure_fact) { Interpolated_Good_Value Good_Values = Get_Previous_and_Next_Good_Value(tbl_point_measure_fact.Timestamp_From, tbl_point_measure_fact.Point_ID); double?new_value = Get_Interpolated_Value(Good_Values); //Save into audit table (FactsRestatementAudit) if (new_value != null) { double final_new_value = Convert.ToDouble(new_value); //Save to Fact Audit if (Good_Values.Previous_Good_Value != null && Good_Values.Next_Good_Value != null) { //For Interpolation when both previous and next value ((24-21)/3 = 1; 21+1; 22; 23) final_new_value = Good_Values.Previous_Good_Value.Raw_Value + final_new_value; Save_To_Fact_Audit_Table(tbl_point_measure_fact, final_new_value); } else { Save_To_Fact_Audit_Table(tbl_point_measure_fact, final_new_value); } //Update the Table Point measure fact with new value IPoint_Measure_Fact_Service point_measure_fact = new Point_Measure_Fact_Service(); tbl_point_measure_fact.Raw_Value = Convert.ToDouble(final_new_value); tbl_point_measure_fact.Point_Status = good_status_code; point_measure_fact.Update_Point_Measure_Fact(tbl_point_measure_fact); return(true); } return(false); }
/// <summary> /// Interpolation = (b-a)/(total_wrong_records+1); b is the next good value, a is last good value; (24-21)/3 = 1; 21+1; 22; 23 /// </summary> /// <param name="Good_Values"></param> /// <returns></returns> private static double?Get_Interpolated_Value(Interpolated_Good_Value Good_Values) { if (Good_Values.Next_Good_Value != null && Good_Values.Previous_Good_Value != null) { return((Good_Values.Next_Good_Value.Raw_Value - Good_Values.Previous_Good_Value.Raw_Value) / (Good_Values.Number_Of_Records + 1)); } else if (Good_Values.Next_Good_Value != null) { return(Good_Values.Next_Good_Value.Raw_Value); } else { return(null); } }
/// <summary> /// This function returns the last and next good value(good value mean point_status "ok") for interpolation /// </summary> /// <param name="from_date"></param> /// <param name="point_Id"></param> public static Interpolated_Good_Value Get_Previous_and_Next_Good_Value(DateTime from_date, long point_Id) { IInnonAnalyticsWarehouseEntities _db_context = new InnonAnalyticsWarehouseEntities(); Interpolated_Good_Value Good_Values = new Interpolated_Good_Value(); //Last previous good value Good_Values.Previous_Good_Value = _db_context.Point_Measure_Fact.Where(point => point.Timestamp_From <= from_date && point.Point_Status == good_status_code && point.Point_ID == point_Id).OrderByDescending(pointorder => pointorder.Timestamp_From).FirstOrDefault(); //Next previous good value Good_Values.Next_Good_Value = _db_context.Point_Measure_Fact.Where(point => point.Timestamp_From >= from_date && point.Point_Status == good_status_code && point.Point_ID == point_Id).OrderBy(pointorder => pointorder.Timestamp_From).FirstOrDefault(); if (Good_Values.Previous_Good_Value != null && Good_Values.Next_Good_Value != null) { Good_Values.Number_Of_Records = _db_context.Point_Measure_Fact.Where(point => point.Timestamp_From > Good_Values.Previous_Good_Value.Timestamp_From && point.Timestamp_From < Good_Values.Next_Good_Value.Timestamp_From && point.Point_ID == point_Id).OrderBy(pointorder => pointorder.Timestamp_From).Count(); } return(Good_Values); }