1
0
Fork 0
mirror of synced 2024-07-05 06:31:08 +12:00

Hamster additions

Hamster additions
This commit is contained in:
Shimon Newman 2024-01-22 11:31:09 +02:00 committed by GitHub
commit e9a13fe93c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -218,6 +218,15 @@ class Hamster extends Action
} }
} }
/** Add billing information to the project */
$organization = $dbForConsole->findOne('teams', [
Query::equal('$internalId', [$teamInternalId])
]);
$billing = $this->getBillingDetails($organization);
$statsPerProject['billing_plan'] = $billing['billing_plan'] ?? null;
$statsPerProject['billing_start_date'] = $billing['billing_start_date'] ?? null;
/** Get Domains */ /** Get Domains */
$statsPerProject['custom_domains'] = $dbForConsole->count('rules', [ $statsPerProject['custom_domains'] = $dbForConsole->count('rules', [
Query::equal('projectInternalId', [$project->getInternalId()]), Query::equal('projectInternalId', [$project->getInternalId()]),
@ -300,7 +309,7 @@ class Hamster extends Action
}); });
/** /**
* Workaround to combine network.inbound+network.outbound as network. * Workaround to combine network.Inbound+network.outbound as bandwidth.
*/ */
$statsPerProject["usage_bandwidth_infinity"] = $statsPerProject["usage_inbound_infinity"] + $statsPerProject["usage_outbound_infinity"]; $statsPerProject["usage_bandwidth_infinity"] = $statsPerProject["usage_inbound_infinity"] + $statsPerProject["usage_outbound_infinity"];
$statsPerProject["usage_bandwidth_24h"] = $statsPerProject["usage_inbound_24h"] + $statsPerProject["usage_outbound_24h"]; $statsPerProject["usage_bandwidth_24h"] = $statsPerProject["usage_inbound_24h"] + $statsPerProject["usage_outbound_24h"];
@ -344,7 +353,6 @@ class Hamster extends Action
/** /**
* @param Document $organization * @param Document $organization
* @param Database $dbForConsole * @param Database $dbForConsole
* @throws \Utopia\Database\Exception
*/ */
private function getStatsForOrganization(Document $organization, Database $dbForConsole): void private function getStatsForOrganization(Document $organization, Database $dbForConsole): void
{ {
@ -358,22 +366,26 @@ class Hamster extends Action
/** Organization name */ /** Organization name */
$statsPerOrganization['name'] = $organization->getAttribute('name'); $statsPerOrganization['name'] = $organization->getAttribute('name');
/** Get Email and of the organization owner */ /** Get Email and of the organization owner */
$membership = $dbForConsole->findOne('memberships', [ $membership = $dbForConsole->findOne('memberships', [
Query::equal('teamInternalId', [$organization->getInternalId()]), Query::equal('teamInternalId', [$organization->getInternalId()]),
]); ]);
if (!$membership || $membership->isEmpty()) { if (!$membership || $membership->isEmpty()) {
throw new \Exception('Membership not found. Skipping organization : ' . $organization->getId()); throw new \Exception('Membership not found. Skipping organization : ' . $organization->getId());
} }
$userId = $membership->getAttribute('userId', null); $userId = $membership->getAttribute('userId', null);
if ($userId) { if ($userId) {
$user = $dbForConsole->getDocument('users', $userId); $user = $dbForConsole->getDocument('users', $userId);
$statsPerOrganization['email'] = $user->getAttribute('email', null); $statsPerOrganization['email'] = $user->getAttribute('email', null);
} }
/** Add billing information */
$billing = $this->getBillingDetails($organization);
$statsPerOrganization['billing_plan'] = $billing['billing_plan'] ?? null;
$statsPerOrganization['billing_start_date'] = $billing['billing_start_date'] ?? null;
$statsPerOrganization['marked_for_deletion'] = $billing['markedForDeletion'] ?? 0;
$statsPerOrganization['billing_plan_downgrade'] = $billing['billing_plan_downgrade'] ?? null;
/** Organization Creation Date */ /** Organization Creation Date */
$statsPerOrganization['created'] = $organization->getAttribute('$createdAt'); $statsPerOrganization['created'] = $organization->getAttribute('$createdAt');
@ -412,6 +424,15 @@ class Hamster extends Action
$statsPerUser['time'] = $user->getAttribute('$time'); $statsPerUser['time'] = $user->getAttribute('$time');
/** Add billing information */
$organization = $dbForConsole->findOne('teams', [
Query::equal('userInternalId', [$user->getInternalId()])
]);
$billing = $this->getBillingDetails($organization);
$statsPerUser['billing_plan'] = $billing['billing_plan'] ?? null;
$statsPerUser['billing_start_date'] = $billing['billing_start_date'] ?? null;
/** Organization name */ /** Organization name */
$statsPerUser['name'] = $user->getAttribute('name'); $statsPerUser['name'] = $user->getAttribute('name');
@ -446,4 +467,28 @@ class Hamster extends Action
Console::error($e->getMessage()); Console::error($e->getMessage());
} }
} }
private function getBillingDetails(Document $team): array
{
$billing = [];
if (!empty($team) && !$team->isEmpty()) {
$billingPlan = $team->getAttribute('billingPlan', null);
$billingPlanDowngrade = $team->getAttribute('billingPlanDowngrade', null);
if (!empty($billingPlan) && empty($billingPlanDowngrade)) {
$billing['billing_plan'] = $billingPlan;
}
if (in_array($billingPlan, ['tier-1', 'tier-2'])) {
$billingStartDate = $team->getAttribute('billingStartDate', null);
$billing['billing_start_date'] = $billingStartDate;
}
$billing['marked_for_deletion'] = $team->getAttribute('markedForDeletion', 0);
$billing['billing_plan_downgrade'] = $billingPlanDowngrade;
}
return $billing;
}
} }