8000 feat: usage breakdown by project by TorstenDittmann · Pull Request #7270 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: usage breakdown by project #7270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions app/controllers/api/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
METRIC_NETWORK_REQUESTS,
METRIC_NETWORK_INBOUND,
METRIC_NETWORK_OUTBOUND,
METRIC_USERS,
]
];

Expand All @@ -60,8 +61,8 @@
};

$limit = match ($period) {
'1h' => (new DateTime($endDate))->diff(new DateTime($startDate))->h,
'1d' => (new DateTime($endDate))->diff(new DateTime($startDate))->days
'1h' => (new DateTime($startDate))->diff(new DateTime($endDate))->days * 24,
'1d' => (new DateTime($startDate))->diff(new DateTime($endDate))->days
};

$format = match ($period) {
Expand Down Expand Up @@ -110,15 +111,50 @@
}
}

$executionsBreakdown = array_map(function ($function) use ($dbForProject) {
$id = $function->getId();
$name = $function->getAttribute('name');
$metric = str_replace('{bucketInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS);
$value = $dbForProject->findOne('stats', [
Query::equal('metric', [$metric]),
Query::equal('period', ['inf'])
]);

return [
'resourceId' => $id,
'name' => $name,
'value' => $value['value'] ?? 0,
];
}, $dbForProject->find('functions'));

$bucketsBreakdown = array_map(function ($bucket) use ($dbForProject) {
$id = $bucket->getId();
$name = $bucket->getAttribute('name');
$metric = str_replace('{bucketInternalId}', $bucket->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE);
$value = $dbForProject->findOne('stats', [
Query::equal('metric', [$metric]),
Query::equal('period', ['inf'])
]);

return [
'resourceId' => $id,
'name' => $name,
'value' => $value['value'] ?? 0,
];
}, $dbForProject->find('buckets'));

$response->dynamic(new Document([
'requests' => ($usage[METRIC_NETWORK_REQUESTS]),
'network' => ($usage[METRIC_NETWORK_INBOUND] + $usage[METRIC_NETWORK_OUTBOUND]),
'users' => ($usage[METRIC_USERS]),
'executionsTotal' => $total[METRIC_EXECUTIONS],
'documentsTotal' => $total[METRIC_DOCUMENTS],
'databasesTotal' => $total[METRIC_DATABASES],
'usersTotal' => $total[METRIC_USERS],
'bucketsTotal' => $total[METRIC_BUCKETS],
'filesStorageTotal' => $total[METRIC_FILES_STORAGE],
'executionsBreakdown' => $executionsBreakdown,
'bucketsBreakdown' => $bucketsBreakdown
]), Response::MODEL_USAGE_PROJECT);
});

Expand Down
6 changes: 3 additions & 3 deletions src/Appwrite/Utopia/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Appwrite\Utopia;

use Exception;
use Swoole\Http\Request as SwooleRequest;
use Utopia\Swoole\Response as SwooleResponse;
use Swoole\Http\Response as SwooleHTTPResponse;
use Utopia\Database\Document;
Expand Down Expand Up @@ -39,7 +38,6 @@
use Appwrite\Utopia\Response\Model\Country;
use Appwrite\Utopia\Response\Model\Currency;
use Appwrite\Utopia\Response\Model\Document as ModelDocument;
use Appwrite\Utopia\Response\Model\Domain;
use Appwrite\Utopia\Response\Model\Error;
use Appwrite\Utopia\Response\Model\ErrorDev;
use Appwrite\Utopia\Response\Model\Execution;
Expand All @@ -60,7 +58,6 @@
use Appwrite\Utopia\Response\Model\Log;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\Metric;
use Appwrite\Utopia\Response\Model\Permissions;
use Appwrite\Utopia\Response\Model\Phone;
use Appwrite\Utopia\Response\Model\Platform;
use Appwrite\Utopia\Response\Model\Project;
Expand All @@ -79,6 +76,7 @@
use Appwrite\Utopia\Response\Model\HealthVersion;
use Appwrite\Utopia\Response\Model\Installation;
use Appwrite\Utopia\Response\Model\LocaleCode;
use Appwrite\Utopia\Response\Model\MetricBreakdown;
use Appwrite\Utopia\Response\Model\Provider;
use Appwrite\Utopia\Response\Model\ProviderRepository;
use Appwrite\Utopia\Response\Model\Runtime;
Expand Down Expand Up @@ -113,6 +111,7 @@ class Response extends SwooleResponse
public const MODEL_ERROR = 'error';
public const MODEL_METRIC = 'metric';
public const MODEL_METRIC_LIST = 'metricList';
public const MODEL_METRIC_BREAKDOWN = 'metricBreakdown';
public const MODEL_ERROR_DEV = 'errorDev';
public const MODEL_BASE_LIST = 'baseList';
public const MODEL_USAGE_DATABASES = 'usageDatabases';
Expand Down Expand Up @@ -394,6 +393,7 @@ public function __construct(SwooleHTTPResponse $response)
->setModel(new HealthTime())
->setModel(new HealthVersion())
->setModel(new Metric())
->setModel(new MetricBreakdown())
->setModel(new UsageDatabases())
->setModel(new UsageDatabase())
->setModel(new UsageCollection())
Expand Down
52 changes: 52 additions & 0 deletions src/Appwrite/Utopia/Response/Model/MetricBreakdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Appwrite\Utopia\Response\Model;

