Compare commits

...
Author SHA1 Message Date
shimon f6cae2df29 infinity daily metric 2023-12-13 19:04:17 +02:00
shimon 77c34e3283 infinity daily metric 2023-12-13 19:00:53 +02:00
shimon e9087dfec7 Merge branch 'refactor-usage-sn' of github.com:appwrite/appwrite into feat-new-usage-metric 2023-12-13 19:00:26 +02:00
shimon 7daa9b4d37 infinity daily metric 2023-12-13 15:40:07 +02:00
4 changed files with 38 additions and 3 deletions
+1 -1
View File
@@ -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
+1
View File
@@ -673,6 +673,7 @@ services:
- _APP_USAGE_STATS
- _APP_LOGGING_PROVIDER
- _APP_LOGGING_CONFIG
- _APP_USAGE_AGGREGATION_INTERVAL
appwrite-schedule:
entrypoint: schedule
+2 -1
View File
@@ -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',
'infd' => 'Y-m-d 00:00',
];
protected const INFINITY_PERIOD = '_inf_';
+34 -1
View File
@@ -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,38 @@ class UsageHook extends Usage
$time = 'inf' === $period ? null : date($format, time());
$id = \md5("{$time}_{$period}_{$key}");
/**
* Infinity daily
*/
if ('infd' === $period) {
$isInfDaily = array_reduce(["files", "buckets", "users"], function ($carry, $word) use ($key) {
return $carry || str_contains($key, $word);
}, false);
if (!$isInfDaily) {
continue;
}
$infinity = $dbForProject->getDocument('stats', \md5(self::INFINITY_PERIOD . $key));
$infinityDaily = $dbForProject->getDocument('stats', $id);
if ($infinityDaily->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => $period,
'time' => $time,
'metric' => $key,
'value' => $infinity['value'] ?? 0,
'region' => App::getEnv('_APP_REGION', 'default'),
]));
} else {
$infinityDaily->setAttribute('value', $infinity['value'] ?? 0);
$dbForProject->updateDocument('stats', $infinityDaily->getId(), $infinityDaily);
}
continue;
}
try {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,