summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/SyncRunDetails.php
blob: 408e8f6b2e5fa82526d2104a68d47f9adf6bd37b (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
<?php

namespace Icinga\Module\Director\Web\Widget;

use Icinga\Module\Director\Objects\DirectorActivityLog;
use ipl\Html\HtmlDocument;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\SyncRun;
use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Widget\NameValueTable;
use function sprintf;

class SyncRunDetails extends NameValueTable
{
    use TranslationHelper;

    const URL_ACTIVITIES = 'director/config/activities';

    /** @var SyncRun */
    protected $run;

    public function __construct(SyncRun $run)
    {
        $this->run = $run;
        $this->getAttributes()->add('data-base-target', '_next'); // eigentlich nur runSummary
        $this->addNameValuePairs([
            $this->translate('Start time') => $run->get('start_time'),
            $this->translate('Duration')   => sprintf('%.2fs', $run->get('duration_ms') / 1000),
            $this->translate('Activity')   => $this->runSummary($run)
        ]);
    }

    /**
     * @param SyncRun $run
     * @return array
     */
    protected function runSummary(SyncRun $run)
    {
        $html = [];
        $total = $run->countActivities();
        if ($total === 0) {
            $html[] = $this->translate('No changes have been made');
        } else {
            if ($total === 1) {
                $html[] = $this->translate('One object has been modified');
            } else {
                $html[] = sprintf(
                    $this->translate('%s objects have been modified'),
                    $total
                );
            }

            /** @var Db $db */
            $db = $run->getConnection();
            $formerId = $db->fetchActivityLogIdByChecksum($run->get('last_former_activity'));
            if ($formerId === null) {
                return $html;
            }
            $lastId = $db->fetchActivityLogIdByChecksum($run->get('last_related_activity'));

            if ($formerId !== $lastId) {
                $idRangeEx = sprintf(
                    'id>%d&id<=%d',
                    $formerId,
                    $lastId
                );
            } else {
                $idRangeEx = null;
            }

            $links = new HtmlDocument();
            $links->setSeparator(', ');
            $links->add([
                $this->activitiesLink(
                    'objects_created',
                    $this->translate('%d created'),
                    DirectorActivityLog::ACTION_CREATE,
                    $idRangeEx
                ),
                $this->activitiesLink(
                    'objects_modified',
                    $this->translate('%d modified'),
                    DirectorActivityLog::ACTION_MODIFY,
                    $idRangeEx
                ),
                $this->activitiesLink(
                    'objects_deleted',
                    $this->translate('%d deleted'),
                    DirectorActivityLog::ACTION_DELETE,
                    $idRangeEx
                ),
            ]);

            if ($idRangeEx && count($links) > 1) {
                $links->add(new Link(
                    $this->translate('Show all actions'),
                    self::URL_ACTIVITIES,
                    ['idRangeEx' => $idRangeEx]
                ));
            }

            if (! $links->isEmpty()) {
                $html[] = ': ';
                $html[] = $links;
            }
        }

        return $html;
    }

    protected function activitiesLink($key, $label, $action, $rangeFilter)
    {
        $count = $this->run->get($key);
        if ($count > 0) {
            if ($rangeFilter) {
                return new Link(
                    sprintf($label, $count),
                    self::URL_ACTIVITIES,
                    ['action' => $action, 'idRangeEx' => $rangeFilter]
                );
            }

            return sprintf($label, $count);
        }

        return null;
    }
}