Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6a12767
Feature commit
thomgiles Oct 14, 2024
25cc80a
Merge branch 'dev' of https://github.com/HDRUK/gateway-api-2 into dev
thomgiles Oct 14, 2024
9558c62
tiltfile
thomgiles Oct 14, 2024
7e22964
Whitespace
thomgiles Oct 14, 2024
5016b82
Moved to GetValueByPossibleKeys
thomgiles Oct 15, 2024
94218ad
Update to GetValueByPossibleKeys
thomgiles Oct 15, 2024
0e9306d
Merge branch 'dev' into Feature/GAT-5343-5/Edit-Dataset-Creates-Linkages
thomgiles Oct 15, 2024
f4ef531
Moved to a modular function approach
thomgiles Oct 15, 2024
e721218
removed dd() in test
thomgiles Oct 15, 2024
6a5571d
Added commenting
thomgiles Oct 15, 2024
bcfe82a
Tidy commenting
thomgiles Oct 15, 2024
3bec236
Moved trait call to MetadataOnboard.php
thomgiles Oct 15, 2024
3243de1
Reverted
thomgiles Oct 15, 2024
31662b6
removed modifcation to gdwm_v1p1
thomgiles Oct 15, 2024
3e4a65a
reverted headers
thomgiles Oct 15, 2024
c18dd0e
Defined $textMatches explicitly to keep phpstan happy
thomgiles Oct 15, 2024
d66d8bf
Removed Pluck
thomgiles Oct 16, 2024
112ad46
Moved Dataset Fetch to only get active datasets
thomgiles Oct 16, 2024
bc35094
further prevention of self loops (including test)
thomgiles Oct 16, 2024
f0d2c8e
Subsumed DatasetVersionHasSpatialCoverage
thomgiles Oct 16, 2024
650a1f9
Moved to Job with Queue
thomgiles Oct 16, 2024
3b12f1b
Fix for isset()
thomgiles Oct 16, 2024
1933953
Removed Logging
thomgiles Oct 16, 2024
c08f6e0
Returned Trait for ref
thomgiles Oct 16, 2024
fc5fc4f
Update to dataset logic to remove redundant code
thomgiles Oct 16, 2024
75e89b8
Fixed createSqlLinkageFromDataset() function
thomgiles Oct 16, 2024
5a10aaa
Removed caching
thomgiles Oct 16, 2024
8bb62c0
Fix
thomgiles Oct 16, 2024
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: 21 additions & 1 deletion app/Http/Controllers/Api/V1/DatasetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\User;
use App\Models\Dataset;
use App\Jobs\TermExtraction;
use App\Jobs\DatasetSqlLinkageJob;
use Illuminate\Http\Request;
use App\Models\DatasetVersion;
use App\Http\Traits\CheckAccess;
Expand All @@ -18,7 +19,6 @@
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;


use App\Http\Traits\MetadataOnboard;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Traits\AddMetadataVersion;
Expand All @@ -43,6 +43,7 @@ class DatasetController extends Controller
use MetadataOnboard;
use CheckAccess;


