More TS conversions

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2019-02-14 22:59:57 +01:00
parent 5764b90fa0
commit cd991bf32b
4 changed files with 65 additions and 26 deletions

9
package-lock.json generated
View file

@ -946,6 +946,15 @@
"@types/sizzle": "*" "@types/sizzle": "*"
} }
}, },
"@types/moment-timezone": {
"version": "0.5.10",
"resolved": "https://registry.npmjs.org/@types/moment-timezone/-/moment-timezone-0.5.10.tgz",
"integrity": "sha512-sSFfubyYd0Z9C89/M5wZ+GJOWNTYTPhXAB2wrzmElJcQROyWTj0y1NCLSwYqovDYeFezlFE8+aopIyZTMsDVnA==",
"dev": true,
"requires": {
"moment": ">=2.14.0"
}
},
"@types/node": { "@types/node": {
"version": "11.9.3", "version": "11.9.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-11.9.3.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-11.9.3.tgz",

View file

@ -45,6 +45,7 @@
"not op_mini all" "not op_mini all"
], ],
"devDependencies": { "devDependencies": {
"@types/moment-timezone": "^0.5.10",
"@types/reactstrap": "^7.1.3" "@types/reactstrap": "^7.1.3"
} }
} }

View file

@ -36,9 +36,9 @@ interface GraphControlsProps {
resolution: number | null; resolution: number | null;
stacked: boolean; stacked: boolean;
onChangeRange: (range: number | null) => void; onChangeRange: (range: number) => void;
onChangeEndTime: (endTime: number | null) => void; onChangeEndTime: (endTime: number) => void;
onChangeResolution: (resolution: number | null) => void; onChangeResolution: (resolution: number) => void;
onChangeStacking: (stacked: boolean) => void; onChangeStacking: (stacked: boolean) => void;
} }
@ -101,7 +101,7 @@ class GraphControls extends Component<GraphControlsProps> {
if (range === null) { if (range === null) {
this.changeRangeInput(this.props.range); this.changeRangeInput(this.props.range);
} else { } else {
this.props.onChangeRange(this.parseRange(rangeText)); this.props.onChangeRange(range);
} }
} }

View file

