From 0565d5585e2eb828b499db9b32882b8b312d549f Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Fri, 3 May 2024 18:07:13 -0400 Subject: [PATCH] Filter zero costs --- .../main/home/project-settings/UsagePage.tsx | 70 ++++++++----------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/dashboard/src/main/home/project-settings/UsagePage.tsx b/dashboard/src/main/home/project-settings/UsagePage.tsx index ecd3c3d4d4..39368d6f2e 100644 --- a/dashboard/src/main/home/project-settings/UsagePage.tsx +++ b/dashboard/src/main/home/project-settings/UsagePage.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useState } from "react"; +import React, { useMemo, useState } from "react"; import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; import styled from "styled-components"; @@ -18,18 +18,24 @@ import Bars from "./Bars"; dayjs.extend(utc); function UsagePage(): JSX.Element { - const [currentPeriodStart, setCurrentPeriodStart] = useState( - null + const { plan } = useCustomerPlan(); + + const startDate = dayjs.utc(plan?.starting_on); + const endDate = dayjs().utc().startOf("day"); + const numberOfDays = startDate.daysInMonth(); + + const [currentPeriodStart, setCurrentPeriodStart] = useState( + startDate.toDate() ); - const [currentPeriodEnd, setCurrentPeriodEnd] = useState(null); - const [currentPeriodDuration, setcurrentPeriodDuration] = useState(30); + const [currentPeriodEnd, setCurrentPeriodEnd] = useState(endDate.toDate()); + const [currentPeriodDuration, setCurrentPeriodDuration] = + useState(numberOfDays); const { usage } = useCustomerUsage( currentPeriodStart, currentPeriodEnd, "day" ); - const { plan } = useCustomerPlan(); const { costs } = useCustomerCosts( currentPeriodStart, currentPeriodEnd, @@ -37,26 +43,6 @@ function UsagePage(): JSX.Element { ); let totalCost = 0; - // Initial period setup - useEffect(() => { - if (plan) { - const now = new Date(); - const endDate = dayjs.utc(now).startOf("day").toDate(); - const startDate = dayjs - .utc(now) - .subtract(1, "month") - .startOf("day") - .toDate(); - - // Set the limit to the current period's number of days - const numberOfDays = startDate.getUTCDate(); - - setcurrentPeriodDuration(numberOfDays); - setCurrentPeriodStart(startDate); - setCurrentPeriodEnd(endDate); - } - }, [plan]); - const processedUsage = useMemo(() => { const before = usage; const resultMap = new Map(); @@ -89,17 +75,19 @@ function UsagePage(): JSX.Element { }, [usage]); const processedCosts = useMemo(() => { - return costs?.map((dailyCost) => { - dailyCost.start_timestamp = new Date( - dailyCost.start_timestamp - ).toLocaleDateString("en-US", { - month: "short", - day: "numeric", - }); - dailyCost.cost = dailyCost.cost / 100; - totalCost += dailyCost.cost; - return dailyCost; - }); + return costs + ?.map((dailyCost) => { + dailyCost.start_timestamp = new Date( + dailyCost.start_timestamp + ).toLocaleDateString("en-US", { + month: "short", + day: "numeric", + }); + dailyCost.cost = parseFloat((dailyCost.cost / 100).toFixed(4)); + totalCost += dailyCost.cost; + return dailyCost; + }) + .filter((dailyCost) => dailyCost.cost > 0); }, [costs]); const generateOptions = (): Array<{ value: string; label: string }> => { @@ -126,9 +114,11 @@ function UsagePage(): JSX.Element { <>