mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
feat: project sms usage
This commit is contained in:
@@ -31,7 +31,8 @@ App::get('/v1/project/usage')
|
||||
->param('period', '1d', new WhiteList(['1h', '1d']), 'Period used', true)
|
||||
->inject('response')
|
||||
->inject('dbForProject')
|
||||
->action(function (string $startDate, string $endDate, string $period, Response $response, Database $dbForProject) {
|
||||
->inject('getSmsPrice')
|
||||
->action(function (string $startDate, string $endDate, string $period, Response $response, Database $dbForProject, callable $getSmsPrice) {
|
||||
$stats = $total = $usage = [];
|
||||
$format = 'Y-m-d 00:00:00';
|
||||
$firstDay = (new DateTime($startDate))->format($format);
|
||||
@@ -257,6 +258,46 @@ App::get('/v1/project/usage')
|
||||
];
|
||||
}, $dbForProject->find('functions'));
|
||||
|
||||
// This total is includes free and paid SMS usage
|
||||
$authPhoneTotal = Authorization::skip(fn () => $dbForProject->sum('stats', 'value', [
|
||||
Query::equal('metric', [METRIC_AUTH_METHOD_PHONE]),
|
||||
Query::equal('period', ['1d']),
|
||||
Query::greaterThanEqual('time', $firstDay),
|
||||
Query::lessThan('time', $lastDay),
|
||||
]));
|
||||
|
||||
// This estimate is only for paid SMS usage
|
||||
$authPhoneMetrics = Authorization::skip(fn () => $dbForProject->find('stats', [
|
||||
Query::startsWith('metric', METRIC_AUTH_METHOD_PHONE_COUNTRY_CODE . '.'),
|
||||
Query::equal('period', ['1d']),
|
||||
Query::greaterThanEqual('time', $firstDay),
|
||||
Query::lessThan('time', $lastDay),
|
||||
]));
|
||||
|
||||
$authPhoneEstimate = 0.0;
|
||||
$authPhoneCountryBreakdown = [];
|
||||
foreach ($authPhoneMetrics as $metric) {
|
||||
$parts = explode('.', $metric->getAttribute('metric'));
|
||||
$countryCode = $parts[3] ?? null;
|
||||
if ($countryCode === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $metric->getAttribute('value', 0);
|
||||
|
||||
if (is_callable($getSmsPrice)) {
|
||||
$authPhoneEstimate += $value * $getSmsPrice($countryCode);
|
||||
}
|
||||
|
||||
$authPhoneCountryBreakdown[] = [
|
||||
'name' => $countryCode,
|
||||
'value' => $value,
|
||||
'estimate' => is_callable($getSmsPrice)
|
||||
? $value * $getSmsPrice($countryCode)
|
||||
: 0.0,
|
||||
];
|
||||
}
|
||||
|
||||
// merge network inbound + outbound
|
||||
$projectBandwidth = [];
|
||||
foreach ($usage[METRIC_NETWORK_INBOUND] as $item) {
|
||||
@@ -303,6 +344,9 @@ App::get('/v1/project/usage')
|
||||
'executionsMbSecondsBreakdown' => $executionsMbSecondsBreakdown,
|
||||
'buildsMbSecondsBreakdown' => $buildsMbSecondsBreakdown,
|
||||
'functionsStorageBreakdown' => $functionsStorageBreakdown,
|
||||
'authPhoneTotal' => $authPhoneTotal,
|
||||
'authPhoneEstimate' => $authPhoneEstimate,
|
||||
'authPhoneCountryBreakdown' => $authPhoneCountryBreakdown,
|
||||
]), Response::MODEL_USAGE_PROJECT);
|
||||
});
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ class MetricBreakdown extends Model
|
||||
'description' => 'Resource ID.',
|
||||
'default' => '',
|
||||
'example' => '5e5ea5c16897e',
|
||||
'required' => false,
|
||||
])
|
||||
->addRule('name', [
|
||||
'type' => self::TYPE_STRING,
|
||||
@@ -27,6 +28,13 @@ class MetricBreakdown extends Model
|
||||
'description' => 'The value of this metric at the timestamp.',
|
||||
'default' => 0,
|
||||
'example' => 1,
|
||||
])
|
||||
->addRule('estimate', [
|
||||
'type' => self::TYPE_FLOAT,
|
||||
'description' => 'The estimated value of this metric at the end of the period.',
|
||||
'default' => 0,
|
||||
'example' => 1,
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -152,6 +152,25 @@ class UsageProject extends Model
|
||||
'example' => [],
|
||||
'array' => true
|
||||
])
|
||||
->addRule('authPhoneTotal', [
|
||||
'type' => self::TYPE_INTEGER,
|
||||
'description' => 'Total aggregated number of phone auth.',
|
||||
'default' => 0,
|
||||
'example' => 0,
|
||||
])
|
||||
->addRule('authPhoneEstimate', [
|
||||
'type' => self::TYPE_FLOAT,
|
||||
'description' => 'Estimated total aggregated cost of phone auth.',
|
||||
'default' => 0,
|
||||
'example' => 0,
|
||||
])
|
||||
->addRule('authPhoneCountryBreakdown', [
|
||||
'type' => Response::MODEL_METRIC_BREAKDOWN,
|
||||
'description' => 'Aggregated breakdown in totals of phone auth by country.',
|
||||
'default' => [],
|
||||
'example' => [],
|
||||
'array' => true
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user