mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-28 00:17:12 +01:00
feat: frontend traffic tab (#9385)
This commit is contained in:
parent
d5d172647c
commit
bf78d74dc5
@ -17,6 +17,9 @@ const NetworkTrafficUsage = lazy(
|
|||||||
const BackendConnections = lazy(
|
const BackendConnections = lazy(
|
||||||
() => import('./NetworkTrafficUsage/BackendConnections'),
|
() => import('./NetworkTrafficUsage/BackendConnections'),
|
||||||
);
|
);
|
||||||
|
const FrontendNetworkTrafficUsage = lazy(
|
||||||
|
() => import('./NetworkTrafficUsage/FrontendNetworkTrafficUsage'),
|
||||||
|
);
|
||||||
|
|
||||||
const tabs = [
|
const tabs = [
|
||||||
{
|
{
|
||||||
@ -45,6 +48,10 @@ const consumptionModelTabs = [
|
|||||||
label: 'Backend Connections',
|
label: 'Backend Connections',
|
||||||
path: '/admin/network/backend-connections',
|
path: '/admin/network/backend-connections',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: 'Frontend Traffic',
|
||||||
|
path: '/admin/network/frontend-data-usage',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const Network = () => {
|
export const Network = () => {
|
||||||
@ -103,6 +110,10 @@ export const Network = () => {
|
|||||||
path='backend-connections'
|
path='backend-connections'
|
||||||
element={<BackendConnections />}
|
element={<BackendConnections />}
|
||||||
/>
|
/>
|
||||||
|
<Route
|
||||||
|
path='frontend-data-usage'
|
||||||
|
element={<FrontendNetworkTrafficUsage />}
|
||||||
|
/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</PageContent>
|
</PageContent>
|
||||||
</div>
|
</div>
|
||||||
|
@ -0,0 +1,82 @@
|
|||||||
|
import type { FC } from 'react';
|
||||||
|
import { usePageTitle } from 'hooks/usePageTitle';
|
||||||
|
import { Alert, Box } from '@mui/material';
|
||||||
|
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
||||||
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
||||||
|
import {
|
||||||
|
BarElement,
|
||||||
|
CategoryScale,
|
||||||
|
Chart as ChartJS,
|
||||||
|
Legend,
|
||||||
|
LinearScale,
|
||||||
|
Title,
|
||||||
|
Tooltip,
|
||||||
|
} from 'chart.js';
|
||||||
|
|
||||||
|
import { Bar } from 'react-chartjs-2';
|
||||||
|
import annotationPlugin from 'chartjs-plugin-annotation';
|
||||||
|
import { customHighlightPlugin } from 'component/common/Chart/customHighlightPlugin';
|
||||||
|
import { PeriodSelector } from './PeriodSelector';
|
||||||
|
import { getChartLabel } from './chart-functions';
|
||||||
|
import { useTrafficStats } from './hooks/useStats';
|
||||||
|
import { StyledBox, TopRow } from './SharedComponents';
|
||||||
|
import { useChartDataSelection } from './hooks/useChartDataSelection';
|
||||||
|
|
||||||
|
const FrontendNetworkTrafficUsage: FC = () => {
|
||||||
|
usePageTitle('Network - Frontend Traffic Usage');
|
||||||
|
|
||||||
|
const { isOss } = useUiConfig();
|
||||||
|
|
||||||
|
const { chartDataSelection, setChartDataSelection, options } =
|
||||||
|
useChartDataSelection();
|
||||||
|
|
||||||
|
const includedTraffic = 0;
|
||||||
|
const { chartData } = useTrafficStats(
|
||||||
|
includedTraffic,
|
||||||
|
chartDataSelection,
|
||||||
|
'/api/frontend',
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ConditionallyRender
|
||||||
|
condition={isOss()}
|
||||||
|
show={<Alert severity='warning'>Not enabled.</Alert>}
|
||||||
|
elseShow={
|
||||||
|
<>
|
||||||
|
<StyledBox>
|
||||||
|
<TopRow>
|
||||||
|
<Box>
|
||||||
|
Frontend traffic is determined by the total SDK
|
||||||
|
requests to the Frontend API
|
||||||
|
</Box>
|
||||||
|
<PeriodSelector
|
||||||
|
selectedPeriod={chartDataSelection}
|
||||||
|
setPeriod={setChartDataSelection}
|
||||||
|
/>
|
||||||
|
</TopRow>
|
||||||
|
<Bar
|
||||||
|
data={chartData}
|
||||||
|
plugins={[customHighlightPlugin()]}
|
||||||
|
options={options}
|
||||||
|
aria-label={getChartLabel(chartDataSelection)}
|
||||||
|
/>
|
||||||
|
</StyledBox>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Register dependencies that we need to draw the chart.
|
||||||
|
ChartJS.register(
|
||||||
|
annotationPlugin,
|
||||||
|
CategoryScale,
|
||||||
|
LinearScale,
|
||||||
|
BarElement,
|
||||||
|
Title,
|
||||||
|
Tooltip,
|
||||||
|
Legend,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Use a default export to lazy-load the charting library.
|
||||||
|
export default FrontendNetworkTrafficUsage;
|
@ -51,10 +51,6 @@ describe('toTrafficUsageChartData', () => {
|
|||||||
|
|
||||||
const expectedOutput = {
|
const expectedOutput = {
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
|
||||||
data: [0, 6, 0, 2, 4, 5],
|
|
||||||
...fromEndpointInfo('/api/admin'),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
data: [7, 0, 11, 13, 0, 10],
|
data: [7, 0, 11, 13, 0, 10],
|
||||||
...fromEndpointInfo('/api/client'),
|
...fromEndpointInfo('/api/client'),
|
||||||
@ -70,7 +66,9 @@ describe('toTrafficUsageChartData', () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(toTrafficUsageChartData(input)).toMatchObject(expectedOutput);
|
expect(toTrafficUsageChartData(input, '/api/client')).toMatchObject(
|
||||||
|
expectedOutput,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('daily data conversion', () => {
|
test('daily data conversion', () => {
|
||||||
|
@ -15,9 +15,11 @@ export type ChartDatasetType = ChartDataset<'bar'>;
|
|||||||
|
|
||||||
export const toTrafficUsageChartData = (
|
export const toTrafficUsageChartData = (
|
||||||
traffic: TrafficUsageDataSegmentedCombinedSchema,
|
traffic: TrafficUsageDataSegmentedCombinedSchema,
|
||||||
|
filter?: string,
|
||||||
): { datasets: ChartDatasetType[]; labels: string[] } => {
|
): { datasets: ChartDatasetType[]; labels: string[] } => {
|
||||||
const { newRecord, labels } = getLabelsAndRecords(traffic);
|
const { newRecord, labels } = getLabelsAndRecords(traffic);
|
||||||
const datasets = traffic.apiData
|
const datasets = traffic.apiData
|
||||||
|
.filter((apiData) => (filter ? apiData.apiPath === filter : true))
|
||||||
.sort(
|
.sort(
|
||||||
(item1, item2) =>
|
(item1, item2) =>
|
||||||
endpointsInfo[item1.apiPath].order -
|
endpointsInfo[item1.apiPath].order -
|
||||||
|
@ -17,6 +17,7 @@ import { averageTrafficPreviousMonths } from '../average-traffic-previous-months
|
|||||||
export const useTrafficStats = (
|
export const useTrafficStats = (
|
||||||
includedTraffic: number,
|
includedTraffic: number,
|
||||||
chartDataSelection: ChartDataSelection,
|
chartDataSelection: ChartDataSelection,
|
||||||
|
filter?: string,
|
||||||
) => {
|
) => {
|
||||||
const { result } = useTrafficSearch(
|
const { result } = useTrafficSearch(
|
||||||
chartDataSelection.grouping,
|
chartDataSelection.grouping,
|
||||||
@ -34,7 +35,7 @@ export const useTrafficStats = (
|
|||||||
}
|
}
|
||||||
const traffic = result.data;
|
const traffic = result.data;
|
||||||
|
|
||||||
const chartData = newToChartData(traffic);
|
const chartData = newToChartData(traffic, filter);
|
||||||
const usageTotal = calculateTotalUsage(traffic);
|
const usageTotal = calculateTotalUsage(traffic);
|
||||||
const overageCost = calculateOverageCost(
|
const overageCost = calculateOverageCost(
|
||||||
usageTotal,
|
usageTotal,
|
||||||
|
Loading…
Reference in New Issue
Block a user