diff --git a/.eslintrc b/.eslintrc index 4ef5c4fd..8c66a254 100644 --- a/.eslintrc +++ b/.eslintrc @@ -74,6 +74,7 @@ "globals": { "t": true, "globals": true, - "request": true + "request": true, + "METRICDICT": true } } diff --git a/src/components/PrometheusChart/BaseCard.jsx b/src/components/PrometheusChart/BaseCard.jsx index df5a369e..e28ee604 100644 --- a/src/components/PrometheusChart/BaseCard.jsx +++ b/src/components/PrometheusChart/BaseCard.jsx @@ -1,113 +1,149 @@ -// Copyright 2021 99cloud -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import React, { Component } from 'react'; import { Card, Select } from 'antd'; -import VisibleObserver from 'components/VisibleObserver'; -import PropTypes from 'prop-types'; -import { observer } from 'mobx-react'; -import isEqual from 'react-fast-compare'; -import FetchPrometheusStore from './store/FetchPrometheusStore'; +import React, { createContext, useEffect, useState } from 'react'; +import VisibleObserver from '../VisibleObserver'; +import { createFetchPrometheusClient, createDataHandler } from './utils'; import style from './style.less'; -@observer -class BaseFetch extends Component { - static propTypes = { - constructorParams: PropTypes.shape({ - requestType: PropTypes.oneOf(['current', 'range']).isRequired, - metricKey: PropTypes.string.isRequired, - formatDataFn: PropTypes.func.isRequired, - typeKey: PropTypes.string, - deviceKey: PropTypes.string, - }).isRequired, - params: PropTypes.object, - currentRange: PropTypes.array.isRequired, - interval: PropTypes.number.isRequired, - title: PropTypes.string.isRequired, - extra: PropTypes.func, - renderContent: PropTypes.func.isRequired, - visibleHeight: PropTypes.number, +export const PrometheusContext = createContext({ + data: [], + device: '', + devices: [], +}); + +function getChartData(data, device, devices) { + if (device && devices.length !== 0) { + return data.filter((d) => d.device === device); + } + return data; +} + +const BaseCard = (props) => { + const { + createFetchParams, + handleDataParams, + fetchDataParams, + title, + visibleHeight, + extra, + renderContent, + } = props; + + const [initData, setInitData] = useState([]); + + const [chartData, setChartData] = useState([]); + + const [device, setDevice] = useState(''); + + const [devices, setDevices] = useState([]); + + const [isLoading, setIsLoading] = useState(true); + + const fetchData = createFetchPrometheusClient(createFetchParams); + + const dataHandler = createDataHandler(handleDataParams); + + const passedContext = { + data: chartData, + device, + devices, + modifyKeys: handleDataParams.modifyKeys, }; - static defaultProps = { - // src/pages/monitor/containers/PhysicalNode/index.less:91 - // 为了不在视区范围内的时候,仍然撑开元素。防止出现在视区的时候挤开下面的元素。(由下往上翻) - visibleHeight: 100, + useEffect(() => { + (async () => { + setIsLoading(true); + const data = await fetchData(fetchDataParams); + const { + retData: handledData, + device: newDevice, + devices: newDevices, + } = dataHandler(data); + // save base response + setInitData(handledData); + + // save device information + setDevice(newDevice); + setDevices(newDevices); + + // refresh component + const newChartData = getChartData(handledData, newDevice, newDevices); + setChartData(newChartData); + + // setLoading + setIsLoading(false); + })(); + }, []); + + const handleDeviceChange = (newDevice) => { + setIsLoading(true); + // refresh component + const newChartData = getChartData(initData, newDevice, devices); + setDevice(newDevice); + setChartData(newChartData); + setIsLoading(false); }; - constructor(props) { - super(props); - const { constructorParams } = this.props; + const filterChartData = (filt) => { + setIsLoading(true); + // refresh component + const newChartData = initData.filter(filt); + setChartData(newChartData); + setIsLoading(false); + }; - this.store = new FetchPrometheusStore(constructorParams); - } + const extraRender = ( + <> + {!isLoading && device && devices.length !== 0 && ( + ({ - label: i, - value: i, - }))} - onChange={(e) => this.store.handleDeviceChange.call(this.store, e)} - /> - )} - {extra && extra(this.store)} - - ); - } - - render() { - const { isLoading } = this.store; - const { visibleHeight } = this.props; - return ( + return ( + - {(visible) => (visible ? this.props.renderContent(this.store) : null)} + {(visible) => + visible ? ( + + {(v) => renderContent(v)} + + ) : null + } - ); - } -} + + ); +}; -export default BaseFetch; +BaseCard.defaultProps = { + visibleHeight: 100, +}; + +export default BaseCard; diff --git a/src/components/PrometheusChart/ChartCard.jsx b/src/components/PrometheusChart/ChartCard.jsx index 710fed43..8840732c 100644 --- a/src/components/PrometheusChart/ChartCard.jsx +++ b/src/components/PrometheusChart/ChartCard.jsx @@ -12,63 +12,52 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Chart, Line, Tooltip } from 'bizcharts'; -import BaseCard from 'components/PrometheusChart/BaseCard'; import React from 'react'; -import { ChartType, getXScale } from 'components/PrometheusChart/utils/utils'; +import { merge } from 'lodash'; +import { Button, Modal } from 'antd'; +import { Chart, Line, Tooltip } from 'bizcharts'; +import ArrowsAltOutlined from '@ant-design/icons/ArrowsAltOutlined'; + +import { ChartType, getXScale } from './utils/utils'; import { baseLineProps, baseToolTipProps, multilineProps, -} from 'components/PrometheusChart/utils/baseProps'; -import { toJS } from 'mobx'; -import { observer } from 'mobx-react'; - -import { merge } from 'lodash'; -import ArrowsAltOutlined from '@ant-design/icons/lib/icons/ArrowsAltOutlined'; -import { Button, Modal } from 'antd'; -import BaseContent from 'components/PrometheusChart/component/BaseContent'; +} from './utils/baseProps'; +import BaseCard from './BaseCard'; const ChartCard = (props) => { - const { - constructorParams, - params, - currentRange, - interval, - chartProps, - title, - extra, - isModal = false, - BaseContentConfig = {}, - } = props; + const { chartProps } = props; + const renderContent = (contextValue) => { + const { + height, + scale, + chartType, + toolTipProps = baseToolTipProps, + } = chartProps; - const { - height, - scale, - chartType, - toolTipProps = baseToolTipProps, - } = chartProps; + const { data } = contextValue; - let lineProps; - switch (chartType) { - case ChartType.ONELINE: - case ChartType.ONELINEDEVICES: - lineProps = baseLineProps; - break; - case ChartType.MULTILINE: - case ChartType.MULTILINEDEVICES: - lineProps = multilineProps; - break; - default: - lineProps = baseLineProps; - } + scale.x = merge( + {}, + getXScale(props.fetchDataParams.currentRange), + scale.x || {} + ); - const renderContent = (store) => { - let data = toJS(store.data); - if (store.device) { - data = data.filter((d) => d.device === store.device); + let lineProps; + switch (chartType) { + case ChartType.ONELINE: + case ChartType.ONELINEDEVICES: + lineProps = baseLineProps; + break; + case ChartType.MULTILINE: + case ChartType.MULTILINEDEVICES: + lineProps = multilineProps; + break; + default: + lineProps = baseLineProps; } - scale.x = merge({}, getXScale(props.currentRange), scale.x || {}); + return ( @@ -77,53 +66,81 @@ const ChartCard = (props) => { ); }; - const ModalContent = observer(() => ( -
- ( - { + const { + title, + createFetchParams, + handleDataParams, + fetchDataParams, + isModal = false, + } = props; + let defaultNode = {}; + const { params: fParams = {} } = fetchDataParams; + const { instance, hostname, ...rest } = fParams; + if (fParams) { + if (instance) { + defaultNode.instance = instance; + } else if (hostname) { + defaultNode.hostname = hostname; + } + } + + return ( + <> + {props.extra && props.extra()} + {!isModal && ( +
- )); + + ); + }; return ( ( - <> - {extra && extra(s)} - {!isModal && ( -