1 package jalview.ws.slivkaws;
3 import jalview.datamodel.SequenceI;
4 import jalview.gui.WebserviceInfo;
5 import jalview.ws.api.JalviewServiceEndpointProviderI;
6 import jalview.ws.api.JalviewWebServiceI;
7 import jalview.ws.api.JobId;
8 import jalview.ws.api.ServiceWithParameters;
9 import jalview.ws.gui.WsJob;
10 import jalview.ws.params.ArgumentI;
11 import jalview.ws.params.ParamDatastoreI;
12 import jalview.ws.params.ParamManager;
13 import jalview.ws.params.WsParamSetI;
15 import java.io.ByteArrayInputStream;
16 import java.io.IOError;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.util.Arrays;
21 import java.util.EnumMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Optional;
27 import uk.ac.dundee.compbio.slivkaclient.FieldType;
28 import uk.ac.dundee.compbio.slivkaclient.FormField;
29 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
30 import uk.ac.dundee.compbio.slivkaclient.JobState;
31 import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
32 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
33 import uk.ac.dundee.compbio.slivkaclient.SlivkaForm;
34 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
35 import uk.ac.dundee.compbio.slivkaclient.ValidationException;
37 public abstract class SlivkaWSInstance extends ServiceWithParameters
38 implements JalviewServiceEndpointProviderI, JalviewWebServiceI
40 protected final SlivkaClient client;
42 protected final SlivkaService service;
44 protected SlivkaDatastore store = null;
46 protected static final EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
48 stateMap.put(JobState.PENDING, WsJob.JobState.QUEUED);
49 stateMap.put(JobState.REJECTED, WsJob.JobState.INVALID);
50 stateMap.put(JobState.ACCEPTED, WsJob.JobState.QUEUED);
51 stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED);
52 stateMap.put(JobState.RUNNING, WsJob.JobState.RUNNING);
53 stateMap.put(JobState.COMPLETED, WsJob.JobState.FINISHED);
54 stateMap.put(JobState.INTERRUPED, WsJob.JobState.CANCELLED);
55 stateMap.put(JobState.DELETED, WsJob.JobState.CANCELLED);
56 stateMap.put(JobState.FAILED, WsJob.JobState.FAILED);
57 stateMap.put(JobState.ERROR, WsJob.JobState.SERVERERROR);
58 stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN);
60 protected final Set<WsJob.JobState> failedStates = new HashSet<>(Arrays.asList(
61 WsJob.JobState.INVALID, WsJob.JobState.BROKEN, WsJob.JobState.FAILED,
62 WsJob.JobState.SERVERERROR, WsJob.JobState.CANCELLED
65 public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
67 super(service.getName(), action, service.getLabel(), "Slivka", client.getUrl().toString());
69 this.service = service;
72 protected final JobId submit(List<SequenceI> sequences,
73 WsParamSetI preset, List<ArgumentI> args) throws Throwable
75 SlivkaForm form = service.getForm();
76 Optional<FormField> inputField = form.getFields().stream()
77 .filter(f -> f.getType() == FieldType.FILE).findFirst();
78 if (inputField.isPresent())
80 StringBuilder builder = new StringBuilder();
81 for (SequenceI seq : sequences)
83 builder.append(">").append(seq.getName()).append("\n")
84 .append(seq.getSequence()).append("\n");
86 InputStream stream = new ByteArrayInputStream(
87 builder.toString().getBytes());
88 RemoteFile file = client.uploadFile(stream, "input.fa",
90 form.insert(inputField.get().getName(), file);
94 for (ArgumentI arg : args)
96 // multiple choice field names are name$number to avoid duplications
97 // the number is stripped here
98 String fieldName = arg.getName().split("\\$", 2)[0];
99 FormField field = form.getField(fieldName);
100 if (field.getType() == FieldType.BOOLEAN)
102 form.insert(fieldName,
103 (arg.getValue() != null && !arg.getValue().isBlank())
109 form.insert(fieldName, field.valueOf(arg.getValue()));
113 return new JobId(service.getName(), service.getName(), form.submit());
117 public final void updateStatus(WsJob job)
121 job.setState(stateMap.get(client.getJobState(job.getJobId())));
122 } catch (IOException e)
124 throw new IOError(e);
129 public final boolean updateJobProgress(WsJob job) throws IOException
131 List<RemoteFile> files = client.getJobResults(job.getJobId());
132 Optional<RemoteFile> logFile = files.stream()
133 .filter(f -> f.getLabel().equals("log")).findFirst();
134 boolean newContent = false;
135 if (logFile.isPresent())
137 InputStream stream = logFile.get().getContent();
138 long nextChunk = stream.skip(job.getNextChunk());
139 int len = appendJobStatus(job, stream);
140 job.setnextChunk(nextChunk + len);
141 newContent |= len > 0;
143 if (failedStates.contains(job.getJobState()))
145 Optional<RemoteFile> errLogFile = files.stream()
146 .filter(f -> f.getLabel().equals("error-log")).findFirst();
147 if (errLogFile.isPresent())
149 newContent |= appendJobStatus(job, errLogFile.get().getContent()) > 0;
155 private int appendJobStatus(WsJob job, InputStream stream) throws IOException
157 StringBuilder builder = new StringBuilder(job.getStatus());
158 InputStreamReader reader = new InputStreamReader(stream);
159 char[] buffer = new char[4096];
162 while ((len = reader.read(buffer)) != -1)
165 builder.append(buffer, 0, len);
167 job.setStatus(builder.toString());
172 public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
174 if (_lex instanceof FormValidationException)
176 FormValidationException formError = (FormValidationException) _lex;
177 String[] messages = new String[formError.getErrors().size()];
179 for (ValidationException e : formError.getErrors())
181 messages[i++] = String.format("%s: %s,", e.getField().getName(), e.getMessage());
183 j.setState(WsJob.JobState.INVALID);
184 j.setStatus(String.join(", ", messages));
191 public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo)
197 final SlivkaService getService()
203 public final Object getEndpoint()
209 public final void initParamStore(ParamManager userParameterStore)
215 store = new SlivkaDatastore(service);
216 } catch (IOException e)
218 throw new IOError(e);
224 public boolean hasParameters()
230 public final ParamDatastoreI getParamStore()
234 initParamStore(null);