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
This commit is contained in:
160
analytics-hub/lib/Controller/AdminController.php
Normal file
160
analytics-hub/lib/Controller/AdminController.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\AnalyticsHub\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\IResponse;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
|
||||
use OCA\AnalyticsHub\Service\LLMService;
|
||||
use OCA\AnalyticsHub\Service\DataProcessor;
|
||||
|
||||
/**
|
||||
* Admin Settings Controller
|
||||
* Handles app configuration via admin UI
|
||||
*/
|
||||
class AdminController {
|
||||
|
||||
private GoogleAnalyticsService $gaService;
|
||||
private LLMService $llmService;
|
||||
private DataProcessor $dataProcessor;
|
||||
|
||||
public function __construct(
|
||||
GoogleAnalyticsService $gaService,
|
||||
LLMService $llmService,
|
||||
DataProcessor $dataProcessor
|
||||
) {
|
||||
$this->gaService = $gaService;
|
||||
$this->llmService = $llmService;
|
||||
$this->dataProcessor = $dataProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save configuration
|
||||
* POST /settings/save
|
||||
*/
|
||||
public function save(IRequest $request): JSONResponse {
|
||||
$params = $request->getParams();
|
||||
|
||||
// Validate required fields
|
||||
if (!isset($params['google_client_id'])) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'google_client_id is required'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (!isset($params['google_client_secret'])) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'google_client_secret is required'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (!isset($params['anthropic_api_key'])) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'anthropic_api_key is required'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (!isset($params['clients_json'])) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'clients_json is required'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
// Save Google OAuth config
|
||||
$this->saveConfigValue('google_client_id', $params['google_client_id']);
|
||||
$this->saveConfigValue('google_client_secret', $params['google_client_secret']);
|
||||
$this->saveConfigValue('anthropic_api_key', $params['anthropic_api_key']);
|
||||
|
||||
// Save client configuration
|
||||
$clientsJson = $params['clients_json'];
|
||||
if (!json_decode($clientsJson)) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'Invalid JSON format'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$this->saveConfigValue('clients_json', $clientsJson);
|
||||
|
||||
// Test connections
|
||||
$gaConfigured = $this->gaService->isConfigured();
|
||||
$llmConfigured = $this->llmService->isConfigured();
|
||||
|
||||
return new JSONResponse([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'google_analytics_configured' => $gaConfigured,
|
||||
'llm_configured' => $llmConfigured,
|
||||
'message' => 'Configuration saved successfully'
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration
|
||||
* GET /settings/load
|
||||
*/
|
||||
public function load(IRequest $request): DataResponse {
|
||||
$config = [
|
||||
'google_client_id' => $this->getConfigValue('google_client_id'),
|
||||
'google_client_secret' => $this->getConfigValue('google_client_secret'),
|
||||
'anthropic_api_key' => $this->getConfigValue('anthropic_api_key'),
|
||||
'clients_json' => $this->getConfigValue('clients_json'),
|
||||
];
|
||||
|
||||
return new DataResponse([
|
||||
'success' => true,
|
||||
'data' => $config
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get app status
|
||||
* GET /settings/status
|
||||
*/
|
||||
public function getStatus(IRequest $request): DataResponse {
|
||||
$status = [
|
||||
'app_name' => AppInfo::APP_NAME,
|
||||
'version' => AppInfo::getVersion(),
|
||||
'status' => 'operational',
|
||||
'google_analytics' => $this->gaService->isConfigured() ? 'configured' : 'not_configured',
|
||||
'llm_service' => $this->llmService->isConfigured() ? 'configured' : 'not_configured',
|
||||
'total_clients' => $this->gaService->getClientCount(),
|
||||
'last_report_time' => $this->gaService->getLastReportTime()
|
||||
];
|
||||
|
||||
return new DataResponse([
|
||||
'success' => true,
|
||||
'data' => $status
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper methods
|
||||
*/
|
||||
private function saveConfigValue(string $key, string $value): void {
|
||||
$this->getConfig()->setAppValue($key, $value, AppInfo::APP_NAME);
|
||||
}
|
||||
|
||||
private function getConfigValue(string $key): ?string {
|
||||
return $this->getConfig()->getAppValue($key, AppInfo::APP_NAME);
|
||||
}
|
||||
}
|
||||
171
analytics-hub/lib/Controller/ApiV1Controller.php
Normal file
171
analytics-hub/lib/Controller/ApiV1Controller.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\AnalyticsHub\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\IResponse;
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Http\JSONResponse;
|
||||
|
||||
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
|
||||
use OCA\AnalyticsHub\Service\LLMService;
|
||||
use OCA\AnalyticsHub\Service\DataProcessor;
|
||||
use OCA\AnalyticsHub\Model\ClientConfig;
|
||||
use OCA\AnalyticsHub\Model\Report;
|
||||
|
||||
/**
|
||||
* API V1 Controller - Exposes REST APIs for agent integration
|
||||
*/
|
||||
class ApiV1Controller {
|
||||
|
||||
private GoogleAnalyticsService $gaService;
|
||||
private LLMService $llmService;
|
||||
private DataProcessor $dataProcessor;
|
||||
|
||||
public function __construct(
|
||||
GoogleAnalyticsService $gaService,
|
||||
LLMService $llmService,
|
||||
DataProcessor $dataProcessor
|
||||
) {
|
||||
$this->gaService = $gaService;
|
||||
$this->llmService = $llmService;
|
||||
$this->dataProcessor = $dataProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available reports
|
||||
* GET /api/reports
|
||||
*/
|
||||
public function getReports(IRequest $request): DataResponse {
|
||||
$this->validateAgentAccess();
|
||||
|
||||
$reports = $this->gaService->getAllReports();
|
||||
|
||||
return new DataResponse([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'reports' => $reports
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get specific report by ID
|
||||
* GET /api/report/{id}
|
||||
*/
|
||||
public function getReport(IRequest $request, int $id): DataResponse {
|
||||
$this->validateAgentAccess();
|
||||
|
||||
$report = $this->gaService->getReportById($id);
|
||||
|
||||
if (!$report) {
|
||||
return new DataResponse([
|
||||
'success' => false,
|
||||
'error' => 'Report not found'
|
||||
], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
return new DataResponse([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'report' => $report
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate new report
|
||||
* POST /api/generate
|
||||
*/
|
||||
public function generateReport(IRequest $request): JSONResponse {
|
||||
$this->validateAgentAccess();
|
||||
|
||||
$params = $request->getParams();
|
||||
|
||||
$clientSlug = $params['client_slug'] ?? null;
|
||||
$dateRange = $params['date_range'] ?? '7d';
|
||||
|
||||
if (!$clientSlug) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'client_slug is required'
|
||||
], Http::STATUS_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get client configuration
|
||||
$client = $this->gaService->getClientBySlug($clientSlug);
|
||||
|
||||
if (!$client) {
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => 'Client not found'
|
||||
], Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
|
||||
// Fetch GA4 data
|
||||
$rawData = $this->gaService->fetchGA4Data($client, $dateRange);
|
||||
|
||||
// Process and validate
|
||||
$processed = $this->dataProcessor->process($rawData, $client);
|
||||
|
||||
// Generate report via LLM
|
||||
$markdown = $this->llmService->generate($processed, $client);
|
||||
|
||||
// Save report to Nextcloud
|
||||
$report = $this->gaService->saveReport($client, $markdown);
|
||||
|
||||
return new JSONResponse([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'report_id' => $report->getId(),
|
||||
'report_date' => date('Y-m-d'),
|
||||
'file_path' => $report->getFilePath(),
|
||||
'markdown_preview' => substr($markdown, 0, 500) . '...'
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog("Generate report failed: {$e->getMessage()}");
|
||||
|
||||
return new JSONResponse([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get app status
|
||||
* GET /api/status
|
||||
*/
|
||||
public function getStatus(IRequest $request): DataResponse {
|
||||
$this->validateAgentAccess();
|
||||
|
||||
$status = [
|
||||
'app_name' => AppInfo::APP_NAME,
|
||||
'version' => AppInfo::getVersion(),
|
||||
'status' => 'operational',
|
||||
'google_analytics' => $this->gaService->isConfigured() ? 'configured' : 'not_configured',
|
||||
'llm_service' => $this->llmService->isConfigured() ? 'configured' : 'not_configured',
|
||||
'total_clients' => $this->gaService->getClientCount(),
|
||||
'last_report_time' => $this->gaService->getLastReportTime()
|
||||
];
|
||||
|
||||
return new DataResponse([
|
||||
'success' => true,
|
||||
'data' => $status
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate agent access using app password
|
||||
*/
|
||||
private function validateAgentAccess(): void {
|
||||
// Authentication will be handled by Nextcloud middleware
|
||||
// This is a placeholder for future enhancement
|
||||
}
|
||||
}
|
||||
83
analytics-hub/lib/Controller/ReportController.php
Normal file
83
analytics-hub/lib/Controller/ReportController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\AnalyticsHub\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http;
|
||||
|
||||
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
|
||||
use OCA\AnalyticsHub\Service\LLMService;
|
||||
use OCA\AnalyticsHub\Service\DataProcessor;
|
||||
use OCA\AnalyticsHub\Model\ClientConfig;
|
||||
|
||||
/**
|
||||
* Report Controller - Internal report generation logic
|
||||
*/
|
||||
class ReportController {
|
||||
|
||||
private GoogleAnalyticsService $gaService;
|
||||
private LLMService $llmService;
|
||||
private DataProcessor $dataProcessor;
|
||||
|
||||
public function __construct(
|
||||
GoogleAnalyticsService $gaService,
|
||||
LLMService $llmService,
|
||||
DataProcessor $dataProcessor
|
||||
) {
|
||||
$this->gaService = $gaService;
|
||||
$this->llmService = $llmService;
|
||||
$this->dataProcessor = $dataProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate report for a specific client
|
||||
* Called by cron job
|
||||
*/
|
||||
public function generateForClient(ClientConfig $client): ?string {
|
||||
\OCP\Util::writeLog("Generating report for: {$client->getName()}");
|
||||
|
||||
try {
|
||||
// Fetch GA4 data (last 7 days)
|
||||
$rawData = $this->gaService->fetchGA4Data($client, '7d');
|
||||
|
||||
// Process and validate
|
||||
$processed = $this->dataProcessor->process($rawData, $client);
|
||||
|
||||
// Generate report via LLM
|
||||
$markdown = $this->llmService->generate($processed, $client);
|
||||
|
||||
// Save to Nextcloud
|
||||
$report = $this->gaService->saveReport($client, $markdown);
|
||||
|
||||
\OCP\Util::writeLog("Report generated: {$report->getFilePath()}");
|
||||
|
||||
return $markdown;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
\OCP\Util::writeLog("Report generation failed for {$client->getName()}: {$e->getMessage()}");
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate reports for all active clients
|
||||
* Called by cron job
|
||||
*/
|
||||
public function generateForAllClients(): array {
|
||||
$clients = $this->gaService->getActiveClients();
|
||||
$results = [];
|
||||
|
||||
foreach ($clients as $client) {
|
||||
try {
|
||||
$this->generateForClient($client);
|
||||
$results[$client->getSlug()] = 'success';
|
||||
} catch (\Exception $e) {
|
||||
$results[$client->getSlug()] = "error: {$e->getMessage()}";
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user