1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

feat: store token in env var

This commit is contained in:
Christy Jacob 2023-03-17 21:37:29 +05:30
parent 007d47e586
commit 42ebaf6541
5 changed files with 142 additions and 157 deletions

1
.env
View file

@ -47,6 +47,7 @@ _APP_SMTP_USERNAME=
_APP_SMTP_PASSWORD=
_APP_HAMSTER_RECIPIENTS=
_APP_HAMSTER_INTERVAL=86400
_APP_MIXPANEL_TOKEN=bce512333a58ec62f44541328607f53c
_APP_SMS_PROVIDER=sms://username:password@mock
_APP_SMS_FROM=+123456789
_APP_STORAGE_LIMIT=30000000

View file

@ -44,10 +44,10 @@
"appwrite/php-clamav": "1.1.*",
"appwrite/php-runtimes": "0.11.*",
"utopia-php/abuse": "0.16.*",
"utopia-php/analytics": "0.10.0",
"utopia-php/analytics": "0.10.1",
"utopia-php/audit": "0.17.*",
"utopia-php/cache": "0.8.*",
"utopia-php/cli": "0.14.*",
"utopia-php/cli": "0.15.*",
"utopia-php/config": "0.2.*",
"utopia-php/database": "0.28.*",
"utopia-php/queue": "0.5.*",
@ -56,7 +56,7 @@
"utopia-php/pools": "0.4.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/domains": "1.1.*",
"utopia-php/framework": "0.25.*",
"utopia-php/framework": "0.26.*",
"utopia-php/image": "0.5.*",
"utopia-php/dsn": "0.1.*",
"utopia-php/locale": "0.4.*",

