summaryrefslogtreecommitdiffstats
path: root/application/clicommands/ListCommand.php
blob: 2486ae0f34afd05aa1bcbc136b9a802b8e0b14e2 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Reporting\Clicommands;

use Icinga\Module\Reporting\Cli\Command;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use InvalidArgumentException;
use ipl\Stdlib\Filter;

class ListCommand extends Command
{
    /**
     * List reports
     *
     * USAGE
     *
     *     icingacli reporting list [OPTIONS]
     *
     * OPTIONS
     *
     * --sort=<id|name|author>
     *     Sort the reports by the given column. Defaults to id.
     *
     * --direction=<asc|desc>
     *     Sort the reports by the specified sort column in ascending or descending order. Defaults to asc.
     *
     * --filter=<name>
     *     Filter the reports by the specified report name. Performs a wildcard search by default.
     *
     * EXAMPLES
     *
     * Sort the reports by name:
     *     icingacli reporting list --sort=name
     *
     * Sort the reports by author in descending order:
     *     icingacli reporting list --sort=author --direction=DESC
     *
     * Filter the reports that contain "Host" in the report name:
     *     icingacli reporting list --filter=Host
     *
     * Filter the reports that begin with "Service":
     *     icingacli reporting list --filter=Service*
     *
     * Filter the reports that end with "SLA":
     *     icingacli reporting list --filter=*SLA
     */
    public function indexAction()
    {
        /** @var string $sort */
        $sort = $this->params->get('sort', 'id');
        $sort = strtolower($sort);

        if ($sort !== 'id' && $sort !== 'name' && $sort !== 'author') {
            throw new InvalidArgumentException(sprintf('Sorting by %s is not supported', $sort));
        }

        $direction = $this->params->get('direction', 'ASC');

        $reports = Model\Report::on(Database::get());
        $reports
            ->with(['reportlets'])
            ->orderBy($sort, $direction);

        $filter = $this->params->get('filter');
        if ($filter !== null) {
            if (strpos($filter, '*') === false) {
                $filter = '*' . $filter . '*';
            }
            $reports->filter(Filter::like('name', $filter));
        }

        if ($reports->count() === 0) {
            print $this->translate("No reports found\n");
            exit;
        }

        $dataCallbacks = [
            'ID'     => function ($report) {
                return $report->id;
            },
            'Name'   => function ($report) {
                return $report->name;
            },
            'Author' => function ($report) {
                return $report->author;
            },
            'Type'   => function ($report) {
                return (new $report->reportlets->class())->getName();
            }
        ];

        $this->outputTable($reports, $dataCallbacks);
    }

    protected function outputTable($reports, array $dataCallbacks)
    {
        $columnsAndLengths = [];
        foreach ($dataCallbacks as $key => $_) {
            $columnsAndLengths[$key] = strlen($key);
        }

        $rows = [];
        foreach ($reports as $report) {
            $row = [];
            foreach ($dataCallbacks as $key => $callback) {
                $row[] = $callback($report);
                $columnsAndLengths[$key] = max($columnsAndLengths[$key], mb_strlen($callback($report)));
            }

            $rows[] = $row;
        }

        $format = '|';
        $beautifier = '|';
        foreach ($columnsAndLengths as $length) {
            $headerFormat = " %-" . sprintf('%ss |', $length);
            $format .= $headerFormat;
            $beautifier .= sprintf($headerFormat, str_repeat('-', $length));
        }
        $format .= "\n";
        $beautifier .= "\n";

        printf($format, ...array_keys($columnsAndLengths));
        print $beautifier;

        foreach ($rows as $row) {
            printf($format, ...$row);
        }
    }
}