1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-04 13:48:56 +02:00

feat(1-3267): make the chart mostly work

This commit is contained in:
Thomas Heartman 2025-01-24 12:40:49 +01:00
parent c9a78fa674
commit 26f7eb0ca4
No known key found for this signature in database
GPG Key ID: BD1F880DAED1EE78
4 changed files with 66 additions and 46 deletions

View File

@ -33,6 +33,7 @@ import { BILLING_TRAFFIC_BUNDLE_PRICE } from 'component/admin/billing/BillingDas
import { useLocationSettings } from 'hooks/useLocationSettings'; import { useLocationSettings } from 'hooks/useLocationSettings';
import { PeriodSelector } from './PeriodSelector'; import { PeriodSelector } from './PeriodSelector';
import { useUiFlag } from 'hooks/useUiFlag'; import { useUiFlag } from 'hooks/useUiFlag';
import { format } from 'date-fns';
const StyledBox = styled(Box)(({ theme }) => ({ const StyledBox = styled(Box)(({ theme }) => ({
display: 'grid', display: 'grid',
@ -189,29 +190,36 @@ export const NetworkTrafficUsage: FC = () => {
return createBarChartOptions( return createBarChartOptions(
theme, theme,
(tooltipItems: any) => { (tooltipItems: any) => {
const periodItem = record[period]; if (period.format === 'daily') {
const tooltipDate = new Date( const periodItem = record[period.month];
periodItem.year, const tooltipDate = new Date(
periodItem.month, periodItem.year,
Number.parseInt(tooltipItems[0].label), periodItem.month,
); Number.parseInt(tooltipItems[0].label),
return tooltipDate.toLocaleDateString( );
locationSettings?.locale ?? 'en-US', return tooltipDate.toLocaleDateString(
{ locationSettings?.locale ?? 'en-US',
month: 'long', {
day: 'numeric', month: 'long',
year: 'numeric', day: 'numeric',
}, year: 'numeric',
); },
);
} else {
return new Date(tooltipItems[0].label).toLocaleDateString(
locationSettings?.locale ?? 'en-US',
{
month: 'long',
year: 'numeric',
},
);
}
}, },
includedTraffic, includedTraffic,
); );
}, [theme, period]); }, [theme, period]);
const traffic = useInstanceTrafficMetrics2({ const traffic = useInstanceTrafficMetrics2(period);
format: 'daily',
month: period,
});
const data = newToChartData(traffic.usage); // <- why don't we do this? const data = newToChartData(traffic.usage); // <- why don't we do this?
@ -244,7 +252,13 @@ export const NetworkTrafficUsage: FC = () => {
useEffect(() => { useEffect(() => {
if (data) { if (data) {
// if daily, there is a sum. if monthly, use the count from the current month // if daily, there is a sum. if monthly, use the count from the current month
const usage = toTrafficUsageSum(data.datasets); let usage: number;
if (period.format === 'monthly') {
usage = data.datasets.length; // this is wrong
} else {
usage = toTrafficUsageSum(data.datasets);
}
setUsageTotal(usage); setUsageTotal(usage);
if (includedTraffic > 0) { if (includedTraffic > 0) {
const calculatedOverageCost = calculateOverageCost( const calculatedOverageCost = calculateOverageCost(
@ -256,7 +270,9 @@ export const NetworkTrafficUsage: FC = () => {
setEstimatedMonthlyCost( setEstimatedMonthlyCost(
calculateEstimatedMonthlyCost( calculateEstimatedMonthlyCost(
period, period.format === 'daily'
? period.month
: format(new Date(), 'yyyy-MM'),
data.datasets, data.datasets,
includedTraffic, includedTraffic,
new Date(), new Date(),

View File

@ -1,4 +1,5 @@
import { styled } from '@mui/material'; import { styled } from '@mui/material';
import type { ChartDataSelection } from 'hooks/api/getters/useInstanceTrafficMetrics/useInstanceTrafficMetrics';
import { type FC, useState } from 'react'; import { type FC, useState } from 'react';
export type Period = { export type Period = {
@ -147,8 +148,8 @@ type Selection =
}; };
type Props = { type Props = {
selectedPeriod: string; selectedPeriod: ChartDataSelection;
setPeriod: (period: string) => void; setPeriod: (period: ChartDataSelection) => void;
}; };
export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => { export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
@ -157,14 +158,14 @@ export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
// this is for dev purposes; only to show how the design will work when you select a range. // this is for dev purposes; only to show how the design will work when you select a range.
const [tempOverride, setTempOverride] = useState<Selection | null>(); const [tempOverride, setTempOverride] = useState<Selection | null>();
const select = (value: Selection) => { // const select = (value: Selection) => {
if (value.type === 'month') { // if (value.type === 'month') {
setTempOverride(null); // setTempOverride(null);
setPeriod(value.value); // setPeriod(value.value);
} else { // } else {
setTempOverride(value); // setTempOverride(value);
} // }
}; // };
const rangeOptions = [3, 6, 12].map((monthsBack) => ({ const rangeOptions = [3, 6, 12].map((monthsBack) => ({
value: monthsBack, value: monthsBack,
@ -183,17 +184,17 @@ export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
<li key={period.label}> <li key={period.label}>
<button <button
className={ className={
!tempOverride && selectedPeriod.format === 'daily' &&
period.key === selectedPeriod period.key === selectedPeriod.month
? 'selected' ? 'selected'
: '' : ''
} }
type='button' type='button'
disabled={!period.selectable} disabled={!period.selectable}
onClick={() => { onClick={() => {
select({ setPeriod({
type: 'month', format: 'daily',
value: period.key, month: period.key,
}); });
}} }}
> >
@ -211,16 +212,15 @@ export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
<li key={option.label}> <li key={option.label}>
<button <button
className={ className={
tempOverride && selectedPeriod.format === 'monthly' &&
tempOverride.type === 'range' && option.value === selectedPeriod.monthsBack
option.value === tempOverride.monthsBack
? 'selected' ? 'selected'
: '' : ''
} }
type='button' type='button'
onClick={() => { onClick={() => {
select({ setPeriod({
type: 'range', format: 'monthly',
monthsBack: option.value, monthsBack: option.value,
}); });
}} }}

View File

@ -34,7 +34,7 @@ export const useInstanceTrafficMetrics = (
); );
}; };
type Selection = export type ChartDataSelection =
| { | {
format: 'daily'; format: 'daily';
month: string; month: string;
@ -44,7 +44,7 @@ type Selection =
monthsBack: number; monthsBack: number;
}; };
const fromSelection = (selection: Selection) => { const fromSelection = (selection: ChartDataSelection) => {
const fmt = (date: Date) => format(date, 'yyyy-MM-dd'); const fmt = (date: Date) => format(date, 'yyyy-MM-dd');
if (selection.format === 'daily') { if (selection.format === 'daily') {
const month = new Date(selection.month); const month = new Date(selection.month);
@ -92,10 +92,10 @@ export type InstanceTrafficMetricsResponse2 = {
}; };
export const useInstanceTrafficMetrics2 = ( export const useInstanceTrafficMetrics2 = (
selection: Selection, selection: ChartDataSelection,
): InstanceTrafficMetricsResponse2 => { ): InstanceTrafficMetricsResponse2 => {
const { from, to } = fromSelection(selection); // const { from, to } = fromSelection(selection);
console.log('would use these from and to dates', from, to); // console.log('would use these from and to dates', from, to);
const apiPath = const apiPath =
selection.format === 'daily' selection.format === 'daily'

View File

@ -1,5 +1,6 @@
import { useState } from 'react'; import { useState } from 'react';
import type { import type {
ChartDataSelection,
IInstanceTrafficMetricsResponse, IInstanceTrafficMetricsResponse,
SegmentedSchema, SegmentedSchema,
SegmentedSchemaApiData, SegmentedSchemaApiData,
@ -377,7 +378,10 @@ export const useTrafficDataEstimation = () => {
// "label": "December 2024" // "label": "December 2024"
// } // }
// } // }
const [period, setPeriod] = useState<string>(selectablePeriods[0].key); const [period, setPeriod] = useState<ChartDataSelection>({
format: 'daily',
month: selectablePeriods[0].key,
});
return { return {
calculateTrafficDataCost, calculateTrafficDataCost,