Skip to content

Commit 41db0f6

Browse files
Move legacy project relationship methods to project service (#3293)
Historically, models provided helper methods to access relationships for specific needs. Custom helpers like this are an antipattern in active record systems like Eloquent, so it doesn't make sense to simply move these helpers over to our Eloquent models. Instead, it's traditional for services to fill this role. As such, this PR moves the methods which only depend on a model's project ID to the `ProjectService` class. With much of the business logic in the legacy `Project` model out of the way, this should enable us to migrate away from the legacy model more quickly.
1 parent b4d1b48 commit 41db0f6

File tree

57 files changed

+609
-564
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+609
-564
lines changed

app/Http/Controllers/CTestConfigurationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Services\ProjectService;
56
use Illuminate\Http\Response;
67

78
final class CTestConfigurationController extends AbstractProjectController
@@ -11,7 +12,7 @@ public function get(int $id): Response
1112
$this->setProjectById($id);
1213

1314
$view = $this->view('project.ctest-configuration', '')
14-
->with('subprojects', $this->project->GetSubProjects());
15+
->with('subprojects', ProjectService::getSubProjects((int) $this->project->Id));
1516
return response($view, 200, ['Content-Type' => 'text/plain']);
1617
}
1718
}

app/Http/Controllers/CoverageController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Project as EloquentProject;
66
use App\Models\User;
7+
use App\Services\ProjectService;
78
use App\Utils\PageTimer;
89
use App\Utils\TestingDay;
910
use CDash\Database;
@@ -1365,8 +1366,8 @@ public function apiCompareCoverage(): JsonResponse
13651366

