示例#1
0
        public void Incidence(int startYear, int startMonth, int startDay)
        {
            DateTime             startDate = new DateTime(startYear, startMonth, startDay);
            List <DensityDevice> devices   = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 1, 1, "", true);

            TestInit.RefreshDensityCache(devices);
            EventsController service = new EventsController(TestInit.ServiceProvider.GetRequiredService <DensityContext>(),
                                                            TestInit.ServiceProvider.GetRequiredService <IMemoryCache>());

            EventDbSimulator.ResetDatabase(TestInit.ServiceProvider);


            using (DensityContext context = TestInit.ServiceProvider.GetRequiredService <DensityContext>())
            {
                foreach (DensityDevice device in devices)
                {
                    foreach (var relation in device.DensityDevice_DensityChannels)
                    {
                        foreach (TrafficRegion region in relation.Channel.Regions)
                        {
                            DateTime time = startDate;
                            //添加一个持续长度满足10分钟
                            AddDuration(context, region, time, 10);

                            //添加连续3次 总长
                            time = startDate.AddHours(1);
                            AddInterval(context, region, time, 5, 3, 3);

                            //10分钟关联前后5分钟
                            time = startDate.AddHours(2);
                            time = AddDuration(context, region, time, 5);
                            time = time.AddMinutes(3);
                            time = AddDuration(context, region, time, 10);
                            time = time.AddMinutes(3);
                            AddDuration(context, region, time, 5);

                            //交集 5分钟持续三次+10分钟
                            time = startDate.AddHours(3);
                            time = AddInterval(context, region, time, 5, 3, 3);
                            time = time.AddMinutes(3);
                            AddDuration(context, region, time, 10);
                        }
                    }
                }
                context.BulkSaveChanges(options => options.BatchSize = 1000);
                foreach (DensityDevice device in devices)
                {
                    foreach (var relation in device.DensityDevice_DensityChannels)
                    {
                        foreach (TrafficRegion region in relation.Channel.Regions)
                        {
                            var list = service.AnalysisIncidence(region.DataId, startDate, startDate);
                            Assert.AreEqual(startDate, list[0][0].DateTime);
                            Assert.AreEqual(startDate.AddMinutes(10), list[0][0].EndTime);

                            Assert.AreEqual(startDate.AddHours(1), list[0][1].DateTime);
                            Assert.AreEqual(startDate.AddHours(1).AddMinutes(5 * 3 + 3 * 2), list[0][1].EndTime);

                            Assert.AreEqual(startDate.AddHours(2), list[0][2].DateTime);
                            Assert.AreEqual(startDate.AddHours(2).AddMinutes(5 + 3 + 10 + 3 + 5), list[0][2].EndTime);

                            Assert.AreEqual(startDate.AddHours(3), list[0][3].DateTime);
                            Assert.AreEqual(startDate.AddHours(3).AddMinutes(5 * 3 + 3 * 2 + 3 + 10), list[0][3].EndTime);
                        }
                    }
                }
            }
        }
