You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.4 KiB

4 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. class StatisticController extends Controller
  7. {
  8. public $map = [
  9. 'test' => 0, // The sum of all tasks that are under review
  10. 'total' => 0, // Sum of all tasks
  11. 'overdue' => 0, // The sum of all overworked tasks
  12. 'spent_time' => 0, // The sum of all the minutes spent on tasks
  13. 'estimated_time' => 0, // The sum of all the minutes spent performing tasks is estimated
  14. 'over_spent_time' => 0, // The sum of all the minutes spent overworking tasks
  15. 'under_spent_time' => 0, // The sum of all the minutes left until estimated time
  16. ];
  17. public function index(Request $request, int $business, ?int $project = null)
  18. {
  19. $builder = DB::table('tasks')->where('business_id','=',$business);
  20. if ($project) {
  21. $builder->where('project_id', '=', $project);
  22. }
  23. $tasks = $builder->get();
  24. $tags = DB::table('tag_task')
  25. ->whereIn('task_id', $tasks->pluck('id')->toArray())
  26. ->get()
  27. ->groupBy('task_id')
  28. ->toArray();
  29. $result = [];
  30. foreach ($tasks as $task) {
  31. $this->addProjects($result, $task);
  32. $this->addSprints($result, $task);
  33. $this->addSystems($result, $task);
  34. $this->addTags($result, $task, $tags[$task->id] ?? []);
  35. }
  36. return $result;
  37. }
  38. public function addProjects(&$result, $task)
  39. {
  40. $key = "projects.{$task->project_id}";
  41. $this->subset($key, $result, $task);
  42. }
  43. public function addSprints(&$result, $task)
  44. {
  45. $key = "projects.{$task->project_id}.";
  46. $key .= 'sprints.';
  47. $key .= ($task->sprint_id ?? 0) . '.';
  48. $key .= ($task->assignee_id ?? 0) . '.';
  49. $key .= $task->workflow_id . '.';
  50. $key .= $task->status_id;
  51. $this->subset($key, $result, $task);
  52. }
  53. public function addSystems(&$result, $task)
  54. {
  55. $key = "projects.{$task->project_id}.";
  56. $key .= 'systems.';
  57. $key .= ($task->system_id ?? 0) . '.';
  58. $key .= ($task->assignee_id ?? 0) . '.';
  59. $key .= $task->workflow_id . '.';
  60. $key .= $task->status_id;
  61. $this->subset($key, $result, $task);
  62. }
  63. public function addTags(&$result, $task, $tags)
  64. {
  65. foreach ($tags as $tag) {
  66. $key = "projects.{$task->project_id}.";
  67. $key .= 'tags.';
  68. $key .= $tag->id . '.';
  69. $key .= ($task->assignee_id ?? 0) . '.';
  70. $key .= $task->workflow_id . '.';
  71. $key .= $task->status_id;
  72. $this->subset($key, $result, $task);
  73. }
  74. }
  75. public function subset($key , &$result, $task)
  76. {
  77. $node = Arr::get($result, $key, $this->map);
  78. $node['test'] += $task->ready_to_test;
  79. $node['total'] += 1;
  80. $node['overdue'] += !$task->on_time;
  81. $node['spent_time'] += $task->spent_time;
  82. $node['estimated_time'] += $task->estimated_time;
  83. $node['over_spent_time'] += $task->estimated_time < $task->spent_time ? $task->spent_time - $task->estimated_time : 0;
  84. $node['under_spent_time'] += $task->estimated_time > $task->spent_time ? $task->estimated_time - $task->spent_time : 0;
  85. Arr::set($result, $key, $node);
  86. }
  87. }