Browse Source

文章统计优化

懒得勤快 7 years ago
parent
commit
4fb3b890d8

+ 2 - 19
src/Common/CommonHelper.cs

@@ -125,26 +125,9 @@ namespace Common
         }
 
         /// <summary>
-        /// 网站运营开始时间
+        /// 平均访问量
         /// </summary>
-        public static int RunningDays
-        {
-            get
-            {
-                try
-                {
-                    using (var redisHelper = RedisHelper.GetInstance())
-                    {
-                        int days = redisHelper.GetServer().Keys(0, "Interview:20*").Count();
-                        return days;
-                    }
-                }
-                catch
-                {
-                    return 1;
-                }
-            }
-        }
+        public static double AverageCount { get; set; }
 
         /// <summary>
         /// 网站启动时间

+ 2 - 62
src/Masuit.MyBlogs.WebApp/Controllers/PostController.cs

@@ -49,20 +49,6 @@ namespace Masuit.MyBlogs.WebApp.Controllers
         [Route("{id:int}/{kw}"), Route("{id:int}")]
         public ActionResult Details(int id, string kw)
         {
-            List<string> list = new List<string>();
-            if (!string.IsNullOrEmpty(kw))
-            {
-                list = LuceneHelper.CutKeywords(kw);
-                if (Regex.Match(kw, BanRegex).Length > 0 || Regex.Match(kw, ModRegex).Length > 0)
-                {
-                    ViewBag.Keyword = "";
-                    return RedirectToAction("Details", "Post", new
-                    {
-                        id
-                    });
-                }
-            }
-
             Post post = PostBll.GetById(id);
             if (post != null)
             {
@@ -83,48 +69,9 @@ namespace Masuit.MyBlogs.WebApp.Controllers
 
                 if (string.IsNullOrEmpty(Session.GetByRedis<string>("post" + id)))
                 {
-                    //post.ViewCount++;
-                    var record = post.PostAccessRecord.FirstOrDefault(r => r.AccessTime == DateTime.Today);
-                    if (record != null)
-                    {
-                        record.ClickCount += 1;
-                    }
-                    else
-                    {
-                        post.PostAccessRecord.Add(new PostAccessRecord
-                        {
-                            ClickCount = 1,
-                            AccessTime = DateTime.Today
-                        });
-                    }
-
-                    PostBll.UpdateEntitySaved(post);
+                    HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.RecordPostVisit), args: id);
                     Session.SetByRedis("post" + id, id.ToString());
                 }
-
-                foreach (var s in list)
-                {
-                    if (s.Contains(new[]
-                    {
-                        @"\?",
-                        @"\*",
-                        @"\+",
-                        @"\-",
-                        @"\[",
-                        @"\]",
-                        @"\{",
-                        @"\}",
-                        @"\(",
-                        @"\)",
-                        "�"
-                    }))
-                    {
-                        continue;
-                    }
-
-                    post.Content = Regex.IsMatch(s, @"^\p{P}.*") ? post.Content.Replace(s, $"<span style='color:red;background-color:yellow;font-size: 1.1em;font-weight:700;'>{s}</span>") : Regex.Replace(post.Content, s, $"<span style='color:red;background-color:yellow;font-size: 1.1em;font-weight:700;'>{s}</span>", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
-                }
-
                 return View(post);
             }
 
@@ -406,13 +353,6 @@ namespace Masuit.MyBlogs.WebApp.Controllers
                 var s = RedisHelper.GetString("ArticleViewToken");
                 Session.SetByRedis("ArticleViewToken", s);
                 return ResultData(null);
-                //if(!RedisHelper.KeyExists("ArticleViewToken"))
-                //{
-                //    RedisHelper.SetString("ArticleViewToken", string.Empty.CreateShortToken(6));
-                //}
-                //var token = RedisHelper.GetString("ArticleViewToken");
-                //SendMail("懒得勤快的博客_文章验证码", "文章验证码:" + token, email);
-                //return ResultData(null,true,"文章验证码已经发送至您的邮箱,请注意查收");
             }
 
             return ResultData(null, false, "您目前没有权限访问这篇文章的加密部分,请联系站长开通这篇文章的访问权限!");
@@ -748,7 +688,7 @@ namespace Masuit.MyBlogs.WebApp.Controllers
             if (b)
             {
 #if !DEBUG
-                if(notify && "false" == GetSettings("DisabledEmailBroadcast"))
+                if (notify && "false" == GetSettings("DisabledEmailBroadcast"))
                 {
                     var cast = BroadcastBll.LoadEntities(c => c.Status == Status.Subscribed).ToList();
                     string link = Request.Url?.Scheme + "://" + Request.Url?.Authority + "/" + p.Id;

+ 21 - 0
src/Masuit.MyBlogs.WebApp/Models/Hangfire/HangfireBackJob.cs

@@ -170,6 +170,26 @@ namespace Masuit.MyBlogs.WebApp.Models.Hangfire
             LogManager.Error(ex);
         }
 
+        public void RecordPostVisit(int pid)
+        {
+            Post post = PostBll.GetById(pid);
+            var record = post.PostAccessRecord.FirstOrDefault(r => r.AccessTime == DateTime.Today);
+            if (record != null)
+            {
+                record.ClickCount += 1;
+            }
+            else
+            {
+                post.PostAccessRecord.Add(new PostAccessRecord
+                {
+                    ClickCount = 1,
+                    AccessTime = DateTime.Today
+                });
+            }
+
+            PostBll.UpdateEntitySaved(post);
+        }
+
         private (AnalysisModel, AnalysisModel) Analysis()
         {
             List<Interview> list;
@@ -186,6 +206,7 @@ namespace Masuit.MyBlogs.WebApp.Models.Hangfire
             {
                 return (null, null);
             }
+            CommonHelper.AverageCount = list.Count * 1.0 / list.GroupBy(i => i.ViewTime.Date).Count();
             var dap = list.Select(i =>
             {
                 var (a, d) = (list.GroupBy(g => g.Uid).Count(), list.Count(g => g.InterviewDetails.Count == 1));

+ 1 - 0
src/Masuit.MyBlogs.WebApp/Models/Hangfire/IHangfireBackJob.cs

@@ -16,5 +16,6 @@ namespace Masuit.MyBlogs.WebApp.Models.Hangfire
         void PublishPost(Post p);
         void AggregateInterviews();
         void InterviewTrace(Guid uid, string url);
+        void RecordPostVisit(int pid);
     }
 }

+ 1 - 1
src/Masuit.MyBlogs.WebApp/Views/Shared/_Layout.cshtml

@@ -220,7 +220,7 @@
             <div class="cd-overlay"></div>
             <footer class="footer wow fadeIn">
                 <div class="container">
-                    当前浏览人数:@CommonHelper.OnlineCount 人,总访客量:@CommonHelper.InterviewCount,今日访问量:@CommonHelper.Todaypv 人,平均访问量:@((CommonHelper.InterviewCount / CommonHelper.RunningDays).ToString("N"))人/天
+                    当前浏览人数:@CommonHelper.OnlineCount 人,总访客量:@CommonHelper.InterviewCount,今日访问量:@CommonHelper.Todaypv 人,平均访问量:@(CommonHelper.AverageCount.ToString("N"))人/天
                     <section>
                         友情链接:
                         <a href="/swagger">博客开放平台</a>