From 48bf74b881d61ea25943f2fb44700e0bf3cee920 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 20 Jun 2023 13:24:26 +0300 Subject: [PATCH] users addition --- src/Appwrite/Platform/Tasks/CalcTierStats.php | 117 ++++++++++++------ 1 file changed, 77 insertions(+), 40 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/CalcTierStats.php b/src/Appwrite/Platform/Tasks/CalcTierStats.php index 4e525e709b..a8ae0dd903 100644 --- a/src/Appwrite/Platform/Tasks/CalcTierStats.php +++ b/src/Appwrite/Platform/Tasks/CalcTierStats.php @@ -23,6 +23,7 @@ class CalcTierStats extends Action 'Organization ID', 'Organization Members', 'Teams', + 'Users', 'Requests', 'Bandwidth', 'Domains', @@ -124,17 +125,6 @@ class CalcTierStats extends Action /** Get Project ID */ $stats['Project ID'] = $project->getId(); - ///** Get Project Name */ - //$stats['Project Name'] = $project->getAttribute('name'); - - /** Get Organization Name and Id */ - //$teamId = $project->getAttribute('teamId', null); - //$teamName = null; - //if ($teamId) { - //$team = $dbForConsole->getDocument('teams', $teamId); - //$teamName = $team->getAttribute('name'); - // } - $stats['Organization ID'] = $project->getAttribute('teamId', null); /** Get Total Members */ @@ -144,11 +134,22 @@ class CalcTierStats extends Action Query::equal('teamInternalId', [$teamInternalId]) ]); } else { - $stats['Users'] = 0; + $stats['Organization Members'] = 0; } /** Get Total internal Teams */ - $stats['Teams'] = $dbForProject->count('teams', []); + try { + $stats['Teams'] = $dbForProject->count('teams', []); + } catch (\Throwable) { + $stats['Teams'] = 0; + } + + /** Get Total users */ + try { + $stats['Users'] = $dbForProject->count('users', []); + } catch (\Throwable) { + $stats['Users'] = 0; + } /** Get Usage stats */ $range = '90d'; @@ -194,65 +195,101 @@ class CalcTierStats extends Action $stats[$metrics[$key]] = $value; } - /** Get Domains */ - $stats['Domains'] = $dbForConsole->count('domains', [ - Query::equal('projectInternalId', [$project->getInternalId()]), - ]); - + try { + /** Get Domains */ + $stats['Domains'] = $dbForConsole->count('domains', [ + Query::equal('projectInternalId', [$project->getInternalId()]), + ]); + } catch (\Throwable) { + $stats['Domains'] = 0; + } + try { /** Get Api keys */ - $stats['Api keys'] = $dbForConsole->count('keys', [ + $stats['Api keys'] = $dbForConsole->count('keys', [ Query::equal('projectInternalId', [$project->getInternalId()]), - ]); + ]); + } catch (\Throwable) { + $stats['Api keys'] = 0; + } + try { /** Get Webhooks */ - $stats['Webhooks'] = $dbForConsole->count('webhooks', [ + $stats['Webhooks'] = $dbForConsole->count('webhooks', [ Query::equal('projectInternalId', [$project->getInternalId()]), - ]); + ]); + } catch (\Throwable) { + $stats['Webhooks'] = 0; + } + try { /** Get Platforms */ - $stats['Platforms'] = $dbForConsole->count('platforms', [ + $stats['Platforms'] = $dbForConsole->count('platforms', [ Query::equal('projectInternalId', [$project->getInternalId()]), - ]); + ]); + } catch (\Throwable) { + $stats['Platforms'] = 0; + } /** Get Files & Buckets */ $filesCount = 0; $filesSum = 0; $maxFileSize = 0; - $buckets = $dbForProject->find('buckets', []); $counter = 0; - foreach ($buckets as $bucket) { - $file = $dbForProject->findOne('bucket_' . $bucket->getInternalId(), [Query::orderDesc('sizeOriginal'),]); - if (empty($file)) { - continue; + try { + $buckets = $dbForProject->find('buckets', []); + foreach ($buckets as $bucket) { + $file = $dbForProject->findOne('bucket_' . $bucket->getInternalId(), [Query::orderDesc('sizeOriginal'),]); + if (empty($file)) { + continue; + } + $filesSum += $dbForProject->sum('bucket_' . $bucket->getInternalId(), 'sizeOriginal', [], 0); + $filesCount += $dbForProject->count('bucket_' . $bucket->getInternalId(), []); + if ($file->getAttribute('sizeOriginal') > $maxFileSize) { + $maxFileSize = $file->getAttribute('sizeOriginal'); + } + $counter++; } - $filesSum += $dbForProject->sum('bucket_' . $bucket->getInternalId(), 'sizeOriginal', [], 0); - $filesCount += $dbForProject->count('bucket_' . $bucket->getInternalId(), []); - if ($file->getAttribute('sizeOriginal') > $maxFileSize) { - $maxFileSize = $file->getAttribute('sizeOriginal'); - } - $counter++; + } catch (\Throwable) { + ; } $stats['Buckets'] = $counter; $stats['Files'] = $filesCount; $stats['Storage (bytes)'] = $filesSum; $stats['Max File Size (bytes)'] = $maxFileSize; + + try { /** Get Total Functions */ - $stats['Databases'] = $dbForProject->count('databases', []); + $stats['Databases'] = $dbForProject->count('databases', []); + } catch (\Throwable) { + $stats['Databases'] = 0; + } /** Get Total Functions */ - $stats['Functions'] = $dbForProject->count('functions', []); + try { + $stats['Functions'] = $dbForProject->count('functions', []); + } catch (\Throwable) { + $stats['Functions'] = 0; + } /** Get Total Deployments */ - $stats['Deployments'] = $dbForProject->count('deployments', []); + try { + $stats['Deployments'] = $dbForProject->count('deployments', []); + } catch (\Throwable) { + $stats['Deployments'] = 0; + } /** Get Total Executions */ - $stats['Executions'] = $dbForProject->count('executions', []); + try { + $stats['Executions'] = $dbForProject->count('executions', []); + } catch (\Throwable) { + $stats['Executions'] = 0; + } $csv->insertOne(array_values($stats)); } catch (\Throwable $th) { - Console::error('Failed on project ("' . $project->getId() . '") version with error on line no: ' . $th->getline() . 'with message: ' . $th->getMessage()); + Console::error('Failed on project ("' . $project->getId() . '") version with error on File: ' . $th->getFile() . ' line no: ' . $th->getline() . ' with message: ' . $th->getMessage()); } finally { $pools ->get($db)