Ignore:
Timestamp:
2021-08-04T15:06:10+12:00 (3 years ago)
Author:
davidb
Message:

Prevent one failed request from causing all other transcribed files from being returned
Implement timeout failsafe

Location:
gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy
Files:
2 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/TranscriptionServlet.java

    r35248 r35249  
    2323import org.apache.logging.log4j.LogManager;
    2424import org.apache.logging.log4j.Logger;
    25 import org.atea.nlptools.koreromaoriinterface.exceptions.ReoTuhituhiException;
    2625import org.atea.nlptools.koreromaoriinterface.models.AudioFilePart;
    27 import org.atea.nlptools.koreromaoriinterface.models.MyTranscriptionResponse;
     26import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResultDto;
    2827import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult;
    2928import org.atea.nlptools.koreromaoriinterface.services.ReoTuhituhiApiService;
     
    115114
    116115            // Transform the Tuhituhi response into our own format
    117             List<MyTranscriptionResponse> responses = new ArrayList<MyTranscriptionResponse>(results.size());
     116            List<TranscriptionResultDto> responses = new ArrayList<TranscriptionResultDto>(results.size());
    118117            for (AudioFilePart result : results) {
    119                 responses.add(MyTranscriptionResponse.FromTranscriptionResult(result.getTranscriptionResult(), result.fileName));
     118                responses.add(TranscriptionResultDto.FromTranscriptionResult(result.getTranscriptionResult(), result.fileName));
    120119            }
    121120
     
    123122            String json = jsonSerialiser.toJson(responses, transcriptionListType);
    124123            writer.append(json);
    125         }
    126         catch (ReoTuhituhiException rtex)
    127         {
    128             response.sendError(502, "Call to the Reo Tuhituhi API failed.");
    129124        }
    130125        catch (Exception ex)
  • gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/models/TranscriptionResult.java

    r35239 r35249  
    1111    private String transcription;
    1212
    13     public String getLog() {
     13    /**
     14     * Initialises an empty instance of the {@link TranscriptionResult} class.
     15     */
     16    public TranscriptionResult() {
     17
     18    }
     19
     20    /**
     21     * Initialises a new instance of the {@link TranscriptionResult} class.
     22     *
     23     * @param log
     24     * @param metadata
     25     * @param modelVersion
     26     * @param success
     27     * @param transcription
     28     */
     29    public TranscriptionResult(
     30        String log,
     31        TranscriptionResultMetadata[] metadata,
     32        String modelVersion,
     33        Boolean success,
     34        String transcription)
     35    {
     36        this.log = log;
     37        this.metadata = metadata;
     38        this.modelVersion = modelVersion;
     39        this.success = success;
     40        this.transcription = transcription;
     41    }
     42
     43    /**
     44     * Initialises a new instance of the {@link TranscriptionResult} class, with the intention of logging an unsuccessful result.
     45     * As such, each field is set to null bar the provided log, and {@link getSuccess} being set to false.
     46     *
     47     * @param log
     48     */
     49    public TranscriptionResult(
     50        String log)
     51    {
     52        this.log = log;
     53        this.metadata = null;
     54        this.modelVersion = null;
     55        this.success = false;
     56        this.transcription = null;
     57    }
     58
     59    public String getLog()
     60    {
    1461        return log;
    1562    }
    1663   
    17     public TranscriptionResultMetadata[] getMetadata() {
     64    public TranscriptionResultMetadata[] getMetadata()
     65    {
    1866        return metadata;
    1967    }
    2068   
    21     public String getModelVersion() {
     69    public String getModelVersion()
     70    {
    2271        return modelVersion;
    2372    }
    2473   
    25     public Boolean getSuccess() {
     74    public Boolean getSuccess()
     75    {
    2676        return success;
    2777    }
    2878   
    29     public String getTranscription() {
     79    public String getTranscription()
     80    {
    3081        return transcription;
    3182    }
  • gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/services/ReoTuhituhiApiService.java

    r35248 r35249  
    33import java.io.IOException;
    44import java.io.InputStream;
     5import java.net.SocketTimeoutException;
    56import java.util.LinkedList;
    67import java.util.List;
     
    4344     */
    4445    public List<AudioFilePart> getTranscriptions(Iterable<AudioFilePart> audioFileParts)
    45         throws HttpRequestException, JsonSyntaxException, Exception
     46        throws HttpRequestException, IOException
    4647    {
    4748        List<AudioFilePart> apiResults = new LinkedList<AudioFilePart>();
     
    4950        for (AudioFilePart part : audioFileParts)
    5051        {
    51             TranscriptionResult res = getTranscription(part.dataStream);
     52            TranscriptionResult res = null;
     53            try
     54            {
     55                res = getTranscription(part.dataStream);
     56            }
     57            catch (ReoTuhituhiException rtex)
     58            {
     59                res = new TranscriptionResult("The Reo Tuhituhi API didn't respond successfully.");
     60            }
     61            catch (JsonSyntaxException jsex)
     62            {
     63                res = new TranscriptionResult("That's on me! I failed to parse the response from the Reo Tuhituhi API.");
     64            }
     65            catch (HttpRequestException hrex)
     66            {
     67                if (hrex.getCause().getClass() == SocketTimeoutException.class) {
     68                    res = new TranscriptionResult("The Reo Tuhituhi API timed out.");
     69                }
     70                else {
     71                    throw hrex;
     72                }
     73            }
     74           
    5275            part.setTranscriptionResult(res);
    53 
    5476            apiResults.add(part);
    5577        }
     
    6688     * @throws JsonSyntaxException Thrown when the result cannot be parsed.
    6789     * @throws ReoTuhituhiException Thrown when the Reo Tuhituhi API returns an invalid response.
     90     * @throws IOException Thrown at any time.
    6891     */
    6992    public TranscriptionResult getTranscription(InputStream audioStream)
     
    7497        HttpRequestService request = HttpRequestService
    7598            .post(apiEndpoint)
     99            .connectTimeout(2000)
     100            .readTimeout(5000)
    76101            .authorization("Basic " + apiKey)
    77102            .send(audioStream);
     
    87112            );
    88113
    89             throw new ReoTuhituhiException(request.code(), request.message(), "Non-OK status code");
     114            throw new ReoTuhituhiException(request.code(), request.message(), "API returned a non-OK status code");
    90115        }
    91116
     
    100125            );
    101126
    102             throw new ReoTuhituhiException(request.code(), request.message(), "Invalid content type: " + request.contentType());
     127            throw new ReoTuhituhiException(request.code(), request.message(), "API specified an invalid content type: " + request.contentType());
    103128        }
    104129
Note: See TracChangeset for help on using the changeset viewer.