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();
        }
        public void Save_Point_Measure_Fact_Service(IList <RawDataDTO> list_raw_data_dto, ElementDTO element)
        {
            try
            {
                datawarehousecontext = new InnonAnalyticsWarehouseEntities();
                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.Add(point_measure_fact);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

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



            //datawarehousecontext.Point_Measure_Fact_Entities.Add()
        }
Esempio n. 3
0
        private void Save_Aggregate_To_WareHouse(List <Aggregate_Raw_Data> _list_aggregation, AggerationType _aggerationType, long Element_ID)
        {
            if (_list_aggregation != null)
            {
                if (_list_aggregation.Any())
                {
                    try
                    {
                        _db_datawarehouse_context = new InnonAnalyticsWarehouseEntities();

                        switch (_aggerationType)
                        {
                        case AggerationType.Hour:
                            _db_datawarehouse_context.Point_Agg_Hour.AddRange(Point_Agg_Hour_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Hour_List(_list_aggregation));
                            break;

                        case AggerationType.Day:
                            _db_datawarehouse_context.Point_Agg_Day.AddRange(Point_Agg_Day_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Day_List(_list_aggregation));
                            break;

                        case AggerationType.Week:
                            //_db_datawarehouse_context.Point_Agg_Week.AddRange(Point_Agg_Week_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Week_List(_list_aggregation));
                            break;

                        case AggerationType.Month:
                            _db_datawarehouse_context.Point_Agg_Month.AddRange(Point_Agg_Month_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Month_List(_list_aggregation));
                            break;

                        case AggerationType.Year:
                            _db_datawarehouse_context.Point_Agg_Year.AddRange(Point_Agg_Year_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Year_List(_list_aggregation));
                            break;

                        default:
                            File_Log.SaveLog_ToFile(new Exception("Invalid AggregationType"), LoggingActions.Error, "Point_ID " + Element_ID);
                            break;
                        }
                        _db_datawarehouse_context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        File_Log.SaveLog_ToFile(ex, LoggingActions.Error, "Aggragation Type " + _aggerationType + " Point_ID " + Element_ID);
                    }
                }
            }
        }
Esempio n. 4
0
        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();
            }
        }
Esempio n. 5
0
        protected void Update_Month_Aggregation(long Element_ID)
        {
            try
            {
                InnonAnalyticsWarehouseEntities db_datawarehouse_context = new InnonAnalyticsWarehouseEntities();
                //Get last inserted record in the table Point_Agg_Hour
                int      months   = int.Parse(ConfigurationManagerHelper.AppSettings["aggregate_months"].ToString());
                DateTime datetime = DateTime.Now.AddMonths(-months);
                long     MonthID  = Helper.Get_Month_ID(datetime.Year, datetime.Month);
                IList <Point_Agg_Month> tbl_point_Agg_Months = db_datawarehouse_context.Point_Agg_Month.Where(point_Months => point_Months.Point_ID == Element_ID && point_Months.Month_ID >= MonthID).ToList();
                if (tbl_point_Agg_Months.Count() > 0)
                {
                    List <Aggregate_Raw_Data> _list_aggregation = new List <Aggregate_Raw_Data>();
                    _list_aggregation = Get_Aggregate_Raw_Data(Element_ID, Helper.Get_Last_Day_Of_Month(MonthID), AggerationType.Month);

                    //Compare the raw data and aggregate data
                    _list_aggregation = _list_aggregation.Where(aggregate => tbl_point_Agg_Months.Any(aggr_Months =>
                                                                                                      Helper.Get_Month_ID(aggregate.Month_Year_ID, aggregate.Month_ID) == aggr_Months.Month_ID &&
                                                                                                      (aggregate.TotalCount != aggr_Months.Month_Count || aggregate.SumValue != aggr_Months.Sum_Value))).ToList();

                    if (_list_aggregation.Count() > 0)
                    {
                        tbl_point_Agg_Months     = Point_Agg_Month_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Month_List(_list_aggregation);
                        db_datawarehouse_context = new InnonAnalyticsWarehouseEntities();
                        foreach (Point_Agg_Month tbl_point_Agg_Month in tbl_point_Agg_Months)
                        {
                            db_datawarehouse_context.Point_Agg_Month.Attach(tbl_point_Agg_Month);
                            db_datawarehouse_context.Entry(tbl_point_Agg_Month).State = System.Data.Entity.EntityState.Modified;
                        }
                        db_datawarehouse_context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                File_Log.SaveLog_ToFile(ex, Common.Enums.LoggingActions.Error, "Update_Month_Aggregation passing parameter element_id:- " + Element_ID);
            }
        }
Esempio n. 6
0
        protected void Update_Day_Aggregation(long Element_ID)
        {
            try
            {
                InnonAnalyticsWarehouseEntities db_datawarehouse_context = new InnonAnalyticsWarehouseEntities();
                //Get last inserted record in the table Point_Agg_Hour
                double   days     = double.Parse(ConfigurationManagerHelper.AppSettings["aggregate_days"].ToString());
                DateTime datetime = DateTime.Now.AddDays(-(days + 1)); // add 1 because Date_ID in Point_Agg_Day 00:00:00 time
                IList <Point_Agg_Day> tbl_point_Agg_Days = db_datawarehouse_context.Point_Agg_Day.Where(point_Days => point_Days.Point_ID == Element_ID && point_Days.Date_ID >= datetime).ToList();
                if (tbl_point_Agg_Days.Count() > 0)
                {
                    List <Aggregate_Raw_Data> _list_aggregation = new List <Aggregate_Raw_Data>();
                    _list_aggregation = Get_Aggregate_Raw_Data(Element_ID, Helper.Get_Last_Day(datetime), AggerationType.Day);

                    //Compare the raw data and aggregate data
                    _list_aggregation = _list_aggregation.Where(aggregate => tbl_point_Agg_Days.Any(aggr_Days =>
                                                                                                    aggregate.Date_ID == aggr_Days.Date_ID &&
                                                                                                    (aggregate.TotalCount != aggr_Days.Day_Count || aggregate.SumValue != aggr_Days.Sum_Value))).ToList();

                    if (_list_aggregation.Count() > 0)
                    {
                        tbl_point_Agg_Days       = Point_Agg_Day_Convert.Convert_List_Aggregate_Raw_Data_To_Point_Agg_Day_List(_list_aggregation);
                        db_datawarehouse_context = new InnonAnalyticsWarehouseEntities();
                        foreach (Point_Agg_Day tbl_point_Agg_Day in tbl_point_Agg_Days)
                        {
                            db_datawarehouse_context.Point_Agg_Day.Attach(tbl_point_Agg_Day);
                            db_datawarehouse_context.Entry(tbl_point_Agg_Day).State = System.Data.Entity.EntityState.Modified;
                        }
                        db_datawarehouse_context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                File_Log.SaveLog_ToFile(ex, Common.Enums.LoggingActions.Error, "Update_Day_Aggregation passing parameter element_id:- " + Element_ID);
            }
        }
Esempio n. 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);
            }
        }