Merge pull request #10472 from appwrite/stats-resources-try-catch

Stats resources and usage sorting by unique field
This commit is contained in:
Jake Barnby 2025-09-18 15:20:19 +12:00 committed by GitHub
commit 30003caeb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 99 additions and 5 deletions

View file

@ -434,18 +434,43 @@ class StatsResources extends Action
{
$message = 'Stats writeDocuments project: ' . $project->getId() . '(' . $project->getSequence() . ')';
/**
* sort by unique index key reduce locks/deadlocks
*/
usort($this->documents, function ($a, $b) {
// Metric DESC
$cmp = strcmp($b['metric'], $a['metric']);
if ($cmp !== 0) {
return $cmp;
}
// Period ASC
$cmp = strcmp($a['period'], $b['period']);
if ($cmp !== 0) {
return $cmp;
}
// Time ASC, NULLs first
if ($a['time'] === null) {
return ($b['time'] === null) ? 0 : -1;
}
if ($b['time'] === null) {
return 1;
}
return strcmp($a['time'], $b['time']);
});
try {
$dbForLogs->createOrUpdateDocuments(
'stats',
$this->documents
$this->documents,
);
Console::success($message . ' | Documents: ' . count($this->documents));
} catch (\Throwable $e) {
Console::error('Error: ' . $message . ' | Exception: ' . $e->getMessage());
throw $e;
} finally {
$this->documents = [];
}
}
}

View file

@ -424,12 +424,40 @@ class StatsUsage extends Action
try {
$dbForProject = $getProjectDB($projectStats['project']);
Console::log('Processing batch with ' . count($projectStats['stats']) . ' stats');
/**
* Sort by unique index key reduce locks/deadlocks
*/
usort($projectStats['stats'], function ($a, $b) {
// Metric DESC
$cmp = strcmp($b['metric'], $a['metric']);
if ($cmp !== 0) {
return $cmp;
}
// Period ASC
$cmp = strcmp($a['period'], $b['period']);
if ($cmp !== 0) {
return $cmp;
}
// Time ASC, NULLs first
if ($a['time'] === null) {
return ($b['time'] === null) ? 0 : -1;
}
if ($b['time'] === null) {
return 1;
}
return strcmp($a['time'], $b['time']);
});
$dbForProject->createOrUpdateDocumentsWithIncrease('stats', 'value', $projectStats['stats']);
Console::success('Batch successfully written to DB');
unset($this->projects[$sequence]);
} catch (Throwable $e) {
Console::error('Error processing stats: ' . $e->getMessage());
} finally {
unset($this->projects[$sequence]);
}
}
@ -468,12 +496,53 @@ class StatsUsage extends Action
try {
Console::log('Processing batch with ' . count($this->statDocuments) . ' stats');
/**
* Sort by UNIQUE KEY "_key_metric_period_time" ("_tenant","metric" DESC,"period","time")
* Here we sort by _tenant as well because of setTenantPerDocument
*/
usort($this->statDocuments, function ($a, $b) {
// Tenant ASC
$cmp = $a['$tenant'] <=> $b['$tenant'];
if ($cmp !== 0) {
return $cmp;
}
// Metric DESC
$cmp = strcmp($b['metric'], $a['metric']);
if ($cmp !== 0) {
return $cmp;
}
// Period ASC
$cmp = strcmp($a['period'], $b['period']);
if ($cmp !== 0) {
return $cmp;
}
// Time ASC, NULLs first
if ($a['time'] === null) {
return ($b['time'] === null) ? 0 : -1;
}
if ($b['time'] === null) {
return 1;
}
return strcmp($a['time'], $b['time']);
});
$dbForLogs->createOrUpdateDocumentsWithIncrease(
'stats',
'value',
$this->statDocuments
);
Console::success('Usage logs pushed to Logs DB');
/**
* todo: Do we need to unset $this->statDocuments?
*/
} catch (Throwable $th) {
Console::error($th->getMessage());
}