Skip to content

Commit

Permalink
feat: get combines images after save
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Nov 7, 2023
1 parent 3cad384 commit 51389a5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
54 changes: 54 additions & 0 deletions app/api/chemotion/chem_spectra_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,60 @@ def raw_file(att)
post 'refresh' do
convert_for_refresh(params)
end

desc 'Combine spectra'
params do
requires :spectra_ids, type: [Integer]
requires :front_spectra_idx, type: Integer # index of front spectra
end
post 'combine_spectra' do
pm = to_rails_snake_case(params)

list_file = []
list_file_names = []
container_id = -1
combined_image_filename = ""
Attachment.where(id: pm[:spectra_ids]).each do |att|
container = att.container
combined_image_filename = "#{container.name}.new_combined.png"
container_id = att.attachable_id
list_file_names.push(att.filename)
list_file.push(att.abs_path)
end

_, image = Chemotion::Jcamp::CombineImg.combine(
list_file, pm[:front_spectra_idx], list_file_names
)

content_type('application/json')
unless image.nil?
att = Attachment.find_by(filename: combined_image_filename, attachable_id: container_id)
if att.nil?
att = Attachment.new(
bucket: container_id,
filename: combined_image_filename,
key: SecureRandom.uuid,
created_by: current_user.id,
created_for: current_user.id,
content_type: 'image/png',
file_path: image.path,
attachable_type: 'Container',
attachable_id: container_id,
storage: Rails.configuration.storage.primary_store,
)
att.save!
else
att.update!(
storage: Rails.configuration.storage.primary_store,
file_path: image.path,
attachable_type: 'Container',
attachable_id: container_id,
)
end
end

{ status: true }
end
end

resource :predict do
Expand Down
12 changes: 8 additions & 4 deletions app/packs/src/apps/mydb/elements/details/ViewSpectra.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class ViewSpectra extends React.Component {
}

saveOp({
peaks, shift, scan, thres, analysis, keepPred, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt, simulatenmr = false
peaks, shift, scan, thres, analysis, keepPred, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt, simulatenmr = false, layout
}) {
const { handleSubmit } = this.props;
const { curveIdx } = curveSt;
Expand All @@ -405,7 +405,9 @@ class ViewSpectra extends React.Component {
const selectedIntegration = integrations[curveIdx];
const { multiplicities } = multiplicity;
const selectedMutiplicity = multiplicities[curveIdx];


const isSaveCombined = FN.isCyclicVoltaLayout(layout);
const { spcInfos } = this.state;
LoadingActions.start.defer();
SpectraActions.SaveToFile.defer(
si,
Expand All @@ -422,6 +424,8 @@ class ViewSpectra extends React.Component {
cyclicvolta,
curveIdx,
simulatenmr,
spcInfos,
isSaveCombined,
);
}

Expand Down Expand Up @@ -455,10 +459,10 @@ class ViewSpectra extends React.Component {
}

saveCloseOp({
peaks, shift, scan, thres, analysis, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt
peaks, shift, scan, thres, analysis, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt, layout
}) {
this.saveOp({
peaks, shift, scan, thres, analysis, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt
peaks, shift, scan, thres, analysis, integration, multiplicity, waveLength, cyclicvoltaSt, curveSt, layout
});
this.closeOp();
}
Expand Down
66 changes: 64 additions & 2 deletions app/packs/src/fetchers/AttachmentFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ export default class AttachmentFetcher {
waveLengthStr,
cyclicvolta,
curveIdx,
simulatenmr
simulatenmr,
previousSpcInfos,
isSaveCombined
) {
const params = {
attachmentId: attId,
Expand Down Expand Up @@ -541,11 +543,41 @@ export default class AttachmentFetcher {
body: JSON.stringify(decamelizeKeys(params)),
})
.then(response => response.json())
.then(json => json)
.then((json) => {
if (!isSaveCombined) {
return json;
}
const oldSpcInfos = [...previousSpcInfos].filter((spc) => {
return spc.idx !== attId;
});
let jcampIds = oldSpcInfos.map((spc) => (spc.idx));
const fetchedFilesIdxs = json.files.map((file) => (file.id));
jcampIds = [...jcampIds, ...fetchedFilesIdxs];

AttachmentFetcher.combineSpectra(jcampIds, curveIdx).then((res) => {
return json;
}).catch((errMsg) => {
console.log(errMsg); // eslint-disable-line
});
})
.catch(errorMessage => {
console.log(errorMessage);
});

// const oldSpcInfos = [...previousSpcInfos].filter((spc) => {
// return spc.idx !== spcInfo.idx;
// });
// let jcampIds = oldSpcInfos.map((spc) => (spc.idx));
// const fetchedFilesIdxs = fetchedFiles.files.map((file) => (file.id));
// jcampIds = [...jcampIds, ...fetchedFilesIdxs];

// AttachmentFetcher.combineSpectra(jcampIds, spcInfo.idSp, curveIdx).then((res) => {
// dispatch({ fetchedFiles, spcInfo });
// cb();
// }).catch((errMsg) => {
// console.log(errMsg); // eslint-disable-line
// });

return promise;
}

Expand Down Expand Up @@ -640,4 +672,34 @@ export default class AttachmentFetcher {

return promise;
}

static combineSpectra(jcampIds, curveIdx) {
const promise = fetch(
'/api/v1/chemspectra/file/combine_spectra',
{
credentials: 'same-origin',
method: 'POST',
headers:
{
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
spectra_ids: jcampIds,
front_spectra_idx: curveIdx,
}),
},
)
.then((response) => {
return response.json();
})
.then((json) => {
return json;
})
.catch((errorMessage) => {
console.log(errorMessage);
});

return promise;
}
}
4 changes: 2 additions & 2 deletions app/packs/src/stores/alt/actions/SpectraActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class SpectraActions {
};
}

SaveToFile(spcInfo, peaksStr, shift, scan, thres, integration, multiplicity, predict, cb, keepPred = false, waveLengthStr, cyclicvolta, curveIdx = 0, simulatenmr = false) {
SaveToFile(spcInfo, peaksStr, shift, scan, thres, integration, multiplicity, predict, cb, keepPred = false, waveLengthStr, cyclicvolta, curveIdx = 0, simulatenmr = false, previousSpcInfos, isSaveCombined = false) {
return (dispatch) => {
AttachmentFetcher.saveSpectrum(spcInfo.idx, peaksStr, shift, scan, thres, integration, multiplicity, predict, keepPred, waveLengthStr, cyclicvolta, curveIdx, simulatenmr)
AttachmentFetcher.saveSpectrum(spcInfo.idx, peaksStr, shift, scan, thres, integration, multiplicity, predict, keepPred, waveLengthStr, cyclicvolta, curveIdx, simulatenmr, previousSpcInfos, isSaveCombined)
.then((fetchedFiles) => {
dispatch({ fetchedFiles, spcInfo });
cb();
Expand Down

0 comments on commit 51389a5

Please sign in to comment.