Files
vkv/app/Jobs/UnpairedClassificationJob.php
Zdeněk Burda 41e3ce6f25 Initial commit
2026-01-09 21:26:40 +01:00

142 lines
5.1 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\EvaluationRun;
use App\Models\EvaluationRuleSet;
use App\Models\QsoResult;
use App\Models\WorkingQso;
use App\Enums\QsoErrorCode;
use App\Services\Evaluation\EvaluationCoordinator;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Throwable;
/**
* Job: UnpairedClassificationJob
*
* WHY:
* - Odděluje klasifikaci nenapárovaných QSO od samotného matchingu.
* ORDER:
* - Musí běžet po PASS 1 + PASS 2 matchingu, protože vychází z finální
* informace, že QSO opravdu nemá protistranu.
* - Krok je nevratný: následné kroky (duplicate/scoring) už jen vycházejí
* z výsledné error_code.
*
* Vstup:
* - QsoResult bez matched_log_qso_id
* - WorkingQso (pro identifikaci protistanice)
* - EvaluationRuleSet (unique_qso)
*
* Výstup:
* - error_code: NOT_IN_COUNTERPART_LOG / NO_COUNTERPART_LOG / UNIQUE
* - is_nil/is_valid + error_side
*
* Co job NEDĚLÁ:
* - neprovádí matching ani scoring,
* - neupravuje původní log_qsos.
*/
class UnpairedClassificationJob implements ShouldQueue
{
use Queueable;
public function __construct(
protected int $evaluationRunId
) {
}
public function handle(): void
{
$run = EvaluationRun::find($this->evaluationRunId);
if (! $run || $run->isCanceled()) {
return;
}
$coordinator = new EvaluationCoordinator();
try {
$ruleSet = EvaluationRuleSet::find($run->rule_set_id);
if (! $ruleSet) {
$coordinator->eventError($run, 'Unpaired klasifikace nelze spustit: chybí ruleset.', [
'step' => 'unpaired_classification',
]);
return;
}
$coordinator->eventInfo($run, 'Unpaired: krok spuštěn.', [
'step' => 'unpaired_classification',
'round_id' => $run->round_id,
]);
$coordinator->eventInfo($run, 'Klasifikace nenapárovaných QSO.', [
'step' => 'match',
'round_id' => $run->round_id,
'step_progress_done' => null,
'step_progress_total' => $run->progress_total,
]);
QsoResult::where('evaluation_run_id', $run->id)
->whereNull('matched_log_qso_id')
->chunkById(500, function ($results) use ($run, $ruleSet) {
foreach ($results as $result) {
$wqso = WorkingQso::where('evaluation_run_id', $run->id)
->where('log_qso_id', $result->log_qso_id)
->first();
if (! $wqso) {
continue;
}
$hasCounterpartLog = false;
if ($wqso->band_id && $wqso->rcall_norm) {
$hasCounterpartLog = WorkingQso::where('evaluation_run_id', $run->id)
->where('band_id', $wqso->band_id)
->where('call_norm', $wqso->rcall_norm)
->exists();
}
if ($hasCounterpartLog) {
$result->error_code = QsoErrorCode::NOT_IN_COUNTERPART_LOG;
$result->is_nil = true;
} else {
$isUnique = false;
// UNIQUE je globální v rámci runu (min. evaluation_run_id + band_id).
if ($ruleSet->uniqueQsoEnabled() && $wqso->rcall_norm) {
$uniqueQuery = WorkingQso::where('evaluation_run_id', $run->id)
->where('rcall_norm', $wqso->rcall_norm);
if ($wqso->band_id) {
$uniqueQuery->where('band_id', $wqso->band_id);
}
$count = $uniqueQuery->count();
$isUnique = $count === 1;
}
if ($isUnique) {
$result->error_code = QsoErrorCode::UNIQUE;
$result->is_nil = false;
} else {
$result->error_code = QsoErrorCode::NO_COUNTERPART_LOG;
$result->is_nil = true;
}
}
$result->error_side = 'NONE';
$result->is_valid = false;
$result->save();
}
});
EvaluationRun::where('id', $run->id)->increment('progress_done');
$coordinator->eventInfo($run, 'Unpaired: krok dokončen.', [
'step' => 'unpaired_classification',
'round_id' => $run->round_id,
]);
} catch (Throwable $e) {
$coordinator->eventError($run, 'Unpaired: krok selhal.', [
'step' => 'unpaired_classification',
'round_id' => $run->round_id,
'error' => $e->getMessage(),
]);
throw $e;
}
}
}