Skip to content

Commit

Permalink
some progress!
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsayo committed May 12, 2024
1 parent 758b3e0 commit 6efd21e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion web-portal/backend/src/usage/usage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class UsageService {
case '24h':
return '1h';
case '1h':
return '60s';
return '60';
case '7d':
return '1d';
case '30d':
Expand Down
24 changes: 22 additions & 2 deletions web-portal/backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ export class UserService {

const tenantId = await this.getTenantIdByEnterpriseId(enterpriseId);

return { id, active, createdAt, orgs, tenantId };

return { id, active, createdAt, orgs, tenantId, netBalance:0 };
}

const { id, active, createdAt, orgs } = existingUser;

const tenantId = await this.getTenantIdByEnterpriseId(orgs[0].enterpriseId);
return { id, active, createdAt, orgs, tenantId };

const netBalance = await this.getTenantBalance(tenantId);


return { id, active, createdAt, orgs, tenantId, netBalance };
}

async getTenantIdByEnterpriseId(enterpriseId: string) {
Expand All @@ -110,4 +115,19 @@ export class UserService {

return tenants[0].id;
}

async getTenantBalance(tenantId: string) {
const netBalance = await this.prisma.$queryRaw`
SELECT payment.balance - relay.usage as net FROM
(SELECT
COALESCE(SUM(case when "transactionType"='CREDIT' then amount else 0 end) -
SUM(case when "transactionType"='DEBIT' then amount else 0 end), 0)
AS balance FROM "PaymentLedger" WHERE "tenantId" = ${tenantId}) as payment,
(SELECT
COALESCE(SUM(case when "transactionType"='CREDIT' then amount else 0 end) -
SUM(case when "transactionType"='DEBIT' then amount else 0 end), 0)
AS usage FROM "RelayLedger" WHERE "tenantId" = ${tenantId}) as relay
`;

return netBalance
}
7 changes: 4 additions & 3 deletions web-portal/frontend/components/dashboard/insights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const NoRequests = () => {
😵
</Title>
<Title order={4} c="umbra.1">
No requests yet
No usage data yet
</Title>
</Stack>
);
Expand All @@ -124,6 +124,7 @@ export const UsageChart: React.FC<{
data: Array<{ time: string; requests: number }>;
totalRequests: number;
}> = ({ width = 600, data, totalRequests }) => {
console.log(data);
return (
<Card shadow="none" padding="lg" radius="md" bg="#fff" w={width}>
<Title order={3} fw={500}>
Expand Down Expand Up @@ -161,8 +162,8 @@ const Insights: React.FC = () => {
const { data: promUserData } = useTenantUsage(String(tenantId), timeOption);

const chartData = path?.startsWith("/apps/")
? promData?.data?.result[0].values
: promUserData?.data?.result[0].values;
? promData?.data?.result[0]?.values
: promUserData?.data?.result[0]?.values;

const readableChartData = _.map(chartData, ([timestamp, value]) => {
return {
Expand Down

0 comments on commit 6efd21e

Please sign in to comment.