/**
* @OA\Get(
* path="/api/v1/datasets",
Expand Down Expand Up @@ -418,12 +419,21 @@ public function show(GetDataset $request, int $id): JsonResponse|BinaryFileRespo
// - For the FE i just need a tools linkage count so i'm gonna return `count(dataset->allTools)` for now
// - Same for collections
// - Leaving this as it is as im not 100% sure what any FE knock-on effect would be

// notes Tom 11th Oct 2024...
// - this is where we could be injecting these values into the "extra" array prior to validation
// - We can restrict this to IDs using the following logic
// - $toolIds = implode(', ', array_column($dataset->allTools, 'id'));
// - however, this could be built into the function
// - getRelationsViaDatasetVersion in the Dataset Model

$dataset->setAttribute('durs_count', $dataset->latestVersion()->durHasDatasetVersions()->count());
$dataset->setAttribute('publications_count', $dataset->latestVersion()->publicationHasDatasetVersions()->count());
$dataset->setAttribute('tools_count', count($dataset->allTools));
$dataset->setAttribute('collections_count', count($dataset->allCollections));
$dataset->setAttribute('spatialCoverage', $dataset->allSpatialCoverages ?? []);
$dataset->setAttribute('durs', $dataset->allDurs ?? []);
$dataset->setAttribute('tools', $dataset->allTools ?? []);
$dataset->setAttribute('publications', $dataset->allPublications ?? []);
$dataset->setAttribute('named_entities', $dataset->allNamedEntities ?? []);
$dataset->setAttribute('collections', $dataset->allCollections ?? []);
Comment on lines 430 to 439

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the attached tickets it is about creating a script.

Expand Down Expand Up @@ -763,6 +773,16 @@ public function update(UpdateDataset $request, int $id)
);

$versionNumber = $currDataset->lastMetadataVersionNumber()->version;

// Dispatch the dataset to update the SQL linkages tables
// DatasetVersionHasTool
// DatasetVersionHasDatasetVersion
// PublicationHasDatasetVersion
// DatasetVersionHasSpatialCoverage
// This uses the input metadata and new dataset version to create the indexes.
//$this->createSqlLinkageFromDataset($input['metadata'], $currDataset, false);
DatasetSqlLinkageJob::dispatch($input['metadata'], $currDataset, false);

// Dispatch term extraction to a subprocess if the dataset moves from draft to active
if($request['status'] === Dataset::STATUS_ACTIVE && Config::get('ted.enabled')) {

Expand Down
107 changes: 99 additions & 8 deletions app/Http/Traits/GetValueByPossibleKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,117 @@
trait GetValueByPossibleKeys

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these are part of a library not a trait.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://laravel-news.com/creating-helpers

{
/**
* Search for a value in an array by trying multiple possible keys in order.
* Search for a value in an array by trying multiple possible keys or paths in order.
*
* Note 15th Oct 2024 - Tom Giles - I have now enhanced this function for full searching:
* The function now supports both simple keys and dot notation paths. for example ['tools']
* or ['linkages.tools'] or even ['metadata.linkages.tools'] will now return the ['tools']
* object. Previously the full path in the array was requried. I have done this because
* widening the potential search parathenisis provides more flexible in terms maintaining
* functionality across data model changes.
*
* @param array $array The array to search.
* @param array $keys The list of possible keys to try, in order.
* @param array $keys The list of possible keys or paths to try, in order.
* @param mixed $default The default value to return if none of the keys are found.
* @return mixed The value of the first key found, or the default value if none are found.
*/
public function getValueByPossibleKeys(array $array, array $keys, $default = null)
{
foreach ($keys as $key) {
$value = Arr::get($array, $key, null);
if (!is_null($value)) {
return $value;
if (strpos($key, '.') !== false) {
// Treat key as a dot notation path
$value = $this->findByPath($array, $key);
if (!is_null($value)) {
return $value;
}
} else {
// Treat key as a simple key and search recursively
$value = $this->recursiveSearch($array, $key);
if (!is_null($value)) {
return $value;
}
}
}
// Removing, as it doesn't really serve a purpose dumping this to stdout.
// Suggest, if we care about it, then we audit it instead.

// Optionally log if no keys are found
// Log::info('No value found for any of the specified keys', [
// 'keys' => $keys,
// ]);

return $default;
}
}

/**
* Recursively search for a key in a multi-dimensional array.
*
* @param array $array The array to search.
* @param string $key The key to search for.
* @return mixed|null The value if found, or null otherwise.
*/
protected function recursiveSearch(array $array, string $key)
{
foreach ($array as $k => $v) {
if ($k === $key) {
return $v;
}

if (is_array($v)) {
$result = $this->recursiveSearch($v, $key);
if (!is_null($result)) {
return $result;
}
}
}
return null;
}

