Skip to content

Commit

Permalink
Merge pull request #491 from zsnmwy/fix/react_store
Browse files Browse the repository at this point in the history
feat: dashboard error handler
  • Loading branch information
qmhu committed Aug 19, 2022
2 parents 485a32b + 6b43374 commit 9ad7615
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 152 deletions.
13 changes: 8 additions & 5 deletions pkg/web/src/components/BoardChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Style from './index.module.less';
import { useInstantPrometheusQuery, useRangePrometheusQuery } from '../../services/prometheusApi';
import { useCraneUrl } from '../../hooks';
import ReactEcharts from 'echarts-for-react';
import {useTranslation} from "react-i18next";
import { useTranslation } from 'react-i18next';

export enum ETrend {
up,
Expand Down Expand Up @@ -241,7 +241,7 @@ const BoardChart = ({
tips,
}: IBoardProps) => {
const craneUrl: any = useCraneUrl();
const {t} = useTranslation();
const { t } = useTranslation();

let fetchDataResult;
try {
Expand All @@ -265,7 +265,11 @@ const BoardChart = ({
let count: React.ReactNode = null;
if (error) {
count = error as any;
} else if ((typeof result?.isFetching === 'boolean' && result?.isFetching === true) || result?.data?.emptyData) {
} else if (
(typeof result?.isFetching === 'boolean' && result?.isFetching === true) ||
(typeof result?.isError === 'boolean' && result?.isError === true) ||
result?.data?.emptyData
) {
count = 'No Data';
} else {
count = `${countPrefix || ''}${result?.data?.latestValue || ''}`;
Expand All @@ -285,12 +289,11 @@ const BoardChart = ({
trendNum = `${(Math.floor(calc * 100) / 100) * -1}%`;
trend = calc < 0 ? ETrend.up : ETrend.down;
} else {
console.log('emptyData', fetchDataResult.preResult?.data);
trendNum = t('历史数据不足');
trend = ETrend.error;
}

if (result?.isError) MessagePlugin.error(`[${title}] Check Your Network Or Query Params !!!`, 10 * 1000);
if (result?.isError) MessagePlugin.error(`[${title}] Check Your Network Or Query Params !!!`);

return (
<Card
Expand Down
7 changes: 4 additions & 3 deletions pkg/web/src/components/PieChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export interface IPieChart {
const fetchPieData = (craneUrl: string, title: string, timeDateRangePicker: string[], step: string, query: string) => {
const start = dayjs(timeDateRangePicker[0]).valueOf();
const end = dayjs(timeDateRangePicker[1]).valueOf();
const minutesDuration = Math.round((end - start)/1000/60)
query = query.replaceAll("{DURATION}", minutesDuration.toString())
const minutesDuration = Math.round((end - start) / 1000 / 60);
query = query.replaceAll('{DURATION}', minutesDuration.toString());
const { data, isError } = useRangePrometheusQuery({ craneUrl, start, end, step, query });
if (isError) MessagePlugin.error(`[${title}] Check Your Network Or Query Params !!!`, 10 * 1000);
if (isError) MessagePlugin.error(`[${title}] Check Your Network Or Query Params !!!`);
if (isError) return {};
const result: any = {};
data?.data?.map((namesapce) => {
const namespaceName = namesapce.metric.namespace;
Expand Down
17 changes: 8 additions & 9 deletions pkg/web/src/components/SeriesLineChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ const fetchLinesData = (craneUrl: string, timeDateRangePicker: string[], step: s
return lines.map((line) => {
const { name, query } = line;
const { data, isError } = useRangePrometheusQuery({ craneUrl, start, end, step, query });
if (isError) MessagePlugin.error(`[${name}] Check Your Network Or Query Params !!!`, 10 * 1000);
console.log(name, data?.metricData, data?.emptyData);
if (isError) MessagePlugin.error(`[${name}] Check Your Network Or Query Params !!!`);
return {
...line,
data: data?.emptyData ? [] : data?.metricData,
data: isError || data?.emptyData ? [] : data?.metricData,
};
});
};
Expand All @@ -78,16 +77,16 @@ const buildLineChartOption = (lineStyle: LineStyle | undefined, linesData: ISeri
type: 'line',
data: series.data,
showSymbol: false,
}));
}));
return {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
backgroundColor: '#6a7985',
},
},
},
legend: {
data: legend,
Expand Down Expand Up @@ -148,8 +147,8 @@ const SeriesLineChart = ({
const [presets] = useState<Record<string, [Date, Date]>>({
[t('最近7天')]: [dayjs().subtract(6, 'day').toDate(), dayjs().toDate()],
[t('最近3天')]: [dayjs().subtract(2, 'day').toDate(), dayjs().toDate()],
[t('最近2天')]: [dayjs().subtract(24, 'h').toDate(), dayjs().toDate()],
[t('最近1天')]: [dayjs().subtract(1, 'h').toDate(), dayjs().toDate()],
[t('最近1天')]: [dayjs().subtract(24, 'h').toDate(), dayjs().toDate()],
[t('最近1小时')]: [dayjs().subtract(1, 'h').toDate(), dayjs().toDate()],
});

const onTimeChange = (time: any) => {
Expand Down
5 changes: 5 additions & 0 deletions pkg/web/src/layouts/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { ViewListIcon } from 'tdesign-icons-react';
import { Button, Col, Layout, MessagePlugin, Row, Select } from 'tdesign-react';
import { useFetchClusterListQuery } from '../../../services/clusterApi';
import { recommendationRuleApi } from '../../../services/recommendationRuleApi';
import { recommendationApi } from '../../../services/recommendationApi';
import { prometheusApi } from '../../../services/prometheusApi';
import { namespaceApi } from '../../../services/namespaceApi';
import { grafanaApi } from '../../../services/grafanaApi';

const { Header } = Layout;

Expand Down
11 changes: 5 additions & 6 deletions pkg/web/src/pages/Cost/CarbonInsight/components/CarbonChart.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import React from 'react';
import {Col, Row} from 'tdesign-react';
import { Col, Row } from 'tdesign-react';
import Style from './CarbonChart.module.less';
import SeriesLineChart, {ISeriesLineChart} from '../../../../components/SeriesLineChart';
import {useTranslation} from "react-i18next";

import SeriesLineChart, { ISeriesLineChart } from '../../../../components/SeriesLineChart';
import { useTranslation } from 'react-i18next';

const CarbonChart = () => {
const {t} = useTranslation();
const { t } = useTranslation();

const item: ISeriesLineChart = {
title: t('碳排放'),
subTitle: t('(克/小时)'),
datePicker: true,
step: '1h',
xAxis: {type: 'time'},
xAxis: { type: 'time' },
lines: [
{
name: t('carbon emissions'),
Expand Down
14 changes: 6 additions & 8 deletions pkg/web/src/pages/Dashboard/Base/components/CpuChart.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import React from 'react';
import {Col, Row} from 'tdesign-react';
import { Col, Row } from 'tdesign-react';
import Style from './CpuChart.module.less';
import SeriesLineChart, {ISeriesLineChart, LineStyle} from '../../../../components/SeriesLineChart';
import SeriesLineChart, { ISeriesLineChart, LineStyle } from '../../../../components/SeriesLineChart';
import { useTranslation } from 'react-i18next';



const CpuChart = () => {
const {t} = useTranslation();
const { t } = useTranslation();

const item: ISeriesLineChart = {
title: 'CPU 资源使用',
subTitle: '(Core)',
datePicker: true,
step: '1h',
xAxis: {type: 'time'},
xAxis: { type: 'time' },
lines: [
{
name: 'capacity',
Expand Down Expand Up @@ -51,4 +49,4 @@ const CpuChart = () => {
);
};

export default React.memo(CpuChart);
export default React.memo(CpuChart);
13 changes: 6 additions & 7 deletions pkg/web/src/pages/Dashboard/Base/components/MemoryChart.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import React from 'react';
import {Col, Row} from 'tdesign-react';
import { Col, Row } from 'tdesign-react';
import Style from './MemoryChart.module.less';
import SeriesLineChart, {ISeriesLineChart} from '../../../../components/SeriesLineChart';
import SeriesLineChart, { ISeriesLineChart } from '../../../../components/SeriesLineChart';
import { useTranslation } from 'react-i18next';


const MemoryChart = () => {
const {t} = useTranslation();
const { t } = useTranslation();

const item: ISeriesLineChart = {
title: t('Memory 资源使用'),
subTitle: '( GB )',
datePicker: true,
step: '1h',
xAxis: {type: 'time'},
xAxis: { type: 'time' },
lines: [
{
name: 'capacity',
Expand Down Expand Up @@ -51,4 +50,4 @@ const MemoryChart = () => {
);
};

export default React.memo(MemoryChart);
export default React.memo(MemoryChart);
11 changes: 5 additions & 6 deletions pkg/web/src/pages/Dashboard/Base/components/MiddleChart.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react';
import {Col, Row} from 'tdesign-react';
import { Col, Row } from 'tdesign-react';
import Style from './MiddleChart.module.less';
import SeriesLineChart, {LineStyle} from '../../../../components/SeriesLineChart';
import SeriesLineChart, { LineStyle } from '../../../../components/SeriesLineChart';
import PieChart from '../../../../components/PieChart';
import {useTranslation} from 'react-i18next';
import { useTranslation } from 'react-i18next';
import { useCraneDiscount } from 'hooks';

const MiddleChart = () => {

const {t} = useTranslation();
const { t } = useTranslation();

const craneDiscount: any = useCraneDiscount();

Expand Down Expand Up @@ -64,6 +63,6 @@ sum(
</Col>
</Row>
);
}
};

export default React.memo(MiddleChart);
62 changes: 32 additions & 30 deletions pkg/web/src/pages/Dashboard/Base/components/TopPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import {Col, Row} from 'tdesign-react';
import Board, {IBoardProps, TimeType} from 'components/BoardChart';
import { Col, Row } from 'tdesign-react';
import Board, { IBoardProps, TimeType } from 'components/BoardChart';
import { useTranslation } from 'react-i18next';
import { useCraneDiscount } from 'hooks';

const TopPanel = () => {
const {t} = useTranslation();
const { t } = useTranslation();

const craneDiscount: any = useCraneDiscount();

Expand All @@ -22,7 +22,7 @@ const TopPanel = () => {
by (node)) * (${craneDiscount}/100.0)`,
timeType: TimeType.Range,
tips: t("过去一个月集群总成本。从安装Crane时间开始,按小时累加集群成本"),
tips: t('过去一个月集群总成本。从安装Crane时间开始,按小时累加集群成本'),
},
{
title: t('预估每月成本'),
Expand All @@ -35,7 +35,7 @@ const TopPanel = () => {
by (node)) * 730 * (${craneDiscount}/100.0)`,
timeType: TimeType.Range,
tips: t("以最近一小时成本估算未来一个月的成本。每小时成本 * 24 * 30"),
tips: t('以最近一小时成本估算未来一个月的成本。每小时成本 * 24 * 30'),
},
{
title: t('预估CPU总成本'),
Expand All @@ -49,7 +49,7 @@ const TopPanel = () => {
) * 730 * (${craneDiscount}/100.)`,
countPrefix: '¥ ',
timeType: TimeType.Range,
tips: t("以最近一小时CPU成本估算未来一个月的CPU成本。每小时CPU成本 * 24 * 30"),
tips: t('以最近一小时CPU成本估算未来一个月的CPU成本。每小时CPU成本 * 24 * 30'),
},
{
title: t('预估Memory总成本'),
Expand All @@ -62,33 +62,35 @@ const TopPanel = () => {
) * 730 * (${craneDiscount}/100.)`,
countPrefix: '¥ ',
timeType: TimeType.Range,
tips: t("以最近一小时Memory成本估算未来一个月的Memory成本。每小时Memory成本 * 24 * 30"),
tips: t('以最近一小时Memory成本估算未来一个月的Memory成本。每小时Memory成本 * 24 * 30'),
},
];

return <Row gutter={[16, 16]}>
{PANE_LIST.map((item, index) => (
<Col key={item.title} xs={6} xl={3}>
<Board
title={item.title}
trend={item.trend}
trendNum={item.trendNum}
count={item.count}
countPrefix={item.countPrefix}
lineColor={item.lineColor}
desc={t('自从上周以来')}
Icon={item.Icon}
dark={index === 0}
query={item.query}
timeType={item.timeType}
start={item.start}
end={item.end}
step={item.step}
tips={item.tips}
/>
</Col>
))}
</Row>
return (
<Row gutter={[16, 16]}>
{PANE_LIST.map((item, index) => (
<Col key={item.title} xs={6} xl={3}>
<Board
title={item.title}
trend={item.trend}
trendNum={item.trendNum}
count={item.count}
countPrefix={item.countPrefix}
lineColor={item.lineColor}
desc={t('自从上周以来')}
Icon={item.Icon}
dark={index === 0}
query={item.query}
timeType={item.timeType}
start={item.start}
end={item.end}
step={item.step}
tips={item.tips}
/>
</Col>
))}
</Row>
);
};

export default React.memo(TopPanel);
Loading

0 comments on commit 9ad7615

Please sign in to comment.