Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cloud version metrics multi line #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/pages/MachineDashboard/Cards/CPUCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { connect } from 'react-redux';
import { IRootState } from '@/store';

import LineCard from '@/components/DashboardCard/LineCard';
import { getDataByType } from '@/utils/dashboard';
import { getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import { VALUE_TYPE } from '@/utils/promQL';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => {
const { cpuStat, metricsFilterValues } = state.machine;
Expand All @@ -15,7 +16,7 @@ const mapState = (state: IRootState) => {
data: getDataByType({
data: cpuStat,
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: getMetricsUniqName(MetricScene.CPU),
aliasConfig,
}),
valueType: VALUE_TYPE.percentage,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/MachineDashboard/Cards/LoadCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { connect } from 'react-redux';
import { IRootState } from '@/store';

import LineCard from '@/components/DashboardCard/LineCard';
import { getDataByType } from '@/utils/dashboard';
import { getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import { VALUE_TYPE } from '@/utils/promQL';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => {
const { loadStat, metricsFilterValues } = state.machine;
Expand All @@ -14,7 +15,7 @@ const mapState = (state: IRootState) => {
data: getDataByType({
data: loadStat,
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: getMetricsUniqName(MetricScene.MACHINE),
aliasConfig,
}),
valueType: VALUE_TYPE.number,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/MachineDashboard/Cards/MemoryCard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { connect } from 'react-redux';
import LineCard from '@/components/DashboardCard/LineCard';
import { IRootState } from '@/store';
import { getDataByType } from '@/utils/dashboard';
import { getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import { VALUE_TYPE } from '@/utils/promQL';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => {
const { memoryStat, memorySizeStat, metricsFilterValues } = state.machine;
Expand All @@ -12,7 +13,7 @@ const mapState = (state: IRootState) => {
data: getDataByType({
data: memoryStat,
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: getMetricsUniqName(MetricScene.MACHINE),
aliasConfig,
}),
sizes: memorySizeStat,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/MachineDashboard/Cards/NetworkIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { connect } from 'react-redux';
import { IRootState } from '@/store';

import LineCard from '@/components/DashboardCard/LineCard';
import { getDataByType } from '@/utils/dashboard';
import { getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import { VALUE_TYPE } from '@/utils/promQL';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => {
const { networkInStat, metricsFilterValues } = state.machine;
Expand All @@ -14,7 +15,7 @@ const mapState = (state: IRootState) => {
data: getDataByType({
data: networkInStat,
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: getMetricsUniqName(MetricScene.NETWORK),
aliasConfig,
}),
valueType: VALUE_TYPE.byteSecondNet,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/MachineDashboard/Cards/NetworkOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { IRootState } from '@/store';
import { VALUE_TYPE } from '@/utils/promQL';

import LineCard from '@/components/DashboardCard/LineCard';
import { getDataByType } from '@/utils/dashboard';
import { getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => {
const { networkOutStat, metricsFilterValues } = state.machine;
Expand All @@ -14,7 +15,7 @@ const mapState = (state: IRootState) => {
data: getDataByType({
data: networkOutStat,
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: getMetricsUniqName(MetricScene.NETWORK),
aliasConfig,
}),
valueType: VALUE_TYPE.byteSecondNet,
Expand Down
3 changes: 3 additions & 0 deletions src/pages/MachineDashboard/Detail/CPUDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { connect } from 'react-redux';
import Detail from '.';
import { IDispatch, IRootState } from '@/store';
import { SUPPORT_METRICS } from '@/utils/promQL';
import { getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => ({
type: 'cpu',
metricOptions: SUPPORT_METRICS.cpu,
dataTypeObj: getMetricsUniqName(MetricScene.CPU),
loading: !!state.loading.effects.machine.asyncGetCPUStatByRange,
});

Expand Down
3 changes: 3 additions & 0 deletions src/pages/MachineDashboard/Detail/DiskDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { connect } from 'react-redux';
import Detail from '.';
import { IDispatch, IRootState } from '@/store';
import { SUPPORT_METRICS } from '@/utils/promQL';
import { getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => ({
type: 'disk',
instances: state.machine.instanceList,
metricOptions: SUPPORT_METRICS.disk,
dataTypeObj: getMetricsUniqName(MetricScene.MACHINE),
metricsFilterValues: state.machine.metricsFilterValues,
loading: state.loading.effects.machine.asyncGetDiskStatByRange,
});
Expand Down
3 changes: 3 additions & 0 deletions src/pages/MachineDashboard/Detail/LoadDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { connect } from 'react-redux';
import Detail from '.';
import { IDispatch, IRootState } from '@/store';
import { SUPPORT_METRICS } from '@/utils/promQL';
import { getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => ({
type: 'load',
instances: state.machine.instanceList,
metricOptions: SUPPORT_METRICS.load,
dataTypeObj: getMetricsUniqName(MetricScene.MACHINE),
metricsFilterValues: state.machine.metricsFilterValues,
loading: state.loading.effects.machine.asyncGetLoadByRange,
});
Expand Down
3 changes: 3 additions & 0 deletions src/pages/MachineDashboard/Detail/MemoryDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { connect } from 'react-redux';
import Detail from '.';
import { IDispatch, IRootState } from '@/store';
import { SUPPORT_METRICS } from '@/utils/promQL';
import { getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => ({
type: 'memory',
metricOptions: SUPPORT_METRICS.memory,
dataTypeObj: getMetricsUniqName(MetricScene.MACHINE),
loading: state.loading.effects.machine.asyncGetMemoryStatByRange,
});

Expand Down
3 changes: 3 additions & 0 deletions src/pages/MachineDashboard/Detail/NetworkDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { connect } from 'react-redux';
import Detail from '.';
import { IDispatch, IRootState } from '@/store';
import { SUPPORT_METRICS } from '@/utils/promQL';
import { getMetricsUniqName } from '@/utils/dashboard';
import { MetricScene } from '@/utils/interface';

const mapState = (state: IRootState) => ({
type: 'network',
metricOptions: SUPPORT_METRICS.network,
dataTypeObj: getMetricsUniqName(MetricScene.NETWORK),
loading: state.loading.effects.machine.asyncGetNetworkStatByRange,
});

Expand Down
6 changes: 4 additions & 2 deletions src/pages/MachineDashboard/Detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ interface IProps
valueType: VALUE_TYPE;
}[];
loading: true;
dataTypeObj: any;
}

let pollingTimer: any;

function Detail(props: IProps) {

const { metricOptions, loading, aliasConfig, asyncGetDataSourceByRange, asyncUpdateBaseLine, cluster, instances, metricsFilterValues, updateMetricsFiltervalues, type } = props;
const { metricOptions, loading, aliasConfig, asyncGetDataSourceByRange, asyncUpdateBaseLine, cluster, instances, metricsFilterValues, updateMetricsFiltervalues, type, dataTypeObj } = props;

const [maxNum, setMaxNum] = useState<number>(0);
const [dataSources, setDataSources] = useState<any[]>([]);
Expand Down Expand Up @@ -158,12 +159,13 @@ function Detail(props: IProps) {
getDiskData({
data: dataSources[i] || [],
type: metricsFilterValues.instanceList,
nameObj: dataTypeObj,
aliasConfig,
}) :
getDataByType({
data: dataSources[i] || [],
type: metricsFilterValues.instanceList,
name: 'instance',
nameObj: dataTypeObj,
aliasConfig,
});
setMaxNum(100);
Expand Down
5 changes: 3 additions & 2 deletions src/pages/ServiceDashboard/Detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
calcTimeRange,
getBaseLineByUnit,
getMaxNum,
getMetricsUniqName,
} from '@/utils/dashboard';
import { IDispatch, IRootState } from '@/store';
import { VALUE_TYPE } from '@/utils/promQL';
import { IMetricOption, ServiceMetricsPanelValue } from '@/utils/interface';
import { IMetricOption, MetricScene, ServiceMetricsPanelValue } from '@/utils/interface';

import LineChart from '@/components/Charts/LineChart';
import { configDetailChart, updateDetailChart } from '@/utils/chart/chart';
Expand Down Expand Up @@ -216,7 +217,7 @@ function ServiceDetail(props: IProps) {
const data = getDataByType({
data: dataSources[i] || [],
type: instanceList,
name: 'instanceName',
nameObj: getMetricsUniqName(MetricScene.SERVICE),
aliasConfig,
});
chart.maxNum = getMaxNum(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import intl from 'react-intl-universal';
import { connect } from 'react-redux';
import { Popover } from 'antd';
import Icon from '@/components/Icon';
import { IServicePanelConfig } from '@/utils/interface';
import { calcTimeRange, getDataByType } from '@/utils/dashboard';
import { IServicePanelConfig, MetricScene } from '@/utils/interface';
import { calcTimeRange, getDataByType, getMetricsUniqName } from '@/utils/dashboard';
import Card from '@/components/Service/ServiceCard/Card';
import { IDispatch, IRootState } from '@/store';
import { shouldCheckCluster } from '@/utils';
Expand Down Expand Up @@ -117,7 +117,7 @@ function CustomServiceQueryPanel(props: IProps) {
data={getDataByType({
data,
type: metricsFilterValues.instanceList,
name: 'instanceName',
nameObj: getMetricsUniqName(MetricScene.SERVICE),
aliasConfig,
})}
loading={false}
Expand Down
74 changes: 65 additions & 9 deletions src/utils/dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs';
import _ from 'lodash';
import { ILineChartMetric, IStatRangeItem } from '@/utils/interface';
import { DashboardType, ILineChartMetric, IStatRangeItem, MetricScene } from '@/utils/interface';
import { VALUE_TYPE } from '@/utils/promQL';

export const DETAIL_DEFAULT_RANGE = 60 * 60 * 24 * 1000;
Expand Down Expand Up @@ -145,10 +145,14 @@ export const getProperByteDesc = (bytes: any, conversion: number) => {
export const getDataByType = (payload: {
data: IStatRangeItem[];
type?: string | string[];
name: string;
nameObj: {
name: string;
showName: (name: string) => string;
};
aliasConfig?: any;
}) => {
const { name, type, data, aliasConfig } = payload;
const { nameObj, type, data, aliasConfig } = payload;
const { name, showName } = nameObj;
const res = [] as ILineChartMetric[];
data.forEach(instance => {
instance.values.forEach(([timstamps, value]) => {
Expand All @@ -161,7 +165,7 @@ export const getDataByType = (payload: {
}
if (shouldPush) {
res.push({
type: aliasConfig && aliasConfig[_name] ? aliasConfig[_name] : _name,
type: showName(aliasConfig && aliasConfig[_name] ? aliasConfig[_name] : _name),
value: Number(value),
time: timstamps,
});
Expand All @@ -175,13 +179,18 @@ export const getDiskData = (payload: {
data: IStatRangeItem[];
type?: string | string[];
aliasConfig?: any;
nameObj: {
name: string;
showName: (name: string) => string;
};
}) => {
const { type, data } = payload;
const { type, data, nameObj } = payload;
const { name, showName } = nameObj;
const res = [] as ILineChartMetric[];
data.forEach(instance => {
instance.values.forEach(([timstamps, value]) => {
const device = instance.metric['device'];
const _name = instance.metric['instance'];
const _name = instance.metric[name];
let shouldPush = false;
if (typeof type === 'string') {
shouldPush = (type === 'all' && _name !== 'total') || _name === type;
Expand All @@ -190,7 +199,7 @@ export const getDiskData = (payload: {
}
if (shouldPush) {
res.push({
type: `${_name}-${device}`,
type: `${showName(_name)}-${device}`,
value: Number(value),
time: timstamps,
});
Expand Down Expand Up @@ -218,7 +227,7 @@ export enum TIME_OPTION_TYPE {
HOUR12 = '12hour',
DAY1 = '1day',
DAY3 = '3day',
DAY7 = '7day',
DAY7 = '7day',
DAY14 = '14day',
}

Expand Down Expand Up @@ -278,7 +287,7 @@ export const calcTimeRange = (timeRange: TIME_OPTION_TYPE | [number, number]): [
if (typeof timeRange === 'string') {
const value = TIMEOPTIONS.find(t => t.name === timeRange)?.value!;
return [end - value, end];
} else if ( typeof timeRange === 'object' && timeRange.length === 2) {
} else if (typeof timeRange === 'object' && timeRange.length === 2) {
return timeRange;
}
throw new Error('timeRange is not valid');
Expand Down Expand Up @@ -383,3 +392,50 @@ export function getVersion(v: string) {
const match = v?.match(VERSION_REGEX);
return match ? match[0] : v;
}

export const getMetricsUniqName = (scene: MetricScene) => {
const dashboardType = VERSION_TYPE?.type;
switch (dashboardType) {
case DashboardType.CLOUD:
if (scene === MetricScene.MACHINE) {
return {
name: 'pod',
showName: (name) => name ? name.slice(name.lastIndexOf('-') + 1) : name
}
}
if (scene === MetricScene.CPU || scene === MetricScene.NETWORK) {
return {
name: 'instance',
showName: (name) => name ? name.slice(name.lastIndexOf('-') + 1) : name
}
}
if (scene === MetricScene.SERVICE) {
return {
name: 'kubernetes_pod_name',
showName: (name) => name ? name.slice(name.split('-').slice(2).join('-')) : name
}
}
if (scene === MetricScene.DISK) {
return {
name: 'pod',
showName: (name) => name ? name.slice(name.split('-').slice(2).join('-')) : name
}
}
break;
default:
if (scene === MetricScene.SERVICE) {
return {
name: 'instanceName',
showName: (name) => name
}
}
return {
name: 'instance',
showName: (name) => name
}
}
return {
name: 'instance',
showName: (name) => name
}
}
Loading