private static void Save_To_Fact_Audit_Table(Point_Measure_Fact tbl_point_measure_fact, double newvalue)
        {
            IFactsRestatementAudit_Service facts           = new FactsRestatementAudit_Service();
            FactsRestatementAudit          tbl_facts_audit = FactsRestatementAudit_Convert.Convert_Point_Measure_Fact_To_FactsRestatmentAudit(tbl_point_measure_fact, newvalue);

            facts.Save(tbl_facts_audit);
        }
Beispiel #2
0
        public IList <Point_Measure_Fact> List_Point_Measure_Fact_Service(IList <RawDataDTO> list_raw_data_dto, ElementDTO element)
        {
            try
            {
                IList <Point_Measure_Fact> point_measure_facts = new List <Point_Measure_Fact>();
                Point_Measure_Fact         point_measure_fact;

                for (int j = 0; j < list_raw_data_dto.Count; j++)
                {
                    point_measure_fact = new Point_Measure_Fact();
                    try
                    {
                        point_measure_fact.Client_ID = 0;
                        point_measure_fact.DataRecordingFrequency = element.Recorded_Freq;

                        point_measure_fact.Date_ID = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP).Date;

                        point_measure_fact.External_ID = list_raw_data_dto[j].ID.ToString();

                        point_measure_fact.Hour_ID = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP.Value.ToString("yyyy-MM-dd HH:") + "00"); // 07/05/2015 11:00

                        point_measure_fact.Last_Update_Time = System.DateTime.Now;
                        point_measure_fact.Max_Value        = element.Max_Value;
                        point_measure_fact.Min_Value        = element.Min_Value;
                        point_measure_fact.Point_ID         = element.ID;
                        point_measure_fact.Schedule_ID      = 0;
                        point_measure_fact.Timestamp_From   = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP);

                        if (j + 1 < list_raw_data_dto.Count)
                        {
                            point_measure_fact.Timestamp_To = Convert.ToDateTime(list_raw_data_dto[j + 1].TIMESTAMP);
                        }
                        else
                        {
                            point_measure_fact.Timestamp_To = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP);
                        }

                        point_measure_fact.Unit_Of_Measurement = element.Unit.Unit_Name;
                        point_measure_fact.Value = Convert.ToDouble(list_raw_data_dto[j].VALUE);

                        point_measure_facts.Add(point_measure_fact);
                    }
                    catch (Exception ex)
                    {
                    }
                }



                return(point_measure_facts);
            }
            catch (Exception ex)
            {
                throw ex;
            }



            //datawarehousecontext.Point_Measure_Fact_Entities.Add()
        }
        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);
        }
        public void Update_Point_Measure_Fact(Point_Measure_Fact tbl_Point_Measure_Fact)
        {
            datawarehousecontext = new InnonAnalyticsWarehouseEntities();
            Point_Measure_Fact point_measure_fact = datawarehousecontext.Point_Measure_Fact.Find(tbl_Point_Measure_Fact.ID);

            point_measure_fact.Raw_Value    = tbl_Point_Measure_Fact.Raw_Value;
            point_measure_fact.Point_Status = tbl_Point_Measure_Fact.Point_Status;
            datawarehousecontext.SaveChanges();
        }
