Skip to content

Commit

Permalink
Decouple fetching layer from Redux, part 1: Beamline API
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Nov 7, 2023
1 parent 590b949 commit e13d7be
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 98 deletions.
8 changes: 4 additions & 4 deletions mxcubeweb/routes/beamline.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ def beamline_abort_action(name):
:param str name: Owner / Actuator of the process/action to abort
Replies with status code 200 on success and 520 on exceptions.
Replies with status code 200 on success and 500 on exceptions.
"""
try:
app.beamline.beamline_abort_action(name)
except Exception:
err = str(sys.exc_info()[1])
return make_response(err, 520)
return make_response(err, 500)
else:
logging.getLogger("user_level_log").error("%s, aborted" % name)
return make_response("{}", 200)
Expand All @@ -203,7 +203,7 @@ def beamline_run_action(name):
:param str name: action to run
Replies with status code 200 on success and 520 on exceptions.
Replies with status code 200 on success and 500 on exceptions.
"""
try:
params = request.get_json()["parameters"]
Expand All @@ -213,7 +213,7 @@ def beamline_run_action(name):
try:
app.beamline.beamline_run_action(name, params)
except Exception as ex:
return make_response(str(ex), 520)
return make_response(str(ex), 500)
else:
return make_response("{}", 200)

Expand Down
40 changes: 4 additions & 36 deletions ui/src/actions/beamline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable promise/prefer-await-to-then */
/* eslint-disable sonarjs/no-duplicate-string */
import fetch from 'isomorphic-fetch';
import { sendPrepareBeamlineForNewSample } from '../api/beamline';
// The different states a beamline attribute can assume.
export const STATE = {
IDLE: 'READY',
Expand Down Expand Up @@ -46,30 +47,6 @@ export function updateBeamlineHardwareObjectStateAction(data) {
return { type: BL_UPDATE_HARDWARE_OBJECT_STATE, data };
}

export function sendGetAllhardwareObjects() {
const url = 'mxcube/api/v0.1/beamline/';

return (dispatch) => {
fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json', // eslint-disable-line sonarjs/no-duplicate-string
'Content-type': 'application/json',
},
credentials: 'include',
})
.then((response) => response.json())
.then(
(data) => {
dispatch(getBeamlineAttrsAction(data));
},
() => {
throw new Error(`GET ${url} failed`);
},
);
};
}

export function setBeamlineAttribute(name, value) {
return updateBeamlineHardwareObjectAction({ name, value });
}
Expand Down Expand Up @@ -119,17 +96,8 @@ export function sendAbortCurrentAction(name) {
};
}

