stash - users usage tab

This commit is contained in:
Ishaan Jaff 2024-05-08 10:38:27 -07:00
parent cae390b51a
commit 7bfd02350a
2 changed files with 56 additions and 8 deletions

View file

@ -3,13 +3,14 @@ import { BarChart, BarList, Card, Title, Table, TableHead, TableHeaderCell, Tabl
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import ViewUserSpend from "./view_user_spend"; import ViewUserSpend from "./view_user_spend";
import { Grid, Col, Text, LineChart, TabPanel, TabPanels, TabGroup, TabList, Tab, Select, SelectItem } from "@tremor/react"; import { Grid, Col, Text, LineChart, TabPanel, TabPanels, TabGroup, TabList, Tab, Select, SelectItem, DateRangePicker, DateRangePickerValue } from "@tremor/react";
import { import {
userSpendLogsCall, userSpendLogsCall,
keyInfoCall, keyInfoCall,
adminSpendLogsCall, adminSpendLogsCall,
adminTopKeysCall, adminTopKeysCall,
adminTopModelsCall, adminTopModelsCall,
adminTopEndUsersCall,
teamSpendLogsCall, teamSpendLogsCall,
tagsSpendLogsCall, tagsSpendLogsCall,
modelMetricsCall, modelMetricsCall,
@ -112,6 +113,10 @@ const UsagePage: React.FC<UsagePageProps> = ({
const [topTagsData, setTopTagsData] = useState<any[]>([]); const [topTagsData, setTopTagsData] = useState<any[]>([]);
const [uniqueTeamIds, setUniqueTeamIds] = useState<any[]>([]); const [uniqueTeamIds, setUniqueTeamIds] = useState<any[]>([]);
const [totalSpendPerTeam, setTotalSpendPerTeam] = useState<any[]>([]); const [totalSpendPerTeam, setTotalSpendPerTeam] = useState<any[]>([]);
const [dateValue, setDateValue] = useState<DateRangePickerValue>({
from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
to: new Date(),
});
const firstDay = new Date( const firstDay = new Date(
currentDate.getFullYear(), currentDate.getFullYear(),
@ -127,6 +132,10 @@ const UsagePage: React.FC<UsagePageProps> = ({
let startTime = formatDate(firstDay); let startTime = formatDate(firstDay);
let endTime = formatDate(lastDay); let endTime = formatDate(lastDay);
// const updateEndUserUsage(startTime: string, endTime: string) {
// }
function formatDate(date: Date) { function formatDate(date: Date) {
const year = date.getFullYear(); const year = date.getFullYear();
let month = date.getMonth() + 1; // JS month index starts from 0 let month = date.getMonth() + 1; // JS month index starts from 0
@ -193,6 +202,12 @@ const UsagePage: React.FC<UsagePageProps> = ({
const top_tags = await tagsSpendLogsCall(accessToken); const top_tags = await tagsSpendLogsCall(accessToken);
setTopTagsData(top_tags.top_10_tags); setTopTagsData(top_tags.top_10_tags);
// get spend per end-user
let spend_user_call = await adminTopEndUsersCall(accessToken, null);
setTopUsers(spend_user_call);
console.log("spend/user result", spend_user_call);
} else if (userRole == "App Owner") { } else if (userRole == "App Owner") {
await userSpendLogsCall( await userSpendLogsCall(
accessToken, accessToken,
@ -224,7 +239,6 @@ const UsagePage: React.FC<UsagePageProps> = ({
spend: k["spend"], spend: k["spend"],
})); }));
setTopKeys(filtered_keys); setTopKeys(filtered_keys);
setTopUsers(getTopUsers(response));
setKeySpendData(response); setKeySpendData(response);
} }
}); });
@ -342,7 +356,42 @@ const UsagePage: React.FC<UsagePageProps> = ({
</Grid> </Grid>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<DateRangePicker
enableSelect={true}
value={dateValue}
onValueChange={(value) => {
setDateValue(value);
updateModelMetrics(selectedModelGroup, value.from, value.to); // Call updateModelMetrics with the new date range
}}
/>
<Text className="mt-4">End Users of your LLM API calls. Tracked When a `user` param is passed in your LLM calls</Text>
<Card className="mt-4">
<Table className="max-h-[70vh] min-h-[500px]">
<TableHead>
<TableRow>
<TableHeaderCell>End User</TableHeaderCell>
<TableHeaderCell>Spend</TableHeaderCell>
<TableHeaderCell>Total Events</TableHeaderCell>
</TableRow>
</TableHead>
<TableBody>
{topUsers?.map((user: any, index: number) => (
<TableRow key={index}>
<TableCell>{user.end_user}</TableCell>
<TableCell>{user.total_spend?.toFixed(4)}</TableCell>
<TableCell>{user.total_events}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Card>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<Grid numItems={2} className="gap-2 h-[75vh] w-full mb-4"> <Grid numItems={2} className="gap-2 h-[75vh] w-full mb-4">

View file

@ -158,12 +158,11 @@ const ViewUserDashboard: React.FC<ViewUserDashboardProps> = ({
<Card className="w-full mx-auto flex-auto overflow-y-auto max-h-[80vh] mb-4"> <Card className="w-full mx-auto flex-auto overflow-y-auto max-h-[80vh] mb-4">
<div className="mb-4 mt-1"> <div className="mb-4 mt-1">
<Text><b>Key Owners: </b> Users on LiteLLM that created API Keys. Automatically tracked by LiteLLM</Text> <Text><b>Key Owners: </b> Users on LiteLLM that created API Keys. Automatically tracked by LiteLLM</Text>
<Text className="mt-1"><b>End Users: </b>End Users of your LLM API calls. Tracked When a `user` param is passed in your LLM calls</Text>
</div> </div>
<TabGroup> <TabGroup>
<TabList variant="line" defaultValue="1"> <TabList variant="line" defaultValue="1">
<Tab value="1">Key Owners</Tab> <Tab value="1">Key Owners</Tab>
<Tab value="2">End-Users</Tab>
</TabList> </TabList>
<TabPanels> <TabPanels>
<TabPanel> <TabPanel>
@ -220,7 +219,7 @@ const ViewUserDashboard: React.FC<ViewUserDashboardProps> = ({
<div className="flex items-center"> <div className="flex items-center">
<div className="flex-1"></div> <div className="flex-1"></div>
<div className="flex-1 flex justify-between items-center"> <div className="flex-1 flex justify-between items-center">
<Text className="w-1/4 mr-2 text-right">Key</Text> {/* <Text className="w-1/4 mr-2 text-right">Key</Text>
<Select defaultValue="1" className="w-3/4"> <Select defaultValue="1" className="w-3/4">
{keys?.map((key: any, index: number) => { {keys?.map((key: any, index: number) => {
if ( if (
@ -239,10 +238,10 @@ const ViewUserDashboard: React.FC<ViewUserDashboardProps> = ({
); );
} }
})} })}
</Select> </Select> */}
</div> </div>
</div> </div>
<Table className="max-h-[70vh] min-h-[500px]"> {/* <Table className="max-h-[70vh] min-h-[500px]">
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableHeaderCell>End User</TableHeaderCell> <TableHeaderCell>End User</TableHeaderCell>
@ -260,7 +259,7 @@ const ViewUserDashboard: React.FC<ViewUserDashboardProps> = ({
</TableRow> </TableRow>
))} ))}
</TableBody> </TableBody>
</Table> </Table> */}
</TabPanel> </TabPanel>
</TabPanels> </TabPanels>
</TabGroup> </TabGroup>