()
| 213 | } |
| 214 | |
| 215 | function DashboardCost() { |
| 216 | // It's getting WET in here |
| 217 | const supabase = useSupabaseClient<Database>(); |
| 218 | const { profile, refreshProfile } = useProfile(); |
| 219 | const [cost, setCost] = useState([{}] as { date: string; value: number }[]); |
| 220 | const [sum, setSum] = useState(0); |
| 221 | const [loading, setLoading] = useState(true); |
| 222 | |
| 223 | useEffect(() => { |
| 224 | if (!profile) return; |
| 225 | (async () => { |
| 226 | const res = await supabase |
| 227 | .from("usage") |
| 228 | .select("*") |
| 229 | .eq("org_id", profile?.org_id!); |
| 230 | if (res.error) throw res.error; |
| 231 | setCost(res.data.map((d) => ({ date: d.date, value: d.usage }))); |
| 232 | setSum(res.data.reduce((acc, curr) => acc + curr.usage, 0)); |
| 233 | setLoading(false); |
| 234 | })(); |
| 235 | }, [profile, refreshProfile, supabase]); |
| 236 | return ( |
| 237 | <div className="bg-gray-800 min-h-screen"> |
| 238 | <Navbar current={"Usage"} /> |
| 239 | <div className="h-[calc(100%-4rem)] flex flex-col gap-y-4 mx-auto max-w-7xl px-4 sm:px-6 lg:px-8 bg-gray-800 pb-8 place-items-center"> |
| 240 | <div className="mt-8 bg-gray-850 rounded-md px-6 py-4 overflow-visible max-w-7xl w-full"> |
| 241 | <div className="place-items-center m-auto text-center"> |
| 242 | <h1 className="text-2xl text-gray-100">OpenAI API usage </h1> |
| 243 | <p className="text-gray-200 mt-2 text-xl font-bold text-purple-500"> |
| 244 | {`Total Cost $${Math.round(sum * 100) / 100}`} |
| 245 | </p> |
| 246 | </div> |
| 247 | <div className="max-w-5xl mx-auto"> |
| 248 | {!loading && <DatesBarGraph data={cost} ylabel="Cost (USD)" />} |
| 249 | </div> |
| 250 | </div> |
| 251 | </div> |
| 252 | </div> |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | export const getServerSideProps = pageGetServerSideProps; |
nothing calls this directly
no test coverage detected