public void Statistics(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) { DateTime startDate = new DateTime(startYear, startMonth, startDay); DateTime endDate = new DateTime(endYear, endMonth, endDay); int days = Convert.ToInt32((endDate - startDate).TotalDays + 1); List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 6, 6, "", true); //创建每小时两条条数据 EventDbSimulator.CreateData(TestInit.ServiceProvider, devices, startDate, endDate, DataCreateMode.Fixed, true); TestInit.RefreshDensityCache(devices); EventsController service = new EventsController(TestInit.ServiceProvider.GetRequiredService <DensityContext>(), TestInit.ServiceProvider.GetRequiredService <IMemoryCache>()); foreach (DensityDevice device in devices) { foreach (var relation in device.DensityDevice_DensityChannels) { var roadList = service.StatisticsByRoad(relation.Channel.CrossingId.Value, startDate, endDate.AddDays(1)); //验证按路口按小时分组统计次数 Assert.AreEqual(days * 24, roadList.Count); //验证按区域按小时分组统计次数 foreach (TrafficRegion region in relation.Channel.Regions) { var regionList = service.StatisticsByRegion(region.DataId, startDate, endDate.AddDays(1)); Assert.AreEqual(days * 24, regionList.Count); } } } }
public void Last10() { List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 1, 10, "", true); TestInit.RefreshDensityCache(devices); EventDbSimulator.CreateData(TestInit.ServiceProvider, devices, DateTime.Today, true); EventsController service = new EventsController(TestInit.ServiceProvider.GetRequiredService <DensityContext>(), TestInit.ServiceProvider.GetRequiredService <IMemoryCache>()); var v = service.QueryLast10(); int itemCount = devices[0].DensityDevice_DensityChannels[0].Channel.Regions.Count; for (int i = 0; i < 10; ++i) { Assert.AreEqual(v[i].DataId, devices[0].DensityDevice_DensityChannels[0].Channel.Regions[itemCount - 1 - i].DataId); } }
public void QueryList() { DateTime startDate = new DateTime(2019, 5, 10); DateTime endDate = new DateTime(2019, 5, 11); int days = Convert.ToInt32((endDate - startDate).TotalDays + 1); int months = startDate.Month == endDate.Month ? 1 : 2; List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 1, 2, "127.0.0.1", true); DensityDbSimulator.CreateData(TestInit.ServiceProvider, devices, DataCreateMode.Sequence, startDate, endDate, true); DensitiesManager manager = TestInit.ServiceProvider.GetRequiredService <DensitiesManager>(); int average = 720; foreach (DensityDevice device in devices) { foreach (var relation in device.DensityDevice_DensityChannels) { foreach (TrafficRegion region in relation.Channel.Regions) { var oneMinuteList = manager.QueryList(region.DataId, DateTimeLevel.Minute, startDate, endDate.AddDays(1).AddMinutes(-1)); //验证查询数量 Assert.AreEqual(24 * 60 * days, oneMinuteList.Count); //验证平均密度 Assert.AreEqual(average, Convert.ToInt32(oneMinuteList.Average(d => d.Value))); var fiveMinuteList = manager.QueryList(region.DataId, DateTimeLevel.FiveMinutes, startDate, endDate.AddDays(1).AddMinutes(-5)); Assert.AreEqual(24 * 60 / 5 * days, fiveMinuteList.Count); Assert.AreEqual(average, Convert.ToInt32(fiveMinuteList.Average(d => d.Value))); var fifteenMinuteList = manager.QueryList(region.DataId, DateTimeLevel.FifteenMinutes, startDate, endDate.AddDays(1).AddMinutes(-15)); Assert.AreEqual(24 * 60 / 15 * days, fifteenMinuteList.Count); Assert.AreEqual(average, Convert.ToInt32(fifteenMinuteList.Average(d => d.Value))); var sixtyMinuteList = manager.QueryList(region.DataId, DateTimeLevel.Hour, startDate, endDate.AddDays(1).AddHours(-1)); Assert.AreEqual(24 * days, sixtyMinuteList.Count); Assert.AreEqual(average, Convert.ToInt32(sixtyMinuteList.Average(d => d.Value))); var dayList = manager.QueryList(region.DataId, DateTimeLevel.Day, startDate, endDate); Assert.AreEqual(days, dayList.Count); Assert.AreEqual(average, Convert.ToInt32(dayList.Average(d => d.Value))); var monthList = manager.QueryList(region.DataId, DateTimeLevel.Month, TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate), TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, endDate)); Assert.AreEqual(months, monthList.Count); Assert.AreEqual(average, Convert.ToInt32(monthList.Average(d => d.Value))); } } } }
public void Rank(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) { DateTime startDate = new DateTime(startYear, startMonth, startDay); DateTime endDate = new DateTime(endYear, endMonth, endDay); List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 6, 6, "", true); //随机出的值表示次数,每次1分钟 Dictionary <TrafficEvent, int> trafficEvents = EventDbSimulator.CreateData(TestInit.ServiceProvider, devices, startDate, endDate, DataCreateMode.Random, true); TestInit.RefreshDensityCache(devices); EventsController service = new EventsController(TestInit.ServiceProvider.GetRequiredService <DensityContext>(), TestInit.ServiceProvider.GetRequiredService <IMemoryCache>()); IMemoryCache memoryCache = TestInit.ServiceProvider.GetRequiredService <IMemoryCache>(); //要验证的值 var regions = trafficEvents .Select(p => new KeyValuePair <string, int>(p.Key.DataId, p.Value)) .OrderByDescending(p => p.Value) .ToList(); var roads = trafficEvents .GroupBy(p => memoryCache.GetRegion(p.Key.DataId).Channel.CrossingId) .Select(g => new KeyValuePair <int, int>(g.Key.Value, g.Sum(p => p.Value))) .OrderByDescending(p => p.Value) .ToList(); //区域次数 var countRegionTop = service.CountRankByRegion(startDate, endDate.AddDays(1)); for (int i = 0; i < countRegionTop.Count; ++i) { //验证次数和区域编号 Assert.AreEqual(countRegionTop[i].Value, regions[i].Value); Assert.AreEqual(countRegionTop[i].Axis, regions[i].Key); } //路口次数 var countRoadTop = service.CountRankByRoad(startDate, endDate.AddDays(1)); for (int i = 0; i < countRoadTop.Count; ++i) { //验证次数和路口编号 Assert.AreEqual(countRoadTop[i].Value, roads[i].Value); Assert.AreEqual(countRoadTop[i].Axis, roads[i].Key); } //区域时长 var spanRegionTop = service.SpanRankByRegion(startDate, endDate.AddDays(1)); for (int i = 0; i < spanRegionTop.Count; ++i) { //验证时长(秒)和区域编号 Assert.AreEqual(spanRegionTop[i].Value, regions[i].Value); Assert.AreEqual(spanRegionTop[i].Axis, regions[i].Key); } //路口时长 var spanRoadTop = service.SpanRankByRoad(startDate, endDate.AddDays(1)); for (int i = 0; i < countRoadTop.Count; ++i) { //验证时长(秒)和路口编号 Assert.AreEqual(spanRoadTop[i].Value, roads[i].Value); Assert.AreEqual(spanRoadTop[i].Axis, roads[i].Key); } }
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); } } } } }
public void QueryComparison() { //创建某天的模拟数据 DateTime startDate = new DateTime(2019, 7, 1); List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 1, 2, "", true); List <DateTime> dates = new List <DateTime> { startDate.AddYears(-1), startDate.AddMonths(-1), startDate.AddDays(-1), startDate }; DensityDbSimulator.CreateData(TestInit.ServiceProvider, devices, DataCreateMode.Sequence, dates, true); DensitiesManager manager = TestInit.ServiceProvider.GetRequiredService <DensitiesManager>(); foreach (DensityDevice device in devices) { foreach (var relation in device.DensityDevice_DensityChannels) { foreach (TrafficRegion region in relation.Channel.Regions) { //验证5分钟 var fiveCharts = manager.QueryComparison(region.DataId, DateTimeLevel.FiveMinutes, startDate, startDate.AddDays(1).AddMinutes(-5)); for (int i = 0; i < fiveCharts.Count; ++i) { fiveCharts[i] = fiveCharts[i].OrderBy(c => c.Axis).ToList(); } //验证同比环比的图表时间对其到第一个时间段 Assert.AreEqual(startDate, fiveCharts[0][0].Axis); Assert.AreEqual(startDate, fiveCharts[1][0].Axis); Assert.AreEqual(startDate, fiveCharts[2][0].Axis); //验证同比环比的remark是真实的时间 Assert.AreEqual(startDate.ToString("yyyy-MM-dd HH:mm"), fiveCharts[0][0].Remark); Assert.AreEqual(startDate.AddDays(-1).ToString("yyyy-MM-dd HH:mm"), fiveCharts[1][0].Remark); Assert.AreEqual(startDate.AddMinutes(-5).ToString("yyyy-MM-dd HH:mm"), fiveCharts[2][0].Remark); //验证图表中的数据数量 foreach (List <TrafficChart <DateTime, int> > charts in fiveCharts) { Assert.AreEqual(24 * 60 / 5, charts.Count); } var fifteenCharts = manager.QueryComparison(region.DataId, DateTimeLevel.FifteenMinutes, startDate, startDate.AddDays(1).AddMinutes(-15)); for (int i = 0; i < fifteenCharts.Count; ++i) { fifteenCharts[i] = fifteenCharts[i].OrderBy(c => c.Axis).ToList(); } Assert.AreEqual(startDate, fifteenCharts[0][0].Axis); Assert.AreEqual(startDate, fifteenCharts[1][0].Axis); Assert.AreEqual(startDate, fifteenCharts[2][0].Axis); Assert.AreEqual(startDate.ToString("yyyy-MM-dd HH:mm"), fifteenCharts[0][0].Remark); Assert.AreEqual(startDate.AddDays(-1).ToString("yyyy-MM-dd HH:mm"), fifteenCharts[1][0].Remark); Assert.AreEqual(startDate.AddMinutes(-15).ToString("yyyy-MM-dd HH:mm"), fifteenCharts[2][0].Remark); foreach (List <TrafficChart <DateTime, int> > charts in fifteenCharts) { Assert.AreEqual(24 * 60 / 15, charts.Count); } var hourCharts = manager.QueryComparison(region.DataId, DateTimeLevel.Hour, startDate, startDate.AddDays(1).AddHours(-1)); for (int i = 0; i < hourCharts.Count; ++i) { hourCharts[i] = hourCharts[i].OrderBy(c => c.Axis).ToList(); } Assert.AreEqual(startDate, hourCharts[0][0].Axis); Assert.AreEqual(startDate, hourCharts[1][0].Axis); Assert.AreEqual(startDate, hourCharts[2][0].Axis); Assert.AreEqual(startDate.ToString("yyyy-MM-dd HH"), hourCharts[0][0].Remark); Assert.AreEqual(startDate.AddDays(-1).ToString("yyyy-MM-dd HH"), hourCharts[1][0].Remark); Assert.AreEqual(startDate.AddHours(-1).ToString("yyyy-MM-dd HH"), hourCharts[2][0].Remark); foreach (List <TrafficChart <DateTime, int> > charts in hourCharts) { Assert.AreEqual(24, charts.Count); } var dayCharts = manager.QueryComparison(region.DataId, DateTimeLevel.Day, startDate, startDate); for (int i = 0; i < dayCharts.Count; ++i) { dayCharts[i] = dayCharts[i].OrderBy(c => c.Axis).ToList(); } Assert.AreEqual(startDate, dayCharts[0][0].Axis); Assert.AreEqual(startDate, dayCharts[1][0].Axis); Assert.AreEqual(startDate, dayCharts[2][0].Axis); Assert.AreEqual(startDate.ToString("yyyy-MM-dd"), dayCharts[0][0].Remark); Assert.AreEqual(startDate.AddMonths(-1).ToString("yyyy-MM-dd"), dayCharts[1][0].Remark); Assert.AreEqual(startDate.AddDays(-1).ToString("yyyy-MM-dd"), dayCharts[2][0].Remark); foreach (List <TrafficChart <DateTime, int> > charts in dayCharts) { Assert.AreEqual(1, charts.Count); } var monthCharts = manager.QueryComparison(region.DataId, DateTimeLevel.Month, TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate), TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate)); for (int i = 0; i < monthCharts.Count; ++i) { monthCharts[i] = monthCharts[i].OrderBy(c => c.Axis).ToList(); } Assert.AreEqual(TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate), monthCharts[0][0].Axis); Assert.AreEqual(TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate), monthCharts[1][0].Axis); Assert.AreEqual(TimePointConvert.CurrentTimePoint(DateTimeLevel.Month, startDate), monthCharts[2][0].Axis); Assert.AreEqual(startDate.ToString("yyyy-MM"), monthCharts[0][0].Remark); Assert.AreEqual(startDate.AddYears(-1).ToString("yyyy-MM"), monthCharts[1][0].Remark); Assert.AreEqual(startDate.AddMonths(-1).ToString("yyyy-MM"), monthCharts[2][0].Remark); foreach (List <TrafficChart <DateTime, int> > charts in monthCharts) { Assert.AreEqual(1, charts.Count); } } } } }
public void QueryVipRegions() { List <DensityDevice> devices = new List <DensityDevice>(); int deviceCount = 1; int channelCount = 1; int regionCount = 12; HashSet <string> vips = new HashSet <string>(); //随机创建重点区域 Random random = new Random(); using (IServiceScope serviceScope = TestInit.ServiceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); int deviceId = 1; int crossingId = 1; int regionId = 1; int channelId = 1; for (int i = 0; i < deviceCount; ++i) { DensityDevice densityDevice = new DensityDevice { DeviceId = deviceId++, Ip = "192.168.200.204", Port = 18000 + i }; densityDevice.DeviceName = "设备" + densityDevice.Port; densityDevice.DensityDevice_DensityChannels = new List <DensityDevice_DensityChannel>(); for (int j = 0; j < channelCount; ++j) { RoadCrossing roadCrossing = new RoadCrossing { CrossingId = crossingId, CrossingName = "路口" + crossingId }; DensityChannel channel = new DensityChannel() { ChannelId = channelId.ToString(), ChannelName = $"通道 {densityDevice.DeviceId} {j+1}", ChannelIndex = j + 1, CrossingId = crossingId, Regions = new List <TrafficRegion>(), RoadCrossing = roadCrossing }; DensityDevice_DensityChannel relation = new DensityDevice_DensityChannel { ChannelId = channel.ChannelId, DeviceId = densityDevice.DeviceId, Channel = channel }; channelId++; crossingId++; densityDevice.DensityDevice_DensityChannels.Add(relation); for (int k = 0; k < regionCount; ++k) { int value = random.Next(1, 2); TrafficRegion region = new TrafficRegion { ChannelId = channel.ChannelId, Channel = channel, Region = "1", RegionIndex = k + 1, RegionName = "区域" + regionId++, IsVip = value == 1 }; if (value == 1) { vips.Add(region.DataId); } channel.Regions.Add(region); } } context.Devices.Add(densityDevice); devices.Add(densityDevice); } context.SaveChanges(); } DensityDbSimulator.CreateData(TestInit.ServiceProvider, devices, DataCreateMode.Fixed, DateTime.Today, DateTime.Today); TestInit.RefreshDensityCache(devices); DensitiesManager manager = TestInit.ServiceProvider.GetRequiredService <DensitiesManager>(); var v = manager.QueryVipRegions(); foreach (TrafficDensity density in v) { Assert.IsTrue(vips.Contains(density.DataId)); } } }
public void QueryDayTop10() { DateTime now = DateTime.Now; DateTime today = now.Date; List <DensityDevice> devices = DensityDbSimulator.CreateDensityDevice(TestInit.ServiceProvider, 1, 1, 12, "", true); DensityDbSimulator.ResetDatabase(TestInit.ServiceProvider); TestInit.RefreshDensityCache(devices); DensitiesManager manager = TestInit.ServiceProvider.GetRequiredService <DensitiesManager>(); var regions = DensityDbSimulator.CreateData(TestInit.ServiceProvider, devices , new List <DataCreateMode> { DataCreateMode.Fixed, DataCreateMode.Fixed, DataCreateMode.Random } , new List <DateTime> { today.AddDays(-1), now.AddHours(-2).AddMinutes(30), now.AddHours(-1).AddMinutes(30) } , new List <DateTime> { today.AddDays(-1).AddMinutes(1), now.AddHours(-2).AddMinutes(31), now.AddHours(-1).AddMinutes(31) } , true); var densities = regions.OrderByDescending(p => p.Value).ToList(); var dayTop10 = manager.QueryTop10(today); for (int i = 0; i < dayTop10.Count; ++i) { Assert.AreEqual(densities[i].Key.DataId, dayTop10[i].DataId); //今天 Assert.AreEqual(Math.Ceiling((densities[i].Value + 1) / 2.0), dayTop10[i].Value); } DateTime time = DateTime.Now.AddHours(-1); var hourTop10 = manager.QueryTop10(time); for (int i = 0; i < hourTop10.Count; ++i) { Assert.AreEqual(densities[i].Key.DataId, hourTop10[i].DataId); //本小时 Assert.AreEqual(densities[i].Value, hourTop10[i].Value); } var dayChangeTop10 = manager.QueryChangeTop10(today, today.AddDays(-1), today.AddDays(-1).Add(now.TimeOfDay)); for (int i = 0; i < dayChangeTop10.Count; ++i) { Assert.AreEqual(densities[i].Key.DataId, dayChangeTop10[i].DataId); //今天 Assert.AreEqual(Math.Ceiling((densities[i].Value + 1) / 2.0), dayChangeTop10[i].Value); //昨天 Assert.AreEqual(1, dayChangeTop10[i].LastValue); } var hourChangeTop10 = manager.QueryChangeTop10(now.AddHours(-1), now.AddHours(-2), now.AddHours(-1)); for (int i = 0; i < hourChangeTop10.Count; ++i) { Assert.AreEqual(densities[i].Key.DataId, hourChangeTop10[i].DataId); //本小时 Assert.AreEqual(hourTop10[i].Value, hourChangeTop10[i].Value); //上小时 Assert.AreEqual(1, hourChangeTop10[i].LastValue); } }