mahdihty
4 years ago
10 changed files with 422 additions and 57 deletions
-
14app/Models/TagTask.php
-
8app/Statists/Pipe.php
-
44app/Statists/SprintStatist.php
-
22app/Statists/SystemStatist.php
-
54app/Statists/TagStatist.php
-
32database/factories/TaskFactory.php
-
23database/factories/WorkFactory.php
-
204database/seeds/TaskSeeder.php
-
36database/seeds/WorkSeeder.php
-
42routes/api.php
@ -0,0 +1,14 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App; |
||||
|
|
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class TagTask extends Model |
||||
|
{ |
||||
|
protected $table = 'tag_task'; |
||||
|
public $incrementing = true; |
||||
|
protected $fillable = ['tag_id', 'task_id']; |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Statists; |
||||
|
|
||||
|
interface Pipe |
||||
|
{ |
||||
|
public function handle(&$content, \Closure $next); |
||||
|
} |
@ -0,0 +1,44 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Statists; |
||||
|
|
||||
|
use App\Task; |
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Facades\Cache; |
||||
|
|
||||
|
class SprintStatist implements Pipe |
||||
|
{ |
||||
|
private Task $task; |
||||
|
|
||||
|
public function __construct($task) |
||||
|
{ |
||||
|
$this->task = $task; |
||||
|
} |
||||
|
|
||||
|
public function handle(&$result, $next) |
||||
|
{ |
||||
|
$key = 'sprints.'; |
||||
|
$key .= ($this->task->sprint_id ?? 0).'.'; |
||||
|
$key .= ($this->task->assignee_id ?? 0).'.'; |
||||
|
$key .= $this->task->workflow_id . '.'; |
||||
|
$key .= $this->task->status_id; |
||||
|
|
||||
|
$map = [ |
||||
|
'total' => 0, |
||||
|
'test' => 0, |
||||
|
'overdue' => 0, |
||||
|
'worked' => 0, // total worked in minute
|
||||
|
'estimate' => 0, // estimate total
|
||||
|
]; |
||||
|
|
||||
|
$node = Arr::get($result, $key, $map); |
||||
|
|
||||
|
$node['total'] = $node['total'] + 1; |
||||
|
$node['test'] = $node['test'] + $result->ready_to_test; |
||||
|
$node['overdue'] = $node['overdue'] + ($result->on_time? 0 : 1); |
||||
|
|
||||
|
Arr::set($result, $key, $node); |
||||
|
|
||||
|
return $next($result); |
||||
|
} |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Statists; |
||||
|
|
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Facades\Cache; |
||||
|
|
||||
|
class SystemStatist implements Pipe |
||||
|
{ |
||||
|
public function handle($tasks, $next) |
||||
|
{ |
||||
|
$key = 'systems.'. ($task->system_id ?? 0) . '.' . ($task->assignee_id ?? 0) . |
||||
|
'.' . $task->workflow_id . '.' . $task->status_id; |
||||
|
$node = Arr::get($result, $key, ['total' => 0, 'test' => 0, 'overdue' => 0]); |
||||
|
$node['total'] = $node['total'] + 1; |
||||
|
$node['test'] = $node['test'] + $task->ready_to_test; |
||||
|
$node['overdue'] = $node['overdue'] + ($task->on_time? 0: 1); |
||||
|
Arr::set($result, $key, $node); |
||||
|
|
||||
|
return $next($tasks); |
||||
|
} |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Statists; |
||||
|
|
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Facades\Cache; |
||||
|
|
||||
|
class TagStatist implements Pipe |
||||
|
{ |
||||
|
public function handle($tasks, $next) |
||||
|
{ |
||||
|
$result = []; |
||||
|
|
||||
|
$project = $tasks->first()->project_id; |
||||
|
|
||||
|
$tags = $tasks->pluck('tag_id'); |
||||
|
$users = $tasks->pluck('assignee_id'); |
||||
|
$workflows = $tasks->pluck('workflow_id'); |
||||
|
$statuses = $tasks->pluck('status_id'); |
||||
|
|
||||
|
foreach ($tags as $tag) { |
||||
|
foreach ($users as $user) { |
||||
|
foreach ($workflows as $workflow) { |
||||
|
foreach ($statuses as $status) { |
||||
|
$filter = $tasks |
||||
|
->where('sprint_id', '=', $tag) |
||||
|
->where('assignee_id', '=', $user) |
||||
|
->where('workflow_id', '=', $workflow) |
||||
|
->where('status_id', '=', $status); |
||||
|
|
||||
|
if ($filter->isEmpty()) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
$stat = [ |
||||
|
'task_count' => $filter->count(), |
||||
|
'total_worked_in_minute' => $filter->sum('spent_time'), |
||||
|
'overdue_count' => $filter->where('on_time','=',false)->count(), |
||||
|
'total_estimate_in_minute' => $filter->sum('estimated_time'), |
||||
|
'test_flag_count' => $filter->where('ready_to_test', '=', true)->count(), |
||||
|
]; |
||||
|
|
||||
|
Arr::set($result,"{$tag}.{$user}.{$workflow}.{$status}",$stat); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Cache::put($project, $result); |
||||
|
|
||||
|
return $next($tasks); |
||||
|
} |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
<?php |
||||
|
|
||||
|
/** @var \Illuminate\Database\Eloquent\Factory $factory */ |
||||
|
|
||||
|
use App\Work; |
||||
|
use Carbon\Carbon; |
||||
|
use Faker\Generator as Faker; |
||||
|
|
||||
|
$factory->define(Work::class, function (Faker $faker) { |
||||
|
$started_at = $faker->dateTime('now'); |
||||
|
$ended_at = $faker->dateTime('now'); |
||||
|
// $minute_sum = $ended_at->diffInMinutes($started_at);
|
||||
|
return [ |
||||
|
'business_id' => $faker->numberBetween(1, 200), |
||||
|
'project_id' => $faker->numberBetween(1, 200), |
||||
|
'task_id' => $faker->numberBetween(1, 200), |
||||
|
'user_id' => $faker->numberBetween(1, 5000), |
||||
|
'message' => $faker->name, |
||||
|
'minute_sum' => $faker->numberBetween(100, 5000), |
||||
|
'started_at' => $started_at, |
||||
|
'ended_at' => $ended_at, |
||||
|
]; |
||||
|
}); |
@ -1,62 +1,174 @@ |
|||||
<?php |
<?php |
||||
|
|
||||
use App\Business; |
|
||||
use App\User; |
|
||||
use Carbon\Carbon; |
|
||||
|
use App\TagTask; |
||||
|
use App\Task; |
||||
use Illuminate\Database\Seeder; |
use Illuminate\Database\Seeder; |
||||
use Illuminate\Support\Facades\DB; |
|
||||
|
|
||||
class TaskSeeder extends Seeder |
class TaskSeeder extends Seeder |
||||
{ |
{ |
||||
public function run() |
public function run() |
||||
{ |
{ |
||||
/** @var User $owner */ |
|
||||
// every project has it's own tasks
|
|
||||
|
|
||||
/** |
|
||||
* Every task has it's own tags, which must be present in business that this tags belongs to it. |
|
||||
*/ |
|
||||
$businesses = Business::with('owners', 'projects')->get(); |
|
||||
$tasks = []; |
|
||||
|
|
||||
$left = count($businesses); |
|
||||
$now = Carbon::now()->toDateTimeString(); |
|
||||
$future = Carbon::now()->addDays(10)->toDateTimeString(); |
|
||||
foreach ($businesses as $business) { |
|
||||
foreach ($business->owners as $owner) { |
|
||||
foreach ($business->projects as $project) { |
|
||||
foreach ($business->members as $member) { |
|
||||
for ($i = 1; $i <= 30; $i++) { |
|
||||
foreach ($business->workflows as $workflow) { |
|
||||
$status = $workflow->workstatuses->random(); |
|
||||
$tasks[] = [ |
|
||||
'business_id' => $business->id, |
|
||||
// 'creator_id' => $owner->id,
|
|
||||
// 'project_id' => $project->id,
|
|
||||
// 'user_id' => $member->id,
|
|
||||
// 'workflow_id' => $workflow->id,
|
|
||||
// 'work_status_id' => $status->id,
|
|
||||
// 'name' => 'Task #' . rand(111111, 999999),
|
|
||||
// 'time' => "10:00:00",
|
|
||||
// 'cost' => 10000,
|
|
||||
// 'completed' => $status->done,
|
|
||||
// 'due_date' => $status->done ? $now : $future,
|
|
||||
]; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
for ($i = 1; $i <= 1; $i++) { |
||||
|
\App\Task::insert( |
||||
|
factory(\App\Task::class, 2000)->raw() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
$tags = [ |
||||
|
1 => 'باربری', |
||||
|
2 => 'زیر چاپ', |
||||
|
3 => 'برون سپاری شود', |
||||
|
4 => 'مشتری نپسندید', |
||||
|
]; |
||||
|
|
||||
|
$ids = Task::select('id')->pluck('id')->toArray(); |
||||
|
$tag_task_relation = []; |
||||
|
foreach ($ids as $id) { |
||||
|
foreach (array_rand($tags, mt_rand(2, 4)) as $index => $tag) { |
||||
|
$tag_task_relation[] = [ |
||||
|
'task_id' => $id, |
||||
|
'tag_id' => $tag, |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
TagTask::insert($tag_task_relation); |
||||
|
$this->createRelatedData(); |
||||
|
} |
||||
|
|
||||
|
public function createRelatedData() |
||||
|
{ |
||||
|
Task::whereIn('business_id', [1, 2, 3, 4, 5]) |
||||
|
->update(['business_id' => 1779, 'project_id' => 0, 'workflow_id' => 0, 'status_id' => 0, |
||||
|
'system_id' => 0, 'sprint_id' => 0]); |
||||
|
|
||||
|
$count = Task::where('business_id', '=', 1779)->count(); |
||||
|
|
||||
|
$projects = [ |
||||
|
3566 => [ |
||||
|
'systems' => [8967, 8968, 8969, 8970], |
||||
|
'sprints' => [], |
||||
|
], |
||||
|
3567 => [ |
||||
|
'systems' => [8971], |
||||
|
'sprints' => [526] |
||||
|
], |
||||
|
3568 => [ |
||||
|
'systems' => [8972, 8973], |
||||
|
'sprints' => [5480] |
||||
|
] |
||||
|
]; |
||||
|
|
||||
|
$workflows = [ |
||||
|
2277 => [5100, 5101], |
||||
|
2278 => [5102, 5103], |
||||
|
2279 => [5104, 5105, 5106, 5107] |
||||
|
]; |
||||
|
$take = $count > 50 ? 20 : 10; |
||||
|
$this->updateField(array_keys($projects), $count, $take, 'project_id'); |
||||
|
$this->updateField(array_keys($workflows), $count, $take, 'workflow_id'); |
||||
|
$this->fillSprintAndSystem($projects); |
||||
|
$this->fillStatues($workflows); |
||||
|
$this->fillTaskTag($count); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public function fillSprintAndSystem($projects) |
||||
|
{ |
||||
|
foreach ($projects as $key => $value) { |
||||
|
$count = Task::where('project_id', $key)->count(); |
||||
|
$take = $count > 10 ? 2 : 1; |
||||
|
|
||||
|
$sprintIndex = 0; |
||||
|
$systemIndex = 0; |
||||
|
$i = 0; |
||||
|
while ($count > $i) { |
||||
|
Task::where('project_id', '=', $key) |
||||
|
->where('system_id', 0)->where('sprint_id', 0) |
||||
|
->take($take) |
||||
|
->update( |
||||
|
[ |
||||
|
'sprint_id' => isset($value['sprints'][$sprintIndex]) ? $value['sprints'][$sprintIndex] : 0, |
||||
|
'system_id' => isset($value['systems'][$systemIndex]) ? $value['systems'][$systemIndex] : 0, |
||||
|
]); |
||||
|
|
||||
|
$systemIndex += 1; |
||||
|
$sprintIndex += 1; |
||||
|
if ($systemIndex >= count($value['systems'])) { |
||||
|
$systemIndex = 0; |
||||
} |
} |
||||
|
if ($sprintIndex >= count($value['sprints'])) { |
||||
|
$sprintIndex = 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
$i += $take; |
||||
} |
} |
||||
|
} |
||||
|
Task::whereIn('project_id', array_keys($projects)) |
||||
|
->where('system_id', 0) |
||||
|
->update(['system_id' => null]); |
||||
|
|
||||
$this->command->info(--$left . " Business is left."); |
|
||||
|
Task::whereIn('project_id', array_keys($projects)) |
||||
|
->where('sprint_id', 0) |
||||
|
->update(['sprint_id' => null]); |
||||
|
} |
||||
|
|
||||
|
public function updateField($values, $count, $take, $field) |
||||
|
{ |
||||
|
$i = 0; |
||||
|
$index = 0; |
||||
|
while ($count > $i) { |
||||
|
Task::where($field, '=', 0)->take($take)->update([$field => $values[$index]]); |
||||
|
$index += 1; |
||||
|
if (count($values) <= $index) { |
||||
|
$index = 0; |
||||
|
} |
||||
|
$i += $take; |
||||
} |
} |
||||
|
} |
||||
|
|
||||
|
public function fillStatues($workflows) |
||||
|
{ |
||||
|
foreach ($workflows as $key => $value) { |
||||
|
$count = Task::where('workflow_id', $key)->count(); |
||||
|
$take = $count > 10 ? 2 : 1; |
||||
|
|
||||
|
$index = 0; |
||||
|
$i = 0; |
||||
|
while ($count > $i) { |
||||
|
Task::where('workflow_id', '=', $key) |
||||
|
->where('status_id', 0) |
||||
|
->take($take) |
||||
|
->update( |
||||
|
[ |
||||
|
'status_id' => $value[$index] |
||||
|
]); |
||||
|
|
||||
dd('here'); |
|
||||
$chunks = array_chunk($tasks, 1000); |
|
||||
$number = count($chunks); |
|
||||
foreach ($chunks as $value) { |
|
||||
DB::table('tasks')->insert($value); |
|
||||
$this->command->info(--$number . " Chunk is left."); |
|
||||
|
$index += 1; |
||||
|
if ($index >= count($value)) { |
||||
|
$index = 0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
$i += $take; |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
public function fillTaskTag($count) |
||||
|
{ |
||||
|
$tags = [5037, 5038, 5626]; |
||||
|
$tasks = Task::where('business_id', 1779)->get(); |
||||
|
foreach ($tasks as $task) { |
||||
|
$rand = rand(0,2); |
||||
|
$t = []; |
||||
|
do{ |
||||
|
array_push($t, new TagTask(['task_id' => $task->id, 'tag_id' => $tags[$rand]])); |
||||
|
$rand--; |
||||
|
}while($rand >= 0); |
||||
|
$task->tags()->saveMany($t); |
||||
|
} |
||||
|
|
||||
|
} |
||||
} |
} |
@ -0,0 +1,36 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use App\Work; |
||||
|
use Illuminate\Database\Seeder; |
||||
|
|
||||
|
class WorkSeeder extends Seeder |
||||
|
{ |
||||
|
/** |
||||
|
* Run the database seeds. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function run() |
||||
|
{ |
||||
|
Work::insert( |
||||
|
factory(Work::class, 2000)->raw() |
||||
|
); |
||||
|
|
||||
|
$businessId = 1779; |
||||
|
Work::inRandomOrder()->take(150)->update(['business_id' => $businessId]); |
||||
|
|
||||
|
$projects = [3566, 3567, 3568]; |
||||
|
|
||||
|
foreach ($projects as $projectId) { |
||||
|
Work::where('business_id', $businessId) |
||||
|
->whereNotIn('project_id', $projects) |
||||
|
->take(50)->update(['project_id' => $projectId, 'started_at' => \Carbon\Carbon::now()]); |
||||
|
} |
||||
|
|
||||
|
$works = Work::where('business_id', $businessId)->get(); |
||||
|
foreach ($works as $work){ |
||||
|
\DB::table('works')->where('id', $work->id) |
||||
|
->update(['started_at' => \Carbon\Carbon::now()->subDays(rand(0,90))]); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue