Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Show usage for the current month

Vasek Mlejnskyf9af6a867c8e
diff --git a/apps/docs/src/app/(dashboard)/dashboard/page.tsx b/apps/docs/src/app/(dashboard)/dashboard/page.tsx
index e07707fc8..dd6d949c7 100644
--- a/apps/docs/src/app/(dashboard)/dashboard/page.tsx
+++ b/apps/docs/src/app/(dashboard)/dashboard/page.tsx
@@ -1,7 +1,7 @@
'use client'
import { useEffect, useState } from 'react'
-import { BarChart, CreditCard, Key, LoaderIcon, LucideIcon, Settings, Users } from 'lucide-react'
+import { BarChart, CreditCard, Key, LucideIcon, Settings, Users } from 'lucide-react'
import { BillingContent } from '@/components/Dashboard/Billing'
import { TeamContent } from '@/components/Dashboard/Team'
@@ -107,15 +107,6 @@ export default function Dashboard() {
return <div>Error: {error.message}</div>
}
- if (isLoading) {
- return (
- <div className='flex flex-col space-y-2 items-center justify-center w-full h-full'>
- <div className="text-xl font-bold">Loading...</div>
- <LoaderIcon className="animate-spin" />
- </div>
- )
- }
-
if (user && currentTeam) {
return (
<div className="flex min-h-screen flex-col md:flex-row pt-16 md:pt-32 px-2 md:px-32">
diff --git a/apps/docs/src/components/Dashboard/Usage.tsx b/apps/docs/src/components/Dashboard/Usage.tsx
index 227eeafaf..e5ad19ef3 100644
--- a/apps/docs/src/components/Dashboard/Usage.tsx
+++ b/apps/docs/src/components/Dashboard/Usage.tsx
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
-import { Card, CardContent } from '../ui/card'
-import { LoaderIcon } from 'lucide-react'
-import LineChart from './Chart'
+import { Card, CardContent } from '@/components/ui/card'
+import LineChart from '@/components/Dashboard/Chart'
+import Spinner from '@/components/Spinner'
import { toast } from '@/components/ui/use-toast'
type Usage = {
@@ -14,14 +14,17 @@ const usageUrl = `${process.env.NEXT_PUBLIC_BILLING_API_URL}/teams/usage`
export const UsageContent = ({ currentApiKey }: { currentApiKey: string | null }) => {
const [vcpuData, setVcpuData] = useState<any>(null)
+ const [vcpuHoursThisMonth, setVcpuHoursThisMonth] = useState<number | undefined>()
const [ramData, setRamData] = useState<any>(null)
- const [costUsage, setUsage] = useState<Usage[]>([])
+ const [ramHoursThisMonth, setRamHoursThisMonth] = useState<number | undefined>()
+ const [costUsage, setCostUsage] = useState<{ id: string; data: any }[]>([])
+ const [costThisMonth, setCostMonth] = useState<number | undefined>()
useEffect(() => {
const getUsage = async (apiKey: string) => {
setVcpuData(null)
setRamData(null)
- setUsage([])
+ setCostUsage([])
const response = await fetch(usageUrl, {
headers: {
@@ -39,19 +42,24 @@ export const UsageContent = ({ currentApiKey }: { currentApiKey: string | null }
}
const data = await response.json()
-
const { vcpuSeries, ramSeries } = transformData(data.usages)
- setUsage(data.usages)
+ const costData = transformCostData(data.usages)
+ const latestCost = costData[0].data[costData[0].data.length - 1]
+ setCostUsage(costData)
+ setCostMonth(latestCost.y)
+
setVcpuData(vcpuSeries)
+ setVcpuHoursThisMonth(vcpuSeries[0].data[vcpuSeries[0].data.length - 1].y)
+
setRamData(ramSeries)
+ setRamHoursThisMonth(ramSeries[0].data[ramSeries[0].data.length - 1].y)
}
if (currentApiKey) {
getUsage(currentApiKey)
}
}, [currentApiKey])
-
return (
<div className='flex flex-col w-full h-full pb-10'>
<div className='pb-10'>
@@ -61,42 +69,64 @@ export const UsageContent = ({ currentApiKey }: { currentApiKey: string | null }
</p>
</div>
- {vcpuData && ramData && costUsage ? (
- <div className='flex flex-col 2xl:flex-row w-full space-y-4 2xl:space-y-0 2xl:space-x-4'>
-
- <div className='flex flex-col w-full md:w-2/3'>
- <h2 className='font-bold pb-4 text-xl'>Costs in USD</h2>
- <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
- <CardContent>
- <LineChart className='aspect-[4/3]' series={transformCostData(costUsage)} type='Cost' />
- </CardContent>
- </Card>
- </div>
-
- <div className='flex flex-col w-full md:w-2/3'>
- <h2 className='font-bold pb-4 text-xl'>vCPU hours</h2>
- <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
- <CardContent>
- <LineChart className='aspect-[4/3]' series={vcpuData} type='vCPU' />
- </CardContent>
- </Card>
- </div>
-
- <div className='flex flex-col w-full md:w-2/3'>
- <h2 className='font-bold pb-4 text-xl'>RAM hours</h2>
- <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
- <CardContent>
- <LineChart className='aspect-[4/3]' series={ramData} type='RAM' />
- </CardContent>
- </Card>
- </div>
+ <div className='flex flex-col 2xl:flex-row w-full space-y-4 2xl:space-y-0 2xl:space-x-4'>
+
+ <div className='flex flex-col w-full md:w-2/3'>
+ <h2 className='font-bold pb-4 text-xl'>Costs in USD</h2>
+ {costUsage && costUsage.length > 0 ?
+ <div className="flex flex-col space-y-2">
+ <span className='text-sm text-white/50'>Total costs this month: <b>${costThisMonth?.toFixed(2)}</b></span>
+ <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
+ <CardContent>
+ <LineChart className='aspect-[4/3]' series={costUsage} type='Cost' />
+ </CardContent>
+ </Card>
+ </div>
+ : (
+ <div className="flex items-center justify-center w-full">
+ <Spinner />
+ </div>
+ )
+ }
+ </div>
+ <div className='flex flex-col w-full md:w-2/3'>
+ <h2 className='font-bold pb-4 text-xl'>vCPU hours</h2>
+ {vcpuData ?
+ <div className="flex flex-col space-y-2">
+ <span className='text-sm text-white/50'>Total vCPU hours this month: <b>{vcpuHoursThisMonth?.toFixed(2)}</b></span>
+ <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
+ <CardContent>
+ <LineChart className='aspect-[4/3]' series={vcpuData} type='vCPU' />
+ </CardContent>
+ </Card>
+ </div>
+ : (
+ <div className="flex items-center justify-center w-full">
+ <Spinner />
+ </div>
+ )}
</div>
- ) : (
- <div className='flex items-center justify-center w-2/3'>
- <LoaderIcon className='animate-spin' />
+
+ <div className='flex flex-col w-full md:w-2/3'>
+ <h2 className='font-bold pb-4 text-xl'>RAM hours</h2>
+ {ramData ?
+ <div className="flex flex-col space-y-2">
+ <span className='text-sm text-white/50'>Total RAM hours this month: <b>{ramHoursThisMonth?.toFixed(2)}</b></span>
+ <Card className='w-full bg-inherit/10 border border-white/20 mb-10'>
+ <CardContent>
+ <LineChart className='aspect-[4/3]' series={ramData} type='RAM' />
+ </CardContent>
+ </Card>
+ </div>
+ : (
+ <div className="flex items-center justify-center w-full">
+ <Spinner />
+ </div>
+ )}
</div>
- )}
+
+ </div>
</div>
)
}
diff --git a/apps/docs/src/components/Spinner.tsx b/apps/docs/src/components/Spinner.tsx
index b4d1abe5b..6740dcda5 100644
--- a/apps/docs/src/components/Spinner.tsx
+++ b/apps/docs/src/components/Spinner.tsx
@@ -1,9 +1,5 @@
import { Loader2 as Loader } from 'lucide-react'
-// export interface Props {
-// className?: string
-// }
-
function Spinner({ className = '' }) {
return (
<Loader
@@ -14,14 +10,3 @@ function Spinner({ className = '' }) {
}
export default Spinner
-
-// TODO: Nicer loading dots, I don't like these much
-export function LoadingDots() {
- return (
- <div className="inline-flex align-middle">
- <span className="circle animate-loaderDots"></span>
- <span className="circle animation-delay-200 animate-loaderDots"></span>
- <span className="circle animation-delay-400 animate-loaderDots"></span>
- </div>
- )
-}
diff --git a/apps/docs/src/utils/useUser.tsx b/apps/docs/src/utils/useUser.tsx
index 175e73ca4..387484e69 100644
--- a/apps/docs/src/utils/useUser.tsx
+++ b/apps/docs/src/utils/useUser.tsx
@@ -107,7 +107,6 @@ export const CustomUserContextProvider = (props) => {
if (!session) return
if (!session.user.id) return
-
// @ts-ignore
const { data: userTeams, teamsError } = await supabase
.from('users_teams')
@@ -130,26 +129,6 @@ export const CustomUserContextProvider = (props) => {
}
const pricingTier = defaultTeam.tier
- const isPromoTier = pricingTier.startsWith('promo')
- let promoEndsAt: string | null = null
- // Fetch promo end date
- if (isPromoTier) {
- console.log('pricingTier', pricingTier)
- const { data: promoData, error: promoError } = await supabase
- .from('tiers')
- .select('promo_ends_at')
- .eq('id', pricingTier)
- if (promoError) Sentry.captureException(promoError)
- console.log('promoData', promoData)
- promoEndsAt = promoData?.[0]?.promo_ends_at
- }
-
- // const { data: apiKeys, error: apiKeysError } = await supabase
- // .from('team_api_keys')
- // .select('*')
- // .in('team_id', (teams?.map((team) => team.id) as any)) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
- // if (apiKeysError) Sentry.captureException(apiKeysError)
-
const defaultTeamId = defaultTeam?.id // TODO: Adjust when user can be part of multiple teams
const { data: accessToken, error: accessTokenError } = await supabase
@@ -160,7 +139,6 @@ export const CustomUserContextProvider = (props) => {
.single()
if (accessTokenError) Sentry.captureException(accessTokenError)
- console.log('teams', teams)
setUser({
...session?.user,
teams: teams ?? [],
@@ -169,8 +147,6 @@ export const CustomUserContextProvider = (props) => {
error: teamsError,
pricingTier: {
id: pricingTier,
- isPromo: isPromoTier,
- ...(isPromoTier ? { endsAt: promoEndsAt } : {})
},
})
setIsLoading(false)