From d041dd8ef11b1fca0c214c10f4d40b42167e84cf Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 13 May 2022 15:24:05 +0200 Subject: [PATCH] ENHANCEMENT: using features of symfony framework --- src/Command/RunCommand.php | 2 +- src/Entity/Job.php | 14 +++++++--- src/Repository/JobRepository.php | 46 +++++++++++++++++++------------- templates/job/index.html.twig | 16 +++++------ 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index d273e71..4ac6b0b 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -37,7 +37,7 @@ class RunCommand extends Command $jobId = (int)$input->getArgument('jobid'); $jobRunning = $jobRepo->isLockedJob($jobId); if($jobRunning) { - $output->writeln('JobRepository is already running'); + $output->writeln('Job is already running'); return Command::FAILURE; } $jobRepo->setJobRunning($jobId, true); diff --git a/src/Entity/Job.php b/src/Entity/Job.php index 29f171c..93540b9 100644 --- a/src/Entity/Job.php +++ b/src/Entity/Job.php @@ -44,7 +44,7 @@ class Job * @var int */ #[ORM\Column(type: "integer", nullable: true)] - private int $lastrun; + private ?int $lastrun; /** * @var int @@ -106,6 +106,15 @@ class Job return $this; } + public function addData(string $name, mixed $value): Job + { + $data = json_decode($this->data, true); + $data[$name] = $value; + $this->data = json_encode($data); + + return $this; + } + /** * @return int */ @@ -145,7 +154,7 @@ class Job /** * @return int */ - public function getLastrun(): int + public function getLastrun(): ?int { return $this->lastrun; } @@ -178,5 +187,4 @@ class Job return $this; } - } \ No newline at end of file diff --git a/src/Repository/JobRepository.php b/src/Repository/JobRepository.php index 8d9965e..ae2972b 100644 --- a/src/Repository/JobRepository.php +++ b/src/Repository/JobRepository.php @@ -4,6 +4,7 @@ namespace App\Repository; +use App\Entity\Job; use App\Entity\Run; use App\Service\Secret; use DateTime; @@ -85,32 +86,39 @@ class JobRepository extends EntityRepository public function getAllJobs(bool $idiskey = false) { $runRepo = $this->getEntityManager()->getRepository(Run::class); - $jobsSql = "SELECT * FROM job"; - $jobsStmt = $this->getEntityManager()->getConnection()->prepare($jobsSql); - $jobsRslt = $jobsStmt->executeQuery(); - $jobs = $jobsRslt->fetchAllAssociative(); + + /** @var Job[] $jobs */ + $jobs = parent::findAll(); $returnbyid = []; + $names = []; + $hosts = []; + $services = []; foreach ($jobs as $key=>&$job) { - $job['data'] = json_decode($job['data'], true); - $job['host-displayname'] = $job['data']['host']; - $job['host'] = $job['data']['host']; - $job['service'] = $job['data']['service'] ?? ''; - $job['norun'] = isset($job['lastrun']) && $job['nextrun'] > $job['lastrun']; - $job['running'] = $job['running'] != 0; - $failed = count($runRepo->getRunsForJob($job['id'], true, $job['data']['fail-days'])); - $all = count($runRepo->getRunsForJob($job['id'], false, $job['data']['fail-days'])); - $job['needschecking'] = $all > 0 && (($failed / $all) * 100) > $job['data']['fail-pct']; - if(!empty($job['data']['containertype']) && $job['data']['containertype'] != 'none') { - $job['host-displayname'] = $job['data']['service'] . ' on ' . $job['data']['host']; + $jobData = $job->getData(); + $job->addData('host-displayname', $jobData['host']); + $job->addData('host', $jobData['host']); + $job->addData('service', $jobData['service'] ?? ''); + $job->addData('norun', $job->getLastrun() !== null && $job->getNextrun() > $job->getLastrun()); + $job->addData('running', $job->getRunning() != 0); + $failed = count($runRepo->getRunsForJob($job->getId(), true, $jobData['fail-days'])); + $all = count($runRepo->getRunsForJob($job->getId(), false, $jobData['fail-days'])); + $job->addData('needschecking', $all > 0 && (($failed / $all) * 100) > $jobData['fail-pct']); + if(!empty($jobData['containertype']) && $jobData['containertype'] != 'none') { + $job->addData('host-displayname', $jobData['service'] . ' on ' . $jobData['host']); } - if($idiskey) $returnbyid[$job['id']] = $job; + + $names[] = $job->getName(); + $hosts[] = $job->getData()['host'];; + $services[] = $job->getData()['service']; + + if($idiskey) $returnbyid[$job->getId()] = $job; } if($idiskey) return $returnbyid; array_multisort( - array_column($jobs, 'name'), SORT_ASC, - array_column($jobs, 'host'), SORT_ASC, - array_column($jobs, 'service'), SORT_ASC, + $names, SORT_ASC, + $hosts, SORT_ASC, + $services, SORT_ASC, $jobs); return $jobs; } diff --git a/templates/job/index.html.twig b/templates/job/index.html.twig index ce984df..362fd9a 100644 --- a/templates/job/index.html.twig +++ b/templates/job/index.html.twig @@ -16,19 +16,19 @@ {% for job in jobs %} - - {% if job.needschecking %}{% endif %} + + {% if job.data.needschecking %}{% endif %} - {% if job.needschecking %}{% endif %} + {% if job.data.needschecking %}{% endif %} {{ job.name | parsetags | raw }} - {{ attribute(job, 'host-displayname') }} + {{ attribute(job.data, 'host-displayname') }} {{ job.interval | interval }} {{ job.nextrun | date("d/m/Y H:i:s") }} - - - - + + + + {% endfor %}