use of org.opencastproject.assetmanager.api.query.ASelectQuery 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());
}
use of org.opencastproject.assetmanager.api.query.ASelectQuery in project opencast by opencast.
the class SchedulerServiceImpl method getCalendar.
@Override
public String getCalendar(Opt<String> captureAgentId, Opt<String> seriesId, Opt<Date> cutoff) throws SchedulerException {
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
Predicate predicate = withOrganization(query).and(withOwner(query)).and(query.hasPropertiesOf(p.namespace())).and(p.optOut().eq(false)).and(withVersion(query)).and(p.end().ge(DateTime.now().minusHours(1).toDate()));
for (String agentId : captureAgentId) {
predicate = predicate.and(p.agent().eq(agentId));
}
for (String series : seriesId) {
predicate = predicate.and(query.seriesId().eq(series));
}
for (Date d : cutoff) {
predicate = predicate.and(p.start().le(d));
}
ASelectQuery select = query.select(query.snapshot(), p.agent().target(), p.start().target(), p.end().target(), query.propertiesOf(CA_NAMESPACE)).where(predicate);
Stream<ARecord> records = select.run().getRecords();
CalendarGenerator cal = new CalendarGenerator(seriesService);
for (ARecord record : records) {
boolean blacklisted;
// isBlacklisted() methods are not implemented in the persistence layer and return always false
// try {
// //blacklisted = isBlacklisted(record.getMediaPackageId());
// } catch (NotFoundException e) {
// continue;
// }
blacklisted = false;
// Skip blacklisted events
if (blacklisted)
continue;
Opt<MediaPackage> optMp = record.getSnapshot().map(episodeToMp);
// If the event media package is empty, skip the event
if (optMp.isNone()) {
logger.warn("Mediapackage for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
continue;
}
Opt<DublinCoreCatalog> catalogOpt = loadEpisodeDublinCoreFromAsset(record.getSnapshot().get());
if (catalogOpt.isNone()) {
logger.warn("No episode catalog available, skipping!");
continue;
}
Map<String, String> caMetadata = record.getProperties().filter(filterByNamespace._2(CA_NAMESPACE)).group(toKey, toValue);
// If the even properties are empty, skip the event
if (caMetadata.isEmpty()) {
logger.warn("Properties for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
continue;
}
String agentId = record.getProperties().apply(Properties.getString(AGENT_CONFIG));
Date start = record.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
Date end = record.getProperties().apply(Properties.getDate(END_DATE_CONFIG));
Date lastModified = record.getSnapshot().get().getArchivalDate();
// Add the entry to the calendar, skip it with a warning if adding fails
try {
cal.addEvent(optMp.get(), catalogOpt.get(), agentId, start, end, lastModified, toPropertyString(caMetadata));
} catch (Exception e) {
logger.warn("Error adding event '{}' to calendar, event is not recorded: {}", record.getMediaPackageId(), getStackTrace(e));
continue;
}
}
// Only validate calendars with events. Without any events, the iCalendar won't validate
if (cal.getCalendar().getComponents().size() > 0) {
try {
cal.getCalendar().validate();
} catch (ValidationException e) {
logger.warn("Recording calendar could not be validated (returning it anyways): {}", getStackTrace(e));
}
}
return cal.getCalendar().toString();
} catch (Exception e) {
if (e instanceof SchedulerException)
throw e;
logger.error("Failed getting calendar: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
use of org.opencastproject.assetmanager.api.query.ASelectQuery in project opencast by opencast.
the class AbstractAssetManagerPropertyRetrievalTest method testPropertyRetrieval.
/**
* Create some media packages and associate some random properties to each of them.
* Then iterate all created properties and create a query for each of them.
*/
@Test
@Parameters
public void testPropertyRetrieval(final Params params) {
// create a set of media packages and add them to the asset manager
final String[] mps = createAndAddMediaPackagesSimple(params.mpCount, 1, 1);
final Random random = new Random(System.nanoTime());
// create a set of random property names
final PropertyName[] propertyNames = createRandomPropertyNames(params.propertyNameSetSize);
// create a random amount of random properties for each media package
final Stream<Property> props = $(mps).bind(new Fn<String, Stream<Property>>() {
@Override
public Stream<Property> apply(final String mp) {
// create a random amount of random properties
return Stream.cont(inc()).take(random.nextInt(params.maxProps - params.minProps + 1) + params.minProps).map(new Fn<Integer, Property>() {
@Override
public Property apply(Integer ignore) {
// try to pick a free property a 100 times
for (int i = 0; i < 100; i++) {
// randomly select a property name
final PropertyName pName = propertyNames[random.nextInt(propertyNames.length)];
// check if the selected property is already associated with the current media package
final ASelectQuery doesPropertyExist = q.select(q.properties(pName)).where(q.mediaPackageId(mp));
if (sizeOf(doesPropertyExist.run().getRecords().bind(ARecords.getProperties)) == 0) {
// create a property with a randomly picked value
final Property p = Property.mk(PropertyId.mk(mp, pName), params.values[random.nextInt(params.values.length)]);
if (am.setProperty(p))
return p;
}
}
fail("Cannot pick another random property that has not been inserted yet");
return null;
}
});
}
}).eval();
assertThat("Number of generated properties", sizeOf(props), allOf(greaterThanOrEqualTo(params.mpCount * params.minProps), lessThanOrEqualTo(params.mpCount * params.maxProps)));
// iterate all properties and try to retrieve them from the AssetManager
for (final Property prop : props) {
final AResult r = q.select(params.mkTarget.apply(prop)).where(params.mkWhere.apply(prop)).run();
// get all properties of the result records
assertThat("Number of records", r.getSize(), params.expectRecords);
final Stream<Property> allProps = r.getRecords().bind(ARecords.getProperties);
assertThat("Total number of properties: " + allProps.mkString(", "), sizeOf(allProps), params.expectPropertiesTotal);
assertThat("Total number of snapshots", sizeOf(r.getRecords().bind(ARecords.getSnapshot)), params.expectSnapshotsTotal);
final Stream<Property> findSavedProperty = r.getRecords().bind(ARecords.getProperties).filter(Booleans.eq(prop));
if (params.expectContainsSavedProperty) {
assertThat("Contains saved property", findSavedProperty, hasItem(prop));
}
}
}
use of org.opencastproject.assetmanager.api.query.ASelectQuery in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherRunProgressState.
@Test
public void testWorkflowDispatcherRunProgressState() throws Exception {
InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
database.storeJobControl("mpId2", "audioTrack2", "jobId2", TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
EasyMock.expect(workspace.putInCollection(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(InputStream.class))).andReturn(new URI("http://anything"));
EasyMock.replay(workspace);
HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
EasyMock.expect(httpEntity.getContent()).andReturn(stream);
CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
StatusLine status = EasyMock.createNiceMock(StatusLine.class);
EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes();
EasyMock.replay(httpEntity, response, status);
Capture<HttpGet> capturedGet = Capture.newInstance();
EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response);
EasyMock.replay(httpClient);
// enrich(q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(q.version().isLatest())).run()).getSnapshots();
// 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 it called the external service to get the results
Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
// 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());
}
use of org.opencastproject.assetmanager.api.query.ASelectQuery in project opencast by opencast.
the class SchedulerServiceImpl method preCollisionEventCheck.
private List<String> preCollisionEventCheck(String trxId, String schedulingSource) throws SchedulerException {
try {
AQueryBuilder query = assetManager.createQuery();
Props p = new Props(query);
TrxProps trxP = new TrxProps(query);
Predicate isSourceWithTrx = trxP.source().eq(schedulingSource).and(trxP.transactionId().eq(trxId));
Predicate hasProperties = p.agent().exists().and(p.start().exists()).and(p.end().exists());
Predicate isNotSource = p.source().eq(schedulingSource).not().and(hasProperties);
Predicate isNotOptedOut = p.optOut().eq(false).or(trxP.optOut().eq(false));
ASelectQuery select = query.select(trxP.optOut().target(), p.optOut().target(), p.agent().target(), p.start().target(), p.end().target(), trxP.agent().target(), trxP.start().target(), trxP.end().target(), trxP.transactionId().target()).where(withOrganization(query).and(isSourceWithTrx.or(isNotSource)).and(query.version().isLatest()).and(isNotOptedOut));
AResult result = select.run();
// Check for conflicts
List<String> conflictingRecords = new ArrayList<>();
Map<String, List<Interval>> invervalMap = new HashMap<>();
for (ARecord record : result.getRecords()) {
String agentId;
Date start;
Date end;
Opt<String> optTrxId = record.getProperties().apply(Properties.getStringOpt(TRANSACTION_ID_CONFIG));
if (optTrxId.isSome() && trxId.equals(optTrxId.get())) {
agentId = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getString(AGENT_CONFIG));
start = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getDate(START_DATE_CONFIG));
end = record.getProperties().filter(filterByNamespace._2(trxP.namespace())).apply(Properties.getDate(END_DATE_CONFIG));
} else {
agentId = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getString(AGENT_CONFIG));
start = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getDate(START_DATE_CONFIG));
end = record.getProperties().filter(filterByNamespace._2(p.namespace())).apply(Properties.getDate(END_DATE_CONFIG));
}
Interval currentInterval = new Interval(start.getTime(), end.getTime());
List<Interval> intervals = invervalMap.get(agentId);
if (intervals == null)
intervals = new ArrayList<>();
boolean overlaps = false;
for (Interval i : intervals) {
if (!i.overlaps(currentInterval))
continue;
overlaps = true;
break;
}
if (!overlaps) {
intervals.add(currentInterval);
} else {
conflictingRecords.add(record.getMediaPackageId());
}
invervalMap.put(agentId, intervals);
}
return conflictingRecords;
} catch (Exception e) {
logger.error("Failed to search for conflicting events: {}", getStackTrace(e));
throw new SchedulerException(e);
}
}
Aggregations