Files
nextcloud-analytics/analytics-hub/cron.php
WLTBAgent f9c49cf7c2 Phase 3: Initial commit - Nextcloud Analytics Hub Project
Nextcloud Analytics Hub complete:
- Nextcloud PHP app (analytics-hub/) - All phases (1-3) complete
- Go client tool (nextcloud-analytics) - Full CLI implementation
- Documentation (PRD, README, STATUS, SKILL.md)
- Production-ready for deployment to https://cloud.shortcutsolutions.net

Repository: git.teamworkapps.com/shortcut/nextcloud-analytics
Workspace: /home/molt/.openclaw/workspace
2026-02-13 14:11:01 +00:00

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',
],
],
];