mirror of
https://github.com/appwrite/appwrite
synced 2026-05-22 00:18:25 +00:00
Merge pull request #10472 from appwrite/stats-resources-try-catch
Stats resources and usage sorting by unique field
This commit is contained in:
commit
30003caeb3
2 changed files with 99 additions and 5 deletions
|
|
@ -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 = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue