import _, { messages } from 'intl' import Component from 'base-component' import Icon from 'icon' import React from 'react' import Tooltip from 'tooltip' import { fetchVmStats } from 'xo' import { Toggle } from 'form' import { injectIntl } from 'react-intl' import { Container, Row, Col } from 'grid' import { CpuLineChart, MemoryLineChart, VifLineChart, XvdLineChart, } from 'xo-line-chart' export default injectIntl( class VmStats extends Component { constructor(props) { super(props) this.state.useCombinedValues = false } loop(vm = this.props.vm) { if (this.cancel) { this.cancel() } if (vm.power_state !== 'Running') { return } let cancelled = false this.cancel = () => { cancelled = true } fetchVmStats(vm, this.state.granularity).then(stats => { if (cancelled) { return } this.cancel = null clearTimeout(this.timeout) this.setState( { stats, selectStatsLoading: false, }, () => { this.timeout = setTimeout(this.loop, stats.interval * 1000) } ) }) } loop = ::this.loop componentWillMount() { this.loop() } componentWillUnmount() { clearTimeout(this.timeout) } componentWillReceiveProps(props) { const vmCur = this.props.vm const vmNext = props.vm if (vmCur.power_state !== 'Running' && vmNext.power_state === 'Running') { this.loop(vmNext) } else if ( vmCur.power_state === 'Running' && vmNext.power_state !== 'Running' ) { this.setState({ stats: undefined, }) } } handleSelectStats(event) { const granularity = event.target.value clearTimeout(this.timeout) this.setState( { granularity, selectStatsLoading: true, }, this.loop ) } handleSelectStats = ::this.handleSelectStats render() { const { intl } = this.props const { granularity, selectStatsLoading, stats, useCombinedValues, } = this.state return !stats ? (
No stats.
) : (