- Renamed analytics-hub/ → analyticshub/ - App ID in info.xml is 'analyticshub' (no hyphen) - Nextcloud requires folder name to match app ID exactly - Fixes 'Could not download app analyticshub' error during installation Installation: - Upload analyticshub/ folder to /var/www/nextcloud/apps/ - Folder name must match app ID in info.xml
88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\AnalyticsHub\Cron;
|
|
|
|
use OCA\AnalyticsHub\AppInfo;
|
|
use OCP\BackgroundJob\TimedJob;
|
|
use OCP\BackgroundJob\IJobList;
|
|
use OCP\Util\LogLevel;
|
|
use OCP\Util;
|
|
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
|
|
use OCA\AnalyticsHub\Service\TokenExpiredException;
|
|
use OCA\AnalyticsHub\Service\RateLimitException;
|
|
|
|
/**
|
|
* Daily Report Generation Job
|
|
* Runs Mon-Fri at 7:00 AM
|
|
*/
|
|
class DailyReportJob extends TimedJob {
|
|
|
|
private GoogleAnalyticsService $gaService;
|
|
|
|
public function __construct(GoogleAnalyticsService $gaService) {
|
|
parent::__construct();
|
|
$this->gaService = $gaService;
|
|
}
|
|
|
|
protected function run($argument) {
|
|
$dayOfWeek = date('N');
|
|
|
|
// Only run Mon-Fri (1-5)
|
|
if ($dayOfWeek < 1 || $dayOfWeek > 5) {
|
|
Util::writeLog('Skipping daily report (weekend)');
|
|
return;
|
|
}
|
|
|
|
Util::writeLog('Starting daily report generation');
|
|
|
|
try {
|
|
// Generate reports for all active clients
|
|
$results = $this->gaService->generateForAllClients();
|
|
|
|
// Log summary
|
|
$success = count(array_filter($results, fn($r) => $r === 'success'));
|
|
$total = count($results);
|
|
|
|
Util::writeLog("Daily reports complete: {$success}/{$total} successful");
|
|
|
|
if ($success === $total) {
|
|
Util::writeLog('All reports generated successfully');
|
|
} else {
|
|
Util::writeLog('Some reports failed', LogLevel::WARN);
|
|
}
|
|
|
|
} catch (TokenExpiredException $e) {
|
|
Util::writeLog('❌ CRITICAL: Refresh token expired - re-run OAuth setup', LogLevel::ERROR);
|
|
} catch (RateLimitException $e) {
|
|
Util::writeLog('⚠️ WARNING: Rate limit exceeded', LogLevel::WARN);
|
|
} catch (\Exception $e) {
|
|
Util::writeLog("Daily report generation failed: {$e->getMessage()}", LogLevel::ERROR);
|
|
}
|
|
}
|
|
|
|
protected function getScheduledTime(): string {
|
|
// Mon-Fri at 7:00 AM
|
|
return '07:00';
|
|
}
|
|
|
|
protected function getInterval(): int {
|
|
// Daily
|
|
return 86400; // 24 hours in seconds
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register cron jobs
|
|
*/
|
|
return [
|
|
'app_id' => AppInfo::APP_NAME,
|
|
'jobs' => [
|
|
[
|
|
'class' => DailyReportJob::class,
|
|
'name' => 'OCA\AnalyticsHub\Cron\DailyReportJob',
|
|
],
|
|
],
|
|
];
|