Skip to content

Commit

Permalink
Access Measurement model through relationship with Project (#1711)
Browse files Browse the repository at this point in the history
This PR adds Eloquent relationship information to the existing
`Measurement` model, and converts a number of raw database queries to
Eloquent queries.
  • Loading branch information
williamjallen committed Sep 18, 2023
1 parent e7eafd8 commit b8f5530
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 60 deletions.
22 changes: 12 additions & 10 deletions app/Http/Controllers/ManageMeasurementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\Measurement;
use App\Models\Project as EloquentProject;
use App\Services\PageTimer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Gate;
Expand Down Expand Up @@ -37,16 +38,17 @@ public function apiGet(): JsonResponse

// Get any measurements associated with this project's tests.
$measurements_response = [];
$measurements = Measurement::where('projectid', $this->project->Id)
$measurements = EloquentProject::findOrFail($this->project->Id)
->measurements()
->orderBy('position', 'asc')
->get();

foreach ($measurements as $measurement) {
$measurement_response = [];
$measurement_response['id'] = $measurement->id;
$measurement_response['name'] = $measurement->name;
$measurement_response['position'] = $measurement->position;
$measurements_response[] = $measurement_response;
$measurements_response[] = [
'id' => $measurement->id,
'name' => $measurement->name,
'position' => $measurement->position,
];
}
$response['measurements'] = $measurements_response;
$pageTimer->end($response);
Expand Down Expand Up @@ -103,10 +105,10 @@ public function apiDelete(): JsonResponse
abort(400, 'Invalid measurement ID provided.');
}

$deleted = Measurement::where([
'id' => (int) request()->input('id'),
'projectid' => $this->project->Id,
])->delete();
$deleted = EloquentProject::findOrFail($this->project->Id)
->measurements()
->where('id', (int) request()->input('id'))
->delete();

