summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/ImportrunTable.php
blob: e6c8a380c0a1cfb04a24f9fb4174f7630a6ee11e (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
83
84
85
86
87
88
89
90
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Objects\ImportSource;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;

class ImportrunTable extends ZfQueryBasedTable
{
    use DbHelper;

    /** @var ImportSource */
    protected $source;

    protected $searchColumns = [
        'source_name',
    ];

    public static function load(ImportSource $source)
    {
        $table = new static($source->getConnection());
        $table->source = $source;
        return $table;
    }

    public function getColumnsToBeRendered()
    {
        return [
            $this->translate('Source name'),
            $this->translate('Timestamp'),
            $this->translate('Imported rows'),
        ];
    }

    public function renderRow($row)
    {
        return $this::row([
            Link::create(
                $row->source_name,
                'director/importrun',
                ['id' => $row->id]
            ),
            $row->start_time,
            $row->cnt_rows
        ]);
    }

    public function prepareQuery()
    {
        $db = $this->db();
        $columns = array(
            'id'          => 'r.id',
            'source_id'   => 's.id',
            'source_name' => 's.source_name',
            'start_time'  => 'r.start_time',
            'rowset'      => 'LOWER(HEX(rs.checksum))',
            'cnt_rows'    => 'COUNT(rsr.row_checksum)',
        );

        if ($this->isPgsql()) {
            $columns['rowset'] = "LOWER(ENCODE(rs.checksum, 'hex'))";
        }

        // TODO: Store row count to rowset
        $query = $db->select()->from(
            ['s' => 'import_source'],
            $columns
        )->join(
            ['r' => 'import_run'],
            'r.source_id = s.id',
            []
        )->joinLeft(
            ['rs' => 'imported_rowset'],
            'rs.checksum = r.rowset_checksum',
            []
        )->joinLeft(
            ['rsr' => 'imported_rowset_row'],
            'rs.checksum = rsr.rowset_checksum',
            []
        )->group('r.id')->group('s.id')->group('rs.checksum')
         ->order('r.start_time DESC');

        if ($this->source) {
            $query->where('r.source_id = ?', $this->source->get('id'));
        }

        return $query;
    }
}