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

[TM-1401] Import cleaned tree species associations. #601

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
1 change: 0 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: pull-request
on:
pull_request:
branches: [main, staging, release/**]
jobs:
lintTest:
runs-on: ubuntu-latest
Expand Down
80 changes: 80 additions & 0 deletions app/Console/Commands/ImportTreeSpeciesAssociations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Console\Commands;

use App\Console\Commands\Traits\Abortable;
use App\Models\V2\TreeSpecies\TreeSpecies;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;

class ImportTreeSpeciesAssociations extends Command
{
use Abortable;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import-tree-species-associations {file}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports a CSV that links UUIDs from v2_tree_species to taxon_ids from tree_species_research';

protected int $treeSpeciesUuidColumn;

protected int $taxonIdColumn;

/**
* Execute the console command.
*/
public function handle()
{
$this->executeAbortableScript(function () {
$process = new Process(['wc', '-l', $this->argument('file')]);
$process->run();
$this->assert($process->isSuccessful(), "WC failed {$process->getErrorOutput()}");

$lines = ((int)explode(' ', $process->getOutput())[0]) - 1;

$fileHandle = fopen($this->argument('file'), 'r');
$this->parseHeaders(fgetcsv($fileHandle));

$this->withProgressBar($lines, function ($progressBar) use ($fileHandle) {
while ($csvRow = fgetcsv($fileHandle)) {
$treeSpeciesUuid = $csvRow[$this->treeSpeciesUuidColumn];
$taxonId = $csvRow[$this->taxonIdColumn];

if ($taxonId != 'NA') {
TreeSpecies::isUuid($treeSpeciesUuid)->update(['taxon_id' => $taxonId]);
}
$progressBar->advance();
}

$progressBar->finish();
});

fclose($fileHandle);
});
}

protected function parseHeaders(array $headerRow): void
{
foreach ($headerRow as $index => $header) {
if ($header == 'tree_species_uuid') {
$this->treeSpeciesUuidColumn = $index;
} elseif ($header == 'taxon_id') {
$this->taxonIdColumn = $index;
}
}

$this->assert(
$this->treeSpeciesUuidColumn != null && $this->taxonIdColumn != null,
'Not all required columns were found'
);
}
}
9 changes: 6 additions & 3 deletions app/Models/V2/TreeSpecies/TreeSpecies.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class TreeSpecies extends Model implements EntityRelationModel
'speciesable_id',
'collection',
'hidden',

'old_id',
'old_model',
'taxon_id',
];

public const COLLECTION_DIRECT_SEEDING = 'direct-seeding';
Expand Down Expand Up @@ -82,6 +80,11 @@ public function speciesable()
return $this->morphTo();
}

public function taxonomicSpecies()
{
return $this->belongsTo(TreeSpeciesResearch::class, 'taxon_id');
}

public function getRouteKeyName()
{
return 'uuid';
Expand Down
Loading