mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
fix(usage.tsx): show key spend per day
This commit is contained in:
parent
43da22ae13
commit
d1d4568dc5
1 changed files with 97 additions and 32 deletions
|
@ -1,9 +1,9 @@
|
|||
import { BarChart, Card, Title } from "@tremor/react";
|
||||
|
||||
import React, { useState, useEffect } from "react";
|
||||
import { Grid, Col, Text } from "@tremor/react";
|
||||
import { Grid, Col, Text, LineChart } from "@tremor/react";
|
||||
import { userSpendLogsCall } from "./networking";
|
||||
import { AreaChart, Flex, Switch, Subtitle } from "@tremor/react";
|
||||
import { start } from "repl";
|
||||
|
||||
interface UsagePageProps {
|
||||
accessToken: string | null;
|
||||
|
@ -12,12 +12,65 @@ interface UsagePageProps {
|
|||
userID: string | null;
|
||||
}
|
||||
|
||||
type DataType = {
|
||||
api_key: string;
|
||||
startTime: string;
|
||||
_sum: {
|
||||
spend: number;
|
||||
type CustomTooltipTypeBar = {
|
||||
payload: any;
|
||||
active: boolean | undefined;
|
||||
label: any;
|
||||
};
|
||||
|
||||
const customTooltip = (props: CustomTooltipTypeBar) => {
|
||||
const { payload, active } = props;
|
||||
if (!active || !payload) return null;
|
||||
|
||||
const value = payload[0].payload;
|
||||
const date = value["startTime"];
|
||||
|
||||
// Convert the object into an array of key-value pairs
|
||||
const entries: [string, number][] = Object.entries(value)
|
||||
.filter(([key]) => key !== "spend" && key !== "startTime")
|
||||
.map(([key, value]) => [key, value as number]); // Type assertion to specify the value as number
|
||||
|
||||
// Sort the array based on the float value in descending order
|
||||
entries.sort((a, b) => b[1] - a[1]);
|
||||
|
||||
// Get the top 5 key-value pairs
|
||||
const topEntries = entries.slice(0, 5);
|
||||
|
||||
return (
|
||||
<div className="w-56 rounded-tremor-default border border-tremor-border bg-tremor-background p-2 text-tremor-default shadow-tremor-dropdown">
|
||||
{date}
|
||||
{topEntries.map(([key, value]) => (
|
||||
<div key={key} className="flex flex-1 space-x-10">
|
||||
<div className="p-2">
|
||||
<p className="text-tremor-content">
|
||||
Token: {key.substring(0, 4)}{" "}
|
||||
<span className="font-medium text-tremor-content-emphasis">
|
||||
Spend: {value}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
// return (
|
||||
// <div className="w-56 rounded-tremor-default border border-tremor-border bg-tremor-background p-2 text-tremor-default shadow-tremor-dropdown">
|
||||
// {payload.map((category: any, idx: number) => {
|
||||
// <div key={idx} className="flex flex-1 space-x-2.5">
|
||||
// <div
|
||||
// className={`flex w-1 flex-col bg-${category.color}-500 rounded`}
|
||||
// />
|
||||
// <div className="space-y-1">
|
||||
// <p className="text-tremor-content">{category.dataKey}</p>
|
||||
// <p className="font-medium text-tremor-content-emphasis">
|
||||
// {category.value} bpm
|
||||
// </p>
|
||||
// </div>
|
||||
// </div>;
|
||||
// })}
|
||||
// </div>
|
||||
// );
|
||||
};
|
||||
|
||||
const UsagePage: React.FC<UsagePageProps> = ({
|
||||
|
@ -28,6 +81,7 @@ const UsagePage: React.FC<UsagePageProps> = ({
|
|||
}) => {
|
||||
const currentDate = new Date();
|
||||
const [keySpendData, setKeySpendData] = useState<any[]>([]);
|
||||
const [keyCategories, setKeyCategories] = useState<string[]>([]);
|
||||
|
||||
const firstDay = new Date(
|
||||
currentDate.getFullYear(),
|
||||
|
@ -63,10 +117,6 @@ const UsagePage: React.FC<UsagePageProps> = ({
|
|||
|
||||
useEffect(() => {
|
||||
if (accessToken && token && userRole && userID) {
|
||||
const cachedKeySpendData = localStorage.getItem("keySpendData");
|
||||
if (cachedKeySpendData) {
|
||||
setKeySpendData(JSON.parse(cachedKeySpendData));
|
||||
} else {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const response = await userSpendLogsCall(
|
||||
|
@ -77,8 +127,24 @@ const UsagePage: React.FC<UsagePageProps> = ({
|
|||
startTime,
|
||||
endTime
|
||||
);
|
||||
|
||||
const uniqueKeys: Set<string> = new Set();
|
||||
|
||||
response.forEach((item: any) => {
|
||||
Object.keys(item).forEach((key) => {
|
||||
if (key !== "spend" && key !== "startTime") {
|
||||
uniqueKeys.add(key);
|
||||
}
|
||||
});
|
||||
});
|
||||
let uniqueKeysList = Array.from(uniqueKeys);
|
||||
setKeyCategories(uniqueKeysList);
|
||||
setKeySpendData(response);
|
||||
localStorage.setItem("keySpendData", JSON.stringify(response));
|
||||
// localStorage.setItem("keySpendData", JSON.stringify(response));
|
||||
// localStorage.setItem(
|
||||
// "keyCategories",
|
||||
// JSON.stringify(uniqueKeysList)
|
||||
// );
|
||||
} catch (error) {
|
||||
console.error("There was an error fetching the data", error);
|
||||
// Optionally, update your UI to reflect the error state here as well
|
||||
|
@ -86,9 +152,7 @@ const UsagePage: React.FC<UsagePageProps> = ({
|
|||
};
|
||||
fetchData();
|
||||
}
|
||||
}
|
||||
}, [accessToken, token, userRole, userID]);
|
||||
|
||||
return (
|
||||
<div style={{ width: "100%" }}>
|
||||
<Grid numItems={1} className="gap-0 p-10 h-[75vh] w-full">
|
||||
|
@ -103,6 +167,7 @@ const UsagePage: React.FC<UsagePageProps> = ({
|
|||
valueFormatter={valueFormatter}
|
||||
yAxisWidth={100}
|
||||
tickGap={5}
|
||||
customTooltip={customTooltip}
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue