Skip to content

Commit

Permalink
converted file to functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
briannguyen4 committed Jan 26, 2022
1 parent 4ad5ad0 commit fdbb3d4
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions superset-frontend/src/SqlLab/components/QueryAutoRefresh/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
Expand All @@ -28,78 +29,73 @@ const QUERY_UPDATE_BUFFER_MS = 5000;
const MAX_QUERY_AGE_TO_POLL = 21600000;
const QUERY_TIMEOUT_LIMIT = 10000;

class QueryAutoRefresh extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
offline: props.offline,
};
}

UNSAFE_componentWillMount() {
this.startTimer();
}

componentDidUpdate(prevProps) {
if (prevProps.offline !== this.state.offline) {
this.props.actions.setUserOffline(this.state.offline);
}
}
const QueryAutoRefresh = ({ offline, queries, queriesLastUpdate, actions }) => {
const [offlineState, setOfflineState] = useState(offline);

componentWillUnmount() {
this.stopTimer();
}

shouldCheckForQueries() {
const shouldCheckForQueries = () => {
// if there are started or running queries, this method should return true
const { queries } = this.props;
const now = new Date().getTime();
const isQueryRunning = q =>
['running', 'started', 'pending', 'fetching'].indexOf(q.state) >= 0;

return Object.values(queries).some(
q => isQueryRunning(q) && now - q.startDttm < MAX_QUERY_AGE_TO_POLL,
);
}
};

startTimer() {
if (!this.timer) {
this.timer = setInterval(this.stopwatch.bind(this), QUERY_UPDATE_FREQ);
const startTimer = () => {
if (!timer) {
timer = setInterval(stopwatch(), QUERY_UPDATE_FREQ);
}
}
};

stopTimer() {
clearInterval(this.timer);
this.timer = null;
}
const stopTimer = () => {
clearInterval(timer);
timer = null;
};

stopwatch() {
const stopwatch = () => {
// only poll /superset/queries/ if there are started or running queries
if (this.shouldCheckForQueries()) {
if (shouldCheckForQueries()) {
SupersetClient.get({
endpoint: `/superset/queries/${
this.props.queriesLastUpdate - QUERY_UPDATE_BUFFER_MS
queriesLastUpdate - QUERY_UPDATE_BUFFER_MS
}`,
timeout: QUERY_TIMEOUT_LIMIT,
})
.then(({ json }) => {
if (Object.keys(json).length > 0) {
this.props.actions.refreshQueries(json);
actions.refreshQueries(json);
}
this.setState({ offline: false });

setOfflineState(false);
})
.catch(() => {
this.setState({ offline: true });
setOfflineState(true);
});
} else {
this.setState({ offline: false });
setOfflineState(false);
}
}
};

useEffect(() => {
console.log('component mounted!');
startTimer();
}, []);

useEffect(() => {
return () => {
stopTimer();
};
}, []);

useEffect(() => {
actions.setUserOffline(offline);
}, [offline]);

return null;
};

render() {
return null;
}
}
QueryAutoRefresh.propTypes = {
offline: PropTypes.bool.isRequired,
queries: PropTypes.object.isRequired,
Expand Down

0 comments on commit fdbb3d4

Please sign in to comment.