示例#2
0
        public static Dictionary <TrafficEvent, int> CreateData(IServiceProvider serviceProvider, List <DensityDevice> devices, DateTime startDate, DateTime endDate, DataCreateMode mode, bool initDatabase = false)
        {
            if (initDatabase)
            {
                ResetDatabase(serviceProvider);
            }

            using (IServiceScope serviceScope = serviceProvider.CreateScope())
            {
                using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>())
                {
                    Dictionary <TrafficEvent, int> result = new Dictionary <TrafficEvent, int>();
                    Random random = new Random();

                    int hours = Convert.ToInt32((endDate - startDate).TotalHours + 24);
                    foreach (DensityDevice device in devices)
                    {
                        foreach (var relation in device.DensityDevice_DensityChannels)
                        {
                            foreach (TrafficRegion region in relation.Channel.Regions)
                            {
                                if (mode == DataCreateMode.Fixed)
                                {
                                    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
                                    {
                                        for (int h = 0; h < 24; ++h)
                                        {
                                            TrafficEvent trafficEvent1 = new TrafficEvent
                                            {
                                                DataId   = region.DataId,
                                                DateTime = date.AddHours(h),
                                                EndTime  = date.AddHours(h).AddMinutes(1)
                                            };
                                            context.Events.Add(trafficEvent1);
                                            TrafficEvent trafficEvent2 = new TrafficEvent
                                            {
                                                DataId   = region.DataId,
                                                DateTime = date.AddHours(h).AddMinutes(30),
                                                EndTime  = date.AddHours(h).AddMinutes(30).AddMinutes(1)
                                            };
                                            context.Events.Add(trafficEvent2);
                                        }
                                    }
                                }
                                else if (mode == DataCreateMode.Random)
                                {
                                    int value = random.Next(1, hours);
                                    result.Add(new TrafficEvent
                                    {
                                        DataId = region.DataId
                                    }, value);
                                    for (int h = 0; h < value; ++h)
                                    {
                                        TrafficEvent trafficEvent = new TrafficEvent
                                        {
                                            DataId   = region.DataId,
                                            DateTime = startDate.AddHours(h),
                                            EndTime  = startDate.AddHours(h).AddMinutes(1)
                                        };
                                        context.Events.Add(trafficEvent);
                                    }
                                }
                            }
                        }
                    }

                    context.BulkSaveChanges(options => options.BatchSize = 1000);
                    return(result);
                }
            }
        }
示例#3
0
 protected override void Handle(TrafficDensity[] datas)
 {
     using (IServiceScope serviceScope = _serviceProvider.CreateScope())
     {
         using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>())
         {
             if (datas[0].DateLevel == DateTimeLevel.Minute)
             {
                 try
                 {
                     context.Densities_One.AddRange(datas.Select(data => new TrafficDensity_One
                     {
                         DataId   = data.DataId,
                         DateTime = data.DateTime,
                         Value    = data.Value
                     }));
                     context.BulkSaveChanges(options => options.BatchSize = datas.Length);
                     Success_One += datas.Length;
                 }
                 catch (Exception ex)
                 {
                     _logger.LogError((int)LogEvent.高点数据块, ex, "一分钟密度数据入库异常");
                     Failed_One += datas.Length;
                 }
             }
             else if (datas[0].DateLevel == DateTimeLevel.FiveMinutes)
             {
                 try
                 {
                     context.Densities_Five.AddRange(datas.Select(data => new TrafficDensity_Five
                     {
                         DataId   = data.DataId,
                         DateTime = data.DateTime,
                         Value    = data.Value
                     }));
                     context.BulkSaveChanges(options => options.BatchSize = datas.Length);
                     Success_Five += datas.Length;
                 }
                 catch (Exception ex)
                 {
                     _logger.LogError((int)LogEvent.高点数据块, ex, "五分钟密度数据入库异常");
                     Failed_Five += datas.Length;
                 }
             }
             else if (datas[0].DateLevel == DateTimeLevel.FifteenMinutes)
             {
                 try
                 {
                     context.Densities_Fifteen.AddRange(datas.Select(data => new TrafficDensity_Fifteen
                     {
                         DataId   = data.DataId,
                         DateTime = data.DateTime,
                         Value    = data.Value
                     }));
                     context.BulkSaveChanges(options => options.BatchSize = datas.Length);
                     Success_Fifteen += datas.Length;
                 }
                 catch (Exception ex)
                 {
                     _logger.LogError((int)LogEvent.高点数据块, ex, "十五分钟密度数据入库异常");
                     Failed_Fifteen += datas.Length;
                 }
             }
             else if (datas[0].DateLevel == DateTimeLevel.Hour)
             {
                 try
                 {
                     context.Densities_hour.AddRange(datas.Select(data => new TrafficDensity_Hour
                     {
                         DataId   = data.DataId,
                         DateTime = data.DateTime,
                         Value    = data.Value
                     }));
                     context.BulkSaveChanges(options => options.BatchSize = datas.Length);
                     Success_Sixty += datas.Length;
                 }
                 catch (Exception ex)
                 {
                     _logger.LogError((int)LogEvent.高点数据块, ex, "一小时密度数据入库异常");
                     Failed_Sixty += datas.Length;
                 }
             }
         }
     }
 }