@ -20,28 +20,57 @@ import Graph from './Graph';
import DataTable from './DataTable'; import DataTable from './DataTable';
import TimeInput from './TimeInput'; import TimeInput from './TimeInput';
class Panel extends Component { interface PanelProps {
constructor(props) { metricNames: string[];
removePanel: () => void;
// TODO Put initial panel values here.
}
interface PanelState {
expr: string;
type: 'graph' | 'table';
range: number;
endTime: number | null;
resolution: number | null;
stacked: boolean;
data: any; // TODO: Define data.
lastQueryParams: {
startTime: number,
endTime: number,
resolution: number,
} | null,
loading: boolean;
error: string | null;
stats: null; // TODO: Stats.
}
class Panel extends Component<PanelProps, PanelState> {
private abortInFlightFetch: (() => void) | null = null;
constructor(props: PanelProps) {
super(props); super(props);
this.state = { this.state = {
expr: 'rate(node_cpu_seconds_total[1m])', expr: 'rate(node_cpu_seconds_total[1m])',
type: 'graph', // TODO enum? type: 'graph',
range: 3600, range: 3600,
endTime: null, // This is in milliseconds. endTime: null, // This is in milliseconds.
resolution: null, resolution: null,
stacked: false, stacked: false,
data: null, data: null,
lastQueryParams: null,
loading: false,
error: null, error: null,
stats: null, stats: null,
}; };
} }
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps: PanelProps, prevState: PanelState) {
const needsRefresh = ['type', 'range', 'endTime', 'resolution'].some(v => { if (prevState.type !== this.state.type ||
return prevState[v] !== this.state[v]; prevState.range !== this.state.range ||
}) prevState.endTime !== this.state.endTime ||
if (needsRefresh) { prevState.resolution !== this.state.resolution) {
if (prevState.type !== this.state.type) { if (prevState.type !== this.state.type) {
// If the other options change, we still want to show the old data until the new // If the other options change, we still want to show the old data until the new
// query completes, but this is not a good idea when we actually change between // query completes, but this is not a good idea when we actually change between
@ -56,7 +85,7 @@ class Panel extends Component {
this.executeQuery(); this.executeQuery();
} }
executeQuery = ()=> { executeQuery = (): void => {
if (this.state.expr === '') { if (this.state.expr === '') {
return; return;
} }
@ -70,12 +99,12 @@ class Panel extends Component {
this.abortInFlightFetch = () => abortController.abort(); this.abortInFlightFetch = () => abortController.abort();
this.setState({loading: true}); this.setState({loading: true});
let endTime = this.getEndTime() / 1000; const endTime = this.getEndTime().valueOf() / 1000; // TODO: shouldn'T valueof only work when it's a moment?
let startTime = endTime - this.state.range; const startTime = endTime - this.state.range;
let resolution = this.state.resolution || Math.max(Math.floor(this.state.range / 250), 1); const resolution = this.state.resolution || Math.max(Math.floor(this.state.range / 250), 1);
let url = new URL('http://demo.robustperception.io:9090/');//window.location.href); const url = new URL('http://demo.robustperception.io:9090/');//window.location.href);
let params = { const params: {[key: string]: string} = {
'query': this.state.expr, 'query': this.state.expr,
}; };
@ -100,7 +129,7 @@ class Panel extends Component {
} }
Object.keys(params).forEach(key => url.searchParams.append(key, params[key])) Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url, {cache: 'no-store', signal: abortController.signal}) fetch(url.toString(), {cache: 'no-store', signal: abortController.signal})
.then(resp => resp.json()) .then(resp => resp.json())
.then(json => { .then(json => {
if (json.status !== 'success') { if (json.status !== 'success') {
@ -131,26 +160,26 @@ class Panel extends Component {
}); });
} }
handleExpressionChange = (expr) => { handleExpressionChange = (expr: string): void => {
this.setState({expr: expr}); this.setState({expr: expr});
} }
handleChangeRange = (range) => { handleChangeRange = (range: number): void => {
this.setState({range: range}); this.setState({range: range});
} }
getEndTime = () => { getEndTime = (): number | moment.Moment => {
if (this.state.endTime === null) { if (this.state.endTime === null) {
return moment(); return moment();
} }
return this.state.endTime; return this.state.endTime;
} }
handleChangeEndTime = (endTime) => { handleChangeEndTime = (endTime: number) => {
this.setState({endTime: endTime}); this.setState({endTime: endTime});
} }
handleChangeResolution = (resolution) => { handleChangeResolution = (resolution: number) => {
// TODO: Where should we validate domain model constraints? In the parent's // TODO: Where should we validate domain model constraints? In the parent's
// change handler like here, or in the calling component? // change handler like here, or in the calling component?
if (resolution > 0) { if (resolution > 0) {
@ -194,7 +223,7 @@ class Panel extends Component {
// self.submitQuery(); // self.submitQuery();
// }; // };
handleChangeStacking = (stacked) => { handleChangeStacking = (stacked: boolean) => {
this.setState({stacked: stacked}); this.setState({stacked: stacked});
} }
@ -259,7 +288,7 @@ class Panel extends Component {
<TabPane tabId="table"> <TabPane tabId="table">
{this.state.type === 'table' && {this.state.type === 'table' &&
<> <>
<div class="table-controls"> <div className="table-controls">
<TimeInput endTime={this.state.endTime} range={this.state.range} onChangeEndTime={this.handleChangeEndTime} /> <TimeInput endTime={this.state.endTime} range={this.state.range} onChangeEndTime={this.handleChangeEndTime} />
</div> </div>
<DataTable data={this.state.data} /> <DataTable data={this.state.data} />