1
0
Fork 0
mirror of synced 2024-07-09 00:16:13 +12:00

Merge pull request #3718 from appwrite/fix-usage-refactor-permissions

This commit is contained in:
Damodar Lohani 2022-08-26 06:29:38 +05:45 committed by GitHub
commit 8e26f93211
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 13 deletions

View file

@ -1109,7 +1109,7 @@ App::get('/v1/users/usage')
]; ];
$metrics = [ $metrics = [
'users.$all.requests.count', 'users.$all.count.total',
'users.$all.requests.create', 'users.$all.requests.create',
'users.$all.requests.read', 'users.$all.requests.read',
'users.$all.requests.update', 'users.$all.requests.update',
@ -1161,7 +1161,7 @@ App::get('/v1/users/usage')
$usage = new Document([ $usage = new Document([
'range' => $range, 'range' => $range,
'usersCount' => $stats['users.$all.requests.count'] ?? [], 'usersCount' => $stats['users.$all.count.total'] ?? [],
'usersCreate' => $stats['users.$all.requests.create'] ?? [], 'usersCreate' => $stats['users.$all.requests.create'] ?? [],
'usersRead' => $stats['users.$all.requests.read'] ?? [], 'usersRead' => $stats['users.$all.requests.read'] ?? [],
'usersUpdate' => $stats['users.$all.requests.update'] ?? [], 'usersUpdate' => $stats['users.$all.requests.update'] ?? [],

View file

@ -298,7 +298,7 @@ class TimeSeries extends Calculator
* *
* @return void * @return void
*/ */
private function createOrUpdateMetric(string $projectId, int $time, string $period, string $metric, int $value, int $type): void private function createOrUpdateMetric(string $projectId, string $time, string $period, string $metric, int $value, int $type): void
{ {
$id = \md5("{$time}_{$period}_{$metric}"); $id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_console'); $this->database->setNamespace('_console');
@ -347,9 +347,9 @@ class TimeSeries extends Calculator
{ {
$start = DateTime::createFromFormat('U', \strtotime($period['startTime']))->format(DateTime::RFC3339); $start = DateTime::createFromFormat('U', \strtotime($period['startTime']))->format(DateTime::RFC3339);
if (!empty($this->latestTime[$metric][$period['key']])) { if (!empty($this->latestTime[$metric][$period['key']])) {
$start = DateTime::createFromFormat('U', $this->latestTime[$metric][$period['key']])->format(DateTime::RFC3339); $start = $this->latestTime[$metric][$period['key']];
} }
$end = DateTime::createFromFormat('U', \strtotime('now'))->format(DateTime::RFC3339); $end = (new DateTime())->format(DateTime::RFC3339);
$table = $options['table']; //Which influxdb table to query for this metric $table = $options['table']; //Which influxdb table to query for this metric
$groupBy = empty($options['groupBy']) ? '' : ', ' . implode(', ', array_map(fn($groupBy) => '"' . $groupBy . '" ', $options['groupBy'])); //Some sub level metrics may be grouped by other tags like collectionId, bucketId, etc $groupBy = empty($options['groupBy']) ? '' : ', ' . implode(', ', array_map(fn($groupBy) => '"' . $groupBy . '" ', $options['groupBy'])); //Some sub level metrics may be grouped by other tags like collectionId, bucketId, etc
@ -387,12 +387,11 @@ class TimeSeries extends Calculator
} }
} }
$time = DateTime::createFromFormat('U', \strtotime($point['time']))->format(DateTime::RFC3339);
$value = (!empty($point['value'])) ? $point['value'] : 0; $value = (!empty($point['value'])) ? $point['value'] : 0;
$this->createOrUpdateMetric( $this->createOrUpdateMetric(
$projectId, $projectId,
$time, $point['time'],
$period['key'], $period['key'],
$metricUpdated, $metricUpdated,
$value, $value,

View file

@ -8,6 +8,9 @@ use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer; use Tests\E2E\Scopes\SideServer;
use CURLFile; use CURLFile;
use Tests\E2E\Services\Functions\FunctionsBase; use Tests\E2E\Services\Functions\FunctionsBase;
use Utopia\Database\DateTime;
use Utopia\Database\Permission;
use Utopia\Database\Role;
class UsageTest extends Scope class UsageTest extends Scope
{ {
@ -291,9 +294,13 @@ class UsageTest extends Scope
$res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $headers, [ $res = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $headers, [
'collectionId' => 'unique()', 'collectionId' => 'unique()',
'name' => $name, 'name' => $name,
'permission' => 'collection', 'documentSecurity' => false,
'read' => ['role:all'], 'permissions' => [
'write' => ['role:all'] Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]); ]);
$this->assertEquals($name, $res['body']['name']); $this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']); $this->assertNotEmpty($res['body']['$id']);
@ -472,7 +479,7 @@ class UsageTest extends Scope
$this->assertEquals(202, $deployment['headers']['status-code']); $this->assertEquals(202, $deployment['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']); $this->assertNotEmpty($deployment['body']['$id']);
$this->assertIsInt($deployment['body']['$createdAt']); $this->assertEquals(true, DateTime::isValid($deployment['body']['$createdAt']));
$this->assertEquals('index.php', $deployment['body']['entrypoint']); $this->assertEquals('index.php', $deployment['body']['entrypoint']);
// Wait for deployment to build. // Wait for deployment to build.
@ -482,8 +489,8 @@ class UsageTest extends Scope
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$this->assertIsInt($response['body']['$createdAt']); $this->assertEquals(true, DateTime::isValid($response['body']['$createdAt']));
$this->assertIsInt($response['body']['$updatedAt']); $this->assertEquals(true, DateTime::isValid($response['body']['$updatedAt']));
$this->assertEquals($deploymentId, $response['body']['deployment']); $this->assertEquals($deploymentId, $response['body']['deployment']);
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', $headers, [ $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', $headers, [