2022-06-13 22:56:24 +12:00
< ? php
2022-08-09 13:22:18 +12:00
namespace Appwrite\Usage\Calculators ;
2022-06-13 22:56:24 +12:00
2022-10-01 01:22:11 +13:00
use Utopia\App ;
2022-08-09 13:22:18 +12:00
use Appwrite\Usage\Calculator ;
2022-06-13 22:56:24 +12:00
use Utopia\Database\Database ;
use Utopia\Database\Document ;
use InfluxDB\Database as InfluxDatabase ;
use DateTime ;
2022-08-10 14:33:46 +12:00
class TimeSeries extends Calculator
{
2022-10-28 21:40:04 +13:00
/**
* InfluxDB
*
* @ var InfluxDatabase
*/
2022-06-13 22:56:24 +12:00
protected InfluxDatabase $influxDB ;
2022-10-28 21:40:04 +13:00
/**
* Utopia Database
*
* @ var Database
*/
2022-06-13 22:56:24 +12:00
protected Database $database ;
2022-10-28 21:40:04 +13:00
/**
* Error Handler Callback
*
* @ var callable
*/
2022-06-13 23:11:26 +12:00
protected $errorHandler ;
2022-10-28 21:40:04 +13:00
/**
* Latest times for metric that was synced to the database
*
* @ var array
*/
2022-06-13 23:11:26 +12:00
private array $latestTime = [];
2022-10-28 21:40:04 +13:00
/**
* Periods the metrics are collected for
* @ var array
*/
2022-09-23 17:51:26 +12:00
protected array $periods = [
[
'key' => '1h' ,
'startTime' => '-24 hours'
],
[
'key' => '1d' ,
'startTime' => '-30 days'
]
];
2022-10-28 21:40:04 +13:00
/**
* All the metrics that we are collecting
2022-10-28 22:08:58 +13:00
*
2022-10-28 21:40:04 +13:00
* @ var array
*/
2022-06-13 22:56:24 +12:00
protected array $metrics = [
2022-08-09 13:22:18 +12:00
'project.$all.network.requests' => [
2022-08-11 18:05:38 +12:00
'table' => 'appwrite_usage_project_{scope}_network_requests' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'project.$all.network.bandwidth' => [
2022-08-11 18:05:38 +12:00
'table' => 'appwrite_usage_project_{scope}_network_bandwidth' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'project.$all.network.inbound' => [
2022-08-11 18:05:38 +12:00
'table' => 'appwrite_usage_project_{scope}_network_inbound' ,
2022-06-29 19:21:54 +12:00
],
2022-08-09 13:22:18 +12:00
'project.$all.network.outbound' => [
2022-08-11 18:05:38 +12:00
'table' => 'appwrite_usage_project_{scope}_network_outbound' ,
2022-06-29 19:21:54 +12:00
],
2022-08-09 13:22:18 +12:00
/* Users service metrics */
'users.$all.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_users_{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'users.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_users_{scope}_requests_read' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'users.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_users_{scope}_requests_update' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'users.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_users_{scope}_requests_delete' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'databases.$all.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_databases_{scope}_requests_create' ,
2022-08-09 13:22:18 +12:00
],
'databases.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_databases_{scope}_requests_read' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'databases.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_databases_{scope}_requests_update' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'databases.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_databases_{scope}_requests_delete' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'collections.$all.requests.create' => [
2022-08-15 13:10:57 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'collections.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_read' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'collections.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_update' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'collections.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_delete' ,
2022-08-09 13:22:18 +12:00
],
'documents.$all.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'documents.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_read' ,
2022-06-22 22:51:49 +12:00
],
2022-08-09 13:22:18 +12:00
'documents.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_update' ,
2022-08-09 13:22:18 +12:00
],
'documents.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_delete' ,
2022-08-09 13:22:18 +12:00
],
'collections.databaseId.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_create' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'collections.databaseId.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_read' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'collections.databaseId.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_update' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'collections.databaseId.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_collections_{scope}_requests_delete' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_create' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_read' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_update' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_delete' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId/collectionId.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_create' ,
2022-07-18 19:21:18 +12:00
'groupBy' => [ 'databaseId' , 'collectionId' ],
2022-06-22 22:51:49 +12:00
],
2022-08-09 13:22:18 +12:00
'documents.databaseId/collectionId.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_read' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' , 'collectionId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId/collectionId.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_update' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' , 'collectionId' ],
],
2022-08-09 13:22:18 +12:00
'documents.databaseId/collectionId.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_documents_{scope}_requests_delete' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'databaseId' , 'collectionId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'buckets.$all.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_buckets_{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'buckets.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_buckets_{scope}_requests_read' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'buckets.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_buckets_{scope}_requests_update' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'buckets.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_buckets_{scope}_requests_delete' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.$all.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.$all.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_read' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.$all.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_update' ,
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.$all.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_delete' ,
2022-06-13 22:56:24 +12:00
],
2022-08-10 14:33:46 +12:00
2022-08-09 13:22:18 +12:00
'files.bucketId.requests.create' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_create' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'bucketId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.bucketId.requests.read' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_read' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'bucketId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.bucketId.requests.update' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_update' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'bucketId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
'files.bucketId.requests.delete' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_files_{scope}_requests_delete' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'bucketId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-09 13:22:18 +12:00
2022-08-10 13:46:36 +12:00
'sessions.$all.requests.create' => [
'table' => 'appwrite_usage_sessions__{scope}_requests_create' ,
2022-06-13 22:56:24 +12:00
],
2022-08-10 13:46:36 +12:00
'sessions.provider.requests.create' => [
'table' => 'appwrite_usage_sessions_{scope}_requests_create' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'provider' ],
2022-06-13 22:56:24 +12:00
],
2022-08-10 13:46:36 +12:00
'sessions.$all.requests.delete' => [
'table' => 'appwrite_usage_sessions_{scope}_requests_delete' ,
2022-06-13 22:56:24 +12:00
],
2022-08-10 13:46:36 +12:00
'executions.$all.compute.total' => [
'table' => 'appwrite_usage_executions_{scope}_compute' ,
2022-07-17 22:30:58 +12:00
],
2022-08-10 13:46:36 +12:00
'builds.$all.compute.total' => [
'table' => 'appwrite_usage_builds_{scope}_compute' ,
2022-07-17 22:30:58 +12:00
],
2022-08-11 17:25:39 +12:00
'executions.$all.compute.failure' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_executions_{scope}_compute' ,
2022-07-17 22:30:58 +12:00
'filters' => [
'functionStatus' => 'failed' ,
],
],
2022-08-11 17:25:39 +12:00
'builds.$all.compute.failure' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_builds_{scope}_compute' ,
'filters' => [
'functionStatus' => 'failed' ,
],
2022-06-13 22:56:24 +12:00
],
2022-08-10 13:46:36 +12:00
'executions.$all.compute.success' => [
'table' => 'appwrite_usage_executions_{scope}_compute' ,
'filters' => [
'functionStatus' => 'success' ,
],
],
'builds.$all.compute.success' => [
'table' => 'appwrite_usage_builds_{scope}_compute' ,
'filters' => [
'functionStatus' => 'success' ,
],
],
'executions.functionId.compute.total' => [
'table' => 'appwrite_usage_executions_{scope}_compute' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'functionId' ],
2022-06-13 22:56:24 +12:00
],
2022-08-10 13:46:36 +12:00
'builds.functionId.compute.total' => [
'table' => 'appwrite_usage_builds_{scope}_compute' ,
2022-06-29 18:48:40 +12:00
'groupBy' => [ 'functionId' ],
],
2022-10-17 20:37:30 +13:00
2022-08-11 17:25:39 +12:00
'executions.functionId.compute.failure' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_executions_{scope}_compute' ,
2022-06-22 22:51:49 +12:00
'groupBy' => [ 'functionId' ],
2022-06-13 22:56:24 +12:00
'filters' => [
'functionStatus' => 'failed' ,
],
],
2022-08-11 17:25:39 +12:00
'builds.functionId.compute.failure' => [
2022-08-10 13:46:36 +12:00
'table' => 'appwrite_usage_builds_{scope}_compute' ,
2022-06-29 18:48:40 +12:00
'groupBy' => [ 'functionId' ],
'filters' => [
'functionBuildStatus' => 'failed' ,
],
],
2022-08-10 13:46:36 +12:00
'executions.functionId.compute.success' => [
'table' => 'appwrite_usage_executions_{scope}_compute' ,
'groupBy' => [ 'functionId' ],
'filters' => [
'functionStatus' => 'success' ,
],
],
'builds.functionId.compute.success' => [
'table' => 'appwrite_usage_builds_{scope}_compute' ,
'groupBy' => [ 'functionId' ],
'filters' => [
'functionBuildStatus' => 'success' ,
],
],
2022-09-23 17:51:26 +12:00
// counters
2022-09-22 19:34:11 +12:00
'users.$all.count.total' => [
'table' => 'appwrite_usage_users_{scope}_count_total' ,
],
2022-09-23 17:51:26 +12:00
'buckets.$all.count.total' => [
'table' => 'appwrite_usage_buckets_{scope}_count_total' ,
],
'files.$all.count.total' => [
'table' => 'appwrite_usage_files_{scope}_count_total' ,
],
'files.bucketId.count.total' => [
'table' => 'appwrite_usage_files_{scope}_count_total' ,
'groupBy' => [ 'bucketId' ]
],
2022-09-22 19:34:11 +12:00
'databases.$all.count.total' => [
'table' => 'appwrite_usage_databases_{scope}_count_total' ,
],
'collections.$all.count.total' => [
'table' => 'appwrite_usage_collections_{scope}_count_total' ,
],
2022-10-21 14:53:39 +13:00
'documents.$all.count.total' => [
2022-09-22 19:34:11 +12:00
'table' => 'appwrite_usage_documents_{scope}_count_total' ,
],
'collections.databaseId.count.total' => [
'table' => 'appwrite_usage_collections_{scope}_count_total' ,
'groupBy' => [ 'databaseId' ]
],
'documents.databaseId.count.total' => [
'table' => 'appwrite_usage_documents_{scope}_count_total' ,
'groupBy' => [ 'databaseId' ]
],
'documents.databaseId/collectionId.count.total' => [
'table' => 'appwrite_usage_documents_{scope}_count_total' ,
'groupBy' => [ 'databaseId' , 'collectionId' ]
],
2022-10-10 22:13:20 +13:00
'deployments.$all.storage.size' => [
'table' => 'appwrite_usage_deployments_{scope}_storage_size' ,
],
'project.$all.storage.size' => [
'table' => 'appwrite_usage_project_{scope}_storage_size' ,
],
'files.$all.storage.size' => [
'table' => 'appwrite_usage_files_{scope}_storage_size' ,
],
'files.$bucketId.storage.size' => [
'table' => 'appwrite_usage_files_{scope}_storage_size' ,
'groupBy' => [ 'bucketId' ]
],
2022-10-17 20:37:30 +13:00
2022-10-11 18:40:28 +13:00
'builds.$all.compute.time' => [
'table' => 'appwrite_usage_executions_{scope}_compute_time' ,
],
'executions.$all.compute.time' => [
'table' => 'appwrite_usage_executions_{scope}_compute_time' ,
],
2022-10-17 20:37:30 +13:00
2022-10-11 18:40:28 +13:00
'executions.functionId.compute.time' => [
'table' => 'appwrite_usage_executions_{scope}_compute_time' ,
'groupBy' => [ 'functionId' ],
],
'builds.functionId.compute.time' => [
'table' => 'appwrite_usage_builds_{scope}_compute_time' ,
'groupBy' => [ 'functionId' ],
],
2022-10-17 20:37:30 +13:00
2022-10-11 18:40:28 +13:00
'project.$all.compute.time' => [ // Built time + execution time
'table' => 'appwrite_usage_project_{scope}_compute_time' ,
'groupBy' => [ 'functionId' ],
],
2022-10-18 19:53:17 +13:00
'deployments.$all.storage.size' => [
'table' => 'appwrite_usage_deployments_{scope}_storage_size'
],
'project.$all.storage.size' => [
'table' => 'appwrite_usage_project_{scope}_storage_size'
],
'files.$all.storage.size' => [
'table' => 'appwrite_usage_files_{scope}_storage_size'
],
2022-10-21 14:40:26 +13:00
'files.bucketId.storage.size' => [
2022-10-18 19:53:17 +13:00
'table' => 'appwrite_usage_files_{scope}_storage_size' ,
'groupBy' => [ 'bucketId' ]
]
2022-10-10 22:13:20 +13:00
];
2022-10-01 01:22:11 +13:00
public function __construct ( string $region , Database $database , InfluxDatabase $influxDB , callable $errorHandler = null )
2022-06-13 22:56:24 +12:00
{
2022-10-01 01:22:11 +13:00
parent :: __construct ( $region );
2022-06-13 22:56:24 +12:00
$this -> database = $database ;
$this -> influxDB = $influxDB ;
2022-06-13 23:11:26 +12:00
$this -> errorHandler = $errorHandler ;
2022-06-13 22:56:24 +12:00
}
/**
* Create or Update Mertic
* Create or update each metric in the stats collection for the given project
2022-06-14 03:49:27 +12:00
*
2022-06-13 22:56:24 +12:00
* @ param string $projectId
* @ param int $time
* @ param string $period
* @ param string $metric
* @ param int $value
* @ param int $type
2022-06-14 03:49:27 +12:00
*
2022-06-13 22:56:24 +12:00
* @ return void
*/
2022-08-26 10:10:52 +12:00
private function createOrUpdateMetric ( string $projectId , string $time , string $period , string $metric , int $value , int $type ) : void
2022-06-13 22:56:24 +12:00
{
$id = \md5 ( " { $time } _ { $period } _ { $metric } " );
2022-10-09 19:45:46 +13:00
$this -> database -> setNamespace ( '_' . $projectId );
2022-06-20 23:21:36 +12:00
2022-06-13 22:56:24 +12:00
try {
$document = $this -> database -> getDocument ( 'stats' , $id );
if ( $document -> isEmpty ()) {
$this -> database -> createDocument ( 'stats' , new Document ([
'$id' => $id ,
2022-06-14 12:40:33 +12:00
'period' => $period ,
2022-06-13 22:56:24 +12:00
'time' => $time ,
'metric' => $metric ,
'value' => $value ,
'type' => $type ,
2022-10-01 01:22:11 +13:00
'region' => $this -> region ,
2022-06-13 22:56:24 +12:00
]));
} else {
$this -> database -> updateDocument (
'stats' ,
$document -> getId (),
$document -> setAttribute ( 'value' , $value )
);
}
} catch ( \Exception $e ) { // if projects are deleted this might fail
2022-06-14 03:49:27 +12:00
if ( is_callable ( $this -> errorHandler )) {
2022-06-15 11:40:56 +12:00
call_user_func ( $this -> errorHandler , $e , " sync_project_ { $projectId } _metric_ { $metric } " );
2022-06-13 23:11:26 +12:00
} else {
throw $e ;
}
2022-06-13 22:56:24 +12:00
}
}
/**
* Sync From InfluxDB
* Sync stats from influxDB to stats collection in the Appwrite database
2022-06-14 03:49:27 +12:00
*
2022-06-13 22:56:24 +12:00
* @ param string $metric
* @ param array $options
* @ param array $period
2022-06-14 03:49:27 +12:00
*
2022-06-13 22:56:24 +12:00
* @ return void
*/
2022-06-13 23:11:26 +12:00
private function syncFromInfluxDB ( string $metric , array $options , array $period ) : void
2022-06-13 22:56:24 +12:00
{
$start = DateTime :: createFromFormat ( 'U' , \strtotime ( $period [ 'startTime' ])) -> format ( DateTime :: RFC3339 );
2022-06-13 23:11:26 +12:00
if ( ! empty ( $this -> latestTime [ $metric ][ $period [ 'key' ]])) {
2022-08-26 10:10:52 +12:00
$start = $this -> latestTime [ $metric ][ $period [ 'key' ]];
2022-06-13 22:56:24 +12:00
}
2022-08-26 10:10:52 +12:00
$end = ( new DateTime ()) -> format ( DateTime :: RFC3339 );
2022-06-13 22:56:24 +12:00
$table = $options [ 'table' ]; //Which influxdb table to query for this metric
2022-06-22 22:51:49 +12:00
$groupBy = empty ( $options [ 'groupBy' ]) ? '' : ', ' . implode ( ', ' , array_map ( fn ( $groupBy ) => '"' . $groupBy . '" ' , $options [ 'groupBy' ])); //Some sub level metrics may be grouped by other tags like collectionId, bucketId, etc
2022-06-13 22:56:24 +12:00
$filters = $options [ 'filters' ] ? ? []; // Some metrics might have additional filters, like function's status
if ( ! empty ( $filters )) {
$filters = ' AND ' . implode ( ' AND ' , array_map ( fn ( $filter , $value ) => " \" { $filter } \" =' { $value } ' " , array_keys ( $filters ), array_values ( $filters )));
} else {
$filters = '' ;
}
2022-06-15 11:23:23 +12:00
$query = " SELECT sum(value) AS \" value \" " ;
$query .= " FROM \" { $table } \" " ;
$query .= " WHERE \" time \" > ' { $start } ' " ;
$query .= " AND \" time \" < ' { $end } ' " ;
$query .= " AND \" metric_type \" ='counter' { $filters } " ;
2022-10-18 16:19:12 +13:00
$query .= " GROUP BY time( { $period [ 'key' ] } ), \" projectId \" , \" projectInternalId \" { $groupBy } " ;
2022-06-15 11:23:23 +12:00
$query .= " FILL(null) " ;
2022-06-22 22:51:49 +12:00
2022-07-18 19:21:18 +12:00
try {
$result = $this -> influxDB -> query ( $query );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$projectId = $point [ 'projectId' ];
2022-06-13 22:56:24 +12:00
2022-07-18 19:21:18 +12:00
if ( ! empty ( $projectId ) && $projectId !== 'console' ) {
$metricUpdated = $metric ;
if ( ! empty ( $groupBy )) {
foreach ( $options [ 'groupBy' ] as $groupBy ) {
$groupedBy = $point [ $groupBy ] ? ? '' ;
if ( empty ( $groupedBy )) {
continue ;
}
$metricUpdated = str_replace ( $groupBy , $groupedBy , $metricUpdated );
2022-06-22 22:51:49 +12:00
}
2022-06-13 22:56:24 +12:00
}
2022-07-18 19:21:18 +12:00
$value = ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ;
2022-10-31 20:44:44 +13:00
if ( empty ( $point [ 'projectInternalId' ] ? ? null )) {
2022-11-15 22:33:07 +13:00
continue ;
2022-10-31 20:27:51 +13:00
}
2022-07-18 19:21:18 +12:00
$this -> createOrUpdateMetric (
2022-10-09 19:45:46 +13:00
$point [ 'projectInternalId' ],
2022-08-26 10:10:52 +12:00
$point [ 'time' ],
2022-07-18 19:21:18 +12:00
$period [ 'key' ],
$metricUpdated ,
$value ,
0
);
2022-09-09 19:52:57 +12:00
$this -> latestTime [ $metric ][ $period [ 'key' ]] = $point [ 'time' ];
2022-07-18 19:21:18 +12:00
}
}
} catch ( \Exception $e ) { // if projects are deleted this might fail
if ( is_callable ( $this -> errorHandler )) {
call_user_func ( $this -> errorHandler , $e , " sync_metric_ { $metric } _influxdb " );
} else {
throw $e ;
2022-06-13 22:56:24 +12:00
}
}
}
2022-06-13 23:11:26 +12:00
/**
* Collect Stats
* Collect all the stats from Influd DB to Database
2022-06-14 03:49:27 +12:00
*
2022-06-13 23:11:26 +12:00
* @ return void
*/
public function collect () : void
{
2022-09-23 17:51:26 +12:00
foreach ( $this -> periods as $period ) {
foreach ( $this -> metrics as $metric => $options ) { //for each metrics
try {
$this -> syncFromInfluxDB ( $metric , $options , $period );
} catch ( \Exception $e ) {
if ( is_callable ( $this -> errorHandler )) {
call_user_func ( $this -> errorHandler , $e );
} else {
throw $e ;
}
2022-09-22 19:34:11 +12:00
}
}
}
2022-06-13 23:11:26 +12:00
}
2022-08-10 14:33:46 +12:00
}