13661367
// Are there any subproject groups?
13671368
$subproject_groups = [];
1368-
if ($this->project->GetNumberOfSubProjects($end_UTCDate) > 0) {
1369-
$subproject_groups = $this->project->GetSubProjectGroups();
1369+
if (ProjectService::getNumberOfSubProjects((int) $this->project->Id, $end_UTCDate) > 0) {
1370+
$subproject_groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
13701371
}
13711372
foreach ($subproject_groups as $group) {
13721373
// Keep track of coverage info on a per-group basis.

app/Http/Controllers/ProjectOverviewController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Services\ProjectService;
56
use App\Utils\PageTimer;
67
use Illuminate\Http\JsonResponse;
78
use Illuminate\Support\Facades\DB;
@@ -25,7 +26,7 @@ public function apiOverview(): JsonResponse
2526
$pageTimer = new PageTimer();
2627

2728
// Check if this project has SubProjects.
28-
$has_subprojects = $this->project->GetNumberOfSubProjects() > 0;
29+
$has_subprojects = ProjectService::getNumberOfSubProjects((int) $this->project->Id) > 0;
2930

3031
// Handle optional date argument.
3132
$date = htmlspecialchars($_GET['date'] ?? date(FMT_DATE));
@@ -108,7 +109,7 @@ public function apiOverview(): JsonResponse
108109
$coverage_build_group_names = [];
109110
if ($has_subprojects) {
110111
// Detect if the subprojects are split up into groups.
111-
$groups = $this->project->GetSubProjectGroups();
112+
$groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
112113
if (count($groups) > 0) {
113114
$has_subproject_groups = true;
114115
foreach ($groups as $group) {

app/Http/Controllers/SubProjectController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\User;
6+
use App\Services\ProjectService;
67
use App\Utils\PageTimer;
78
use CDash\Model\SubProject;
89
use Illuminate\Http\JsonResponse;
@@ -78,7 +79,7 @@ public function apiManageSubProject(): JsonResponse
7879
$response['threshold'] = $this->project->GetCoverageThreshold();
7980

8081
$subprojects_response = []; // JSON for subprojects
81-
foreach ($this->project->GetSubProjects() as $subproject) {
82+
foreach (ProjectService::getSubProjects((int) $this->project->Id) as $subproject) {
8283
$subprojects_response[] = [
8384
'id' => $subproject->id,
8485
'name' => $subproject->name,
@@ -88,7 +89,7 @@ public function apiManageSubProject(): JsonResponse
8889
$response['subprojects'] = $subprojects_response;
8990

9091
$groups = [];
91-
foreach ($this->project->GetSubProjectGroups() as $subProjectGroup) {
92+
foreach (ProjectService::getSubProjectGroups((int) $this->project->Id) as $subProjectGroup) {
9293
$group = [
9394
'id' => $subProjectGroup->GetId(),
9495
'name' => $subProjectGroup->GetName(),
@@ -128,7 +129,7 @@ public function apiViewSubProjects(): JsonResponse
128129
$date = htmlspecialchars(pdo_real_escape_string($_GET['date']));
129130
$date_specified = true;
130131
} else {
131-
$last_start_timestamp = $this->project->GetLastSubmission();
132+
$last_start_timestamp = ProjectService::getLastStartTimestamp((int) $this->project->Id);
132133
$date = strlen($last_start_timestamp) > 0 ? $last_start_timestamp : null;
133134
$date_specified = false;
134135
}
@@ -192,7 +193,7 @@ public function apiViewSubProjects(): JsonResponse
192193
$project_response['ntestpass'] = $this->project->GetNumberOfPassingTests($beginning_UTCDate, $end_UTCDate);
193194
$project_response['ntestfail'] = $this->project->GetNumberOfFailingTests($beginning_UTCDate, $end_UTCDate);
194195
$project_response['ntestnotrun'] = $this->project->GetNumberOfNotRunTests($beginning_UTCDate, $end_UTCDate);
195-
$project_last_submission = $this->project->GetLastSubmission();
196+
$project_last_submission = ProjectService::getLastStartTimestamp((int) $this->project->Id);
196197
if (strlen($project_last_submission) === 0) {
197198
$project_response['starttime'] = 'NA';
198199
} else {
@@ -201,7 +202,7 @@ public function apiViewSubProjects(): JsonResponse
201202
$response['project'] = $project_response;
202203

203204
// Look for the subproject
204-
$subprojects = $this->project->GetSubProjects();
205+
$subprojects = ProjectService::getSubProjects((int) $this->project->Id);
205206
$subprojProp = [];
206207
foreach ($subprojects as $subproject) {
207208
$subprojProp[$subproject->id] = ['name' => $subproject->name];
@@ -286,10 +287,10 @@ public function apiDependenciesGraph(): JsonResponse
286287

287288
$date = isset($_GET['date']) ? Carbon::parse($_GET['date']) : null;
288289

289-
$subprojects = $this->project->GetSubProjects();
290+
$subprojects = ProjectService::getSubProjects((int) $this->project->Id);
290291

291292
$subproject_groups = [];
292-
$groups = $this->project->GetSubProjectGroups();
293+
$groups = ProjectService::getSubProjectGroups((int) $this->project->Id);
293294
foreach ($groups as $group) {
294295
$subproject_groups[$group->GetId()] = $group;
295296
}

app/Http/Controllers/SubmissionController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Jobs\ProcessSubmission;
88
use App\Models\PendingSubmissions;
99
use App\Models\Site;
10+
use App\Rules\ProjectNameRule;
1011
use App\Utils\AuthTokenUtil;
1112
use App\Utils\SubmissionUtils;
1213
use App\Utils\UnparsedSubmissionProcessor;
@@ -21,6 +22,7 @@
2122
use Illuminate\Support\Facades\DB;
2223
use Illuminate\Support\Facades\Log;
2324
use Illuminate\Support\Facades\Storage;
25+
use Illuminate\Support\Facades\Validator;
2426
use Illuminate\Support\Str;
2527
use League\Flysystem\UnableToMoveFile;
2628
use League\Flysystem\UnableToReadFile;
@@ -88,7 +90,13 @@ private function submitProcess(): Response
8890
$this->failProcessing(null, Response::HTTP_BAD_REQUEST, 'No project name provided.');
8991
}
9092

91-
if (!Project::validateProjectName($projectname)) {
93+
$validator = Validator::make([
94+
'name' => $projectname,
95+
], [
96+
'name' => new ProjectNameRule(),
97+
]);
98+
99+
if ($validator->fails()) {
92100
Log::info("Rejected submission with invalid project name: $projectname");
93101
$this->failProcessing(null, Response::HTTP_BAD_REQUEST, "Invalid project name: $projectname");
94102
}

app/Http/Controllers/UserController.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Http\JsonResponse;
1515
use Illuminate\Support\Carbon;
1616
use Illuminate\Support\Facades\Auth;
17+
use Illuminate\Support\Facades\DB;
1718
use Illuminate\Support\Facades\Mail;
1819
use Illuminate\Support\Str;
1920
use Illuminate\View\View;
@@ -76,7 +77,7 @@ public function userPageContent(): JsonResponse
7677
$project_response['name'] = $Project->Name;
7778
$project_response['name_encoded'] = urlencode($Project->Name);
7879
$project_response['nbuilds'] = $project_row->builds()->count();
79-
$project_response['average_builds'] = round($Project->GetBuildsDailyAverage(gmdate(FMT_DATETIME, time() - (3600 * 24 * 7)), gmdate(FMT_DATETIME)), 2);
80+
$project_response['average_builds'] = round(self::GetBuildsDailyAverage((int) $Project->Id, gmdate(FMT_DATETIME, time() - (3600 * 24 * 7)), gmdate(FMT_DATETIME)), 2);
8081
$project_response['success'] = $Project->GetNumberOfPassingBuilds($start, gmdate(FMT_DATETIME));
8182
$project_response['error'] = $Project->GetNumberOfErrorBuilds($start, gmdate(FMT_DATETIME));
8283
$project_response['warning'] = $Project->GetNumberOfWarningBuilds($start, gmdate(FMT_DATETIME));
@@ -206,6 +207,33 @@ public function userPageContent(): JsonResponse
206207
return response()->json(cast_data_for_JSON($response));
207208
}
208209

210+
/** Get the number of builds given per day */
211+
private static function GetBuildsDailyAverage(int $projectid, string $startUTCdate, string $endUTCdate): int
212+
{
213+
$project = DB::select('
214+
SELECT starttime
215+
FROM build
216+
WHERE
217+
projectid=?
218+
AND starttime>?
219+
AND starttime<=?
220+
AND parentid IN (-1, 0)
221+
ORDER BY starttime ASC
222+
LIMIT 1
223+
', [$projectid, $startUTCdate, $endUTCdate]);
224+
if ($project === []) {
225+
return 0;
226+
}
227+
$first_build = $project[0]->starttime;
228+
$nb_days = strtotime($endUTCdate) - strtotime($first_build);
229+
$nb_days = intval($nb_days / 86400) + 1;
230+
$nbuilds = \App\Models\Project::findOrFail($projectid)
231+
->builds()
232+
->betweenDates(Carbon::parse($startUTCdate), Carbon::parse($endUTCdate))
233+
->count();
234+
return $nbuilds / $nb_days;
235+
}
236+
209237
/** Report statistics about the last build */
210238
private function ReportLastBuild(string $type, int $projectid, int $siteid, string $projectname, $nightlytime, PDO $PDO): array
211239
{

app/Http/Submission/Handlers/BazelJSONHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
=========================================================================*/
1919

2020
use App\Models\Project as EloquentProject;
21+
use App\Services\ProjectService;
2122
use App\Utils\SubmissionUtils;
2223
use App\Utils\TestCreator;
2324
use CDash\Database;
@@ -67,7 +68,7 @@ public function __construct(Build $build)
6768
protected function HasSubProjects(): bool
6869
{
6970
if ($this->_HasSubProjects === null) {
70-
$this->_HasSubProjects = $this->GetProject()->GetNumberOfSubProjects() > 0;
71+
$this->_HasSubProjects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;
7172
}
7273
return $this->_HasSubProjects;
7374
}

app/Http/Submission/Handlers/CoverageHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use App\Http\Submission\Traits\UpdatesSiteInformation;
2121
use App\Models\Site;
2222
use App\Models\SiteInformation;
23+
use App\Services\ProjectService;
2324
use App\Utils\SubmissionUtils;
2425
use CDash\Model\Build;
2526
use CDash\Model\Coverage;
@@ -133,7 +134,7 @@ public function endElement($parser, $name): void
133134
$this->Build->UpdateBuild($this->Build->Id, -1, -1);
134135
}
135136

136-
$hasSubProjects = $this->GetProject()->GetNumberOfSubProjects() > 0;
137+
$hasSubProjects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;
137138

138139
foreach ($this->Coverages as $coverageInfo) {
139140
$coverage = $coverageInfo[0];

app/Http/Submission/Handlers/CoverageLogHandler.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
=========================================================================*/
1919

2020
use App\Models\Site;
21+
use App\Services\ProjectService;
2122
use App\Utils\SubmissionUtils;
2223
use CDash\Model\Build;
2324
use CDash\Model\CoverageFile;
@@ -95,9 +96,7 @@ public function endElement($parser, $name): void
9596
}
9697

9798
// Does this project have subprojects?
98-
$project = new Project();
99-
$project->Id = $this->GetProject()->Id;
100-
$has_subprojects = $project->GetNumberOfSubProjects() > 0;
99+
$has_subprojects = ProjectService::getNumberOfSubProjects((int) $this->GetProject()->Id) > 0;
101100

102101
// Record the coverage data that we parsed from this file.
103102
foreach ($this->CoverageFiles as $coverageInfo) {

app/Http/Submission/Handlers/ProjectHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
PURPOSE. See the above copyright notices for more information.
1818
=========================================================================*/
1919

20+
use App\Services\ProjectService;
2021
use CDash\Model\Label;
2122
use CDash\Model\Project;
2223
use CDash\Model\SubProject;
@@ -139,7 +140,7 @@ public function endElement($parser, $name): void
139140

140141
if (config('cdash.delete_old_subprojects')) {
141142
// Delete old subprojects that weren't included in this file.
142-
$previousSubProjectIds = $this->GetProject()->GetSubProjects()->pluck('id')->toArray();
143+
$previousSubProjectIds = ProjectService::getSubProjects((int) $this->GetProject()->Id)->pluck('id')->toArray();
143144
foreach ($previousSubProjectIds as $previousId) {
144145
$found = false;
145146
foreach ($this->SubProjects as $subproject) {

0 commit comments

Comments
 (0)