Browse Source

add post statistics

xielin 5 years ago
parent
commit
05c0697dac

+ 143 - 0
app/Console/Commands/PostStatistics.php

@@ -0,0 +1,143 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/12
+ * Time: 16:32
+ */
+
+namespace App\Console\Commands;
+
+
+use App\Models\Behavior;
+use App\Models\CommentRecord;
+use App\Models\GeneralRecord;
+use App\Models\PostLog;
+use App\Models\ReleaseRecord;
+use Illuminate\Console\Command;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class PostStatistics extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:statistics';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '全部内容统计';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(\App\Models\PostStatistics $postStatistics,
+                                GeneralRecord $generalRecord,
+                                ReleaseRecord $releaseRecord,
+                                CommentRecord $commentRecord,
+                                PostLog $postLog,
+                                Behavior $behavior)
+    {
+        parent::__construct();
+        $this->postStatistics = $postStatistics;
+        $this->generalRecord = $generalRecord;
+        $this->releaseRecord = $releaseRecord;
+        $this->commentRecord = $commentRecord;
+        $this->postLog = $postLog;
+        $this->behavior = $behavior;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始统计所有内容");
+        $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
+        $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
+        $post = $this->releaseRecord
+            ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+            ->select(DB::raw('count(*) as post_count'))
+            ->first();
+        $comment = $this->commentRecord
+            ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+            ->select(DB::raw('count(*) as comment_count'))
+            ->first();
+        $behaviors = $this->behavior->whereIn('behavior_identification', ['read', 'like', 'share', 'collect'])->get()->toArray();
+        $postCount = $post->post_count;
+        $commentCount = $comment->comment_count;
+        $readCount = 0;
+        $likeCount = 0;
+        $shareCount = 0;
+        $collectCount = 0;
+        foreach ($behaviors as $behavior) {
+            $behaviorId = $behavior['virus_behavior_id'];
+            if ($behavior['behavior_identification'] == 'read') {
+                $read = $this->generalRecord
+                    ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
+                    ->select(DB::raw('count(*) as read_count'))
+                    ->first();
+                $readCount = $read->read_count;
+            } elseif ($behavior['behavior_identification'] == 'like') {
+                $like = $this->generalRecord
+                    ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
+                    ->select(DB::raw('count(*) as like_count'))
+                    ->first();
+                $likeCount = $like->like_count;
+            } elseif ($behavior['behavior_identification'] == 'share') {
+                $share = $this->generalRecord
+                    ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
+                    ->select(DB::raw('count(*) as share_count'))
+                    ->first();
+                $shareCount = $share->share_count;
+            } elseif ($behavior['behavior_identification'] == 'collect') {
+                $collect = $this->generalRecord
+                    ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
+                    ->select(DB::raw('count(*) as collect_count'))
+                    ->first();
+                $collectCount = $collect->collect_count;
+            }
+        }
+        Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.',帖子数:'.$postCount.'评论数:'.$commentCount.'阅读数:'.$readCount.'点赞数:'.$likeCount.'收藏数:'.$collectCount.'分享数:'.$shareCount);
+        $postLog = $this->postLog
+            ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
+            ->select('content')
+            ->get()->toArray();
+        foreach ($postLog as $log){
+            $logContent = json_decode($log['content'],true);
+            if(isset($logContent['add_pv']) && $logContent['add_pv']){
+                $readCount -= $logContent['add_pv'];
+            }
+            if(isset($logContent['add_praise_count']) && $logContent['add_praise_count']){
+                $likeCount -= $logContent['add_praise_count'];
+            }
+            if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
+                $collectCount -= $logContent['add_collect_count'];
+            }
+            if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
+                $shareCount -= $logContent['add_collect_count'];
+            }
+            Log::debug(date('Y-m-d H:i:s').'统计虚拟数'.$yesterdayStart.',阅读数:'.$logContent['add_pv'].'点赞数:'.$logContent['add_praise_count'].'收藏数:'.$logContent['add_collect_count'].'分享数:'.$logContent['add_collect_count']);
+        }
+        $data['post_count'] = $postCount;
+        $data['read_count'] = $readCount;
+        $data['share_count'] = $shareCount;
+        $data['like_count'] = $likeCount;
+        $data['collect_count'] = $collectCount;
+        $data['comment_count'] = $commentCount;
+        $this->postStatistics->insert($data);
+        Log::info(date('Y-m-d H:i:s').'统计'.date('Y-m-d',$yesterdayStart).'最终记录数据:'.json_encode($data));
+    }
+}

+ 2 - 1
app/Console/Commands/PostYesterday.php

@@ -53,7 +53,8 @@ class PostYesterday extends Command
         $this->line("开始统计昨日内容");
 
         $postData = $this->postData
-            ->where('created_at', '>=', Carbon::now()->startOfDay()->toDateTimeString())
+            ->where('created_at', '>=', Carbon::yesterday()->startOfDay()->toDateTimeString())
+            ->where('created_at', '<=', Carbon::yesterday()->endOfDay()->toDateTimeString())
             ->select(DB::raw('count(*) as count'), DB::raw('sum(create_bean) as bean'))
             ->first();
         Log::info('统计昨日内容'.json_encode($postData));

+ 7 - 2
app/Console/Kernel.php

@@ -4,6 +4,7 @@ namespace App\Console;
 
 use App\Console\Commands\Apollo;
 use App\Console\Commands\Downloads;
+use App\Console\Commands\PostStatistics;
 use App\Console\Commands\PostYesterday;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@@ -18,7 +19,8 @@ class Kernel extends ConsoleKernel
     protected $commands = [
         Apollo::class,
         PostYesterday::class,
-        Downloads::class
+        Downloads::class,
+        PostStatistics::class
     ];
 
     /**
@@ -36,7 +38,10 @@ class Kernel extends ConsoleKernel
             ->withoutOverlapping()->appendOutputTo($path);
 
         $schedule->command('post:yesterday')
-            ->dailyAt('23:50')
+            ->dailyAt('00:10')
+            ->withoutOverlapping()->appendOutputTo($path);
+        $schedule->command('post:statistics')
+            ->dailyAt('00:20')
             ->withoutOverlapping()->appendOutputTo($path);
     }
 }

+ 17 - 0
app/Models/PostStatistics.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostStatistics extends Model
+{
+//
+    protected $table = 'post_statistics';
+    protected $guarded = [];
+}

+ 37 - 0
database/migrations/2019_06_20_011008_create_community_statistics_table.php

@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateCommunityStatisticsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('community_statistics', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->bigIncrements('post_count')->default(0)->comment('内容数');
+            $table->bigIncrements('read_count')->default(0)->comment('阅读数');
+            $table->bigIncrements('share_count')->default(0)->comment('分享数');
+            $table->bigIncrements('like_count')->default(0)->comment('点赞数');
+            $table->bigIncrements('collect_count')->default(0)->comment('收藏数');
+            $table->bigIncrements('comment_count')->default(0)->comment('评论数');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('community_statistics');
+    }
+}