diff options
author | Simon Rettberg | 2017-01-19 16:03:06 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-01-19 16:03:06 +0100 |
commit | 886e13e1af47ba6488ba3c5146d96e48e08403ad (patch) | |
tree | dd0da56ccf6f2e5ae9e4e6b1e5709b911d255164 /modules-available | |
parent | [statistics_reporting] i18n: Move tags used in code to separate file, delete ... (diff) | |
download | slx-admin-886e13e1af47ba6488ba3c5146d96e48e08403ad.tar.gz slx-admin-886e13e1af47ba6488ba3c5146d96e48e08403ad.tar.xz slx-admin-886e13e1af47ba6488ba3c5146d96e48e08403ad.zip |
[statistics_reporting] Overhaul remote reporting structure; default to off
Diffstat (limited to 'modules-available')
4 files changed, 114 insertions, 33 deletions
diff --git a/modules-available/statistics_reporting/hooks/cron.inc.php b/modules-available/statistics_reporting/hooks/cron.inc.php index bd427e64..a48f74c2 100644 --- a/modules-available/statistics_reporting/hooks/cron.inc.php +++ b/modules-available/statistics_reporting/hooks/cron.inc.php @@ -1,32 +1,23 @@ <?php -$nextReporting = Property::get("nextReporting", 0); -$time = time(); +if (RemoteReport::isReportingEnabled()) { + $nextReporting = RemoteReport::getReportingTimestamp(); -$allowReport = Property::get("reportingStatus", "on") == "on"; + // It's time to generate a new report + if ($nextReporting <= time()) { + RemoteReport::writeNextReportingTimestamp(); -if ($nextReporting < $time && $allowReport) { + $from = strtotime("-7 days", $nextReporting); + $to = $nextReporting; - Property::set("nextReporting", strtotime("Sunday 23:59:59")); + $statisticsReport = json_encode(RemoteReport::generateReport($from, $to)); - GetData::$from = strtotime("last sunday - 6 days"); - GetData::$to = strtotime("last sunday 23:59:59"); - GetData::$salt = bin2hex(random_bytes(20)); + $params = array("action" => "statistics", "data" => $statisticsReport); - $data = array_merge(GetData::total(true), array('perLocation' => array(), 'perClient' => array(), 'perUser' => array(), 'perVM' => array())); - $data['perLocation'] = GetData::perLocation(true); - $data['perClient'] = GetData::perClient(true); - $data['perUser'] = GetData::perUser(true); - $data['perVM'] = GetData::perVM(true); + $result = Download::asStringPost(CONFIG_REPORTING_URL, $params, 30, $code); - - $statisticsReport = json_encode($data); - - $params = array("action" => "statistics", "data" => $statisticsReport); - - Download::asStringPost(CONFIG_REPORTING_URL, $params, 300, $code); - - if ($code != 200) { - EventLog::warning("Statistics Reporting: ".$code); + if ($code != 200) { + EventLog::warning("Statistics Reporting failed: " . $code, $result); + } } -} +}
\ No newline at end of file diff --git a/modules-available/statistics_reporting/inc/remotereport.inc.php b/modules-available/statistics_reporting/inc/remotereport.inc.php new file mode 100644 index 00000000..0bf4e7e2 --- /dev/null +++ b/modules-available/statistics_reporting/inc/remotereport.inc.php @@ -0,0 +1,84 @@ +<?php + +class RemoteReport +{ + + const ENABLED_ID = 'statistics-reporting-enabled'; + const NEXT_SUBMIT_ID = 'statistics-reporting-next'; + + /** + * Enable or disable remote reporting of usage statistics. + * + * @param bool|string $isEnabled true or 'on' if reporting should be enabled + */ + public static function setReportingEnabled($isEnabled) + { + $value = ($isEnabled === true || $isEnabled === 'on') ? 'on' : ''; + Property::set(self::ENABLED_ID, $value, 60 * 24 * 14); + } + + /** + * Returns whether remote reporting is enabled or not. + * + * @return bool true if reporting is on, false if off + */ + public static function isReportingEnabled() + { + return Property::get(self::ENABLED_ID, false); + } + + /** + * Get the timestamp of the end of the next 7 day interval to + * report statistics for. Usually if this is < time() you want + * to generate the report. + * + * @return int timestamp of the end of the reporting time frame + */ + public static function getReportingTimestamp() + { + $ts = Property::get(self::NEXT_SUBMIT_ID, 0); + if ($ts === 0) { + // No timestamp stored yet - might be a fresh install + // schedule for next time + self::updateNextReportingTimestamp(); + $ts = Property::get(self::NEXT_SUBMIT_ID, 0); + } elseif ($ts < strtotime('last monday')) { + // Too long ago, move forward to last monday + $ts = strtotime('last monday'); + } + return $ts; + } + + /** + * Update the timestamp of the next scheduled statistics report. + * This sets the end of the next 7 day interval to the start of + * next monday (00:00). + */ + public static function writeNextReportingTimestamp() + { + Property::set(self::NEXT_SUBMIT_ID, strtotime('next monday'), 60 * 24 * 14); + } + + /** + * Generate the multi-dimensional array containing the anonymized + * (weekly) statistics to report. + * + * @param $from start timestamp + * @param $to end timestamp + * @return array wrapped up statistics, ready for reporting + */ + public static function generateReport($from, $to) { + GetData::$from = $from; + GetData::$to = $to; + GetData::$salt = bin2hex(Util::randomBytes(20)); + $data = GetData::total(true); + $data['perLocation'] = GetData::perLocation(true); + $data['perClient'] = GetData::perClient(true); + $data['perUser'] = GetData::perUser(true); + $data['perVM'] = GetData::perVM(true); + $data['tsFrom'] = $from; + $data['tsTo'] = $to; + return $data; + } + +}
\ No newline at end of file diff --git a/modules-available/statistics_reporting/page.inc.php b/modules-available/statistics_reporting/page.inc.php index 27d478dc..0309be68 100644 --- a/modules-available/statistics_reporting/page.inc.php +++ b/modules-available/statistics_reporting/page.inc.php @@ -46,15 +46,21 @@ class Page_Statistics_Reporting extends Page protected function doAjax() { - if (!User::isLoggedIn()) { - echo "No."; - } else { - $this->action = Request::any('action', false, 'string'); - if ($this->action === 'setReporting') { - Property::set("reportingStatus", Request::get('reporting', "on", 'string')); - } elseif ($this->action === 'getReporting') { - echo Property::get("reportingStatus", "on"); + $this->action = Request::any('action', false, 'string'); + if ($this->action === 'setReporting') { + if (!User::isLoggedIn()) { + die("No."); + } + $state = Request::post('reporting', false, 'string'); + if ($state === false) { + die('Missing setting value.'); } + RemoteReport::setReportingEnabled($state); + } elseif ($this->action === 'getReporting') { + echo RemoteReport::isReportingEnabled() ? 'on' : ''; + } else { + echo 'Invalid action.'; } } + } diff --git a/modules-available/statistics_reporting/templates/columnChooser.html b/modules-available/statistics_reporting/templates/columnChooser.html index 47fef2a5..513bc36b 100644 --- a/modules-available/statistics_reporting/templates/columnChooser.html +++ b/modules-available/statistics_reporting/templates/columnChooser.html @@ -189,8 +189,8 @@ function saveSettings() { $.ajax({ url: '?do=statistics_reporting', - type: 'GET', - data: { action: "setReporting", reporting: $("#checkbox-reporting").is(":checked") ? "on" : "off" } + type: 'POST', + data: { action: "setReporting", reporting: $("#checkbox-reporting").is(":checked") ? "on" : "off", token: TOKEN } }); } </script>
\ No newline at end of file |