use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessEventDao method insert.
/**
* Batch inserts the provided {@code events}.
*
* @return the same events updated with autogenerated IDs
*/
public List<ProcessEvent> insert(DSLContext tx, List<NewProcessEvent> events) {
if (events == null || events.isEmpty()) {
return Collections.emptyList();
}
if (events.size() == 1) {
NewProcessEvent ev = events.get(0);
ProcessKey processKey = ev.processKey();
Field<OffsetDateTime> ts = ev.eventDate() != null ? value(ev.eventDate()) : currentOffsetDateTime();
Map<String, Object> m = ev.data() != null ? ev.data() : Collections.emptyMap();
String eventType = ev.eventType();
ProcessEventsRecord r = tx.insertInto(PROCESS_EVENTS).set(PROCESS_EVENTS.INSTANCE_ID, processKey.getInstanceId()).set(PROCESS_EVENTS.INSTANCE_CREATED_AT, processKey.getCreatedAt()).set(PROCESS_EVENTS.EVENT_TYPE, eventType).set(PROCESS_EVENTS.EVENT_DATE, ts).set(PROCESS_EVENTS.EVENT_DATA, objectMapper.toJSONB(m)).returning(PROCESS_EVENTS.EVENT_DATE, PROCESS_EVENTS.EVENT_SEQ).fetchOne();
return Collections.singletonList(ProcessEvent.builder().eventSeq(r.getEventSeq()).eventDate(r.getEventDate()).eventType(eventType).processKey(processKey).data(m).build());
}
InsertSetStep<ProcessEventsRecord> q = tx.insertInto(PROCESS_EVENTS);
InsertSetMoreStep<ProcessEventsRecord> qq = null;
for (Iterator<NewProcessEvent> i = events.iterator(); i.hasNext(); ) {
NewProcessEvent ev = i.next();
ProcessKey pk = ev.processKey();
ProcessEventsRecord r = new ProcessEventsRecord();
r.setInstanceId(pk.getInstanceId());
r.setInstanceCreatedAt(pk.getCreatedAt());
r.setEventType(ev.eventType());
// TODO replace with default = now()?
OffsetDateTime eventDate = ev.eventDate() != null ? ev.eventDate() : OffsetDateTime.now();
r.setEventDate(eventDate);
Map<String, Object> m = ev.data() != null ? ev.data() : Collections.emptyMap();
r.setEventData(objectMapper.toJSONB(m));
qq = q.set(r);
if (i.hasNext()) {
qq.newRecord();
}
}
// return only a subset of columns to save some traffic
Result<ProcessEventsRecord> records = qq.returning(PROCESS_EVENTS.EVENT_DATE, PROCESS_EVENTS.EVENT_SEQ).fetch();
if (records.size() != events.size()) {
throw new IllegalStateException("Invalid result. Returning records count doesn't match the number of events: " + records.size() + " != " + events.size());
}
List<ProcessEvent> result = new ArrayList<>();
for (int i = 0; i < events.size(); i++) {
ProcessEventsRecord r = records.get(i);
NewProcessEvent ev = events.get(i);
Map<String, Object> data = ev.data();
result.add(ProcessEvent.builder().eventDate(r.getEventDate()).eventSeq(r.getEventSeq()).eventType(ev.eventType()).processKey(ev.processKey()).data(data != null ? data : Collections.emptyMap()).build());
}
return result;
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessEventDao method insert.
public void insert(DSLContext tx, List<ProcessKey> processKeys, String eventType, Map<String, Object> data) {
String sql = tx.insertInto(PROCESS_EVENTS).set(PROCESS_EVENTS.INSTANCE_ID, (UUID) null).set(PROCESS_EVENTS.INSTANCE_CREATED_AT, (OffsetDateTime) null).set(PROCESS_EVENTS.EVENT_TYPE, (String) null).set(PROCESS_EVENTS.EVENT_DATE, currentOffsetDateTime()).set(PROCESS_EVENTS.EVENT_DATA, (JSONB) null).returning(PROCESS_EVENTS.EVENT_SEQ).getSQL();
tx.connection(conn -> {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (ProcessKey pk : processKeys) {
ps.setObject(1, pk.getInstanceId());
ps.setObject(2, pk.getCreatedAt());
ps.setString(3, eventType);
ps.setString(4, objectMapper.toJSONB(data).toString());
ps.addBatch();
}
ps.executeBatch();
}
});
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessEventResource method list.
/**
* List process events.
*/
@GET
@ApiOperation(value = "List process events", responseContainer = "list", response = ProcessEventEntry.class)
@Path("/{processInstanceId}/event")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public List<ProcessEventEntry> list(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam @QueryParam("type") String eventType, @ApiParam @QueryParam("after") OffsetDateTimeParam after, @ApiParam @QueryParam("fromId") Long fromId, @ApiParam @QueryParam("eventCorrelationId") UUID eventCorrelationId, @ApiParam @QueryParam("eventPhase") EventPhase eventPhase, @ApiParam @QueryParam("includeAll") @DefaultValue("false") boolean includeAll, @ApiParam @QueryParam("limit") @DefaultValue("-1") int limit) {
ProcessKey processKey = assertProcessKey(processInstanceId);
if (includeAll) {
// verify that the user can access potentially sensitive data
assertAccessRights(processKey);
}
ProcessEventFilter f = ProcessEventFilter.builder().processKey(processKey).after(unwrap(after)).eventType(eventType).eventCorrelationId(eventCorrelationId).eventPhase(eventPhase).limit(limit).fromId(fromId).build();
List<ProcessEventEntry> l = eventManager.list(f);
if (!includeAll) {
l = filterOutSensitiveData(l);
}
return l;
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessEventResource method batchEvent.
/**
* Register multiple events for the specified process.
*/
@POST
@ApiOperation(value = "Register multiple events for the specified process", authorizations = { @Authorization("session_key"), @Authorization("api_key") })
@Path("/{processInstanceId}/eventBatch")
@Consumes(MediaType.APPLICATION_JSON)
@WithTimer
public void batchEvent(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam List<ProcessEventRequest> data) {
ProcessKey processKey = assertProcessKey(processInstanceId);
List<NewProcessEvent> events = data.stream().map(req -> NewProcessEvent.builder().processKey(processKey).eventType(req.getEventType()).eventDate(req.getEventDate()).data(req.getData()).build()).collect(Collectors.toList());
eventManager.event(events);
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessResource method appendLog.
/**
* Appends a process' log.
*
* @param instanceId
* @param data
* @see ProcessLogResourceV2
* @deprecated in favor of the /api/v2/process/{id}/log* endpoints
*/
@POST
@javax.ws.rs.Path("{id}/log")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@WithTimer
@Deprecated
public void appendLog(@PathParam("id") UUID instanceId, InputStream data) {
ProcessKey processKey = assertProcessKey(instanceId);
try {
byte[] ab = IOUtils.toByteArray(data);
int upper = logManager.log(processKey, ab);
// whenever we accept logs from an external source (e.g. from an Agent) we need to check
// the log size limits
int logSizeLimit = processCfg.getLogSizeLimit();
if (upper >= logSizeLimit) {
logManager.error(processKey, "Maximum log size reached: {}. Process cancelled.", logSizeLimit);
processManager.kill(processKey);
}
} catch (IOException e) {
throw new ConcordApplicationException("Error while appending a log: " + e.getMessage());
}
}
Aggregations