소스 검색

文章统计增加UV统计

懒得勤快 3 년 전
부모
커밋
0bd6fffedd

+ 9 - 5
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -1201,7 +1201,8 @@ public class PostController : BaseController
             var list1 = await statsService.GetQuery(e => e.PostId == id && e.Date >= start1).GroupBy(t => t.Date).Select(g => new
             {
                 Date = g.Key,
-                Count = g.Sum(t => t.Count)
+                Count = g.Sum(t => t.Count),
+                UV = g.Sum(t => t.UV)
             }).OrderBy(a => a.Date).ToListAsync(cancellationToken);
             if (list1.Count == 0)
             {
@@ -1212,7 +1213,8 @@ public class PostController : BaseController
             var list2 = await statsService.GetQuery(e => e.PostId == id && e.Date >= start2 && e.Date < start1).GroupBy(t => t.Date).Select(g => new
             {
                 Date = g.Key,
-                Count = g.Sum(t => t.Count)
+                Count = g.Sum(t => t.Count),
+                UV = g.Sum(t => t.UV)
             }).OrderBy(a => a.Date).ToListAsync(cancellationToken);
 
             // 将数据填充成连续的数据
@@ -1220,22 +1222,24 @@ public class PostController : BaseController
             {
                 if (list1.All(a => a.Date != i))
                 {
-                    list1.Add(new { Date = i, Count = 0 });
+                    list1.Add(new { Date = i, Count = 0, UV = 0 });
                 }
             }
             for (var i = start2; i < start1; i = i.AddDays(1))
             {
                 if (list2.All(a => a.Date != i))
                 {
-                    list2.Add(new { Date = i, Count = 0 });
+                    list2.Add(new { Date = i, Count = 0, UV = 0 });
                 }
             }
             return Ok(new[] { list1.OrderBy(a => a.Date), list2.OrderBy(a => a.Date) });
         }
+
         var list = await statsService.GetQuery(e => e.PostId == id).GroupBy(t => t.Date).Select(g => new
         {
             Date = g.Key,
-            Count = g.Sum(t => t.Count)
+            Count = g.Sum(t => t.Count),
+            UV = g.Sum(t => t.UV)
         }).OrderBy(a => a.Date).ToListAsync(cancellationToken);
         return Ok(new[] { list });
     }

+ 2 - 0
src/Masuit.MyBlogs.Core/Extensions/Hangfire/HangfireBackJob.cs

@@ -124,12 +124,14 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
             if (stats != null)
             {
                 stats.Count = recordService.Count(e => e.PostId == pid & e.Time >= DateTime.Today) + 1;
+                stats.UV = recordService.GetQuery(e => e.PostId == pid & e.Time >= DateTime.Today).Select(e => e.IP).Distinct().Count() + 1;
             }
             else
             {
                 recordStatsService.AddEntity(new PostVisitRecordStats()
                 {
                     Count = 1,
+                    UV = 1,
                     Date = DateTime.Today,
                     PostId = pid
                 });

+ 3 - 0
src/Masuit.MyBlogs.Core/Models/Entity/PostVisitRecordStats.cs

@@ -13,4 +13,7 @@ public class PostVisitRecordStats : BaseEntity
 
     [ConcurrencyCheck]
     public int Count { get; set; }
+
+    [ConcurrencyCheck]
+    public int UV { get; set; }
 }

+ 28 - 13
src/Masuit.MyBlogs.Core/Views/Post/PostVisitRecordInsight.cshtml

@@ -111,9 +111,9 @@
 		});
     });
 	showCharts();
-    function showCharts() {
-		var period=document.getElementById("period").value;
-		window.fetch(`/@Model.Id/records-chart?compare=${period>0}&period=${period}`, {
+	function showCharts() {
+		var period = document.getElementById("period").value;
+		window.fetch(`/@Model.Id/records-chart?compare=${period > 0}&period=${period}`, {
 			credentials: 'include',
 			method: 'GET',
 			mode: 'cors'
@@ -121,16 +121,20 @@
 			return response.json();
 		}).then(function (res) {
 			var xSeries = [];
-			var ySeries = [];
+			var yCountSeries = [];
+			var yUvSeries = [];
 			for (let series of res) {
 				var x = [];
-				var y = [];
+				var yCount = [];
+				var yUV = [];
 				for (let item of series) {
 					x.push(new Date(Date.parse(item.Date)).toLocaleDateString());
-					y.push(item.Count);
+					yCount.push(item.Count);
+					yUV.push(item.UV);
 				}
 				xSeries.push(x);
-				ySeries.push(y);
+				yCountSeries.push(yCount);
+				yUvSeries.push(yUV);
 			}
 			var chartDom = document.getElementById('chart');
 			var myChart = echarts.init(chartDom);
@@ -150,7 +154,7 @@
 				},
 				title: {
 					left: 'center',
-					text: '最近访问趋势' //+ (res.reduce((acr, cur) => acr + cur.Count, 0) / (new Date() - new Date(res[0].Date)) * (1000 * 60 * 60 * 24)).toFixed(2)
+					text: '最近访问趋势'
 				},
 				xAxis: xSeries.map(function (item, index) {
 					return {
@@ -167,7 +171,7 @@
 						axisPointer: {
 							label: {
 								formatter: function (params) {
-									return params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
+									return params.value + (params.seriesData.length ? ' 访问量:' + params.seriesData[0].data + ",UV:" + params.seriesData[1].data : '');
 								}
 							}
 						},
@@ -179,13 +183,12 @@
 						type: 'value'
 					}
 				],
-				series: ySeries.map(function (item, index) {
+				series: yCountSeries.map(function (item, index) {
 					return {
 						type: 'line',
 						smooth: true,
 						symbol: 'none',
 						xAxisIndex: index,
-						areaStyle: {},
 						data: item,
 						lineStyle: {
 							type: index === 1 ? 'dashed' : ""
@@ -202,9 +205,21 @@
 							]
 						}
 					}
-				})
+				}).concat(yUvSeries.map(function (item, index) {
+					return {
+						type: 'line',
+						smooth: true,
+						symbol: 'none',
+						xAxisIndex: index,
+						areaStyle: {},
+						data: item,
+						lineStyle: {
+							type: index === 1 ? 'dashed' : ""
+						}
+					}
+				}))
 			};
 			myChart.setOption(option);
 		});
-    }
+	}
 </script>