feat - clea up usage tab

This commit is contained in:
Ishaan Jaff 2024-06-03 15:42:37 -07:00
parent 45cb899687
commit 67312204d7
3 changed files with 57 additions and 37 deletions

View file

@ -979,7 +979,8 @@ export const teamSpendLogsCall = async (accessToken: String) => {
export const tagsSpendLogsCall = async ( export const tagsSpendLogsCall = async (
accessToken: String, accessToken: String,
startTime: String | undefined, startTime: String | undefined,
endTime: String | undefined endTime: String | undefined,
tags: String[] | undefined
) => { ) => {
try { try {
let url = proxyBaseUrl let url = proxyBaseUrl
@ -990,6 +991,11 @@ export const tagsSpendLogsCall = async (
url = `${url}?start_date=${startTime}&end_date=${endTime}`; url = `${url}?start_date=${startTime}&end_date=${endTime}`;
} }
// if tags, convert the list to a comma separated string
if (tags) {
url += `${url}&tags=${tags.join(",")}`;
}
console.log("in tagsSpendLogsCall:", url); console.log("in tagsSpendLogsCall:", url);
const response = await fetch(`${url}`, { const response = await fetch(`${url}`, {
method: "GET", method: "GET",

View file

@ -144,6 +144,7 @@ const UsagePage: React.FC<UsagePageProps> = ({
const [globalActivity, setGlobalActivity] = useState<GlobalActivityData>({} as GlobalActivityData); const [globalActivity, setGlobalActivity] = useState<GlobalActivityData>({} as GlobalActivityData);
const [globalActivityPerModel, setGlobalActivityPerModel] = useState<any[]>([]); const [globalActivityPerModel, setGlobalActivityPerModel] = useState<any[]>([]);
const [selectedKeyID, setSelectedKeyID] = useState<string | null>(""); const [selectedKeyID, setSelectedKeyID] = useState<string | null>("");
const [selectedTags, setSelectedTags] = useState<string[]>(["all-tags"]);
const [dateValue, setDateValue] = useState<DateRangePickerValue>({ const [dateValue, setDateValue] = useState<DateRangePickerValue>({
from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
to: new Date(), to: new Date(),
@ -175,6 +176,10 @@ const UsagePage: React.FC<UsagePageProps> = ({
return formatter.format(number); return formatter.format(number);
} }
useEffect(() => {
updateTagSpendData(dateValue.from, dateValue.to);
}, [dateValue, selectedTags]);
const updateEndUserData = async (startTime: Date | undefined, endTime: Date | undefined, uiSelectedKey: string | null) => { const updateEndUserData = async (startTime: Date | undefined, endTime: Date | undefined, uiSelectedKey: string | null) => {
@ -212,12 +217,15 @@ const UsagePage: React.FC<UsagePageProps> = ({
// startTime put it to the first hour of the selected date // startTime put it to the first hour of the selected date
startTime.setHours(0, 0, 0, 0); startTime.setHours(0, 0, 0, 0);
let top_tags = await tagsSpendLogsCall(accessToken, startTime.toISOString(), endTime.toISOString()); let top_tags = await tagsSpendLogsCall(
accessToken,
startTime.toISOString(),
endTime.toISOString(),
selectedTags.length === 0 ? undefined : selectedTags
);
setTopTagsData(top_tags.spend_per_tag); setTopTagsData(top_tags.spend_per_tag);
console.log("Tag spend data updated successfully"); console.log("Tag spend data updated successfully");
} }
function formatDate(date: Date) { function formatDate(date: Date) {
@ -293,14 +301,15 @@ const UsagePage: React.FC<UsagePageProps> = ({
setTotalSpendPerTeam(total_spend_per_team); setTotalSpendPerTeam(total_spend_per_team);
//get top tags // all_tag_names -> used for dropdown
const top_tags = await tagsSpendLogsCall(accessToken, dateValue.from?.toISOString(), dateValue.to?.toISOString());
setTopTagsData(top_tags.spend_per_tag);
// all_tag_names
const all_tag_names = await allTagNamesCall(accessToken); const all_tag_names = await allTagNamesCall(accessToken);
setAllTagNames(all_tag_names.tag_names); setAllTagNames(all_tag_names.tag_names);
//get top tags
const top_tags = await tagsSpendLogsCall(accessToken, dateValue.from?.toISOString(), dateValue.to?.toISOString(), undefined);
setTopTagsData(top_tags.spend_per_tag);
// get spend per end-user // get spend per end-user
let spend_user_call = await adminTopEndUsersCall(accessToken, null, undefined, undefined); let spend_user_call = await adminTopEndUsersCall(accessToken, null, undefined, undefined);
setTopUsers(spend_user_call); setTopUsers(spend_user_call);
@ -362,13 +371,7 @@ const UsagePage: React.FC<UsagePageProps> = ({
return ( return (
<div style={{ width: "100%" }} className="p-8"> <div style={{ width: "100%" }} className="p-8">
<ViewUserSpend
userID={userID}
userRole={userRole}
accessToken={accessToken}
userSpend={null}
selectedTeam={null}
/>
<TabGroup> <TabGroup>
<TabList className="mt-2"> <TabList className="mt-2">
<Tab>All Up</Tab> <Tab>All Up</Tab>
@ -387,6 +390,13 @@ const UsagePage: React.FC<UsagePageProps> = ({
<TabPanels> <TabPanels>
<TabPanel> <TabPanel>
<Grid numItems={2} className="gap-2 h-[100vh] w-full"> <Grid numItems={2} className="gap-2 h-[100vh] w-full">
<ViewUserSpend
userID={userID}
userRole={userRole}
accessToken={accessToken}
userSpend={null}
selectedTeam={null}
/>
<Col numColSpan={2}> <Col numColSpan={2}>
<Card> <Card>
<Title>Monthly Spend</Title> <Title>Monthly Spend</Title>
@ -763,26 +773,30 @@ const UsagePage: React.FC<UsagePageProps> = ({
<Col> <Col>
<MultiSelect <MultiSelect
defaultValue={["all-tags"]}> value={selectedTags}
<MultiSelectItem onValueChange={(value) => setSelectedTags(value as string[])}
key={"all-tags"}
value={"All Tags"}
> >
All Tags <MultiSelectItem
</MultiSelectItem> key={"all-tags"}
{ value={"all-tags"}
allTagNames && allTagNames?.map((tag: any, index: number) => { onClick={() => setSelectedTags(["all-tags"])}
return ( >
<MultiSelectItem All Tags
key={index} </MultiSelectItem>
value={String(index)} {allTagNames &&
> allTagNames
{tag} .filter((tag) => tag !== "all-tags")
</MultiSelectItem> .map((tag: any, index: number) => {
); return (
}) <MultiSelectItem
} key={tag}
</MultiSelect> value={String(tag)}
>
{tag}
</MultiSelectItem>
);
})}
</MultiSelect>
</Col> </Col>
</Grid> </Grid>

View file

@ -131,7 +131,7 @@ const ViewUserSpend: React.FC<ViewUserSpendProps> = ({ userID, userRole, accessT
${roundedSpend} ${roundedSpend}
</p> </p>
</div> </div>
<div className="ml-auto"> {/* <div className="ml-auto">
<Accordion> <Accordion>
<AccordionHeader><Text>Team Models</Text></AccordionHeader> <AccordionHeader><Text>Team Models</Text></AccordionHeader>
<AccordionBody className="absolute right-0 z-10 bg-white p-2 shadow-lg max-w-xs"> <AccordionBody className="absolute right-0 z-10 bg-white p-2 shadow-lg max-w-xs">
@ -144,7 +144,7 @@ const ViewUserSpend: React.FC<ViewUserSpendProps> = ({ userID, userRole, accessT
</List> </List>
</AccordionBody> </AccordionBody>
</Accordion> </Accordion>
</div> </div> */}
</div> </div>
); );
} }