if ($deleted) {
return response()->json();
Expand Down
18 changes: 17 additions & 1 deletion app/Models/Measurement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $id
Expand All @@ -16,11 +17,26 @@
class Measurement extends Model
{
protected $table = 'measurement';

public $timestamps = false;

protected $fillable = [
'projectid',
'name',
'position',
];

public $timestamps = false;
protected $casts = [
'id' => 'integer',
'projectid' => 'integer',
'position' => 'integer',
];

/**
* @return BelongsTo<Project, self>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'id', 'projectid');
}
}
8 changes: 8 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ public function subprojects(?Carbon $date = null): HasMany
->orWhere('endtime', '=', Carbon::create(1980));
});
}

/**
* @return HasMany<Measurement>
*/
public function measurements(): HasMany
{
return $this->hasMany(Measurement::class, 'projectid', 'id');
}
}
26 changes: 10 additions & 16 deletions app/cdash/app/Controller/Api/QueryTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

namespace CDash\Controller\Api;

use App\Models\Measurement;
use App\Models\TestOutput;
use App\Models\Project as EloquentProject;

use CDash\Database;
use CDash\Model\Build;
Expand Down Expand Up @@ -272,28 +274,20 @@ public function getResponse()
$project_response['showtesttime'] = $this->project->ShowTestTime;
$response['project'] = $project_response;

// Get information about all the builds for the given project
// and date range.
$builds = [];

// Add the date/time
$builds['projectid'] = $this->project->Id;
$builds['currentstarttime'] = $this->currentStartTime;
$builds['teststarttime'] = $this->beginDate;
$builds['testendtime'] = $this->endDate;

// Get the list of extra test measurements that should be displayed on this page.
$this->extraMeasurements = [];
$stmt = $this->db->prepare(
'SELECT name FROM measurement WHERE projectid = ? ORDER BY position');
$this->db->execute($stmt, [$this->project->Id]);
while ($row = $stmt->fetch()) {
$measurements = EloquentProject::findOrFail($this->project->Id)
->measurements()
->orderBy('position')
->get();
/** @var Measurement $measurement */
foreach ($measurements as $measurement) {
// If we have the Processors measurement, then we should also
// compute and display 'Proc Time'.
if ($row['name'] == 'Processors') {
if ($measurement->name === 'Processors') {
$this->hasProcessors = true;
} else {
$this->extraMeasurements[] = $row['name'];
$this->extraMeasurements[] = $measurement->name;
}
}
$this->numExtraMeasurements = count($this->extraMeasurements);
Expand Down
13 changes: 6 additions & 7 deletions app/cdash/app/Controller/Api/TestDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use App\Models\BuildTest;
use App\Models\TestOutput;
use App\Models\Project as EloquentProject;

use CDash\Database;

Expand Down Expand Up @@ -286,13 +287,11 @@ public function getResponse()
}

// Get the list of extra test measurements that have been explicitly added to this project.
$stmt = $this->db->prepare(
'SELECT name FROM measurement WHERE projectid = ? ORDER BY position');
$this->db->execute($stmt, [$this->project->Id]);
$extra_measurements = [];
while ($row = $stmt->fetch()) {
$extra_measurements[] = $row['name'];
}
$extra_measurements = EloquentProject::findOrFail($this->project->Id)
->measurements()
->orderBy('position')
->pluck('name')
->toArray();

// Sort measurements: put those listed explicitly first (sorted by position)
// then sort the rest alphabetically by name.
Expand Down
16 changes: 8 additions & 8 deletions app/cdash/app/Controller/Api/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace CDash\Controller\Api;

use App\Models\BuildTest;
use App\Models\Project as EloquentProject;

use CDash\Config;
use CDash\Database;
Expand Down Expand Up @@ -272,16 +273,15 @@ public function getResponse()
// Get the list of extra measurements that should be displayed on this page.
$response['hasprocessors'] = false;
$processors_idx = -1;
$extra_measurements_stmt = $this->db->prepare(
'SELECT name FROM measurement
WHERE projectid = ?
ORDER BY position');
$this->db->execute($extra_measurements_stmt, [$this->project->Id]);
while ($row = $extra_measurements_stmt->fetch()) {
$this->extraMeasurements[] = $row['name'];
$extra_measurements = EloquentProject::findOrFail($this->project->Id)
->measurements()
->orderBy('position')
->get();
foreach ($extra_measurements as $extra_measurement) {
$this->extraMeasurements[] = $extra_measurement->name;
// If we have the Processors measurement, then we should also
// compute and display 'Proc Time'.
if ($row['name'] == 'Processors') {
if ($extra_measurement->name === 'Processors') {
$processors_idx = count($this->extraMeasurements) - 1;
$response['hasprocessors'] = true;
}
Expand Down
13 changes: 5 additions & 8 deletions app/cdash/public/api/v1/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use CDash\Controller\Api\Index as IndexController;
use CDash\Database;
use App\Models\Banner;
use App\Models\Project as EloquentProject;
use CDash\Model\Build;
use App\Models\BuildInformation;
use CDash\Model\BuildGroup;
Expand Down Expand Up @@ -639,14 +640,10 @@
// We support an additional advanced column called 'Proc Time'.
// This is only shown if this project is setup to display
// an extra test measurement called 'Processors'.
$stmt = DB::select("
SELECT count(*) AS c
FROM measurement
WHERE
projectid = ?
AND name = 'Processors'
", [$Project->Id])[0];
$response['showProcTime'] = $stmt->c > 0;
$response['showProcTime'] = EloquentProject::findOrFail($Project->Id)
->measurements()
->where('name', 'Processors')
->exists();

$response['buildgroups'] = $controller->buildgroupsResponse;
$response['updatetype'] = $controller->updateType;
Expand Down
11 changes: 4 additions & 7 deletions app/cdash/tests/test_managemeasurements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// After including cdash_test_case.php, subsequent require_once calls are
// relative to the top of the CDash source tree
//
require_once dirname(__FILE__) . '/cdash_test_case.php';



require_once dirname(__FILE__) . '/cdash_test_case.php';

use App\Models\Measurement;

class ManageMeasurementsTestCase extends KWWebTestCase
{
Expand Down Expand Up @@ -37,10 +36,8 @@ public function __destruct()
if (!is_null($this->SubProjectBuildId)) {
remove_build($this->SubProjectBuildId);
}
foreach ($this->MeasurementIds as $measurement_id) {
$this->PDO->query(
"DELETE FROM measurement WHERE id = $measurement_id");
}

Measurement::destroy($this->MeasurementIds);
}

// function to validate test results returned by the API.
Expand Down
46 changes: 43 additions & 3 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,17 @@ parameters:
path: app/Http/Controllers/ManageBannerController.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<App\\\\Models\\\\Measurement\\>\\:\\:orderBy\\(\\)\\.$#"
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:delete\\(\\)\\.$#"
count: 1
path: app/Http/Controllers/ManageMeasurementsController.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:orderBy\\(\\)\\.$#"
count: 1
path: app/Http/Controllers/ManageMeasurementsController.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:where\\(\\)\\.$#"
count: 1
path: app/Http/Controllers/ManageMeasurementsController.php

Expand Down Expand Up @@ -3094,9 +3104,14 @@ parameters:
count: 1
path: app/cdash/app/Controller/Api/QueryTests.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:orderBy\\(\\)\\.$#"
count: 1
path: app/cdash/app/Controller/Api/QueryTests.php

-
message: "#^Loose comparison via \"\\=\\=\" is not allowed\\.$#"
count: 10
count: 9
path: app/cdash/app/Controller/Api/QueryTests.php

-
Expand Down Expand Up @@ -3319,6 +3334,16 @@ parameters:
count: 2
path: app/cdash/app/Controller/Api/TestDetails.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:orderBy\\(\\)\\.$#"
count: 1
path: app/cdash/app/Controller/Api/TestDetails.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:pluck\\(\\)\\.$#"
count: 1
path: app/cdash/app/Controller/Api/TestDetails.php

-
message: "#^Loose comparison via \"\\!\\=\" is not allowed\\.$#"
count: 1
Expand Down Expand Up @@ -3626,6 +3651,11 @@ parameters:
count: 5
path: app/cdash/app/Controller/Api/ViewTest.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:orderBy\\(\\)\\.$#"
count: 1
path: app/cdash/app/Controller/Api/ViewTest.php

-
message: "#^Foreach overwrites \\$test with its value variable\\.$#"
count: 1
Expand All @@ -3638,7 +3668,7 @@ parameters:

-
message: "#^Loose comparison via \"\\=\\=\" is not allowed\\.$#"
count: 11
count: 10
path: app/cdash/app/Controller/Api/ViewTest.php

-
Expand Down Expand Up @@ -15574,6 +15604,16 @@ parameters:
count: 3
path: app/cdash/public/api/v1/index.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:exists\\(\\)\\.$#"
count: 1
path: app/cdash/public/api/v1/index.php

-
message: "#^Dynamic call to static method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\HasMany\\<App\\\\Models\\\\Measurement\\>\\:\\:where\\(\\)\\.$#"
count: 1
path: app/cdash/public/api/v1/index.php

-
message: """
#^Call to deprecated function pdo_error\\(\\)\\:
Expand Down

0 comments on commit b8f5530

Please sign in to comment.