chore: refactor code
This commit is contained in:
parent
41ae96ea77
commit
1307f73c31
2 changed files with 28 additions and 49 deletions
|
@ -512,19 +512,19 @@ App::get('/v1/functions/:functionId/usage')
|
||||||
'1d' => 'Y-m-d\T00:00:00.000P',
|
'1d' => 'Y-m-d\T00:00:00.000P',
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach ($metrics as $metric) {
|
foreach ($metrics as $metric) {
|
||||||
$usage[$metric]['total'] = $stats[$metric]['total'];
|
$usage[$metric]['total'] = $stats[$metric]['total'];
|
||||||
$usage[$metric]['data'] = [];
|
$usage[$metric]['data'] = [];
|
||||||
$leap = time() - ($days['limit'] * $days['factor']);
|
$leap = time() - ($days['limit'] * $days['factor']);
|
||||||
while ($leap < time()) {
|
while ($leap < time()) {
|
||||||
$leap += $days['factor'];
|
$leap += $days['factor'];
|
||||||
$formatDate = date($format, $leap);
|
$formatDate = date($format, $leap);
|
||||||
$usage[$metric]['data'][] = [
|
$usage[$metric]['data'][] = [
|
||||||
'value' => $stats[$metric]['data'][$formatDate]['value'] ?? 0,
|
'value' => $stats[$metric]['data'][$formatDate]['value'] ?? 0,
|
||||||
'date' => $formatDate,
|
'date' => $formatDate,
|
||||||
];
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'range' => $range,
|
'range' => $range,
|
||||||
|
@ -604,19 +604,19 @@ App::get('/v1/functions/usage')
|
||||||
'1d' => 'Y-m-d\T00:00:00.000P',
|
'1d' => 'Y-m-d\T00:00:00.000P',
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach ($metrics as $metric) {
|
foreach ($metrics as $metric) {
|
||||||
$usage[$metric]['total'] = $stats[$metric]['total'];
|
$usage[$metric]['total'] = $stats[$metric]['total'];
|
||||||
$usage[$metric]['data'] = [];
|
$usage[$metric]['data'] = [];
|
||||||
$leap = time() - ($days['limit'] * $days['factor']);
|
$leap = time() - ($days['limit'] * $days['factor']);
|
||||||
while ($leap < time()) {
|
while ($leap < time()) {
|
||||||
$leap += $days['factor'];
|
$leap += $days['factor'];
|
||||||
$formatDate = date($format, $leap);
|
$formatDate = date($format, $leap);
|
||||||
$usage[$metric]['data'][] = [
|
$usage[$metric]['data'][] = [
|
||||||
'value' => $stats[$metric]['data'][$formatDate]['value'] ?? 0,
|
'value' => $stats[$metric]['data'][$formatDate]['value'] ?? 0,
|
||||||
'date' => $formatDate,
|
'date' => $formatDate,
|
||||||
];
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'range' => $range,
|
'range' => $range,
|
||||||
'functionsTotal' => $usage[$metrics[0]]['total'],
|
'functionsTotal' => $usage[$metrics[0]]['total'],
|
||||||
|
@ -1721,13 +1721,6 @@ App::post('/v1/functions/:functionId/executions')
|
||||||
$execution->setAttribute('logs', $executionResponse['logs']);
|
$execution->setAttribute('logs', $executionResponse['logs']);
|
||||||
$execution->setAttribute('errors', $executionResponse['errors']);
|
$execution->setAttribute('errors', $executionResponse['errors']);
|
||||||
$execution->setAttribute('duration', $executionResponse['duration']);
|
$execution->setAttribute('duration', $executionResponse['duration']);
|
||||||
|
|
||||||
$queueForUsage
|
|
||||||
->addMetric(METRIC_EXECUTIONS, 1)
|
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS), 1)
|
|
||||||
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000))// per project
|
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000))// per function
|
|
||||||
;
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
$durationEnd = \microtime(true);
|
$durationEnd = \microtime(true);
|
||||||
|
|
||||||
|
@ -1737,13 +1730,12 @@ App::post('/v1/functions/:functionId/executions')
|
||||||
->setAttribute('responseStatusCode', 500)
|
->setAttribute('responseStatusCode', 500)
|
||||||
->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode());
|
->setAttribute('errors', $th->getMessage() . '\nError Code: ' . $th->getCode());
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
|
} finally {
|
||||||
$queueForUsage
|
$queueForUsage
|
||||||
->addMetric(METRIC_EXECUTIONS, 1)
|
->addMetric(METRIC_EXECUTIONS, 1)
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS), 1)
|
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS), 1)
|
||||||
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000))// per project
|
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000)) // per project
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000))// per function
|
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000)) // per function
|
||||||
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1752,10 +1744,6 @@ App::post('/v1/functions/:functionId/executions')
|
||||||
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
|
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$roles = Authorization::getRoles();
|
$roles = Authorization::getRoles();
|
||||||
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
|
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
|
||||||
$isAppUser = Auth::isAppUser($roles);
|
$isAppUser = Auth::isAppUser($roles);
|
||||||
|
|
|
@ -412,16 +412,6 @@ class Functions extends Action
|
||||||
->setAttribute('logs', $executionResponse['logs'])
|
->setAttribute('logs', $executionResponse['logs'])
|
||||||
->setAttribute('errors', $executionResponse['errors'])
|
->setAttribute('errors', $executionResponse['errors'])
|
||||||
->setAttribute('duration', $executionResponse['duration']);
|
->setAttribute('duration', $executionResponse['duration']);
|
||||||
|
|
||||||
/** Trigger usage queue */
|
|
||||||
$queueForUsage
|
|
||||||
->setProject($project)
|
|
||||||
->addMetric(METRIC_EXECUTIONS, 1)
|
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS), 1)
|
|
||||||
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000))// per project
|
|
||||||
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000))
|
|
||||||
->trigger()
|
|
||||||
;
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
$durationEnd = \microtime(true);
|
$durationEnd = \microtime(true);
|
||||||
$execution
|
$execution
|
||||||
|
@ -432,6 +422,7 @@ class Functions extends Action
|
||||||
|
|
||||||
$error = $th->getMessage();
|
$error = $th->getMessage();
|
||||||
$errorCode = $th->getCode();
|
$errorCode = $th->getCode();
|
||||||
|
} finally {
|
||||||
/** Trigger usage queue */
|
/** Trigger usage queue */
|
||||||
$queueForUsage
|
$queueForUsage
|
||||||
->setProject($project)
|
->setProject($project)
|
||||||
|
|
Loading…
Reference in a new issue