/**
* Find a value in a multi-dimensional array based on a dot notation path.
* This function searches through all possible branches to find the path.
*
* @param array $array The array to search.
* @param string $path The dot notation path to search for.
* @return mixed|null The value if found, or null otherwise.
*/
protected function findByPath(array $array, string $path)
{
$segments = explode('.', $path);

return $this->recursivePathSearch($array, $segments);
}

/**
* Helper function to perform recursive path search.
*
* @param array $array The current array segment.
* @param array $segments The remaining path segments to search.
* @return mixed|null The value if found, or null otherwise.
*/
protected function recursivePathSearch(array $array, array $segments)
{
$currentSegment = array_shift($segments);

if (array_key_exists($currentSegment, $array)) {
$value = $array[$currentSegment];
if (empty($segments)) {
return $value;
}

if (is_array($value)) {
return $this->recursivePathSearch($value, $segments);
}
}

// If the current segment is not found, search deeper in the array
foreach ($array as $key => $subArray) {
if (is_array($subArray)) {
$result = $this->recursivePathSearch($subArray, array_merge([$currentSegment], $segments));
if (!is_null($result)) {
return $result;
}
}
}

return null;
}
}
56 changes: 10 additions & 46 deletions app/Http/Traits/MetadataOnboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
use Exception;

use App\Models\Dataset;
use App\Models\DatasetVersion;
use App\Models\DatasetVersionHasSpatialCoverage;
use App\Models\SpatialCoverage;
use App\Jobs\DatasetSqlLinkageJob;
use App\Jobs\TermExtraction;

use Illuminate\Support\Str;

use MetadataManagementController as MMC;

trait MetadataOnboard
Expand Down Expand Up @@ -147,9 +144,15 @@ public function metadataOnboard(
'metadata' => json_encode($input['metadata']),
'version' => 1,
]);

// map coverage/spatial field to controlled list for filtering
$this->mapCoverage($input['metadata'], $version);

// Dispatch the dataset to update the SQL linkages tables
// DatasetVersionHasTool
// DatasetVersionHasDatasetVersion
// PublicationHasDatasetVersion
// DatasetVersionHasSpatialCoverage
// This uses the input metadata and new dataset version to create the indexes.
//$this->createSqlLinkageFromDataset($input['metadata'], $dataset, false);
DatasetSqlLinkageJob::dispatch($input['metadata'], $dataset, false);

// Dispatch term extraction to a subprocess if the dataset is marked as active
if($input['status'] === Dataset::STATUS_ACTIVE && Config::get('ted.enabled')) {
Expand Down Expand Up @@ -180,45 +183,6 @@ public function metadataOnboard(

}

private function mapCoverage(array $metadata, DatasetVersion $version): void
{
if (!isset($metadata['metadata']['coverage']['spatial'])) {
return;
}

$coverage = strtolower($metadata['metadata']['coverage']['spatial']);
$ukCoverages = SpatialCoverage::whereNot('region', 'Rest of the world')->get();
$worldId = SpatialCoverage::where('region', 'Rest of the world')->first()->id;

$matchFound = false;
foreach ($ukCoverages as $c) {
if (str_contains($coverage, strtolower($c['region']))) {

DatasetVersionHasSpatialCoverage::updateOrCreate([
'dataset_version_id' => (int)$version['id'],
'spatial_coverage_id' => (int)$c['id'],
]);
$matchFound = true;
}
}

if (!$matchFound) {
if (str_contains($coverage, 'united kingdom')) {
foreach ($ukCoverages as $c) {
DatasetVersionHasSpatialCoverage::updateOrCreate([
'dataset_version_id' => (int)$version['id'],
'spatial_coverage_id' => (int)$c['id'],
]);
}
} else {
DatasetVersionHasSpatialCoverage::updateOrCreate([
'dataset_version_id' => (int)$version['id'],
'spatial_coverage_id' => (int)$worldId,
]);
}
}
}

public function getVersion(int $version)
{
if($version > 999) {
Expand Down
Loading