diff options
author | Björn Schießle <bjoern@schiessle.org> | 2018-08-03 22:51:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-03 22:51:18 +0300 |
commit | ba7b739cc44124c0221335fa3be010fd55bb26d2 (patch) | |
tree | dd4d1cbc27126068301e46a23c61991d9c26d704 | |
parent | a8f883a1bcc327fbf5218396d3aa67d529759c65 (diff) | |
parent | 625b51ece1385231c33c9e8265cfd17175768cf5 (diff) |
Merge pull request #26 from nextcloud/cleanup-and-performance-improvements
Cleanup and performance improvements
-rw-r--r-- | .travis.yml | 9 | ||||
-rw-r--r-- | appinfo/app.php | 1 | ||||
-rw-r--r-- | appinfo/application.php | 22 | ||||
-rw-r--r-- | appinfo/info.xml | 2 | ||||
-rw-r--r-- | appinfo/routes.php | 7 | ||||
-rw-r--r-- | lib/BackgroundJobs/ComputeStatistics.php | 35 | ||||
-rw-r--r-- | lib/Controller/ExternalApiController.php (renamed from api/externalapi.php) | 50 | ||||
-rw-r--r-- | tests/unit/bootstrap.php | 2 | ||||
-rw-r--r-- | tests/unit/controller/PageControllerTest.php | 4 |
9 files changed, 70 insertions, 62 deletions
diff --git a/.travis.yml b/.travis.yml index c19c380..f18097a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ language: php php: - - 5.6 - - 7 + - 7.0 + - 7.1 + - 7.2 env: global: @@ -36,8 +37,8 @@ script: matrix: include: - - php: 5.6 + - php: 7.0 env: DB=mysql - - php: 5.6 + - php: 7.0 env: DB=pgsql fast_finish: true diff --git a/appinfo/app.php b/appinfo/app.php index 28c96df..f017f9f 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -22,7 +22,6 @@ namespace OCA\Survey_Server\AppInfo; $app = new Application(); -$app->registerOCSApi(); $container = $app->getContainer(); $container->query('OCP\INavigationManager')->add(function () use ($container) { diff --git a/appinfo/application.php b/appinfo/application.php index c8122e2..fbf2f44 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -46,26 +46,4 @@ class Application extends \OCP\AppFramework\App { }); } - /** - * register OCS API Calls - */ - public function registerOCSApi() { - - $container = $this->getContainer(); - $server = $container->getServer(); - - $request = $server->getRequest(); - $statisticService = $container->query('statisticService'); - $api = new ExternalApi($request, $statisticService, $server->getConfig()); - //$api = new ExternalApi($server->getRequest(), $container->query('statisticService')); - - API::register('post', - '/apps/survey_server/api/v1/survey', - array($api, 'receiveSurveyResults'), - 'survey_server', - API::GUEST_AUTH - ); - - } - } diff --git a/appinfo/info.xml b/appinfo/info.xml index 487fd97..5e73180 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -9,6 +9,6 @@ <namespace>Survey_Server</namespace> <category>other</category> <dependencies> - <nextcloud min-version="9" max-version="13"/> + <nextcloud min-version="9" max-version="14"/> </dependencies> </info> diff --git a/appinfo/routes.php b/appinfo/routes.php index 1a0bc12..c714ab4 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -34,6 +34,9 @@ return [ // api ['name' => 'api#add', 'url' => '/api/v1/data', 'verb' => 'POST'], - ['name' => 'api#get', 'url' => '/api/v1/data', 'verb' => 'GET'], - ] + ['name' => 'api#get', 'url' => '/api/v1/data', 'verb' => 'GET'] + ], + 'ocs' => [ + ['name' => 'ExternalApi#receiveSurveyResults', 'url' => '/api/v1/survey', 'verb' => 'POST'], + ], ]; diff --git a/lib/BackgroundJobs/ComputeStatistics.php b/lib/BackgroundJobs/ComputeStatistics.php index 3fe94f5..e328ca2 100644 --- a/lib/BackgroundJobs/ComputeStatistics.php +++ b/lib/BackgroundJobs/ComputeStatistics.php @@ -50,16 +50,39 @@ class ComputeStatistics extends TimedJob { $this->connection = $connection ? $connection : \OC::$server->getDatabaseConnection(); $this->config = $config = $config ? $config : \OC::$server->getConfig(); $this->evaluateStatistics = $evaluateStatistics ? $evaluateStatistics : new EvaluateStatistics(); - $this->setInterval(24 * 60 * 60); + $this->setInterval(60 * 60); } protected function run($argument) { - $result = []; - $result['instances'] = $this->getNumberOfInstances(); - $result['categories'] = $this->getStatisticsOfCategories(); - $result['apps'] = $this->getApps(); - $this->config->setAppValue('survey_server', 'evaluated_statistics', json_encode($result)); + $lastResult = $this->config->getAppValue('survey_server', 'evaluated_statistics', []); + $newResult = json_decode($lastResult, true); + + if (!isset($newResult['lastRun'])) { + $newResult['lastRun'] = []; + } + + $lastRun = []; + $lastRun['categories'] = isset($newResult['lastRun']['categories']) ? (int)$newResult['lastRun']['categories'] : 0; + $lastRun['apps'] = isset($newResult['lastRun']['apps']) ? (int)$newResult['lastRun']['apps'] : 0; + + $selected = array_keys($lastRun, min($lastRun)); + + // this is fast, so let's run this always + $newResult['instances'] = $this->getNumberOfInstances(); + + switch ($selected[0]) { + case 'categories': + $newResult['categories'] = $this->getStatisticsOfCategories(); + $newResult['lastRun']['categories'] = time(); + break; + case 'apps': + $newResult['apps'] = $this->getApps(); + $newResult['lastRun']['apps'] = time(); + break; + } + + $this->config->setAppValue('survey_server', 'evaluated_statistics', json_encode($newResult)); } /** diff --git a/api/externalapi.php b/lib/Controller/ExternalApiController.php index fbb85cc..976038c 100644 --- a/api/externalapi.php +++ b/lib/Controller/ExternalApiController.php @@ -1,36 +1,35 @@ <?php /** - * @author Björn Schießle <bjoern@schiessle.org> + * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org> * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 + * @license GNU AGPL version 3 or any later version * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. + * This program 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 (at your option) any later version. * * This program 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 + * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ - -namespace OCA\Survey_Server\Api; - +namespace OCA\Survey_Server\Controller; use OCA\Survey_Server\Service\StatisticService; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCSController; use OCP\IConfig; use OCP\IRequest; -class ExternalApi { - /** @var IRequest */ - private $request; +class ExternalApiController extends OCSController { + /** @var StatisticService */ private $service; /** @var IConfig */ @@ -39,16 +38,18 @@ class ExternalApi { /** * OCSAuthAPI constructor. * + * @param string $appName * @param IRequest $request * @param StatisticService $service * @param IConfig $config */ public function __construct( + $appName, IRequest $request, StatisticService $service, IConfig $config ) { - $this->request = $request; + parent::__construct($appName, $request); $this->service = $service; $this->config = $config; } @@ -56,29 +57,32 @@ class ExternalApi { /** * request received to ask remote server for a shared secret * - * @return \OC\OCS\Result + * @NoCSRFRequired + * @PublicPage + * + * @param string $data + * @return DataResponse */ - public function receiveSurveyResults() { - - $data = $this->request->getParam('data'); + public function receiveSurveyResults(string $data) { $array = json_decode($data, true); + $array['timestamp'] = time(); $logFile = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/survey.log'; file_put_contents($logFile, json_encode($array). PHP_EOL, FILE_APPEND); if ($array === null) { - return new \OC\OCS\Result(null, Http::STATUS_BAD_REQUEST, 'Invalid data supplied.'); + return new DataResponse(['message' => 'Invalid data supplied.'], Http::STATUS_BAD_REQUEST); } try { $this->service->add($array); } catch (\Exception $e) { - return new \OC\OCS\Result(null, Http::STATUS_BAD_REQUEST, 'Invalid data supplied.'); + return new DataResponse(['message' => 'Invalid data supplied.'], Http::STATUS_BAD_REQUEST); } - return new \OC\OCS\Result(null, Http::STATUS_OK); + return new DataResponse(); } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 630e1ba..e79cc34 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -3,7 +3,7 @@ if (!defined('PHPUNIT_RUN')) { define('PHPUNIT_RUN', 1); } require_once __DIR__ . '/../../../../lib/base.php'; -if(!class_exists('PHPUnit_Framework_TestCase')) { +if(!class_exists('\PHPUnit\Framework\TestCase')) { require_once('PHPUnit/Autoload.php'); } \OC_App::loadApp('survey_server'); diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php index c0467cc..2a77655 100644 --- a/tests/unit/controller/PageControllerTest.php +++ b/tests/unit/controller/PageControllerTest.php @@ -22,12 +22,12 @@ namespace OCA\Survey_Server\Controller; use OCA\Survey_Server\Service\StatisticService; -use PHPUnit_Framework_TestCase; use OCP\AppFramework\Http\TemplateResponse; +use Test\TestCase; -class PageControllerTest extends PHPUnit_Framework_TestCase { +class PageControllerTest extends TestCase { private $controller; /** @var StatisticService | \PHPUnit_Framework_MockObject_MockObject */ |