2019-01-13 16:10:23 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { hot } from 'react-hot-loader';
|
|
|
|
import { connect } from 'react-redux';
|
2019-04-29 11:28:41 -05:00
|
|
|
import { TimeRange, TimeZone, AbsoluteTimeRange } from '@grafana/ui';
|
2019-01-13 16:10:23 -06:00
|
|
|
|
|
|
|
import { ExploreId, ExploreItemState } from 'app/types/explore';
|
|
|
|
import { StoreState } from 'app/types';
|
|
|
|
|
2019-01-29 07:58:57 -06:00
|
|
|
import { toggleGraph, changeTime } from './state/actions';
|
2019-01-13 16:10:23 -06:00
|
|
|
import Graph from './Graph';
|
|
|
|
import Panel from './Panel';
|
2019-04-29 11:28:41 -05:00
|
|
|
import { getTimeZone } from '../profile/state/selectors';
|
2019-05-08 06:51:44 -05:00
|
|
|
import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
|
2019-01-13 16:10:23 -06:00
|
|
|
|
|
|
|
interface GraphContainerProps {
|
|
|
|
exploreId: ExploreId;
|
|
|
|
graphResult?: any[];
|
|
|
|
loading: boolean;
|
2019-04-29 11:28:41 -05:00
|
|
|
range: TimeRange;
|
|
|
|
timeZone: TimeZone;
|
2019-01-13 16:10:23 -06:00
|
|
|
showingGraph: boolean;
|
|
|
|
showingTable: boolean;
|
|
|
|
split: boolean;
|
2019-01-15 12:52:53 -06:00
|
|
|
toggleGraph: typeof toggleGraph;
|
2019-01-29 07:58:57 -06:00
|
|
|
changeTime: typeof changeTime;
|
2019-02-05 06:48:00 -06:00
|
|
|
width: number;
|
2019-01-13 16:10:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class GraphContainer extends PureComponent<GraphContainerProps> {
|
|
|
|
onClickGraphButton = () => {
|
2019-02-11 05:56:37 -06:00
|
|
|
this.props.toggleGraph(this.props.exploreId, this.props.showingGraph);
|
2019-01-13 16:10:23 -06:00
|
|
|
};
|
|
|
|
|
2019-04-29 11:28:41 -05:00
|
|
|
onChangeTime = (absRange: AbsoluteTimeRange) => {
|
|
|
|
const { exploreId, timeZone, changeTime } = this.props;
|
|
|
|
const range = {
|
2019-05-08 06:51:44 -05:00
|
|
|
from: timeZone.isUtc ? toUtc(absRange.from) : dateTime(absRange.from),
|
|
|
|
to: timeZone.isUtc ? toUtc(absRange.to) : dateTime(absRange.to),
|
2019-04-29 11:28:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
changeTime(exploreId, range);
|
2019-01-29 07:58:57 -06:00
|
|
|
};
|
|
|
|
|
2019-01-13 16:10:23 -06:00
|
|
|
render() {
|
2019-04-29 11:28:41 -05:00
|
|
|
const { exploreId, graphResult, loading, showingGraph, showingTable, range, split, width, timeZone } = this.props;
|
2019-02-05 06:48:00 -06:00
|
|
|
const graphHeight = showingGraph && showingTable ? 200 : 400;
|
2019-04-29 11:28:41 -05:00
|
|
|
const timeRange = { from: range.from.valueOf(), to: range.to.valueOf() };
|
2019-01-28 06:09:51 -06:00
|
|
|
|
2019-01-13 16:10:23 -06:00
|
|
|
return (
|
2019-05-17 05:45:11 -05:00
|
|
|
<Panel label="Graph" collapsible isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
|
|
|
|
{graphResult && (
|
|
|
|
<Graph
|
|
|
|
data={graphResult}
|
|
|
|
height={graphHeight}
|
|
|
|
id={`explore-graph-${exploreId}`}
|
|
|
|
onChangeTime={this.onChangeTime}
|
|
|
|
range={timeRange}
|
|
|
|
timeZone={timeZone}
|
|
|
|
split={split}
|
|
|
|
width={width}
|
|
|
|
/>
|
|
|
|
)}
|
2019-01-13 16:10:23 -06:00
|
|
|
</Panel>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state: StoreState, { exploreId }) {
|
|
|
|
const explore = state.explore;
|
|
|
|
const { split } = explore;
|
|
|
|
const item: ExploreItemState = explore[exploreId];
|
2019-05-10 07:00:39 -05:00
|
|
|
const { graphResult, graphIsLoading, range, showingGraph, showingTable } = item;
|
|
|
|
const loading = graphIsLoading;
|
2019-04-29 11:28:41 -05:00
|
|
|
return { graphResult, loading, range, showingGraph, showingTable, split, timeZone: getTimeZone(state.user) };
|
2019-01-13 16:10:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
2019-01-15 12:52:53 -06:00
|
|
|
toggleGraph,
|
2019-01-29 07:58:57 -06:00
|
|
|
changeTime,
|
2019-01-13 16:10:23 -06:00
|
|
|
};
|
|
|
|
|
2019-02-19 08:41:35 -06:00
|
|
|
export default hot(module)(
|
|
|
|
connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(GraphContainer)
|
|
|
|
);
|