99
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f7dec197f1580c0b63141d397d1ffeb4",
"content-hash": "8ef68cd9203018cf26fa24bd522e1d1a",
"packages": [
{
"name": "adhocore/jwt",
@ -1670,21 +1670,21 @@
},
{
"name": "utopia-php/analytics",
"version": "0.10.0",
"version": "0.10.1",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/analytics.git",
"reference": "c367bbc87e88779a33decb37483fef8172a0fda4"
"reference": "70ada5e6b192ae27e6d5467899a4cdd886003835"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/analytics/zipball/c367bbc87e88779a33decb37483fef8172a0fda4",
"reference": "c367bbc87e88779a33decb37483fef8172a0fda4",
"url": "https://api.github.com/repos/utopia-php/analytics/zipball/70ada5e6b192ae27e6d5467899a4cdd886003835",
"reference": "70ada5e6b192ae27e6d5467899a4cdd886003835",
"shasum": ""
},
"require": {
"php": ">=8.0",
"utopia-php/cli": "^0.14.0"
"utopia-php/cli": "^0.15.0"
},
"require-dev": {
"laravel/pint": "dev-main",
@ -1710,9 +1710,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/analytics/issues",
"source": "https://github.com/utopia-php/analytics/tree/0.10.0"
"source": "https://github.com/utopia-php/analytics/tree/0.10.1"
},
"time": "2023-03-16T20:42:19+00:00"
"time": "2023-03-17T14:42:35+00:00"
},
{
"name": "utopia-php/audit",
@ -1812,16 +1812,16 @@
},
{
"name": "utopia-php/cli",
"version": "0.14.0",
"version": "0.15.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/cli.git",
"reference": "c30ef985a4e739758a0d95eb0706b357b6d8c086"
"reference": "ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/cli/zipball/c30ef985a4e739758a0d95eb0706b357b6d8c086",
"reference": "c30ef985a4e739758a0d95eb0706b357b6d8c086",
"url": "https://api.github.com/repos/utopia-php/cli/zipball/ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea",
"reference": "ccb7c8125ffe0254fef8f25744bfa376eb7bd0ea",
"shasum": ""
},
"require": {
@ -1829,8 +1829,10 @@
"utopia-php/framework": "0.*.*"
},
"require-dev": {
"laravel/pint": "1.2.*",
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.6"
"squizlabs/php_codesniffer": "^3.6",
"vimeo/psalm": "4.0.1"
},
"type": "library",
"autoload": {
@ -1842,12 +1844,6 @@
"license": [
"MIT"
],
"authors": [
{
"name": "Eldad Fux",
"email": "eldad@appwrite.io"
}
],
"description": "A simple CLI library to manage command line applications",
"keywords": [
"cli",
@ -1859,9 +1855,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/cli/issues",
"source": "https://github.com/utopia-php/cli/tree/0.14.0"
"source": "https://github.com/utopia-php/cli/tree/0.15.0"
},
"time": "2022-10-09T10:19:07+00:00"
"time": "2023-03-01T05:55:14+00:00"
},
{
"name": "utopia-php/config",
@ -2071,16 +2067,16 @@
},
{
"name": "utopia-php/framework",
"version": "0.25.1",
"version": "0.26.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/framework.git",
"reference": "2391b397135586b2100d39e338827bef8d2f4ad0"
"reference": "e8da5576370366d3bf9c574ec855f8c96fe4f34e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/framework/zipball/2391b397135586b2100d39e338827bef8d2f4ad0",
"reference": "2391b397135586b2100d39e338827bef8d2f4ad0",
"url": "https://api.github.com/repos/utopia-php/framework/zipball/e8da5576370366d3bf9c574ec855f8c96fe4f34e",
"reference": "e8da5576370366d3bf9c574ec855f8c96fe4f34e",
"shasum": ""
},
"require": {
@ -2109,9 +2105,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/framework/issues",
"source": "https://github.com/utopia-php/framework/tree/0.25.1"
"source": "https://github.com/utopia-php/framework/tree/0.26.0"
},
"time": "2022-11-23T18:22:23+00:00"
"time": "2023-01-13T08:14:43+00:00"
},
{
"name": "utopia-php/image",
@ -2319,23 +2315,24 @@
},
{
"name": "utopia-php/orchestration",
"version": "0.9.0",
"version": "0.9.1",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/orchestration.git",
"reference": "1d4f66684b8c4927f31b695817eae6d84aafd172"
"reference": "55f43513b3f940a3f4f9c2cde7682d0c2581beb0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/orchestration/zipball/1d4f66684b8c4927f31b695817eae6d84aafd172",
"reference": "1d4f66684b8c4927f31b695817eae6d84aafd172",
"url": "https://api.github.com/repos/utopia-php/orchestration/zipball/55f43513b3f940a3f4f9c2cde7682d0c2581beb0",
"reference": "55f43513b3f940a3f4f9c2cde7682d0c2581beb0",
"shasum": ""
},
"require": {
"php": ">=8.0",
"utopia-php/cli": "0.14.*"
"utopia-php/cli": "0.15.*"
},
"require-dev": {
"laravel/pint": "^1.2",
"phpunit/phpunit": "^9.3",
"vimeo/psalm": "4.0.1"
},
@ -2362,34 +2359,34 @@
],
"support": {
"issues": "https://github.com/utopia-php/orchestration/issues",
"source": "https://github.com/utopia-php/orchestration/tree/0.9.0"
"source": "https://github.com/utopia-php/orchestration/tree/0.9.1"
},
"time": "2022-11-09T17:38:00+00:00"
"time": "2023-03-17T15:05:06+00:00"
},
{
"name": "utopia-php/platform",
"version": "0.3.1",
"version": "0.3.3",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/platform.git",
"reference": "fe9f64420957dc8fb6201d22b499572f021411e4"
"reference": "a9e7a501f33e0da59779782359a747cb8d34cf6f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/platform/zipball/fe9f64420957dc8fb6201d22b499572f021411e4",
"reference": "fe9f64420957dc8fb6201d22b499572f021411e4",
"url": "https://api.github.com/repos/utopia-php/platform/zipball/a9e7a501f33e0da59779782359a747cb8d34cf6f",
"reference": "a9e7a501f33e0da59779782359a747cb8d34cf6f",
"shasum": ""
},
"require": {
"ext-json": "*",
"ext-redis": "*",
"php": ">=8.0",
"utopia-php/cli": "0.14.*",
"utopia-php/framework": "0.25.*"
"utopia-php/cli": "0.15.*",
"utopia-php/framework": "0.26.*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"squizlabs/php_codesniffer": "^3.6"
"laravel/pint": "1.2.*",
"phpunit/phpunit": "^9.3"
},
"type": "library",
"autoload": {
@ -2411,9 +2408,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/platform/issues",
"source": "https://github.com/utopia-php/platform/tree/0.3.1"
"source": "https://github.com/utopia-php/platform/tree/0.3.3"
},
"time": "2022-11-10T07:04:24+00:00"
"time": "2023-03-07T08:52:22+00:00"
},
{
"name": "utopia-php/pools",
@ -2521,21 +2518,21 @@
},
{
"name": "utopia-php/queue",
"version": "0.5.1",
"version": "0.5.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/queue.git",
"reference": "4b39e1f232b2e619b0d7fb4004f2356df334ddc1"
"reference": "310271c5cd477541208d7fa74a4dea64df8e04a0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/queue/zipball/4b39e1f232b2e619b0d7fb4004f2356df334ddc1",
"reference": "4b39e1f232b2e619b0d7fb4004f2356df334ddc1",
"url": "https://api.github.com/repos/utopia-php/queue/zipball/310271c5cd477541208d7fa74a4dea64df8e04a0",
"reference": "310271c5cd477541208d7fa74a4dea64df8e04a0",
"shasum": ""
},
"require": {
"php": ">=8.0",
"utopia-php/cli": "0.14.*",
"utopia-php/cli": "0.15.*",
"utopia-php/framework": "0.*.*"
},
"require-dev": {
@ -2576,9 +2573,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/queue/issues",
"source": "https://github.com/utopia-php/queue/tree/0.5.1"
"source": "https://github.com/utopia-php/queue/tree/0.5.2"
},
"time": "2022-11-16T19:47:26+00:00"
"time": "2023-03-07T08:54:10+00:00"
},
{
"name": "utopia-php/registry",

View file

@ -576,6 +576,7 @@ services:
- _APP_CONNECTIONS_CACHE
- _APP_HAMSTER_RECIPIENTS
- _APP_HAMSTER_INTERVAL
- _APP_MIXPANEL_TOKEN
appwrite-maintenance:
entrypoint: maintenance

View file

@ -13,53 +13,29 @@ use Utopia\Database\Validator\Authorization;
use League\Csv\Writer;
use PHPMailer\PHPMailer\PHPMailer;
use Utopia\Analytics\Adapter\Mixpanel;
use Utopia\Analytics\Event;
use Utopia\Database\Document;
use Utopia\Pools\Group;
use Utopia\Registry\Registry;
class Hamster extends Action
{
private array $columns = [
'Timestamp',
'Project ID',
'Project Name',
'Functions',
'Deployments',
'Members',
'Domains',
'Platforms - Web',
'Platforms - Android',
'Platforms - iOS',
'Platforms - Flutter',
'Files',
'Buckets',
'Databases',
'Documents',
'Collections',
'Storage',
'Requests',
'Bandwidth',
'Users',
'Sessions',
'Executions'
];
private array $usageStats = [
'Files' => 'files.$all.count.total',
'Buckets' => 'buckets.$all.count.total',
'Databases' => 'databases.$all.count.total',
'Documents' => 'documents.$all.count.total',
'Collections' => 'collections.$all.count.total',
'Storage' => 'project.$all.storage.size',
'Requests' => 'project.$all.network.requests',
'Bandwidth' => 'project.$all.network.bandwidth',
'Users' => 'users.$all.count.total',
'Sessions' => 'sessions.$all.requests.create',
'Executions' => 'executions.$all.compute.total',
'files' => 'files.$all.count.total',
'buckets' => 'buckets.$all.count.total',
'databases' => 'databases.$all.count.total',
'documents' => 'documents.$all.count.total',
'collections' => 'collections.$all.count.total',
'storage' => 'project.$all.storage.size',
'requests' => 'project.$all.network.requests',
'bandwidth' => 'project.$all.network.bandwidth',
'users' => 'users.$all.count.total',
'sessions' => 'sessions.email.requests.create',
'executions' => 'executions.$all.compute.total',
];
protected string $directory = '/usr/local';
protected string $path;
protected string $date;
@ -73,7 +49,7 @@ class Hamster extends Action
public function __construct()
{
$this->mixpanel = new Mixpanel('bce512333a58ec62f44541328607f53c');
$this->mixpanel = new Mixpanel(App::getEnv('_APP_MIXPANEL_TOKEN', ''));
$this
->desc('Get stats for projects')
@ -90,54 +66,70 @@ class Hamster extends Action
{
$stats = [];
/** Set the timestamp in ISO 8601 format */
$stats['Timestamp'] = \date('c');
$stats['time'] = microtime(true);
/** Get Project ID */
$stats['Project ID'] = $project->getId();
$stats['projectId'] = $project->getId();
/** Get Project Name */
$stats['Project Name'] = $project->getAttribute('name');
$stats['projectName'] = $project->getAttribute('name');
/** Get Total Functions */
$stats['Functions'] = $dbForProject->count('functions', [], APP_LIMIT_COUNT);
$stats['functions'] = $dbForProject->count('functions', [], APP_LIMIT_COUNT);
/** Get Total Deployments */
$stats['Deployments'] = $dbForProject->count('deployments', [], APP_LIMIT_COUNT);
$stats['deployments'] = $dbForProject->count('deployments', [], APP_LIMIT_COUNT);
/** Get Total Members */
$teamInternalId = $project->getAttribute('teamInternalId', null);
if ($teamInternalId) {
$stats['Members'] = $dbForConsole->count('memberships', [
$stats['members'] = $dbForConsole->count('memberships', [
Query::equal('teamInternalId', [$teamInternalId])
], APP_LIMIT_COUNT);
} else {
$stats['Members'] = 0;
$stats['members'] = 0;
}
/** Get Email and Name of the project owner */
if ($teamInternalId) {
$membership = $dbForConsole->findOne('memberships', [
Query::equal('teamInternalId', [$teamInternalId]),
]);
$userInternalId = $membership->getAttribute('userInternalId', null);
if ($userInternalId) {
$user = $dbForConsole->findOne('users', [
Query::equal('_id', [$userInternalId]),
]);
$stats['email'] = $user->getAttribute('email', null);
$stats['name'] = $user->getAttribute('name', null);
}
}
/** Get Domains */
$stats['Domains'] = $dbForProject->count('domains', [], APP_LIMIT_COUNT);
$stats['domains'] = $dbForProject->count('domains', [], APP_LIMIT_COUNT);
/** Get Platforms */
$platforms = $dbForConsole->find('platforms', [
Query::equal('projectInternalId', [$project->getInternalId()]),
Query::limit(100)
Query::limit(APP_LIMIT_COUNT)
]);
$stats['Platforms - Web'] = array_count_values(array_filter($platforms, function ($platform) {
return $platform['platform'] === 'web';
$stats['platforms_web'] = sizeof(array_filter($platforms, function ($platform) {
return $platform['type'] === 'web';
}));
$stats['Platforms - Android'] = array_count_values(array_filter($platforms, function ($platform) {
return $platform['platform'] === 'android';
$stats['platforms_android'] = sizeof(array_filter($platforms, function ($platform) {
return $platform['type'] === 'android';
}));
$stats['Platforms - iOS'] = array_count_values(array_filter($platforms, function ($platform) {
return str_contains($platform['platform'], 'apple');
$stats['platforms_iOS'] = sizeof(array_filter($platforms, function ($platform) {
return str_contains($platform['type'], 'apple');
}));
$stats['Platforms - Flutter'] = array_count_values(array_filter($platforms, function ($platform) {
return str_contains($platform['platform'],'flutter');
$stats['platforms_flutter'] = sizeof(array_filter($platforms, function ($platform) {
return str_contains($platform['type'],'flutter');
}));
/** Get Usage stats */
@ -183,26 +175,36 @@ class Hamster extends Action
{
Console::title('Cloud Hamster V1');
Console::success(APP_NAME . ' cloud hamster process v1 has started');
Console::success(APP_NAME . ' cloud hamster process has started');
$interval = (int) App::getEnv('_APP_HAMSTER_INTERVAL', '30'); // 30 seconds (by default)
$sleep = (int) App::getEnv('_APP_HAMSTER_INTERVAL', '30'); // 30 seconds (by default)
Console::loop(function () use ($register, $pools, $cache, $dbForConsole, $interval) {
$jobInitTime = '22:00'; // (hour:minutes)
$now = new \DateTime();
$now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$next = new \DateTime($now->format("Y-m-d $jobInitTime"));
$next->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$delay = $next->getTimestamp() - $now->getTimestamp();
/**
* If time passed for the target day.
*/
if ($delay <= 0) {
$next->add(\DateInterval::createFromDateString('1 days'));
$delay = $next->getTimestamp() - $now->getTimestamp();
}
Console::log('[' . $now->format("Y-m-d H:i:s.v") . '] Delaying for ' . $delay . ' setting loop to [' . $next->format("Y-m-d H:i:s.v") . ']');
Console::loop(function () use ($register, $pools, $cache, $dbForConsole, $sleep) {
$now = date('d-m-Y H:i:s', time());
Console::info("[{$now}] Getting Cloud Usage Stats every {$interval} seconds");
Console::info("[{$now}] Getting Cloud Usage Stats every {$sleep} seconds");
$loopStart = microtime(true);
/* Initialise new Utopia app */
$app = new App('UTC');
$console = $app->getResource('console');
/** CSV stuff */
$this->date = date('Y-m-d');
$this->path = "{$this->directory}/stats_{$this->date}.csv";
$csv = Writer::createFromPath($this->path, 'w');
$csv->insertOne($this->columns);
/** Database connections */
$totalProjects = $dbForConsole->count('projects') + 1;
Console::success("Found a total of: {$totalProjects} projects");
@ -235,7 +237,26 @@ class Hamster extends Action
$dbForProject->setNamespace('_' . $project->getInternalId());
$statsPerProject = $this->getStats($dbForConsole, $dbForProject, $project);
$csv->insertOne(array_values($statsPerProject));
/** Send data to mixpanel */
$res = $this->mixpanel->createProfile($statsPerProject['email'], '', [
'name' => $statsPerProject['name'],
'email' => $statsPerProject['email']
]);
if (!$res) {
Console::error('Failed to create user profile for project: ' . $project->getId());
}
$event = new Event();
$event
->setName('Appwrite Cloud Project Stats')
->setProps($statsPerProject);
$res = $this->mixpanel->createEvent($event);
if (!$res) {
Console::error('Failed to create event for project: ' . $project->getId());
}
} catch (\Throwable $th) {
throw $th;
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
@ -259,8 +280,6 @@ class Hamster extends Action
Console::log('Iterated through ' . $count . '/' . $totalProjects . ' projects...');
}
// var_dump($csv->toString());
// $this->sendEmail($register);
$pools
->get('console')
@ -269,40 +288,7 @@ class Hamster extends Action
$loopTook = microtime(true) - $loopStart;
$now = date('d-m-Y H:i:s', time());
Console::info("[{$now}] Cloud Stats took {$loopTook} seconds");
}, $interval);
}
private function sendEmail(Registry $register)
{
/** @var \PHPMailer\PHPMailer\PHPMailer $mail */
$mail = $register->get('smtp');
$mail->clearAddresses();
$mail->clearAllRecipients();
$mail->clearReplyTos();
$mail->clearAttachments();
$mail->clearBCCs();
$mail->clearCCs();
try {
/** Addresses */
$mail->setFrom(App::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM), 'Appwrite Cloud Hamster');
$recipients = explode(',', App::getEnv('_APP_HAMSTER_RECIPIENTS', ''));
foreach ($recipients as $recipient) {
$mail->addAddress($recipient);
}
/** Attachments */
$mail->addAttachment($this->path);
/** Content */
$mail->Subject = "Cloud Report for {$this->date}";
$mail->Body = "Please find the daily cloud report atttached";
$mail->send();
Console::success('Email has been sent!');
} catch (Exception $e) {
Console::error("Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
}
}, $sleep, $delay);
}
}