diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index aac61d5a03..dcf75782d6 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -120,7 +120,7 @@ App::get('/v1/functions/:functionId/usage') ->label('sdk.namespace', 'functions') ->label('sdk.method', 'getUsage') ->param('functionId', '', function () { return new UID(); }, 'Function unique ID.') - ->param('range', 'last30', function () { return new WhiteList(['daily', 'monthly', 'last30', 'last90']); }, 'Date range.', true) + ->param('range', '30d', function () { return new WhiteList(['24h', '7d', '30d', '90d']); }, 'Date range.', true) ->action(function ($functionId, $range, $response, $project, $projectDB, $register) { /** @var Utopia\Response $response */ /** @var Appwrite\Database\Document $project */ @@ -135,36 +135,32 @@ App::get('/v1/functions/:functionId/usage') } $period = [ - 'daily' => [ - 'start' => DateTime::createFromFormat('U', \strtotime('today')), - 'end' => DateTime::createFromFormat('U', \strtotime('tomorrow')), - 'group' => '1m', + '24h' => [ + 'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')), + 'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')), + 'group' => '30m', ], - 'monthly' => [ - 'start' => DateTime::createFromFormat('U', \strtotime('midnight first day of this month')), - 'end' => DateTime::createFromFormat('U', \strtotime('midnight last day of this month')), + '7d' => [ + 'start' => DateTime::createFromFormat('U', \strtotime('-7 days')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - 'last30' => [ + '30d' => [ 'start' => DateTime::createFromFormat('U', \strtotime('-30 days')), - 'end' => DateTime::createFromFormat('U', \strtotime('tomorrow')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - 'last90' => [ + '90d' => [ 'start' => DateTime::createFromFormat('U', \strtotime('-90 days')), - 'end' => DateTime::createFromFormat('U', \strtotime('today')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - // 'yearly' => [ - // 'start' => DateTime::createFromFormat('U', strtotime('midnight first day of january')), - // 'end' => DateTime::createFromFormat('U', strtotime('midnight last day of december')), - // 'group' => '4w', - // ], ]; $client = $register->get('influxdb'); $executions = []; + $failures = []; $compute = []; if ($client) { @@ -183,6 +179,17 @@ App::get('/v1/functions/:functionId/usage') ]; } + // Failures + $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' AND "functionStatus"=\'failed\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); + $points = $result->getPoints(); + + foreach ($points as $point) { + $failures[] = [ + 'value' => (!empty($point['value'])) ? $point['value'] : 0, + 'date' => \strtotime($point['time']), + ]; + } + // Compute $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_time" WHERE time > \''.$start.'\' AND time < \''.$end.'\' AND "metric_type"=\'counter\' AND "project"=\''.$project->getId().'\' AND "functionId"=\''.$function->getId().'\' GROUP BY time('.$period[$range]['group'].') FILL(null)'); $points = $result->getPoints(); @@ -196,12 +203,19 @@ App::get('/v1/functions/:functionId/usage') } $response->json([ + 'range' => $range, 'executions' => [ 'data' => $executions, 'total' => \array_sum(\array_map(function ($item) { return $item['value']; }, $executions)), ], + 'failures' => [ + 'data' => $failures, + 'total' => \array_sum(\array_map(function ($item) { + return $item['value']; + }, $failures)), + ], 'compute' => [ 'data' => $compute, 'total' => \array_sum(\array_map(function ($item) { @@ -533,8 +547,8 @@ App::post('/v1/functions/:functionId/executions') ->label('sdk.method', 'createExecution') ->label('sdk.description', '/docs/references/functions/create-execution.md') ->param('functionId', '', function () { return new UID(); }, 'Function unique ID.') - ->param('async', 1, function () { return new Range(0, 1); }, 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true) - ->action(function ($functionId, $async, $response, $project, $projectDB) { + // ->param('async', 1, function () { return new Range(0, 1); }, 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true) + ->action(function ($functionId, /*$async,*/ $response, $project, $projectDB) { /** @var Utopia\Response $response */ /** @var Appwrite\Database\Document $project */ /** @var Appwrite\Database\Database $projectDB */ @@ -575,16 +589,14 @@ App::post('/v1/functions/:functionId/executions') throw new Exception('Failed saving execution to DB', 500); } - if((bool)$async) { - // Issue a TLS certificate when domain is verified - Resque::enqueue('v1-functions', 'FunctionsV1', [ - 'projectId' => $project->getId(), - 'functionId' => $function->getId(), - 'executionId' => $execution->getId(), - 'functionTag' => $tag->getId(), - 'functionTrigger' => 'http', - ]); - } + // Issue a TLS certificate when domain is verified + Resque::enqueue('v1-functions', 'FunctionsV1', [ + 'projectId' => $project->getId(), + 'functionId' => $function->getId(), + 'executionId' => $execution->getId(), + 'functionTag' => $tag->getId(), + 'functionTrigger' => 'http', + ]); $response ->setStatusCode(Response::STATUS_CODE_CREATED) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index ed46a867cd..8086563943 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -161,7 +161,7 @@ App::get('/v1/projects/:projectId/usage') ->label('sdk.namespace', 'projects') ->label('sdk.method', 'getUsage') ->param('projectId', '', function () { return new UID(); }, 'Project unique ID.') - ->param('range', 'last30', function () { return new WhiteList(['daily', 'monthly', 'last30', 'last90']); }, 'Date range.', true) + ->param('range', '30d', function () { return new WhiteList(['24h', '7d', '30d', '90d']); }, 'Date range.', true) ->action(function ($projectId, $range, $response, $consoleDB, $projectDB, $register) { /** @var Utopia\Response $response */ /** @var Appwrite\Database\Database $consoleDB */ @@ -175,31 +175,26 @@ App::get('/v1/projects/:projectId/usage') } $period = [ - 'daily' => [ - 'start' => DateTime::createFromFormat('U', \strtotime('today')), - 'end' => DateTime::createFromFormat('U', \strtotime('tomorrow')), - 'group' => '1m', + '24h' => [ + 'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')), + 'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')), + 'group' => '1h', ], - 'monthly' => [ - 'start' => DateTime::createFromFormat('U', \strtotime('midnight first day of this month')), - 'end' => DateTime::createFromFormat('U', \strtotime('midnight last day of this month')), + '7d' => [ + 'start' => DateTime::createFromFormat('U', \strtotime('-7 days')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - 'last30' => [ + '30d' => [ 'start' => DateTime::createFromFormat('U', \strtotime('-30 days')), - 'end' => DateTime::createFromFormat('U', \strtotime('tomorrow')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - 'last90' => [ + '90d' => [ 'start' => DateTime::createFromFormat('U', \strtotime('-90 days')), - 'end' => DateTime::createFromFormat('U', \strtotime('today')), + 'end' => DateTime::createFromFormat('U', \strtotime('now')), 'group' => '1d', ], - // 'yearly' => [ - // 'start' => DateTime::createFromFormat('U', strtotime('midnight first day of january')), - // 'end' => DateTime::createFromFormat('U', strtotime('midnight last day of december')), - // 'group' => '4w', - // ], ]; $client = $register->get('influxdb'); @@ -289,6 +284,7 @@ App::get('/v1/projects/:projectId/usage') $tasksTotal = \count($project->getAttribute('tasks', [])); $response->json([ + 'range' => $range, 'requests' => [ 'data' => $requests, 'total' => \array_sum(\array_map(function ($item) { diff --git a/app/init.php b/app/init.php index b9918574bc..bf390b9217 100644 --- a/app/init.php +++ b/app/init.php @@ -36,7 +36,7 @@ const APP_EMAIL_SECURITY = 'security@localhost.test'; // Default security email const APP_USERAGENT = APP_NAME.'-Server v%s. Please report abuse at %s'; const APP_MODE_ADMIN = 'admin'; const APP_PAGING_LIMIT = 12; -const APP_CACHE_BUSTER = 127; +const APP_CACHE_BUSTER = 128; const APP_VERSION_STABLE = '0.6.2'; const APP_STORAGE_UPLOADS = '/storage/uploads'; const APP_STORAGE_FUNCTIONS = '/storage/functions'; diff --git a/app/server.php b/app/server.php index 3b7513680d..1c93d9d216 100644 --- a/app/server.php +++ b/app/server.php @@ -50,7 +50,7 @@ $http->on('AfterReload', function($serv, $workerId) { }); $http->on('start', function (Server $http) use ($payloadSize) { - Console::success('Server started succefully (max payload is '.$payloadSize.' bytes)'); + Console::success('Server started succefully (max payload is '.number_format($payloadSize).' bytes)'); Console::info("Master pid {$http->master_pid}, manager pid {$http->manager_pid}"); diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 71826d6039..20f7c69e9e 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -241,28 +241,119 @@ $timeout = $this->getParam('timeout', 900); -
  • -

    Usage

    +
  • + +
    + +
    -
    90d + +
    + +
    + + + +
    + +
    + + + +

    Monitor

    + +
    -
    -
    -
    + data-param-function-id="{{router.params.id}}"> +
    +
    +
    + +
    + +
      +
    • Executions
    • +
    + +
    +
    +
    + +
    +
    +
    + +
      +
    • CPU Time (minutes)
    • +
    + +
    +
    +
    + +
    +
    +
    + +
      +
    • Errors
    • +
    + +
  • +
  • + +
    + +

    Logs

    getParam('timeout', 900); - - +
    - - + +