Moved personal metrics to API endpoint

This commit is contained in:
Kalle Fagerberg 2022-07-17 12:33:43 +02:00 committed by thrillfall
parent b9f982cb92
commit 226054a634
3 changed files with 74 additions and 48 deletions

View File

@ -14,5 +14,6 @@ return [
['name' => 'subscription_change#list', 'url' => '/subscriptions', 'verb' => 'GET'],
['name' => 'subscription_change#create', 'url' => '/subscription_change/create', 'verb' => 'POST'],
['name' => 'personal_settings#metrics', 'url' => '/personal_settings/metrics', 'verb' => 'GET'],
]
];

View File

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Controller;
use DateTime;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Settings\ISettings;
class EpisodeActionController extends Controller {
private IL10N $l;
private SubscriptionChangeRepository $subscriptionChangeRepository;
private EpisodeActionRepository $episodeActionRepository;
private string $userId;
public function __construct(
string $AppName,
IRequest $request,
$UserId,
IL10N $l,
SubscriptionChangeRepository $subscriptionChangeRepository,
EpisodeActionRepository $episodeActionRepository,
) {
parent::__construct($AppName, $request);
$this->l = $l;
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
$this->episodeActionRepository = $episodeActionRepository;
$this->userId = $UserId ?? '';
}
/**
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function metrics(): JSONResponse {
$sinceDatetime = (new DateTime)->setTimestamp(0);
$subscriptions = $this->extractUrlList($this->subscriptionChangeRepository->findAllSubscribed($sinceDatetime, $this->userId));
$episodeActions = $this->episodeActionRepository->findAll(0, $this->userId);
$subStats = array();
foreach ($episodeActions as $action) {
$pod = $action->getPodcast();
$sub = $subStats[$pod] ?? array();
$sub['started']++;
$subStats[$pod] = $sub;
}
return new JSONResponse([
'subscriptions' => $subscriptions,
'subStats' => $subStats,
]);
}
/**
* @param array $allSubscribed
* @return mixed
*/
private function extractUrlList(array $allSubscribed): array {
return array_map(static function (SubscriptionChangeEntity $subscription) {
return $subscription->getUrl();
}, $allSubscribed);
}
}

View File

@ -1,52 +1,17 @@
<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Settings;
use DateTime;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Settings\ISettings;
class GPodderSyncPersonal implements ISettings {
private IL10N $l;
private IConfig $config;
private SubscriptionChangeRepository $subscriptionChangeRepository;
private EpisodeActionRepository $episodeActionRepository;
private string $userId;
public function __construct(
IConfig $config,
IL10N $l,
$UserId,
SubscriptionChangeRepository $subscriptionChangeRepository,
EpisodeActionRepository $episodeActionRepository,
) {
$this->config = $config;
$this->l = $l;
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
$this->episodeActionRepository = $episodeActionRepository;
$this->userId = $UserId ?? '';
}
public function getForm(): TemplateResponse {
$sinceDatetime = (new DateTime)->setTimestamp(0);
$subscriptions = $this->extractUrlList($this->subscriptionChangeRepository->findAllSubscribed($sinceDatetime, $this->userId));
$episodeActions = $this->episodeActionRepository->findAll(0, $this->userId);
$subStats = array();
foreach ($episodeActions as $action) {
$pod = $action->getPodcast();
$sub = $subStats[$pod] ?? array();
$sub['started']++;
$subStats[$pod] = $sub;
}
$params = array(
'subscriptions' => $subscriptions,
'subStats' => $subStats,
);
return new TemplateResponse('gpoddersync', 'settings/personal', $params);
return new TemplateResponse('gpoddersync', 'settings/personal', []);
}
public function getSection(): string {
@ -56,14 +21,4 @@ class GPodderSyncPersonal implements ISettings {
public function getPriority(): int {
return 198;
}
/**
* @param array $allSubscribed
* @return mixed
*/
private function extractUrlList(array $allSubscribed): array {
return array_map(static function (SubscriptionChangeEntity $subscription) {
return $subscription->getUrl();
}, $allSubscribed);
}
}