Merge pull request #210 from influxdata/cleanup

Cleanup
pull/214/head
Andrew Watkins 2016-10-10 16:53:47 -07:00 committed by GitHub
commit 4d20797305
2 changed files with 35 additions and 24 deletions

View File

@ -0,0 +1,27 @@
import {proxy} from 'utils/queryUrlGenerator';
import _ from 'lodash';
export function getCpuAndLoadForHosts(proxyLink) {
return proxy({
source: proxyLink,
query: `select mean(usage_user) from cpu where cpu = 'cpu-total' and time > now() - 10m group by host; select mean("load1") from "telegraf"."default"."system" where time > now() - 10m group by host`,
db: 'telegraf',
}).then((resp) => {
const hosts = {};
const precision = 100;
resp.data.results[0].series.forEach((s) => {
const meanIndex = s.columns.findIndex((col) => col === 'mean');
hosts[s.tags.host] = {
name: s.tags.host,
cpu: (Math.round(s.values[0][meanIndex] * precision) / precision).toFixed(2),
};
});
resp.data.results[1].series.forEach((s) => {
const meanIndex = s.columns.findIndex((col) => col === 'mean');
hosts[s.tags.host].load = (Math.round(s.values[0][meanIndex] * precision) / precision).toFixed(2);
});
return _.values(hosts);
});
}

View File

@ -1,8 +1,7 @@
import React, {PropTypes} from 'react'; import React, {PropTypes} from 'react';
import _ from 'lodash';
import FlashMessages from 'shared/components/FlashMessages'; import FlashMessages from 'shared/components/FlashMessages';
import HostsTable from '../components/HostsTable'; import HostsTable from '../components/HostsTable';
import {proxy} from 'utils/queryUrlGenerator'; import {getCpuAndLoadForHosts} from '../apis';
export const HostsPage = React.createClass({ export const HostsPage = React.createClass({
propTypes: { propTypes: {
@ -14,6 +13,7 @@ export const HostsPage = React.createClass({
proxy: PropTypes.string.isRequired, proxy: PropTypes.string.isRequired,
}).isRequired, }).isRequired,
}).isRequired, }).isRequired,
addFlashMessage: PropTypes.func.isRequired,
}, },
getInitialState() { getInitialState() {
@ -23,28 +23,12 @@ export const HostsPage = React.createClass({
}, },
componentDidMount() { componentDidMount() {
proxy({ getCpuAndLoadForHosts(this.props.source.links.proxy).then((hosts) => {
source: this.props.source.links.proxy, this.setState({hosts});
query: `select mean(usage_user) from cpu where cpu = 'cpu-total' and time > now() - 10m group by host; select mean("load1") from "telegraf"."default"."system" where time > now() - 10m group by host`, }).catch(() => {
db: 'telegraf', this.props.addFlashMessage({
}).then((resp) => { type: 'error',
const hosts = {}; text: `There was an error finding hosts. Check that your server is running.`,
const precision = 100;
resp.data.results[0].series.forEach((s) => {
const meanIndex = s.columns.findIndex((col) => col === 'mean');
hosts[s.tags.host] = {
name: s.tags.host,
cpu: (Math.round(s.values[0][meanIndex] * precision) / precision).toFixed(2),
};
});
resp.data.results[1].series.forEach((s) => {
const meanIndex = s.columns.findIndex((col) => col === 'mean');
hosts[s.tags.host].load = (Math.round(s.values[0][meanIndex] * precision) / precision).toFixed(2);
});
this.setState({
hosts: _.values(hosts),
}); });
}); });
}, },