diff options
Diffstat (limited to 'library/Reporting/Report.php')
-rw-r--r-- | library/Reporting/Report.php | 249 |
1 files changed, 91 insertions, 158 deletions
diff --git a/library/Reporting/Report.php b/library/Reporting/Report.php index 7f2eee3..ac5c9b3 100644 --- a/library/Reporting/Report.php +++ b/library/Reporting/Report.php @@ -1,19 +1,22 @@ <?php + // Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2 namespace Icinga\Module\Reporting; use DateTime; use Exception; +use Icinga\Module\Icingadb\ProvidedHook\Reporting\ServiceSlaReport; +use Icinga\Module\Icingadb\ProvidedHook\Reporting\SlaReport; use Icinga\Module\Pdfexport\PrintableHtmlDocument; +use Icinga\Module\Reporting\Model; use Icinga\Module\Reporting\Web\Widget\Template; use ipl\Html\HtmlDocument; -use ipl\Sql; + +use function ipl\I18n\t; class Report { - use Database; - /** @var int */ protected $id; @@ -36,88 +39,43 @@ class Report protected $template; /** - * @param int $id + * Create report from the given model * - * @return static + * @param Model\Report $reportModel * - * @throws Exception + * @return static + * @throws Exception If no reportlets are configured */ - public static function fromDb($id) + public static function fromModel(Model\Report $reportModel): self { $report = new static(); - $db = $report->getDb(); + $report->id = $reportModel->id; + $report->name = $reportModel->name; + $report->author = $reportModel->author; + $report->timeframe = Timeframe::fromModel($reportModel->timeframe); - $select = (new Sql\Select()) - ->from('report') - ->columns('*') - ->where(['id = ?' => $id]); - - $row = $db->select($select)->fetch(); - - if ($row === false) { - throw new Exception('Report not found'); + $template = $reportModel->template->first(); + if ($template !== null) { + $report->template = Template::fromModel($template); } - $report - ->setId($row->id) - ->setName($row->name) - ->setAuthor($row->author) - ->setTimeframe(Timeframe::fromDb($row->timeframe_id)) - ->setTemplate(Template::fromDb($row->template_id)); - - $select = (new Sql\Select()) - ->from('reportlet') - ->columns('*') - ->where(['report_id = ?' => $id]); - - $row = $db->select($select)->fetch(); - - if ($row === false) { - throw new Exception('No reportlets configured.'); + $reportlets = []; + foreach ($reportModel->reportlets as $reportlet) { + $reportlet->report_name = $reportModel->name; + $reportlet->report_id = $reportModel->id; + $reportlets[] = Reportlet::fromModel($reportlet); } - $reportlet = new Reportlet(); - - $reportlet - ->setId($row->id) - ->setClass($row->class); - - $select = (new Sql\Select()) - ->from('config') - ->columns('*') - ->where(['reportlet_id = ?' => $row->id]); - - $rows = $db->select($select)->fetchAll(); - - $config = []; - - foreach ($rows as $row) { - $config[$row->name] = $row->value; + if (empty($reportlets)) { + throw new Exception('No reportlets configured'); } - $reportlet->setConfig($config); - - $report->setReportlets([$reportlet]); - - $select = (new Sql\Select()) - ->from('schedule') - ->columns('*') - ->where(['report_id = ?' => $id]); - - $row = $db->select($select)->fetch(); + $report->reportlets = $reportlets; - if ($row !== false) { - $schedule = new Schedule(); - - $schedule - ->setId($row->id) - ->setStart((new \DateTime())->setTimestamp((int) $row->start / 1000)) - ->setFrequency($row->frequency) - ->setAction($row->action) - ->setConfig(json_decode($row->config, true)); - - $report->setSchedule($schedule); + $schedule = $reportModel->schedule->first(); + if ($schedule !== null) { + $report->schedule = Schedule::fromModel($schedule, $report); } return $report; @@ -132,18 +90,6 @@ class Report } /** - * @param int $id - * - * @return $this - */ - public function setId($id) - { - $this->id = $id; - - return $this; - } - - /** * @return string */ public function getName() @@ -152,18 +98,6 @@ class Report } /** - * @param string $name - * - * @return $this - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** * @return string */ public function getAuthor() @@ -172,18 +106,6 @@ class Report } /** - * @param string $author - * - * @return $this - */ - public function setAuthor($author) - { - $this->author = $author; - - return $this; - } - - /** * @return Timeframe */ public function getTimeframe() @@ -192,18 +114,6 @@ class Report } /** - * @param Timeframe $timeframe - * - * @return $this - */ - public function setTimeframe(Timeframe $timeframe) - { - $this->timeframe = $timeframe; - - return $this; - } - - /** * @return Reportlet[] */ public function getReportlets() @@ -212,18 +122,6 @@ class Report } /** - * @param Reportlet[] $reportlets - * - * @return $this - */ - public function setReportlets(array $reportlets) - { - $this->reportlets = $reportlets; - - return $this; - } - - /** * @return Schedule */ public function getSchedule() @@ -232,18 +130,6 @@ class Report } /** - * @param Schedule $schedule - * - * @return $this - */ - public function setSchedule(Schedule $schedule) - { - $this->schedule = $schedule; - - return $this; - } - - /** * @return Template */ public function getTemplate() @@ -251,18 +137,6 @@ class Report return $this->template; } - /** - * @param Template $template - * - * @return $this - */ - public function setTemplate($template) - { - $this->template = $template; - - return $this; - } - public function providesData() { foreach ($this->getReportlets() as $reportlet) { @@ -300,6 +174,7 @@ class Report public function toCsv() { $timerange = $this->getTimeframe()->getTimerange(); + $convertFloats = version_compare(PHP_VERSION, '8.0.0', '<'); $csv = []; @@ -309,8 +184,41 @@ class Report if ($implementation->providesData()) { $data = $implementation->getData($timerange, $reportlet->getConfig()); $csv[] = array_merge($data->getDimensions(), $data->getValues()); + + $hosts = []; + $isServiceExport = false; + $config = $reportlet->getConfig(); + $exportTotalEnabled = isset($config['export_total']) && $config['export_total']; + if ($exportTotalEnabled) { + $isServiceExport = $reportlet->getClass() === ServiceSlaReport::class; + } + foreach ($data->getRows() as $row) { - $csv[] = array_merge($row->getDimensions(), $row->getValues()); + $values = $row->getValues(); + if ($convertFloats) { + foreach ($values as &$value) { + if (is_float($value)) { + $value = sprintf('%.4F', $value); + } + } + } + + if ($isServiceExport) { + $hosts[$row->getDimensions()[0]] = true; + } + + $csv[] = array_merge($row->getDimensions(), $values); + } + + if ($exportTotalEnabled) { + $precision = $config['sla_precision'] ?? SlaReport::DEFAULT_REPORT_PRECISION; + $total = [$isServiceExport ? count($hosts) : $data->count()]; + if ($isServiceExport) { + $total[] = $data->count(); + } + $total[] = round($data->getAverages()[0], $precision); + + $csv[] = $total; } break; @@ -336,9 +244,34 @@ class Report $data = $implementation->getData($timerange, $reportlet->getConfig()); $dimensions = $data->getDimensions(); $values = $data->getValues(); + + $hosts = []; + $isServiceExport = false; + $config = $reportlet->getConfig(); + $exportTotalEnabled = isset($config['export_total']) && $config['export_total']; + if ($exportTotalEnabled) { + $isServiceExport = $reportlet->getClass() === ServiceSlaReport::class; + } + foreach ($data->getRows() as $row) { - $json[] = \array_combine($dimensions, $row->getDimensions()) - + \array_combine($values, $row->getValues()); + $json[] = array_combine($dimensions, $row->getDimensions()) + + array_combine($values, $row->getValues()); + + if ($isServiceExport) { + $hosts[$row->getDimensions()[0]] = true; + } + } + + if ($exportTotalEnabled) { + $total = [t('Total Hosts') => $isServiceExport ? count($hosts) : $data->count()]; + if ($isServiceExport) { + $total[t('Total Services')] = $data->count(); + } + + $precision = $config['sla_precision'] ?? SlaReport::DEFAULT_REPORT_PRECISION; + $total[t('Total SLA Averages')] = round($data->getAverages()[0], $precision); + + $json[] = $total; } break; |