Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Measurement model through relationship with Project #1711

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -104,4 +104,12 @@ public function subprojects(): HasMany
{
return $this->hasMany(SubProject::class, 'projectid', 'id');
}

/**
* @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 @@ -273,16 +274,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 CDash\Model\BuildInformation;
use CDash\Model\BuildGroup;
Expand Down Expand Up @@ -641,14 +642,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 @@ -3074,9 +3084,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 @@ -3299,6 +3314,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 @@ -3606,6 +3631,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 @@ -3618,7 +3648,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 @@ -15785,6 +15815,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