ENHANCEMENT: using features of symfony framework

This commit is contained in:
Jeroen De Meerleer 2022-05-13 15:24:05 +02:00
parent 73154c42ff
commit d041dd8ef1
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 47 additions and 31 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -16,19 +16,19 @@
</thead>
<tbody>
{% for job in jobs %}
<tr{% if job.running == true %} class="running text-success"{% elseif job.norun == true %} class="norun text-danger"{% endif %}>
<td class="d-none d-md-table-cell align-middle status-col text-center">{% if job.needschecking %}<i class="icon icon-warning text-warning big-icon"></i>{% endif %}</td>
<tr{% if job.data.running == true %} class="running text-success"{% elseif job.data.norun == true %} class="norun text-danger"{% endif %}>
<td class="d-none d-md-table-cell align-middle status-col text-center">{% if job.data.needschecking %}<i class="icon icon-warning text-warning big-icon"></i>{% endif %}</td>
<td class="d-block d-md-table-cell align-middle job-name">
<span class="d-inline d-md-none">{% if job.needschecking %}<i class="icon icon-warning text-warning"></i>{% endif %}</span>
<span class="d-inline d-md-none">{% if job.data.needschecking %}<i class="icon icon-warning text-warning"></i>{% endif %}</span>
{{ job.name | parsetags | raw }}</td>
<td class="d-block d-md-table-cell align-middle">{{ attribute(job, 'host-displayname') }}</td>
<td class="d-block d-md-table-cell align-middle">{{ attribute(job.data, 'host-displayname') }}</td>
<td class="d-block d-md-table-cell align-middle">{{ job.interval | interval }}</td>
<td class="d-block d-md-table-cell align-middle">{{ job.nextrun | date("d/m/Y H:i:s") }}</td>
<td class="text-md-end d-block d-md-table-cell align-middle">
<a href="#" data-href="{{ path('job_runnow', {'id': job.id}) }}" class="runnow btn btn-outline-{% if job.running == true %}success{% elseif job.norun == true %}danger{% else %}primary{% endif %}{% if job.running == true %} disabled{% endif %}"><i class="icon icon-run" aria-hidden="true"></i></a>
<a href="{{ path('job_view', {'id': job.id}) }}" class="btn btn-outline-{% if job.running == true %}success{% elseif job.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-view" aria-hidden="true"></i></a>
<a href="{{ path('job_edit', {'id': job.id}) }}" class="btn btn-outline-{% if job.running == true %}success{% elseif job.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-edit" aria-hidden="true"></i></a>
<a href="#" data-confirmation="Are you sure you want to delete this job?" data-href="{{ path('job_delete', {'id': job.id}) }}" class="delete-btn btn btn-outline-{% if job.running == true %}success{% elseif job.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-delete" aria-hidden="true"></i></a>
<a href="#" data-href="{{ path('job_runnow', {'id': job.id}) }}" class="runnow btn btn-outline-{% if job.data.running == true %}success{% elseif job.data.norun == true %}danger{% else %}primary{% endif %}{% if job.data.running == true %} disabled{% endif %}"><i class="icon icon-run" aria-hidden="true"></i></a>
<a href="{{ path('job_view', {'id': job.id}) }}" class="btn btn-outline-{% if job.data.running == true %}success{% elseif job.data.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-view" aria-hidden="true"></i></a>
<a href="{{ path('job_edit', {'id': job.id}) }}" class="btn btn-outline-{% if job.data.running == true %}success{% elseif job.data.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-edit" aria-hidden="true"></i></a>
<a href="#" data-confirmation="Are you sure you want to delete this job?" data-href="{{ path('job_delete', {'id': job.id}) }}" class="delete-btn btn btn-outline-{% if job.data.running == true %}success{% elseif job.data.norun == true %}danger{% else %}primary{% endif %}"><i class="icon icon-delete" aria-hidden="true"></i></a>
</td>
</tr>
{% endfor %}