Beispiel #5
0
        public void Save_Point_Measure_Fact_Service(IList <RawDataDTO> list_raw_data_dto, ElementDTO element)
        {
            try
            {
                DatawarehouseConnectionContext datawarehousecontext = new DatawarehouseConnectionContext();
                Point_Measure_Fact             point_measure_fact;

                foreach (RawDataDTO raw_dto in list_raw_data_dto)
                {
                    point_measure_fact = new Point_Measure_Fact();

                    try
                    {
                        point_measure_fact.ID = 1;

                        point_measure_fact.Client_ID = 0;
                        point_measure_fact.DataRecordingFrequency = element.Recorded_Freq;
                        point_measure_fact.Date_ID             = System.DateTime.Now.Date;
                        point_measure_fact.External_ID         = raw_dto.ID.ToString();
                        point_measure_fact.Hour_ID             = System.DateTime.Now.Date;
                        point_measure_fact.Last_Update_Time    = System.DateTime.Now;
                        point_measure_fact.Max_Value           = element.Max_Value;
                        point_measure_fact.Min_Value           = element.Min_Value;
                        point_measure_fact.Point_ID            = element.ID;
                        point_measure_fact.Schedule_ID         = 0;
                        point_measure_fact.Timestamp_From      = Convert.ToDateTime(raw_dto.TIMESTAMP);
                        point_measure_fact.Timestamp_To        = Convert.ToDateTime(raw_dto.TIMESTAMP);
                        point_measure_fact.Unit_Of_Measurement = element.Unit.Unit_Name;
                        point_measure_fact.Value = Convert.ToDouble(raw_dto.VALUE);

                        datawarehousecontext.Point_Measure_Fact_Entities.Add(point_measure_fact);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                datawarehousecontext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }



            //datawarehousecontext.Point_Measure_Fact_Entities.Add()
        }
        private static void Final_Update_Point_Measure_Fact_For_WareHouse(DateTime from_date, DateTime to_date, ElementDTO element)
        {
            IInnonAnalyticsWarehouseEntities _db_context = new InnonAnalyticsWarehouseEntities();
            IList <Point_Measure_Fact>       tbl_point_measgure_facts = _db_context.Point_Measure_Fact.Where(point => point.Timestamp_From >= from_date &&
                                                                                                             point.Timestamp_To <= to_date &&
                                                                                                             point.Point_Status == good_status_code &&
                                                                                                             point.Point_ID == element.ID).OrderBy(p => p.Timestamp_From).ToList();

            if (tbl_point_measgure_facts.Any())
            {
                //check very first record if very last_previous_value=null, than it mean the value would be zero for the first record
                Point_Measure_Fact last_previous_value = _db_context.Point_Measure_Fact.Where(pt => pt.Timestamp_From < from_date && pt.Point_ID == element.ID).OrderByDescending(p => p.Timestamp_From).FirstOrDefault();

                if (element.Value_Type == DataValueType.Totalised)
                {
                    for (int i = 0; i < tbl_point_measgure_facts.Count(); i++)
                    {
                        if (i == 0 && last_previous_value == null)
                        {
                            tbl_point_measgure_facts[i].Value = 0;
                        }
                        else if (i == 0)
                        {
                            tbl_point_measgure_facts[i].Value = tbl_point_measgure_facts[i].Raw_Value - last_previous_value.Raw_Value;
                        }
                        else
                        {
                            tbl_point_measgure_facts[i].Value = tbl_point_measgure_facts[i].Raw_Value - tbl_point_measgure_facts[i - 1].Raw_Value;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < tbl_point_measgure_facts.Count(); i++)
                    {
                        tbl_point_measgure_facts[i].Value = tbl_point_measgure_facts[i].Raw_Value;
                    }
                }

                _db_context.SaveChanges();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Datawarehouse finalinsert into the Point_Mesaure_Fact
        /// </summary>
        /// <param name="point_measure_facts"></param>
        private static void Final_Insert_In_To_DatawareHouse_Table(IList <Point_Measure_Fact> point_measure_facts)
        {
            try
            {
                BlockingCollection <Point_Measure_Fact> entityQueue = new BlockingCollection <Point_Measure_Fact>();
                List <Task> writers = new List <Task>();
                if (point_measure_facts.Count > 0)
                {
                    Action writerAction = new Action(() =>
                    {
                        InnonAnalyticsWarehouseEntities ctx = new InnonAnalyticsWarehouseEntities();

                        ctx.Database.CommandTimeout = 300;
                        int totalAdded = 0;
                        try
                        {
                            while (true)
                            {
                                Point_Measure_Fact my = entityQueue.Take();
                                my.Last_Update_Time   = System.DateTime.Now;
                                ctx.Point_Measure_Fact.Add(my);
                                totalAdded++;
                                if (totalAdded > 500)
                                {
                                    //Console.WriteLine("Saving changes, TID: " + Thread.CurrentThread.ManagedThreadId);
                                    try
                                    {
                                        ctx.SaveChanges();
                                    }
                                    catch (Exception ex)
                                    {
                                        Helper.WriteToFile("Error in saving: " + ex.InnerException.Message);
                                    }

                                    ctx.Database.Connection.Close();
                                    ctx = new InnonAnalyticsWarehouseEntities();
                                }
                            }
                        }
                        catch (InvalidOperationException inv)
                        {
                            //WriteToFile("Saving final changes --- " + inv.Message);
                            //Console.WriteLine("Saving final changes, TID: " + Thread.CurrentThread.ManagedThreadId);
                            //  Console.WriteLine("Error Saving final " + inv.Message);
                            ctx.SaveChanges(); // Save any pending changes
                            ctx.Database.Connection.Close();
                            totalAdded = 0;
                        }
                    });


                    for (int i = 0; i < 1000; i++)
                    {
                        writers.Add(Task.Factory.StartNew(writerAction));
                    }

                    foreach (Point_Measure_Fact point_measure_fact in point_measure_facts)
                    {
                        entityQueue.Add(point_measure_fact);
                    }
                }

                entityQueue.CompleteAdding();
                Console.WriteLine("Done generating data.");

                Task.WaitAll(writers.ToArray());
                Console.WriteLine("Finished");
            }
            catch (Exception ex)
            {
                string innerexp = (ex.InnerException != null) ? ex.InnerException.Message : "";
                Helper.WriteToFile("Error in the FinalInsert " + ex.Message + "--" + innerexp);
            }
        }
 public Interpolated_Good_Value()
 {
     Previous_Good_Value = new Point_Measure_Fact();
     Next_Good_Value     = new Point_Measure_Fact();
 }
        public IList <Point_Measure_Fact> List_Point_Measure_Fact_Service(IList <RawDataDTO> list_raw_data_dto, ElementDTO element)
        {
            try
            {
                IList <Point_Measure_Fact> point_measure_facts = new List <Point_Measure_Fact>();
                Point_Measure_Fact         point_measure_fact;

                for (int j = 0; j < list_raw_data_dto.Count; j++)
                {
                    point_measure_fact = new Point_Measure_Fact();
                    try
                    {
                        point_measure_fact.Client_ID = 0;
                        point_measure_fact.DataRecordingFrequency = element.Recorded_Freq;

                        point_measure_fact.Date_ID = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP).Date;

                        point_measure_fact.External_ID = list_raw_data_dto[j].ID.ToString();

                        point_measure_fact.Hour_ID = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP.Value.ToString("yyyy-MM-dd HH:") + "00"); // 07/05/2015 11:00

                        point_measure_fact.Last_Update_Time = System.DateTime.Now;
                        point_measure_fact.Max_Value        = element.Max_Value;
                        point_measure_fact.Min_Value        = element.Min_Value;
                        point_measure_fact.Point_ID         = element.ID;
                        point_measure_fact.Schedule_ID      = 0;
                        point_measure_fact.Timestamp_From   = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP);

                        if (j + 1 < list_raw_data_dto.Count)
                        {
                            point_measure_fact.Timestamp_To = Convert.ToDateTime(list_raw_data_dto[j + 1].TIMESTAMP);
                        }
                        else
                        {
                            point_measure_fact.Timestamp_To = Convert.ToDateTime(list_raw_data_dto[j].TIMESTAMP);
                        }

                        point_measure_fact.Unit_Of_Measurement = element.Unit.Unit_Name;
                        point_measure_fact.Raw_Value           = Convert.ToDouble(list_raw_data_dto[j].VALUE);
                        point_measure_fact.Point_Status        = list_raw_data_dto[j].STATUS_TAG;
                        //Iterpolation method check the spikes or wrong value
                        //  point_measure_fact.Value = Interpolation.InterPolateValue(list_raw_data_dto[j], element, point_measure_fact);

                        if (element.Value_Type == DataValueType.Totalised)
                        {
                            point_measure_fact.Value = Convert.ToDouble(list_raw_data_dto[j].VAL);
                        }
                        else
                        {
                            point_measure_fact.Value = Convert.ToDouble(list_raw_data_dto[j].VALUE);
                        }

                        point_measure_facts.Add(point_measure_fact);
                    }
                    catch (Exception ex)
                    {
                        Helper.WriteToFile("Error in Point mesaure fact service " + ex.Message + "-- Point Id " + element.ID);
                    }
                }
                return(point_measure_facts);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static FactsRestatementAudit Convert_Point_Measure_Fact_To_FactsRestatmentAudit(Point_Measure_Fact point_measure_fact, double newvalue)
        {
            FactsRestatementAudit facts_restatment_audit = new FactsRestatementAudit();

            facts_restatment_audit.Point_ID         = point_measure_fact.Point_ID;
            facts_restatment_audit.New_Value        = newvalue;
            facts_restatment_audit.Old_Value        = point_measure_fact.Raw_Value;
            facts_restatment_audit.Send_To_DW       = true;
            facts_restatment_audit.Timestamp_From   = point_measure_fact.Timestamp_From;
            facts_restatment_audit.Timestamp_To     = point_measure_fact.Timestamp_To;
            facts_restatment_audit.Updated_Datetime = System.DateTime.Now;
            facts_restatment_audit.Updated_User_ID  = 1;
            return(facts_restatment_audit);
        }