Skip to content

Commit

Permalink
Changed the websocket global state variables a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarl committed Jun 6, 2017
1 parent 6f43bd9 commit dbe7e42
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
12 changes: 4 additions & 8 deletions client/app/scripts/actions/app-actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debug from 'debug';
import moment from 'moment';
import { find } from 'lodash';

import ActionTypes from '../constants/action-types';
Expand Down Expand Up @@ -451,20 +450,17 @@ export function startMovingInTime() {
};
}

export function websocketQueryTimestamp(queryTimestamp) {
const requestTimestamp = moment();
export function websocketQueryTimestamp(timestampSinceNow) {
// If the timestamp stands for a time less than one second ago,
// assume we are actually interested in the current time.
if (requestTimestamp.diff(queryTimestamp) >= 1000) {
queryTimestamp = queryTimestamp.toISOString();
} else {
queryTimestamp = null;
if (timestampSinceNow < 1000) {
timestampSinceNow = null;
}

return (dispatch, getState) => {
dispatch({
type: ActionTypes.WEBSOCKET_QUERY_TIMESTAMP,
queryTimestamp,
timestampSinceNow,
});
updateWebsocketChannel(getState(), dispatch);
dispatch(resetNodesDeltaBuffer());
Expand Down
2 changes: 1 addition & 1 deletion client/app/scripts/components/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function mapStateToProps(state) {
return {
errorUrl: state.get('errorUrl'),
filteredNodeCount: state.get('nodes').filter(node => node.get('filtered')).size,
showingCurrentState: !state.get('websocketQueryPastAt'),
showingCurrentState: !state.get('websocketTimestampOffset'),
topologiesLoaded: state.get('topologiesLoaded'),
topology: state.get('currentTopology'),
websocketClosed: state.get('websocketClosed'),
Expand Down
13 changes: 6 additions & 7 deletions client/app/scripts/components/timeline-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ class TimelineControl extends React.Component {
}

componentWillUnmount() {
this.updateTimestamp(moment());
this.updateTimestamp(null);
}

updateTimestamp(timestamp) {
this.props.websocketQueryTimestamp(timestamp);
updateTimestamp(timestampSinceNow) {
this.props.websocketQueryTimestamp(timestampSinceNow);
this.props.clickResumeUpdate();
}

Expand All @@ -132,9 +132,8 @@ class TimelineControl extends React.Component {

handleSliderChange(value) {
const offsetMilliseconds = this.getRangeMilliseconds() - value;
const timestamp = moment().utc().subtract(offsetMilliseconds);
this.props.startMovingInTime();
this.debouncedUpdateTimestamp(timestamp);
this.debouncedUpdateTimestamp(offsetMilliseconds);
this.setState({ offsetMilliseconds });
}

Expand All @@ -150,12 +149,12 @@ class TimelineControl extends React.Component {
rangeOptionSelected: sliderRanges.last1Hour,
});
this.props.startMovingInTime();
this.updateTimestamp(moment());
this.updateTimestamp(null);
}

getTotalOffset() {
const { rangeOptionSelected, offsetMilliseconds } = this.state;
const rangeBehindMilliseconds = moment().diff(rangeOptionSelected.getEnd());
const rangeBehindMilliseconds = moment().utc().diff(rangeOptionSelected.getEnd());
return offsetMilliseconds + rangeBehindMilliseconds;
}

Expand Down
9 changes: 6 additions & 3 deletions client/app/scripts/reducers/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../selectors/topology';
import { activeTopologyZoomCacheKeyPathSelector } from '../selectors/zooming';
import { consolidatedBeginningOfNodesDeltaBuffer } from '../utils/update-buffer-utils';
import { getWebsocketQueryTimestamp } from '../utils/web-api-utils';
import { applyPinnedSearches } from '../utils/search-utils';
import {
findTopologyById,
Expand Down Expand Up @@ -93,7 +94,7 @@ export const initialState = makeMap({
viewport: makeMap(),
websocketClosed: false,
websocketMovingInTime: false,
websocketQueryPastAt: null,
websocketQueryTimestampSinceNow: null,
zoomCache: makeMap(),
serviceImages: makeMap()
});
Expand Down Expand Up @@ -296,7 +297,9 @@ export function rootReducer(state = initialState, action) {
}

case ActionTypes.CLICK_PAUSE_UPDATE: {
return state.set('updatePausedAt', moment().utc());
const pausedAt = state.get('websocketQueryTimestampSinceNow') ?
moment(getWebsocketQueryTimestamp(state)) : moment().utc();
return state.set('updatePausedAt', pausedAt);
}

case ActionTypes.CLICK_RELATIVE: {
Expand Down Expand Up @@ -356,7 +359,7 @@ export function rootReducer(state = initialState, action) {
}

case ActionTypes.WEBSOCKET_QUERY_TIMESTAMP: {
return state.set('websocketQueryPastAt', action.queryTimestamp);
return state.set('websocketQueryTimestampSinceNow', action.timestampSinceNow);
}

case ActionTypes.CLOSE_WEBSOCKET: {
Expand Down
8 changes: 7 additions & 1 deletion client/app/scripts/utils/web-api-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import debug from 'debug';
import moment from 'moment';
import reqwest from 'reqwest';
import { defaults } from 'lodash';
import { fromJS, Map as makeMap, List } from 'immutable';
Expand Down Expand Up @@ -236,10 +237,15 @@ export function getTopologies(options, dispatch, initialPoll) {
});
}

export function getWebsocketQueryTimestamp(state) {
const sinceNow = state.get('websocketQueryTimestampSinceNow');
return sinceNow ? moment().utc().subtract(sinceNow).toISOString() : null;
}

export function updateWebsocketChannel(state, dispatch) {
const topologyUrl = getCurrentTopologyUrl(state);
const topologyOptions = activeTopologyOptionsSelector(state);
const queryTimestamp = state.get('websocketQueryPastAt');
const queryTimestamp = getWebsocketQueryTimestamp(state);
const websocketUrl = buildWebsocketUrl(topologyUrl, topologyOptions, queryTimestamp);
// Only recreate websocket if url changed or if forced (weave cloud instance reload);
const isNewUrl = websocketUrl !== currentUrl;
Expand Down

0 comments on commit dbe7e42

Please sign in to comment.