forked from matomo-org/matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add diagnostic to check last time archiving was run successfully and … (
matomo-org#13972) * Add diagnostic to check last time archiving was run successfully and display notification if empty report and archiving has not been run recently. * Move cron archiving diagnostics next to each other. * Add new test for archiving not done in time check. * Remove TODO comment.
- Loading branch information
Showing
6 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
plugins/Diagnostics/Diagnostic/CronArchivingLastRunCheck.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.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\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() | ||
{ | ||
$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]);; | ||
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]); | ||
|
||
// 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Piwik - free/libre analytics platform | ||
* | ||
* @link http://piwik.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
*/ | ||
|
||
namespace Piwik\Tests\Integration; | ||
|
||
use Piwik\ArchiveProcessor\Rules; | ||
use Piwik\CronArchive; | ||
use Piwik\FrontController; | ||
use Piwik\Option; | ||
use Piwik\Tests\Framework\Fixture; | ||
use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
|
||
class ReportRenderingTest extends IntegrationTestCase | ||
{ | ||
public function test_reportHasCorrectNotification_WhenReportHasNoData_AndArchivingHasNotRunRecently() | ||
{ | ||
$idSite = Fixture::createWebsite('2012-01-02 03:04:44'); | ||
Option::set(CronArchive::OPTION_ARCHIVING_FINISHED_TS, time() - 120000); | ||
Option::set(Rules::OPTION_BROWSER_TRIGGER_ARCHIVING, 0); | ||
|
||
$_GET['idSite'] = $idSite; | ||
$_GET['date'] = '2012-05-06'; | ||
$_GET['period'] = 'day'; | ||
|
||
$frontController = FrontController::getInstance(); | ||
$response = $frontController->dispatch('DevicesDetection', 'getBrand'); | ||
|
||
$this->assertContains('Diagnostics_NoDataForReportArchivingNotRun', $response); | ||
} | ||
} |