Skip to content

Commit

Permalink
Reports : API Requests new report type, various adjustments. (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMis authored Aug 7, 2024
1 parent 476c0d5 commit f3160bd
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 14 deletions.
5 changes: 5 additions & 0 deletions lib/Dependencies/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static function registerFactoriesWithDi()
$repository->useBaseDependenciesService($c->get('RepositoryBaseDependenciesService'));
return $repository;
},
'apiRequestsFactory' => function (ContainerInterface $c) {
$repository = new \Xibo\Factory\ApplicationRequestsFactory();
$repository->useBaseDependenciesService($c->get('RepositoryBaseDependenciesService'));
return $repository;
},
'applicationFactory' => function (ContainerInterface $c) {
$repository = new \Xibo\Factory\ApplicationFactory(
$c->get('user'),
Expand Down
94 changes: 94 additions & 0 deletions lib/Entity/ApplicationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/*
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Xibo\Entity;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Xibo\Service\LogServiceInterface;
use Xibo\Storage\StorageServiceInterface;

class ApplicationRequest implements \JsonSerializable
{
use EntityTrait;

/**
* @SWG\Property(description="The request ID")
* @var int
*/
public $requestId;

/**
* @SWG\Property(description="The user ID")
* @var int
*/
public $userId;

/**
* @SWG\Property(description="The application ID")
* @var string
*/
public $applicationId;

/**
* @SWG\Property(description="The request route")
* @var string
*/
public $url;

/**
* @SWG\Property(description="The request method")
* @var string
*/
public $method;

/**
* @SWG\Property(description="The request start time")
* @var string
*/
public $startTime;

/**
* @SWG\Property(description="The request end time")
* @var string
*/
public $endTime;

/**
* @SWG\Property(description="The request duration")
* @var int
*/
public $duration;

/**
* Entity constructor.
* @param StorageServiceInterface $store
* @param LogServiceInterface $log
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(
StorageServiceInterface $store,
LogServiceInterface $log,
EventDispatcherInterface $dispatcher
) {
$this->setCommonDependencies($store, $log, $dispatcher);
}
}
40 changes: 40 additions & 0 deletions lib/Factory/ApplicationRequestsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Xibo\Factory;

use Xibo\Entity\ApplicationRequest;

/**
* Class ApplicationRequestsFactory
* @package Xibo\Factory
*/
class ApplicationRequestsFactory extends BaseFactory
{
/**
* @return ApplicationRequest
*/
public function createEmpty(): ApplicationRequest
{
return new ApplicationRequest($this->getStore(), $this->getLog(), $this->getDispatcher());
}
}
6 changes: 4 additions & 2 deletions lib/Middleware/ApiAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ public function process(Request $request, RequestHandler $handler): Response
', [
'userId' => $user->userId,
'applicationId' => $validatedRequest->getAttribute('oauth_client_id'),
'url' => $resource,
'url' => htmlspecialchars($request->getUri()->getPath()),
'method' => $request->getMethod(),
'startTime' => Carbon::now(),
'endTime' => Carbon::now(),
'duration' => 0
]);
], 'api_requests_history');

$this->app->getContainer()->get('store')->commitIfNecessary('api_requests_history');

$logger->setUserId($user->userId);
$this->app->getContainer()->set('user', $user);
Expand Down
65 changes: 64 additions & 1 deletion lib/Report/ApiRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Xibo\Entity\ReportForm;
use Xibo\Entity\ReportResult;
use Xibo\Entity\ReportSchedule;
use Xibo\Factory\ApplicationRequestsFactory;
use Xibo\Factory\AuditLogFactory;
use Xibo\Factory\LogFactory;
use Xibo\Helper\DateFormatHelper;
Expand All @@ -45,11 +46,15 @@ class ApiRequests implements ReportInterface
/** @var AuditLogFactory */
private $auditLogFactory;

/** @var ApplicationRequestsFactory */
private $apiRequestsFactory;

/** @inheritdoc */
public function setFactories(ContainerInterface $container)
{
$this->logFactory = $container->get('logFactory');
$this->auditLogFactory = $container->get('auditLogFactory');
$this->apiRequestsFactory = $container->get('apiRequestsFactory');

return $this;
}
Expand Down Expand Up @@ -319,7 +324,7 @@ public function getResults(SanitizerInterface $sanitizedParams)
$rows,
count($rows),
);
} else {
} else if ($type === 'debug') {
$params = [
'fromDt' => $fromDt->format(DateFormatHelper::getSystemFormat()),
'toDt' => $toDt->format(DateFormatHelper::getSystemFormat()),
Expand Down Expand Up @@ -395,6 +400,64 @@ public function getResults(SanitizerInterface $sanitizedParams)
$rows[] = $logRecord;
}

return new ReportResult(
$metadata,
$rows,
count($rows),
);
} else {
$params = [
'fromDt' => $fromDt->format(DateFormatHelper::getSystemFormat()),
'toDt' => $toDt->format(DateFormatHelper::getSystemFormat()),
];

$sql = 'SELECT
`application_requests_history`.applicationId,
`application_requests_history`.requestId,
`application_requests_history`.userId,
`application_requests_history`.url,
`application_requests_history`.method,
`application_requests_history`.startTime,
`oauth_clients`.name AS applicationName,
`user`.`userName`
FROM `application_requests_history`
INNER JOIN `user`
ON `user`.`userId` = `application_requests_history`.`userId`
INNER JOIN `oauth_clients`
ON `oauth_clients`.id = `application_requests_history`.applicationId
WHERE `application_requests_history`.startTime BETWEEN :fromDt AND :toDt
';

if ($sanitizedParams->getInt('userId') !== null) {
$sql .= ' AND `application_requests_history`.`userId` = :userId';
$params['userId'] = $sanitizedParams->getInt('userId');
}

// Sorting?
$sortOrder = $this->gridRenderSort($sanitizedParams);

if (is_array($sortOrder)) {
$sql .= ' ORDER BY ' . implode(',', $sortOrder);
}

$rows = [];

foreach ($this->store->select($sql, $params) as $row) {
$apiRequestRecord = $this->apiRequestsFactory->createEmpty()->hydrate($row);

$apiRequestRecord->setUnmatchedProperty(
'userName',
$row['userName']
);

$apiRequestRecord->setUnmatchedProperty(
'applicationName',
$row['applicationName']
);

$rows[] = $apiRequestRecord;
}

return new ReportResult(
$metadata,
$rows,
Expand Down
23 changes: 22 additions & 1 deletion reports/apirequests-email-template.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<td>{{ item.objectAfter }}</td>
</tr>
{% endfor %}
{% else %}
{% elseif metadata.logType == 'debug' %}
<tr>
<th>{% trans "Date" %}</th>
<th>{% trans "UserName" %}</th>
Expand All @@ -83,6 +83,27 @@
<td>{{ item.message }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<th>{% trans "Date" %}</th>
<th>{% trans "UserName" %}</th>
<th>{% trans "User ID" %}</th>
<th>{% trans "Application" %}</th>
<th>{% trans "Request ID" %}</th>
<th>{% trans "Method" %}</th>
<th>{% trans "Url" %}</th>
</tr>
{% for item in tableData %}
<tr>
<td>{{ item.startTime }}</td>
<td>{{ item.userName }}</td>
<td>{{ item.userId }}</td>
<td>{{ item.applicationName }}</td>
<td>{{ item.requestId }}</td>
<td>{{ item.method }}</td>
<td>{{ item.url }}</td>
</tr>
{% endfor %}
{% endif %}
</table>
<br/>
Expand Down
Loading

0 comments on commit f3160bd

Please sign in to comment.