From 3adaad5066512e64f61a49811facd9ff95d80735 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Tue, 2 Mar 2021 14:44:56 +0330 Subject: [PATCH] statistic directory, TagTask model, change factory, seeder, api --- app/Models/TagTask.php | 14 ++ app/Statists/Pipe.php | 8 ++ app/Statists/SprintStatist.php | 44 +++++++ app/Statists/SystemStatist.php | 22 ++++ app/Statists/TagStatist.php | 54 ++++++++ database/factories/TaskFactory.php | 32 +++-- database/factories/WorkFactory.php | 23 ++++ database/seeds/TaskSeeder.php | 204 ++++++++++++++++++++++------- database/seeds/WorkSeeder.php | 36 +++++ routes/api.php | 42 ++++++ 10 files changed, 422 insertions(+), 57 deletions(-) create mode 100644 app/Models/TagTask.php create mode 100644 app/Statists/Pipe.php create mode 100644 app/Statists/SprintStatist.php create mode 100644 app/Statists/SystemStatist.php create mode 100644 app/Statists/TagStatist.php create mode 100644 database/factories/WorkFactory.php mode change 100644 => 100755 database/seeds/TaskSeeder.php create mode 100755 database/seeds/WorkSeeder.php diff --git a/app/Models/TagTask.php b/app/Models/TagTask.php new file mode 100644 index 0000000..197f403 --- /dev/null +++ b/app/Models/TagTask.php @@ -0,0 +1,14 @@ +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); + } +} diff --git a/app/Statists/SystemStatist.php b/app/Statists/SystemStatist.php new file mode 100644 index 0000000..c268aa3 --- /dev/null +++ b/app/Statists/SystemStatist.php @@ -0,0 +1,22 @@ +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); + } +} diff --git a/app/Statists/TagStatist.php b/app/Statists/TagStatist.php new file mode 100644 index 0000000..33bd15f --- /dev/null +++ b/app/Statists/TagStatist.php @@ -0,0 +1,54 @@ +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); + } +} diff --git a/database/factories/TaskFactory.php b/database/factories/TaskFactory.php index 448b89e..713b2cf 100644 --- a/database/factories/TaskFactory.php +++ b/database/factories/TaskFactory.php @@ -3,20 +3,30 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\Task; -use Carbon\Carbon; use Faker\Generator as Faker; $factory->define(Task::class, function (Faker $faker) { return [ - 'business_id' => null, - 'creator_id' => null, - 'project_id' => null, - 'user_id' => null, - 'workflow_id' => null, - 'name' => $faker->sentences(3, true), - 'time' => null, - 'cost' => 0, - 'completed' => false, - 'due_date' => Carbon::now()->toDateTimeString(), + 'business_id' => $faker->numberBetween(1, 200), + 'creator_id' => $faker->numberBetween(1, 5000), + 'project_id' => $faker->numberBetween(1, 200), + 'assignee_id' => $faker->numberBetween(1, 20), + 'workflow_id' => $faker->numberBetween(1, 2500), + 'system_id' => $faker->numberBetween(1, 20), + 'sprint_id' => $faker->numberBetween(1, 20), + 'status_id' => $faker->numberBetween(1, 5000), + 'approver_id' => $faker->numberBetween(1, 5000), + + 'title' => $faker->name, + 'description' => $faker->sentences(3, true), + 'priority' => $faker->numberBetween(1,10), + 'on_time' => $faker->boolean, + 'ready_to_test' => $faker->boolean, + 'spent_time' => $faker->numberBetween(100, 300), + 'estimated_time' => $faker->numberBetween(100, 300), + 'due_date' => $faker->date(), + 'completed_at' => $faker->date(), + 'work_start' => $faker->date('1971-01-01','now'), + 'work_finish' => $faker->date('1971-01-01','now'), ]; }); diff --git a/database/factories/WorkFactory.php b/database/factories/WorkFactory.php new file mode 100644 index 0000000..d4e9e2c --- /dev/null +++ b/database/factories/WorkFactory.php @@ -0,0 +1,23 @@ +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, + ]; +}); diff --git a/database/seeds/TaskSeeder.php b/database/seeds/TaskSeeder.php old mode 100644 new mode 100755 index 9b148bd..f138ee0 --- a/database/seeds/TaskSeeder.php +++ b/database/seeds/TaskSeeder.php @@ -1,62 +1,174 @@ 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); + } + + } } diff --git a/database/seeds/WorkSeeder.php b/database/seeds/WorkSeeder.php new file mode 100755 index 0000000..d4071de --- /dev/null +++ b/database/seeds/WorkSeeder.php @@ -0,0 +1,36 @@ +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))]); + } + } +} diff --git a/routes/api.php b/routes/api.php index 618384b..c92d4b3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -163,3 +163,45 @@ $router->group(['prefix' => 'users'], function () use ($router) { $router->delete('/avatar', 'UserController@unSetAvatar'); }); }); +$router->group([], function () use ($router) { + $router->post('/log', 'ActivityController@store'); + $router->group(['prefix' => 'businesses/{business}', 'middleware' => ['auth', 'bindBusiness']], function (Router $router) { + $router->get('/tasks', 'TaskController@index'); + $router->get('/works', 'WorkController@index'); + $router->get('statistics', 'StatisticController@index'); + $router->get('projects/{project}/statistics', 'StatisticController@index'); + $router->get('/tasks/{task}', 'TaskController@show'); + $router->group(['prefix' => 'projects/{project}/tasks'], function (Router $router) { + $router->post('/', 'TaskController@store'); + + $router->group(['prefix' => '{task}'], function (Router $router) { + $router->get('/', 'TaskController@show'); + $router->put('/', 'TaskController@update'); + $router->delete('/', 'TaskController@destroy'); + + $router->post('/watchers', 'TaskController@toggleWatcher'); + + $router->group(['prefix' => 'works'], function (Router $router) { +// $router->get('/', 'WorkController@index'); + $router->post('/', 'WorkController@store'); + $router->group(['prefix' => '{work}'], function (Router $router) { + $router->get('/', 'WorkController@show'); + $router->put('/', 'WorkController@update'); + $router->delete('/', 'WorkController@destroy'); + }); + }); + + $router->group(['prefix' => 'comments'], function (Router $router) { + $router->get('/', 'CommentController@index'); + $router->post('/', 'CommentController@store'); + $router->group(['prefix' => '{comment}'], function (Router $router) { + $router->get('/', 'CommentController@show'); + $router->put('/', 'CommentController@update'); + $router->delete('/', 'CommentController@destroy'); + }); + }); + + }); + }); + }); +});