Skip to content

Commit

Permalink
Add calculate maintainer works logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjinghua committed Oct 16, 2016
1 parent 06f14dc commit 319e2fd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
61 changes: 61 additions & 0 deletions app/Console/Commands/CalculateMaintainerWorks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\Role;
use App\Models\User;
use App\Models\MaintainerLog;

class CalculateMaintainerWorks extends Command
{

protected $signature = 'phphub:calculate-maintainer-works';
protected $description = 'Calculate maintainer works';


public function __construct()
{
parent::__construct();
}

public function handle()
{
$start_time = Carbon::now()->subDays(7)->toDateString();
$end_time = Carbon::now()->toDateString();

$role = Role::find(2);
$maintainers = User::byRolesName($role->name);

foreach ($maintainers as $user) {
$data = [];

$topics_count = $user->topics()
->whereBetween('created_at', [$start_time, $end_time])
->count();

$replies_count = $user->replies()
->whereBetween('created_at', [$start_time, $end_time])
->count();

$excellent_count = $user->revisions()
->where('key', 'is_excellent')
->whereBetween('created_at', [$start_time, $end_time])
->get();

$sink_count = $user->revisions()
->where('key', 'order')
->whereBetween('created_at', [$start_time, $end_time])
->count();

$data = compact('topics_count', 'replies_count', 'excellent_count', 'sink_count');
$data['user_id'] = $user->id;
$data['start_time'] = $start_time;
$data['end_time'] = $end_time;
MaintainerLog::create($data);
}

$this->info('Done');
}
}
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel
Commands\CalculateActiveUser::class,
Commands\CalculateHotTopic::class,
Commands\ClearUserData::class,
Commands\CalculateMaintainerWorks::class,
Commands\SyncUserActivedTime::class,
];

Expand Down
15 changes: 15 additions & 0 deletions app/Models/MaintainerLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MaintainerLog extends Model
{
protected $guarded = ['id'];

public function user()
{
return $this->belongsTo(User::class);
}
}
7 changes: 6 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function scopeIsRole($query, $role)

public static function byRolesName($name)
{
$data = Cache::remember('phphub_roles_'.$name, 60, function() use ($name) {
$data = Cache::remember('phphub_roles_'.$name, 60, function () use ($name) {
return User::isRole($name)->orderBy('last_actived_at', 'desc')->get();
});
return $data;
Expand Down Expand Up @@ -103,6 +103,11 @@ public function notifications()
return $this->hasMany(Notification::class)->recent()->with('topic', 'fromUser')->paginate(20);
}

public function revisions()
{
return $this->hasMany(Revision::class);
}

public function scopeRecent($query)
{
return $query->orderBy('created_at', 'desc');
Expand Down
28 changes: 28 additions & 0 deletions database/migrations/2016_10_16_225901_create_maintainer_logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMaintainerLogs extends Migration
{

public function up()
{
Schema::create('maintainer_logs', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('start_time');
$table->timestamp('end_time');
$table->integer('user_id')->unsigned()->index();
$table->integer('topics_count')->unsigned();
$table->integer('replies_Count')->unsigned();
$table->integer('excellent_count')->unsigned();
$table->integer('sink_count')->unsigned();
$table->timestamps();
});
}

public function down()
{
Schema::drop('maintainer_logs');
}
}

0 comments on commit 319e2fd

Please sign in to comment.