export function sendPrepareForNewSample() {
return () => {
fetch('mxcube/api/v0.1/beamline/prepare_beamline', {
method: 'PUT',
credentials: 'include',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
});
};
export function prepareBeamlineForNewSample() {
return () => sendPrepareBeamlineForNewSample();
}

export function sendDisplayImage(path) {
Expand Down
26 changes: 3 additions & 23 deletions ui/src/actions/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable sonarjs/no-duplicate-string */
import fetch from 'isomorphic-fetch';
import { unselectShapes } from './sampleview'; // eslint-disable-line import/no-cycle
import { fetchBeamInfo, fetchBeamlineSetup } from '../api/beamline';

export function addUserMessage(records, target) {
return {
Expand Down Expand Up @@ -96,22 +97,6 @@ export function getInitialState(userInControl) {
'Content-type': 'application/json',
},
});
const beamInfo = fetch('mxcube/api/v0.1/beamline/beam/info', {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
});
const beamlineSetup = fetch('mxcube/api/v0.1/beamline/', {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
});
const sampleVideoInfo = fetch('mxcube/api/v0.1/sampleview/camera', {
method: 'GET',
credentials: 'include',
Expand Down Expand Up @@ -217,19 +202,14 @@ export function getInitialState(userInControl) {
state.queue = json;
})
.catch(notify),
beamInfo
.then(parse)
fetchBeamInfo()
.then((json) => {
state.beamInfo = json;
})
.catch(notify),
beamlineSetup
.then(parse)
fetchBeamlineSetup()
.then((json) => {
state.beamlineSetup = json;
return json;
})
.then((json) => {
state.datapath = json.path;
return json;
})
Expand Down
10 changes: 5 additions & 5 deletions ui/src/actions/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fetch from 'isomorphic-fetch';
import { showErrorPanel, setLoading, getInitialState } from './general';
import { serverIO } from '../serverIO'; // eslint-disable-line import/no-cycle
import { fetchLoginInfo, logIn, signOut } from '../api/login';
import { fetchLoginInfo, sendLogIn, sendSignOut } from '../api/login';

export function setLoginInfo(loginInfo) {
return {
Expand Down Expand Up @@ -80,7 +80,7 @@ export function getLoginInfo() {
);
}

export function doLogIn(proposal, password, navigate) {
export function logIn(proposal, password, navigate) {
// eslint-disable-next-line sonarjs/cognitive-complexity
return (dispatch) => {
const previousUser = localStorage.getItem('currentUser');
Expand All @@ -90,7 +90,7 @@ export function doLogIn(proposal, password, navigate) {
serverIO.connect();
}

logIn(proposal, password, previousUser).then(
sendLogIn(proposal, password, previousUser).then(
(res) => {
if (res.code === 'ok') {
dispatch(showErrorPanel(false));
Expand Down Expand Up @@ -131,12 +131,12 @@ export function forcedSignout() {
};
}

export function doSignOut(navigate) {
export function signOut(navigate) {
return (dispatch) => {
serverIO.disconnect();
localStorage.setItem('currentUser', '');

return signOut().then(() => {
return sendSignOut().then(() => {
dispatch({ type: 'SIGNOUT' });
dispatch(getLoginInfo());

Expand Down
15 changes: 15 additions & 0 deletions ui/src/api/beamline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import api from '.';

const endpoint = api.url('/beamline');

export function fetchBeamlineSetup() {
return endpoint.get('/').json();
}

export function fetchBeamInfo() {
return endpoint.get('/beam/info').json();
}

export function sendPrepareBeamlineForNewSample() {
return endpoint.put(undefined, '/prepare_beamline').res();
}
6 changes: 3 additions & 3 deletions ui/src/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import api from '.';

const endpoint = api.url('/login');

export function logIn(proposal, password, previousUser) {
export function sendLogIn(proposal, password, previousUser) {
return endpoint.post({ proposal, password, previousUser }, '/').json();
}

export function signOut() {
export function sendSignOut() {
return endpoint.headers({ Accept: '*/*' }).get('/signout').res();
}

Expand All @@ -18,6 +18,6 @@ export function sendFeedback(sender, content) {
return endpoint.post({ sender, content }, '/send_feedback').res();
}

export function refreshSession() {
export function sendRefreshSession() {
return endpoint.get('/refresh_session').res();
}
8 changes: 4 additions & 4 deletions ui/src/components/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function LoginComponent(props) {
router,
loading,
setLoading,
doLogIn,
doSignOut,
logIn,
signOut,
showError,
errorMessage,
} = props;
Expand All @@ -34,7 +34,7 @@ function LoginComponent(props) {

function handleSubmit(data) {
setLoading(true);
doLogIn(data.username.toLowerCase(), data.password, router.navigate);
logIn(data.username.toLowerCase(), data.password, router.navigate);
}

return (
Expand All @@ -49,7 +49,7 @@ function LoginComponent(props) {
sendSelectProposal={(selected) =>
sendSelectProposal(selected, router.navigate)
}
singOut={() => doSignOut(router.navigate)}
signOut={() => signOut(router.navigate)}
/>
)}
<Form
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/Login/SelectProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SelectProposal extends React.Component {
}

handleCancel() {
this.props.singOut();
this.props.signOut();
this.props.hide();
}

Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/MXNavbar/MXNavbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MXNavbar extends React.Component {
}

handleSignOutClick() {
this.props.doSignOut();
this.props.signOut();
this.props.router.navigate('/login', { replace: true });
}

Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/SampleQueue/TodoTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class TodoTree extends React.Component {
}

showAddSampleForm() {
this.props.sendPrepareForNewSample();
this.props.prepareBeamlineForNewSample();
this.props.showForm('AddSample');
}

Expand Down
10 changes: 1 addition & 9 deletions ui/src/containers/BeamlineSetupContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import * as sampleViewActions from '../actions/sampleview'; // eslint-disable-li

import { find, filter } from 'lodash';

import {
sendGetAllhardwareObjects,
sendSetAttribute,
sendAbortCurrentAction,
} from '../actions/beamline';
import { sendSetAttribute, sendAbortCurrentAction } from '../actions/beamline';

import { sendCommand } from '../actions/sampleChanger';

Expand Down Expand Up @@ -323,10 +319,6 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
getAllhardwareObjects: bindActionCreators(
sendGetAllhardwareObjects,
dispatch,
),
sampleViewActions: bindActionCreators(sampleViewActions, dispatch),
setAttribute: bindActionCreators(sendSetAttribute, dispatch),
sendCommand: bindActionCreators(sendCommand, dispatch),
Expand Down
8 changes: 4 additions & 4 deletions ui/src/containers/LoginContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {
doLogIn,
doSignOut,
logIn,
signOut,
selectProposal,
sendSelectProposal,
hideProposalsForm,
Expand All @@ -30,8 +30,8 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
doLogIn: bindActionCreators(doLogIn, dispatch),
doSignOut: bindActionCreators(doSignOut, dispatch),
logIn: bindActionCreators(logIn, dispatch),
signOut: bindActionCreators(signOut, dispatch),
setLoading: bindActionCreators(setLoading, dispatch),
selectProposal: bindActionCreators(selectProposal, dispatch),
sendSelectProposal: bindActionCreators(sendSelectProposal, dispatch),
Expand Down
6 changes: 3 additions & 3 deletions ui/src/containers/MXNavbarContainer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import { connect } from 'react-redux';
import MXNavbar from '../components/MXNavbar/MXNavbar';
import { doSignOut } from '../actions/login';
import { signOut } from '../actions/login';

class MXNavbarContainer extends React.Component {
render() {
return (
<MXNavbar
user={this.props.user}
selectedProposal={this.props.selectedProposal}
doSignOut={this.props.doSignOut}
signOut={this.props.signOut}
loggedIn={this.props.loggedIn}
location={this.props.location}
setAutomatic={this.props.setAutomatic}
Expand All @@ -32,7 +32,7 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
doSignOut: (navigate) => dispatch(doSignOut(navigate)),
signOut: (navigate) => dispatch(signOut(navigate)),
};
}

Expand Down
4 changes: 2 additions & 2 deletions ui/src/containers/SampleQueueContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class SampleQueueContainer extends React.Component {
} = this.props.queueActions;
const { collapseItem, showConfirmCollectDialog, selectItem, showList } =
this.props.queueGUIActions;
const { sendPrepareForNewSample } = this.props.beamlineActions;
const { prepareBeamlineForNewSample } = this.props.beamlineActions;
const { loadSample, unloadSample } = this.props.sampleChangerActions;

// go through the queue, check if sample has been collected or not
Expand Down Expand Up @@ -218,7 +218,7 @@ class SampleQueueContainer extends React.Component {
showForm={showForm}
queueStatus={queueStatus}
showList={showList}
sendPrepareForNewSample={sendPrepareForNewSample}
prepareBeamlineForNewSample={prepareBeamlineForNewSample}
/>
<div className="queue-messages">
<div className="queue-messages-title">
Expand Down
4 changes: 2 additions & 2 deletions ui/src/serverIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
import { setEnergyScanResult } from './actions/taskResults';

import { CLICK_CENTRING } from './constants';
import { refreshSession } from './api/login';
import { sendRefreshSession } from './api/login';

class ServerIO {
constructor() {
Expand Down Expand Up @@ -130,7 +130,7 @@ class ServerIO {
this.initialized = true;
this.dispatch = store.dispatch;

this.refreshInterval = setInterval(refreshSession, 9000);
this.refreshInterval = setInterval(sendRefreshSession, 9000);

this.connect();

Expand Down

0 comments on commit e13d7be

Please sign in to comment.