summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/JobTable.php
blob: 81ba07b833f05ae013fbae8e669dae01947adc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace Icinga\Module\Director\Web\Table;

use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;

class JobTable extends ZfQueryBasedTable
{
    protected $searchColumns = [
        'job_name',
    ];

    protected function assemble()
    {
        $this->getAttributes()->add('class', 'jobs');
        parent::assemble();
    }

    public function renderRow($row)
    {
        $caption = [Link::create(
            $row->job_name,
            'director/job',
            ['id' => $row->id]
        )];

        if ($row->last_attempt_succeeded === 'n' && $row->last_error_message) {
            $caption[] = ' (' . $row->last_error_message . ')';
        }

        $tr = $this::row([$caption]);
        $tr->getAttributes()->add('class', $this->getJobClasses($row));

        return $tr;
    }

    protected function getJobClasses($row)
    {
        if ($row->unixts_last_attempt === null) {
            return 'pending';
        }

        if ($row->unixts_last_attempt + $row->run_interval < time()) {
            return 'pending';
        }

        if ($row->last_attempt_succeeded === 'y') {
            return 'ok';
        } elseif ($row->last_attempt_succeeded === 'n') {
            return 'critical';
        } else {
            return 'unknown';
        }
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('Job name'),
        ];
    }

    public function prepareQuery()
    {
        return $this->db()->select()->from(
            ['j' => 'director_job'],
            [
                'id'                     => 'j.id',
                'job_name'               => 'j.job_name',
                'job_class'              => 'j.job_class',
                'disabled'               => 'j.disabled',
                'run_interval'           => 'j.run_interval',
                'last_attempt_succeeded' => 'j.last_attempt_succeeded',
                'ts_last_attempt'        => 'j.ts_last_attempt',
                'unixts_last_attempt'    => 'UNIX_TIMESTAMP(j.ts_last_attempt)',
                'ts_last_error'          => 'j.ts_last_error',
                'last_error_message'     => 'j.last_error_message',
            ]
        )->order('job_name');
    }
}