2016-04-01 16:39:59 +02:00
|
|
|
import _, { messages } from 'messages'
|
2016-05-12 20:28:49 +02:00
|
|
|
import Component from 'base-component'
|
2016-03-31 17:15:23 +02:00
|
|
|
import Icon from 'icon'
|
2016-05-12 20:28:49 +02:00
|
|
|
import React from 'react'
|
2016-06-14 10:44:44 +02:00
|
|
|
import Upgrade from 'xoa-upgrade'
|
2016-04-01 17:00:03 +02:00
|
|
|
import { autobind } from 'utils'
|
2016-05-06 17:18:01 +02:00
|
|
|
import { fetchVmStats } from 'xo'
|
2016-04-01 12:42:55 +02:00
|
|
|
import { injectIntl } from 'react-intl'
|
2016-05-26 10:31:49 +02:00
|
|
|
import { Container, Row, Col } from 'grid'
|
2016-03-31 17:15:23 +02:00
|
|
|
import {
|
|
|
|
|
CpuLineChart,
|
|
|
|
|
MemoryLineChart,
|
|
|
|
|
VifLineChart,
|
|
|
|
|
XvdLineChart
|
|
|
|
|
} from 'xo-line-chart'
|
|
|
|
|
|
2016-04-01 15:33:20 +02:00
|
|
|
export default injectIntl(
|
|
|
|
|
class VmStats extends Component {
|
2016-04-01 17:00:03 +02:00
|
|
|
@autobind
|
2016-04-01 17:11:51 +02:00
|
|
|
loop (vm = this.props.vm) {
|
2016-04-01 15:33:20 +02:00
|
|
|
if (this.cancel) {
|
|
|
|
|
this.cancel()
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 17:11:51 +02:00
|
|
|
if (vm.power_state !== 'Running') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 15:33:20 +02:00
|
|
|
let cancelled = false
|
|
|
|
|
this.cancel = () => { cancelled = true }
|
|
|
|
|
|
2016-05-06 17:18:01 +02:00
|
|
|
fetchVmStats(vm, this.state.granularity).then(stats => {
|
2016-04-01 15:33:20 +02:00
|
|
|
if (cancelled) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.cancel = null
|
|
|
|
|
|
|
|
|
|
clearTimeout(this.timeout)
|
|
|
|
|
this.setState({
|
|
|
|
|
stats,
|
|
|
|
|
selectStatsLoading: false
|
|
|
|
|
}, () => {
|
2016-04-01 17:00:03 +02:00
|
|
|
this.timeout = setTimeout(this.loop, stats.interval * 1000)
|
2016-04-01 15:33:20 +02:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillMount () {
|
|
|
|
|
this.loop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
|
clearTimeout(this.timeout)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 17:11:51 +02:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 17:00:03 +02:00
|
|
|
@autobind
|
2016-04-01 15:33:20 +02:00
|
|
|
handleSelectStats (event) {
|
|
|
|
|
const granularity = event.target.value
|
|
|
|
|
clearTimeout(this.timeout)
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
granularity,
|
|
|
|
|
selectStatsLoading: true
|
2016-04-01 17:00:03 +02:00
|
|
|
}, this.loop)
|
2016-04-01 15:33:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
|
const {
|
|
|
|
|
intl
|
|
|
|
|
} = this.props
|
|
|
|
|
const {
|
|
|
|
|
granularity,
|
|
|
|
|
selectStatsLoading,
|
|
|
|
|
stats
|
|
|
|
|
} = this.state
|
|
|
|
|
|
|
|
|
|
return !stats
|
|
|
|
|
? <p>No stats.</p>
|
2016-06-14 10:44:44 +02:00
|
|
|
: process.env.XOA_PLAN > 2
|
|
|
|
|
? <Container>
|
|
|
|
|
<Row>
|
|
|
|
|
<Col mediumSize={6} className='text-xs-right'>
|
|
|
|
|
{selectStatsLoading && <Icon icon='loading' size={2} />}
|
|
|
|
|
</Col>
|
|
|
|
|
<Col mediumSize={6}>
|
|
|
|
|
<div className='btn-tab'>
|
|
|
|
|
<select className='form-control' onChange={this.handleSelectStats} defaultValue={granularity} >
|
|
|
|
|
<option value='seconds'>{intl.formatMessage(messages.statLastTenMinutes)}</option>
|
|
|
|
|
<option value='minutes'>{intl.formatMessage(messages.statLastTwoHours)}</option>
|
|
|
|
|
<option value='hours'>{intl.formatMessage(messages.statLastWeek)}</option>
|
|
|
|
|
<option value='days'>{intl.formatMessage(messages.statLastYear)}</option>
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
<Row>
|
|
|
|
|
<Col mediumSize={6}>
|
|
|
|
|
<h5 className='text-xs-center'><Icon icon='cpu' size={1} /> {_('statsCpu')}</h5>
|
|
|
|
|
<CpuLineChart data={stats} />
|
|
|
|
|
</Col>
|
|
|
|
|
<Col mediumSize={6}>
|
|
|
|
|
<h5 className='text-xs-center'><Icon icon='memory' size={1} /> {_('statsMemory')}</h5>
|
|
|
|
|
<MemoryLineChart data={stats} />
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
<br />
|
|
|
|
|
<hr />
|
|
|
|
|
<Row>
|
|
|
|
|
<Col mediumSize={6}>
|
|
|
|
|
<h5 className='text-xs-center'><Icon icon='network' size={1} /> {_('statsNetwork')}</h5>
|
|
|
|
|
<VifLineChart data={stats} />
|
|
|
|
|
</Col>
|
|
|
|
|
<Col mediumSize={6}>
|
|
|
|
|
<h5 className='text-xs-center'><Icon icon='disk' size={1} /> {_('statDisk')}</h5>
|
|
|
|
|
<XvdLineChart data={stats} />
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
</Container>
|
|
|
|
|
: <Container>
|
|
|
|
|
<Upgrade place='vmStats' available={3} />
|
|
|
|
|
</Container>
|
2016-04-01 15:33:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-01 12:42:55 +02:00
|
|
|
)
|