Browse Source

Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad

pull/2/head
Mohammad Akbari 4 years ago
parent
commit
11b7d229fa
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 14
      app/Models/TagTask.php
  2. 8
      app/Statists/Pipe.php
  3. 44
      app/Statists/SprintStatist.php
  4. 22
      app/Statists/SystemStatist.php
  5. 54
      app/Statists/TagStatist.php
  6. 32
      database/factories/TaskFactory.php
  7. 23
      database/factories/WorkFactory.php
  8. 204
      database/seeds/TaskSeeder.php
  9. 36
      database/seeds/WorkSeeder.php
  10. 42
      routes/api.php

14
app/Models/TagTask.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'];
}

8
app/Statists/Pipe.php

@ -0,0 +1,8 @@
<?php
namespace App\Statists;
interface Pipe
{
public function handle(&$content, \Closure $next);
}

44
app/Statists/SprintStatist.php

@ -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);
}
}

22
app/Statists/SystemStatist.php

@ -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);
}
}

54
app/Statists/TagStatist.php

@ -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);
}
}

32
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'),
];
});

23
database/factories/WorkFactory.php

@ -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,
];
});

204
database/seeds/TaskSeeder.php

@ -1,62 +1,174 @@
<?php
use App\Business;
use App\User;
use Carbon\Carbon;
use App\TagTask;
use App\Task;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class TaskSeeder extends Seeder
{
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);
}
}
}

36
database/seeds/WorkSeeder.php

@ -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))]);
}
}
}

42
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');
});
});
});
});
});
});
Loading…
Cancel
Save