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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Diagnostics\Diagnostic;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Config;
use Piwik\CronArchive;
use Piwik\Date;
use Piwik\Metrics\Formatter;
use Piwik\Option;
use Piwik\Plugins\Intl\DateTimeFormatProvider;
use Piwik\SettingsPiwik;
use Piwik\Translation\Translator;
/**
* Check if cron archiving has run in the last 24-48 hrs.
*/
class CronArchivingLastRunCheck implements Diagnostic
{
const SECONDS_IN_DAY = 86400;
/**
* @var Translator
*/
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
public function execute()
{
if (!SettingsPiwik::isMatomoInstalled()) {
return [];
}
$label = $this->translator->translate('Diagnostics_CronArchivingLastRunCheck');
$commandToRerun = '<code>' . $this->getArchivingCommand() . '</code>';
$coreArchiveShort = '<code>core:archive</code>';
$mailto = '<code>MAILTO</code>';
// check cron archiving has been enabled
$isBrowserTriggerDisabled = !Rules::isBrowserTriggerEnabled();
if (!$isBrowserTriggerDisabled) {
$comment = $this->translator->translate('Diagnostics_BrowserTriggeredArchivingEnabled', [
'<a href="https://matomo.org/docs/setup-auto-archiving/" target="_blank" rel="noreferrer noopener">', '</a>']);
return [DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_WARNING, $comment)];
}
// check archiving has been run
$lastRunTime = (int)Option::get(CronArchive::OPTION_ARCHIVING_FINISHED_TS);
if (empty($lastRunTime)) {
$comment = $this->translator->translate('Diagnostics_CronArchivingHasNotRun')
. '<br/><br/>' . $this->translator->translate('Diagnostics_CronArchivingRunDetails',
[$coreArchiveShort, $mailto, $commandToRerun, '<a href="https://matomo.org/docs/setup-auto-archiving/" target="_blank" rel="noreferrer noopener">', '</a>']);;
return [DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_ERROR, $comment)];
}
$lastRunTimePretty = Date::factory($lastRunTime)->getLocalized(DateTimeFormatProvider::DATETIME_FORMAT_LONG);
$diffTime = self::getTimeSinceLastSuccessfulRun($lastRunTime);
$formatter = new Formatter();
$diffTimePretty = $formatter->getPrettyTimeFromSeconds($diffTime);
$errorComment = $this->translator->translate('Diagnostics_CronArchivingHasNotRunInAWhile', [$lastRunTimePretty, $diffTimePretty])
. '<br/><br/>' .
$this->translator->translate(
'Diagnostics_CronArchivingRunDetails',
[$coreArchiveShort, $mailto, $commandToRerun, '<a href="https://matomo.org/docs/setup-auto-archiving/" target="_blank" rel="noreferrer noopener">', '</a>']
);
// check archiving has been run recently
if ($diffTime > self::SECONDS_IN_DAY * 2) {
$result = DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_ERROR, $errorComment);
} else if ($diffTime > self::SECONDS_IN_DAY) {
$result = DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_WARNING, $errorComment);
} else {
$comment = $this->translator->translate('Diagnostics_CronArchivingRanSuccessfullyXAgo', $diffTimePretty);
$result = DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_OK, $comment);
}
return [$result];
}
private function getArchivingCommand()
{
$domain = Config::getHostname();
return PIWIK_INCLUDE_PATH . ' --matomo-domain=' . $domain . ' core:archive';
}
public static function getTimeSinceLastSuccessfulRun($lastRunTime = null)
{
if (empty($lastRunTime)) {
$lastRunTime = (int)Option::get(CronArchive::OPTION_ARCHIVING_FINISHED_TS);
}
if (empty($lastRunTime)) {
return null;
}
$now = Date::now()->getTimestamp();
$diffTime = $now - $lastRunTime;
return $diffTime;
}
}
|