use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;

class MetricBreakdown extends Model
{
public function __construct()
{
$this
->addRule('resourceId', [
'type' => self::TYPE_STRING,
'description' => 'Resource ID.',
'default' => '',
'example' => '5e5ea5c16897e',
])
->addRule('name', [
'type' => self::TYPE_STRING,
'description' => 'Resource name.',
'default' => '',
'example' => 'Documents',
])
->addRule('value', [
'type' => self::TYPE_INTEGER,
'description' => 'The value of this metric at the timestamp.',
'default' => 0,
'example' => 1,
]);
}

/**
* Get Name
*
* @return string
*/
public function getName(): string
{
return 'Metric Breakdown';
}

/**
* Get Collection
*
* @return string
*/
public function getType(): string
{
return Response::MODEL_METRIC_BREAKDOWN;
}
}
25 changes: 23 additions & 2 deletions src/Appwrite/Utopia/Response/Model/UsageProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,40 @@ public function __construct()
'example' => 0,
])
->addRule('requests', [
'type' => self::TYPE_INTEGER,
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated number of requests per period.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('network', [
'type' => self::TYPE_INTEGER,
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated number of consumed bandwidth per period.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('users', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated number of users per period.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('executionsBreakdown', [
'type' => Response::MODEL_METRIC_BREAKDOWN,
'description' => 'Aggregated breakdown in totals of executions by functions.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('bucketsBreakdown', [
'type' => Response::MODEL_METRIC_BREAKDOWN,
'description' => 'Aggregated breakdown in totals of usage by buckets.',
'default' => [],
'example' => [],
'array' => true
])
;
}

Expand Down
14 changes: 6 additions & 8 deletions tests/e2e/General/UsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ public function testUsersStats(array $data): array
);
$res = $res['body'];

$this->assertEquals('24h', $res['range']);
$this->assertEquals(9, count($res));
$this->assertEquals(24, count($res['requestsTotal']));
$this->assertEquals(24, count($res['usersTotal']));
$this->assertEquals($usersTotal, $res['usersTotal'][array_key_last($res['usersTotal'])]['value']);
$this->validateDates($res['usersTotal']);
$this->assertEquals($requestsTotal, $res['requestsTotal'][array_key_last($res['requestsTotal'])]['value']);
$this->validateDates($res['requestsTotal']);
$this->assertEquals(11, count($res));
$this->validateDates($res['network']);
$this->validateDates($res['requests']);
$this->validateDates($res['users']);
$this->assertArrayHasKey('executionsBreakdown', $res);
$this->assertArrayHasKey('bucketsBreakdown', $res);

$res = $this->client->call(
Client::METHOD_GET,
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/Services/Projects/ProjectsConsoleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,6 @@ public function testUpdateProjectServicesAll(): void

foreach ($response['body'] as $key => $value) {
if (\preg_match($pattern, $key)) {
\var_dump('Matched key: ' . $key);
$matches[$key] = $value;
}
}
Expand Down
0