diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index ff8b7d5bc..9e1d2d97a 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -192,7 +192,8 @@ App::get('/v1/functions/:functionId/usage') 'group' => '1d', ], ]; - + + /** @var InfluxDB2\Client $client */ $client = $register->get('influxdb'); $executions = []; @@ -202,13 +203,12 @@ App::get('/v1/functions/:functionId/usage') if ($client) { $start = $period[$range]['start']->format(DateTime::RFC3339); $end = $period[$range]['end']->format(DateTime::RFC3339); - $database = $client->selectDB('telegraf'); + $database = $client->createQueryApi(); // Executions $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $executions[] = [ 'value' => (!empty($point['value'])) ? $point['value'] : 0, 'date' => \strtotime($point['time']), @@ -217,9 +217,8 @@ App::get('/v1/functions/:functionId/usage') // Failures $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' AND "functionStatus"=\'failed\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $failures[] = [ 'value' => (!empty($point['value'])) ? $point['value'] : 0, 'date' => \strtotime($point['time']), @@ -228,9 +227,8 @@ App::get('/v1/functions/:functionId/usage') // Compute $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_time" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $compute[] = [ 'value' => round((!empty($point['value'])) ? $point['value'] / 1000 : 0, 2), // minutes 'date' => \strtotime($point['time']), diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 0b9eb64ff..3a14c94c8 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -206,6 +206,7 @@ App::get('/v1/projects/:projectId/usage') ], ]; + /** @var InfluxDB2\Client $client */ $client = $register->get('influxdb'); $requests = []; @@ -215,13 +216,12 @@ App::get('/v1/projects/:projectId/usage') if ($client) { $start = $period[$range]['start']->format(DateTime::RFC3339); $end = $period[$range]['end']->format(DateTime::RFC3339); - $database = $client->selectDB('telegraf'); + $database = $client->createQueryApi(); // Requests $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $requests[] = [ 'value' => (!empty($point['value'])) ? $point['value'] : 0, 'date' => \strtotime($point['time']), @@ -230,9 +230,8 @@ App::get('/v1/projects/:projectId/usage') // Network $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $network[] = [ 'value' => (!empty($point['value'])) ? $point['value'] : 0, 'date' => \strtotime($point['time']), @@ -241,9 +240,8 @@ App::get('/v1/projects/:projectId/usage') // Functions $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); - $points = $result->getPoints(); - foreach ($points as $point) { + foreach ($result as $point) { $functions[] = [ 'value' => (!empty($point['value'])) ? $point['value'] : 0, 'date' => \strtotime($point['time']), diff --git a/app/init.php b/app/init.php index bc5e37d28..5ec434688 100644 --- a/app/init.php +++ b/app/init.php @@ -170,7 +170,12 @@ $register->set('influxdb', function () { // Register DB connection return; } - $client = new InfluxDB\Client($host, $port, '', '', false, false, 5); + $client = new InfluxDB2\Client([ + 'url' => "http://{$host}:{$port}", + 'bucket' => 'telegraf/autogen', + 'timeout' => 5, + 'verifySSL' => false + ]); return $client; }); @@ -190,10 +195,10 @@ $register->set('cache', function () { // Register cache connection $pass = App::getEnv('_APP_REDIS_PASS',''); $auth = []; if(!empty($user)) { - $auth["user"] = $user; + $auth['user'] = $user; } if(!empty($pass)) { - $auth["pass"] = $pass; + $auth['pass'] = $pass; } if(!empty($auth)) { $redis->auth($auth); diff --git a/composer.json b/composer.json index 4b68a5f1c..920d4ccd5 100644 --- a/composer.json +++ b/composer.json @@ -55,11 +55,11 @@ "resque/php-resque": "1.3.6", "matomo/device-detector": "4.2.2", "dragonmantank/cron-expression": "3.1.0", - "influxdb/influxdb-php": "1.15.2", "phpmailer/phpmailer": "6.4.0", "chillerlan/php-qrcode": "4.3.0", "adhocore/jwt": "1.1.2", - "slickdeals/statsd": "3.0.2" + "slickdeals/statsd": "3.0.2", + "influxdata/influxdb-client-php": "^1.12" }, "require-dev": { "appwrite/sdk-generator": "dev-feat-preps-for-0.8", diff --git a/composer.lock b/composer.lock index 8e54ef9bb..3b8283e4d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "32ceddda707fb8f625f84eec08dc3871", + "content-hash": "ea8c94c961eb9a64a93d87114fbc74cc", "packages": [ { "name": "adhocore/jwt", @@ -647,6 +647,52 @@ }, "time": "2021-04-26T09:17:50+00:00" }, + { + "name": "influxdata/influxdb-client-php", + "version": "1.12.0", + "source": { + "type": "git", + "url": "https://github.com/influxdata/influxdb-client-php.git", + "reference": "e04f802a4d9c52b5b497077673269e8463fdb6ea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/influxdata/influxdb-client-php/zipball/e04f802a4d9c52b5b497077673269e8463fdb6ea", + "reference": "e04f802a4d9c52b5b497077673269e8463fdb6ea", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*", + "guzzlehttp/guzzle": "^6.2|^7.0.1", + "php": ">=7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.4|^9.1", + "squizlabs/php_codesniffer": "~2.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "InfluxDB2\\": "src/InfluxDB2" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "InfluxDB (v2+) Client Library for PHP", + "homepage": "https://www.github.com/influxdata/influxdb-client-php", + "keywords": [ + "influxdb" + ], + "support": { + "issues": "https://github.com/influxdata/influxdb-client-php/issues", + "source": "https://github.com/influxdata/influxdb-client-php/tree/1.12.0" + }, + "time": "2021-04-01T06:28:57+00:00" + }, { "name": "influxdb/influxdb-php", "version": "1.15.2",