|
1 | 1 | <?php |
| 2 | +use \Controllers\Host\Package\Package; |
| 3 | +use \Controllers\Host\Package\Event; |
| 4 | + |
2 | 5 | $hostController = new \Controllers\Host\Host(); |
3 | 6 | $hostListingController = new \Controllers\Host\Listing(); |
4 | 7 | $datasets = []; |
5 | 8 | $labels = []; |
6 | 9 | $options = []; |
7 | 10 | $totalNotUptodate = 0; |
8 | 11 | $totalUptodate = 0; |
| 12 | +$totalCompliant = 0; |
| 13 | +$totalNotCompliant = 0; |
9 | 14 |
|
10 | 15 | // Getting hosts list |
11 | 16 | $hosts = $hostListingController->get(); |
|
16 | 21 | // Threshold of the maximum number of available update above which the host is considered as 'not up to date' (but not critical) |
17 | 22 | $complianceThresholdCount = $hostsSettings['compliance_threshold_count']; |
18 | 23 |
|
| 24 | +// Threshold of the maximum number of days since the last update above which the host is considered as 'not up to date' (but not critical) |
| 25 | +$complianceThresholdDays = $hostsSettings['compliance_threshold_days']; |
| 26 | + |
19 | 27 | // Loop through the list of hosts to determine the number of hosts up to date and not up to date |
20 | 28 | foreach ($hosts as $host) { |
21 | 29 | // Open the dedicated database of the host from its ID to be able to retrieve additional information |
22 | | - $hostPackageController = new \Controllers\Host\Package\Package($host['Id']); |
| 30 | + $hostPackageController = new Package($host['Id']); |
| 31 | + $hostPackageEventController = new Event($host['Id']); |
| 32 | + |
| 33 | + $upToDate = true; |
23 | 34 |
|
24 | 35 | // Retrieve the total number of available packages |
25 | 36 | $packagesAvailableTotal = count($hostPackageController->getAvailable()); |
26 | 37 |
|
27 | | - // Retrieve the total number of installed packages |
28 | | - $packagesInstalledTotal = count($hostPackageController->getInstalled()); |
| 38 | + // Retrieve the date of the last package upgrade event |
| 39 | + $latestUpdate = $hostPackageEventController->getLastPackageUpgradeEventDate()['Date'] ?? null; |
29 | 40 |
|
30 | | - /** |
31 | | - * If the total number of available packages retrieved previously is > $complianceThresholdCount (threshold defined by the user) then we increment $totalNotUptodate (counts the number of hosts that are not up to date in the chartjs) |
32 | | - * Else it's $totalUptodate that we increment. |
33 | | - */ |
| 41 | + // The host is not compliant if the available updates count is >= threshold |
34 | 42 | if ($packagesAvailableTotal >= $complianceThresholdCount) { |
35 | | - $totalNotUptodate++; |
36 | | - } else { |
| 43 | + $upToDate = false; |
| 44 | + } |
| 45 | + |
| 46 | + // The host is not compliant if the latest update is older than the threshold in days |
| 47 | + if ($latestUpdate and strtotime($latestUpdate) < strtotime('-' . $complianceThresholdDays . ' days')) { |
| 48 | + $upToDate = false; |
| 49 | + } |
| 50 | + |
| 51 | + if ($upToDate) { |
37 | 52 | $totalUptodate++; |
| 53 | + } else { |
| 54 | + $totalNotUptodate++; |
38 | 55 | } |
| 56 | + |
| 57 | + // Calculate % compliance |
| 58 | + $totalCompliant = round(($totalUptodate / ($totalUptodate + $totalNotUptodate)) * 100, 2); |
| 59 | + |
39 | 60 | } |
40 | 61 |
|
41 | | -$labels[] = 'Up to date'; |
42 | | -$labels[] = 'Need update'; |
| 62 | +$labels[] = 'Compliant'; |
| 63 | +$labels[] = 'Not compliant'; |
43 | 64 | $datasets[0]['data'][] = $totalUptodate; |
44 | 65 | $datasets[0]['data'][] = $totalNotUptodate; |
45 | 66 | $datasets[0]['colors'] = ['#24d794', '#F32F63']; |
| 67 | +$options['title']['text'] = $totalCompliant . '% compliant'; |
| 68 | +$options['title']['align'] = 'left'; |
| 69 | +$options['title']['fontSize'] = 12; |
46 | 70 |
|
47 | | -unset($hostController, $hostListingController, $hostPackageController, $hosts, $hostsSettings, $totalUptodate, $totalNotUptodate, $complianceThresholdCount, $packagesAvailableTotal, $packagesInstalledTotal); |
| 71 | +unset($hostController, $hostListingController, $hostPackageController, $hosts, $hostsSettings, $totalUptodate, $totalNotUptodate, $complianceThresholdCount, $packagesAvailableTotal); |
0 commit comments