From 7daa9b4d371158680e2b9ba913d1ccbb95c343fa Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 13 Dec 2023 15:40:07 +0200 Subject: [PATCH] infinity daily metric --- .env | 2 +- docker-compose.yml | 1 + src/Appwrite/Platform/Workers/Usage.php | 3 ++- src/Appwrite/Platform/Workers/UsageHook.php | 27 ++++++++++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 39cf1e1f3e..d562a82fb8 100644 --- a/.env +++ b/.env @@ -75,7 +75,7 @@ _APP_MAINTENANCE_RETENTION_CACHE=2592000 _APP_MAINTENANCE_RETENTION_EXECUTION=1209600 _APP_MAINTENANCE_RETENTION_ABUSE=86400 _APP_MAINTENANCE_RETENTION_AUDIT=1209600 -_APP_USAGE_AGGREGATION_INTERVAL=5 +_APP_USAGE_AGGREGATION_INTERVAL=50000 _APP_MAINTENANCE_RETENTION_USAGE_HOURLY=8640000 _APP_MAINTENANCE_RETENTION_SCHEDULES=86400 _APP_USAGE_STATS=enabled diff --git a/docker-compose.yml b/docker-compose.yml index 4820df5b51..3eb41ae2ea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -673,6 +673,7 @@ services: - _APP_USAGE_STATS - _APP_LOGGING_PROVIDER - _APP_LOGGING_CONFIG + - _APP_USAGE_AGGREGATION_INTERVAL appwrite-schedule: entrypoint: schedule diff --git a/src/Appwrite/Platform/Workers/Usage.php b/src/Appwrite/Platform/Workers/Usage.php index 6d0a8e10b6..b3f0516afe 100644 --- a/src/Appwrite/Platform/Workers/Usage.php +++ b/src/Appwrite/Platform/Workers/Usage.php @@ -15,7 +15,8 @@ class Usage extends Action protected array $periods = [ '1h' => 'Y-m-d H:00', '1d' => 'Y-m-d 00:00', - 'inf' => '0000-00-00 00:00' + 'inf' => '0000-00-00 00:00', + 'inf_d' => 'Y-m-d 00:00', ]; protected const INFINITY_PERIOD = '_inf_'; diff --git a/src/Appwrite/Platform/Workers/UsageHook.php b/src/Appwrite/Platform/Workers/UsageHook.php index e5a535d9a5..e99635c1b3 100644 --- a/src/Appwrite/Platform/Workers/UsageHook.php +++ b/src/Appwrite/Platform/Workers/UsageHook.php @@ -39,7 +39,8 @@ class UsageHook extends Usage */ public function action($register, $cache, $pools): void { - Timer::tick(30000, function () use ($register, $cache, $pools) { + $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '50000'); + Timer::tick($interval, function () use ($register, $cache, $pools) { $offset = count(self::$stats); $projects = array_slice(self::$stats, 0, $offset, true); @@ -66,6 +67,30 @@ class UsageHook extends Usage $time = 'inf' === $period ? null : date($format, time()); $id = \md5("{$time}_{$period}_{$key}"); + /** + * Infinity daily + */ + if ('inf_d' === $period) { + $infinity = $dbForProject->getDocument('stats', \md5("{self::INFINITY_PERIOD}{$key}")); + $infinityDaily = $dbForProject->getDocument('stats', $id); + + if (empty($infinityDaily) && $infinityDaily->isEmpty()) { + $dbForProject->createDocument('stats', new Document([ + '$id' => $id, + 'period' => $period, + 'time' => $time, + 'metric' => $key, + 'value' => $infinity['value'], + 'region' => App::getEnv('_APP_REGION', 'default'), + ])); + } else { + $infinityDaily->setAttribute('value', $infinity['value']); + $dbForProject->updateDocument('stats', $infinityDaily->getId(), $infinityDaily); + } + continue; + } + + try { $dbForProject->createDocument('stats', new Document([ '$id' => $id,