Search in sources :

Example 6 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionService method getGeneratedTranscription.

@Override
public MediaPackageElement getGeneratedTranscription(String mpId, String jobId) throws TranscriptionServiceException {
    try {
        // If jobId is unknown, look for all jobs associated to that mpId
        if (jobId == null || "null".equals(jobId)) {
            jobId = null;
            for (TranscriptionJobControl jc : database.findByMediaPackage(mpId)) {
                if (TranscriptionJobControl.Status.Closed.name().equals(jc.getStatus()) || TranscriptionJobControl.Status.TranscriptionComplete.name().equals(jc.getStatus()))
                    jobId = jc.getTranscriptionJobId();
            }
        }
        if (jobId == null)
            throw new TranscriptionServiceException("No completed or closed transcription job found in database for media package " + mpId);
        // Results already saved?
        URI uri = workspace.getCollectionURI(TRANSCRIPT_COLLECTION, buildResultsFileName(jobId));
        try {
            workspace.get(uri);
        } catch (Exception e) {
            // Not saved yet so call the ibm watson service to get the results
            getAndSaveJobResults(jobId);
        }
        MediaPackageElementBuilder builder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
        return builder.elementFromURI(uri, Attachment.TYPE, new MediaPackageElementFlavor("captions", "ibm-watson-json"));
    } catch (TranscriptionDatabaseException e) {
        throw new TranscriptionServiceException("Job id not informed and could not find transcription", e);
    }
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException)

Example 7 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherRunTranscriptionCompletedState.

@Test
public void testWorkflowDispatcherRunTranscriptionCompletedState() throws Exception {
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    database.storeJobControl("mpId2", "audioTrack2", "jobId2", TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    database.updateJobControl(JOB_ID, TranscriptionJobControl.Status.TranscriptionComplete.name());
    // Mocks for query, result, etc
    Snapshot snapshot = EasyMock.createNiceMock(Snapshot.class);
    EasyMock.expect(snapshot.getOrganizationId()).andReturn(org.getId());
    ARecord aRec = EasyMock.createNiceMock(ARecord.class);
    EasyMock.expect(aRec.getSnapshot()).andReturn(Opt.some(snapshot));
    Stream<ARecord> recStream = Stream.mk(aRec);
    Predicate p = EasyMock.createNiceMock(Predicate.class);
    EasyMock.expect(p.and(p)).andReturn(p);
    AResult r = EasyMock.createNiceMock(AResult.class);
    EasyMock.expect(r.getSize()).andReturn(1L);
    EasyMock.expect(r.getRecords()).andReturn(recStream);
    Target t = EasyMock.createNiceMock(Target.class);
    ASelectQuery selectQuery = EasyMock.createNiceMock(ASelectQuery.class);
    EasyMock.expect(selectQuery.where(EasyMock.anyObject(Predicate.class))).andReturn(selectQuery);
    EasyMock.expect(selectQuery.run()).andReturn(r);
    AQueryBuilder query = EasyMock.createNiceMock(AQueryBuilder.class);
    EasyMock.expect(query.snapshot()).andReturn(t);
    EasyMock.expect(query.mediaPackageId(EasyMock.anyObject(String.class))).andReturn(p);
    EasyMock.expect(query.select(EasyMock.anyObject(Target.class))).andReturn(selectQuery);
    VersionField v = EasyMock.createNiceMock(VersionField.class);
    EasyMock.expect(v.isLatest()).andReturn(p);
    EasyMock.expect(query.version()).andReturn(v);
    EasyMock.expect(assetManager.createQuery()).andReturn(query);
    EasyMock.replay(snapshot, aRec, p, r, t, selectQuery, query, v, assetManager);
    Capture<Set<String>> capturedMpIds = Capture.newInstance();
    WorkflowDefinition wfDef = new WorkflowDefinitionImpl();
    EasyMock.expect(wfService.getWorkflowDefinitionById(IBMWatsonTranscriptionService.DEFAULT_WF_DEF)).andReturn(wfDef);
    List<WorkflowInstance> wfList = new ArrayList<WorkflowInstance>();
    wfList.add(new WorkflowInstanceImpl());
    Stream<WorkflowInstance> wfListStream = Stream.mk(wfList);
    Workflows wfs = EasyMock.createNiceMock(Workflows.class);
    EasyMock.expect(wfs.applyWorkflowToLatestVersion(EasyMock.capture(capturedMpIds), EasyMock.anyObject(ConfiguredWorkflow.class))).andReturn(wfListStream);
    service.setWfUtil(wfs);
    EasyMock.replay(wfService, wfs);
    WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
    dispatcher.run();
    // Check if only one mp has a workflow created for it
    Assert.assertEquals(1, capturedMpIds.getValue().size());
    // And if it was the correct one
    Assert.assertEquals(MP_ID, capturedMpIds.getValue().iterator().next());
    // Check if status in db was updated
    TranscriptionJobControl job = database.findByJob(JOB_ID);
    Assert.assertNotNull(job);
    Assert.assertEquals(TranscriptionJobControl.Status.Closed.name(), job.getStatus());
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) Set(java.util.Set) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) Workflows(org.opencastproject.assetmanager.util.Workflows) VersionField(org.opencastproject.assetmanager.api.query.VersionField) ArrayList(java.util.ArrayList) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) WorkflowDispatcher(org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) ConfiguredWorkflow(org.opencastproject.workflow.api.ConfiguredWorkflow) Predicate(org.opencastproject.assetmanager.api.query.Predicate) Snapshot(org.opencastproject.assetmanager.api.Snapshot) ARecord(org.opencastproject.assetmanager.api.query.ARecord) Target(org.opencastproject.assetmanager.api.query.Target) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) AResult(org.opencastproject.assetmanager.api.query.AResult) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) Test(org.junit.Test)

