Files
xen-orchestra/src/xo-app/vm/tab-stats.js
T

133 lines
3.7 KiB
JavaScript
Raw Normal View History

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'
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 {
@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
}, () => {
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
})
}
}
@autobind
2016-04-01 15:33:20 +02:00
handleSelectStats (event) {
const granularity = event.target.value
clearTimeout(this.timeout)
this.setState({
granularity,
selectStatsLoading: true
}, 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-05-26 10:31:49 +02:00
: <Container>
2016-04-01 15:33:20 +02:00
<Row>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6} className='text-xs-right'>
{selectStatsLoading && <Icon icon='loading' size={2} />}
2016-04-01 15:33:20 +02:00
</Col>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6}>
2016-04-01 15:33:20 +02:00
<div className='btn-tab'>
<select className='form-control' onChange={this.handleSelectStats} defaultValue={granularity} >
2016-04-01 16:39:59 +02:00
<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>
2016-04-01 15:33:20 +02:00
</select>
</div>
</Col>
</Row>
<Row>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6}>
2016-04-01 15:33:20 +02:00
<h5 className='text-xs-center'><Icon icon='cpu' size={1} /> {_('statsCpu')}</h5>
<CpuLineChart data={stats} />
</Col>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6}>
2016-04-01 15:33:20 +02:00
<h5 className='text-xs-center'><Icon icon='memory' size={1} /> {_('statsMemory')}</h5>
<MemoryLineChart data={stats} />
</Col>
</Row>
<br />
<hr />
2016-04-01 15:33:20 +02:00
<Row>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6}>
2016-04-01 15:33:20 +02:00
<h5 className='text-xs-center'><Icon icon='network' size={1} /> {_('statsNetwork')}</h5>
<VifLineChart data={stats} />
</Col>
2016-05-26 10:31:49 +02:00
<Col mediumSize={6}>
2016-04-01 15:33:20 +02:00
<h5 className='text-xs-center'><Icon icon='disk' size={1} /> {_('statDisk')}</h5>
<XvdLineChart data={stats} />
</Col>
</Row>
2016-05-26 10:31:49 +02:00
</Container>
2016-04-01 15:33:20 +02:00
}
}
2016-04-01 12:42:55 +02:00
)