All files / javascripts/SqlLab/components QueryAutoRefresh.jsx

0% Statements 0/25
0% Branches 0/4
0% Functions 0/4
0% Lines 0/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                               
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as Actions from '../actions';
 
const $ = require('jquery');
const QUERY_UPDATE_FREQ = 1000;
const QUERY_UPDATE_BUFFER_MS = 5000;
 
class QueryAutoRefresh extends React.PureComponent {
  componentWillMount() {
    this.startTimer();
  }
  componentWillUnmount() {
    this.stopTimer();
  }
  startTimer() {
    if (!(this.timer)) {
      this.timer = setInterval(this.stopwatch.bind(this), QUERY_UPDATE_FREQ);
    }
  }
  stopTimer() {
    clearInterval(this.timer);
    this.timer = null;
  }
  stopwatch() {
    const url = '/superset/queries/' + (this.props.queriesLastUpdate - QUERY_UPDATE_BUFFER_MS);
    // No updates in case of failure.
    $.getJSON(url, (data) => {
      if (Object.keys(data).length > 0) {
        this.props.actions.refreshQueries(data);
      }
      this.props.actions.setNetworkStatus(true);
    })
    .fail(() => {
      this.props.actions.setNetworkStatus(false);
    });
  }
  render() {
    return null;
  }
}
QueryAutoRefresh.propTypes = {
  actions: React.PropTypes.object,
  queriesLastUpdate: React.PropTypes.number,
};
QueryAutoRefresh.defaultProps = {
  // queries: null,
};
 
function mapStateToProps(state) {
  return {
    queriesLastUpdate: state.queriesLastUpdate,
  };
}
 
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch),
  };
}
 
export default connect(mapStateToProps, mapDispatchToProps)(QueryAutoRefresh);