Example 8 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class TranscriptionDatabaseTest method testUpdateJobControlToTranscriptionComplete.

@Test
public void testUpdateJobControlToTranscriptionComplete() throws Exception {
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, STATUS, TRACK_DURATION);
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(STATUS, j.getStatus());
    Assert.assertNull(j.getDateCompleted());
    long dt1 = System.currentTimeMillis();
    database.updateJobControl(JOB_ID, TranscriptionJobControl.Status.TranscriptionComplete.name());
    long dt2 = System.currentTimeMillis();
    j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(TranscriptionJobControl.Status.TranscriptionComplete.name(), j.getStatus());
    long completed = j.getDateCompleted().getTime();
    Assert.assertTrue(dt1 <= completed && completed <= dt2);
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) Test(org.junit.Test)

Example 9 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class TranscriptionDatabaseTest method testFindByJob.

@Test
public void testFindByJob() throws Exception {
    long dt1 = System.currentTimeMillis();
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, STATUS, TRACK_DURATION);
    long dt2 = System.currentTimeMillis();
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(STATUS, j.getStatus());
    long created = j.getDateCreated().getTime();
    Assert.assertTrue(dt1 <= created && created <= dt2);
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) Test(org.junit.Test)

Example 10 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class TranscriptionDatabaseTest method testDeleteJobControl.

@Test
public void testDeleteJobControl() throws Exception {
    long dt1 = System.currentTimeMillis();
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, STATUS, TRACK_DURATION);
    long dt2 = System.currentTimeMillis();
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
    Assert.assertEquals(STATUS, j.getStatus());
    long created = j.getDateCreated().getTime();
    Assert.assertTrue(dt1 <= created && created <= dt2);
    database.deleteJobControl(JOB_ID);
    Assert.assertNull(database.findByJob(JOB_ID));
}
Also used : TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) Test(org.junit.Test)

Aggregations

TranscriptionJobControl (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl)16 Test (org.junit.Test)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 InputStream (java.io.InputStream)5 URI (java.net.URI)5 HttpEntity (org.apache.http.HttpEntity)5 StatusLine (org.apache.http.StatusLine)5 HttpGet (org.apache.http.client.methods.HttpGet)5 JSONObject (org.json.simple.JSONObject)4 WorkflowDispatcher (org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher)4 TranscriptionServiceException (org.opencastproject.transcription.api.TranscriptionServiceException)3 TranscriptionDatabaseException (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Snapshot (org.opencastproject.assetmanager.api.Snapshot)2 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)2 ARecord (org.opencastproject.assetmanager.api.query.ARecord)2