1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-03-09 00:18:26 +01: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 { PeriodSelector } from './PeriodSelector';
import { useUiFlag } from 'hooks/useUiFlag';
import { format } from 'date-fns';
const StyledBox = styled(Box)(({ theme }) => ({
display: 'grid',
@ -189,29 +190,36 @@ export const NetworkTrafficUsage: FC = () => {
return createBarChartOptions(
theme,
(tooltipItems: any) => {
const periodItem = record[period];
const tooltipDate = new Date(
periodItem.year,
periodItem.month,
Number.parseInt(tooltipItems[0].label),
);
return tooltipDate.toLocaleDateString(
locationSettings?.locale ?? 'en-US',
{
month: 'long',
day: 'numeric',
year: 'numeric',
},
);
if (period.format === 'daily') {
const periodItem = record[period.month];
const tooltipDate = new Date(
periodItem.year,
periodItem.month,
Number.parseInt(tooltipItems[0].label),
);
return tooltipDate.toLocaleDateString(
locationSettings?.locale ?? 'en-US',
{
month: 'long',
day: 'numeric',
year: 'numeric',
},
);
} else {
return new Date(tooltipItems[0].label).toLocaleDateString(
locationSettings?.locale ?? 'en-US',
{
month: 'long',
year: 'numeric',
},
);
}
},
includedTraffic,
);
}, [theme, period]);
const traffic = useInstanceTrafficMetrics2({
format: 'daily',
month: period,
});
const traffic = useInstanceTrafficMetrics2(period);
const data = newToChartData(traffic.usage); // <- why don't we do this?
@ -244,7 +252,13 @@ export const NetworkTrafficUsage: FC = () => {
useEffect(() => {
if (data) {
// 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);
if (includedTraffic > 0) {
const calculatedOverageCost = calculateOverageCost(
@ -256,7 +270,9 @@ export const NetworkTrafficUsage: FC = () => {
setEstimatedMonthlyCost(
calculateEstimatedMonthlyCost(
period,
period.format === 'daily'
? period.month
: format(new Date(), 'yyyy-MM'),
data.datasets,
includedTraffic,
new Date(),

View File

@ -1,4 +1,5 @@
import { styled } from '@mui/material';
import type { ChartDataSelection } from 'hooks/api/getters/useInstanceTrafficMetrics/useInstanceTrafficMetrics';
import { type FC, useState } from 'react';
export type Period = {
@ -147,8 +148,8 @@ type Selection =
};
type Props = {
selectedPeriod: string;
setPeriod: (period: string) => void;
selectedPeriod: ChartDataSelection;
setPeriod: (period: ChartDataSelection) => void;
};
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.
const [tempOverride, setTempOverride] = useState<Selection | null>();
const select = (value: Selection) => {
if (value.type === 'month') {
setTempOverride(null);
setPeriod(value.value);
} else {
setTempOverride(value);
}
};
// const select = (value: Selection) => {
// if (value.type === 'month') {
// setTempOverride(null);
// setPeriod(value.value);
// } else {
// setTempOverride(value);
// }
// };
const rangeOptions = [3, 6, 12].map((monthsBack) => ({
value: monthsBack,
@ -183,17 +184,17 @@ export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
<li key={period.label}>
<button
className={
!tempOverride &&
period.key === selectedPeriod
selectedPeriod.format === 'daily' &&
period.key === selectedPeriod.month
? 'selected'
: ''
}
type='button'
disabled={!period.selectable}
onClick={() => {
select({
type: 'month',
value: period.key,
setPeriod({
format: 'daily',
month: period.key,
});
}}
>
@ -211,16 +212,15 @@ export const PeriodSelector: FC<Props> = ({ selectedPeriod, setPeriod }) => {
<li key={option.label}>
<button
className={
tempOverride &&
tempOverride.type === 'range' &&
option.value === tempOverride.monthsBack
selectedPeriod.format === 'monthly' &&
option.value === selectedPeriod.monthsBack
? 'selected'
: ''
}
type='button'
onClick={() => {
select({
type: 'range',
setPeriod({
format: 'monthly',
monthsBack: option.value,
});
}}

View File

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

View File

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