Search in sources :

Example 1 with ActivityStep

use of io.syndesis.server.endpoint.v1.handler.activity.ActivityStep in project syndesis by syndesisio.

the class DBActivityTrackingService method getActivities.

@Override
public List<Activity> getActivities(String integrationId, String from, Integer requestedLimit) throws IOException {
    String path = "/activity/exchanges/" + integrationId;
    int limit = 10;
    if (requestedLimit != null) {
        limit = requestedLimit;
    }
    if (limit > 1000) {
        // max out to 1000 per request.
        limit = 1000;
    }
    GetOptions options = new GetOptions().order(// reverse the order since we want most recent exchanges first.
    GetOptions.Order.DESC).startAfter(from).limitToFirst(// allow paging
    limit);
    byte[] data = jsondb.getAsByteArray(path, options);
    if (data == null) {
        return new ArrayList<>();
    }
    JsonNode map = Json.reader().readTree(new ByteArrayInputStream(data));
    List<Activity> rc = new ArrayList<>();
    Iterator<Map.Entry<String, JsonNode>> i = map.fields();
    while (i.hasNext()) {
        Map.Entry<String, JsonNode> entry = i.next();
        try {
            String value = entry.getValue().textValue();
            Activity activity = Json.reader().forType(Activity.class).readValue(value);
            if (activity.getSteps() == null) {
                activity.setSteps(new ArrayList<ActivityStep>());
            }
            rc.add(activity);
        } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException ignored) {
            // We could get stuff like class cast exceptions..
            LOG.debug("Could convert entry: {}", entry, ignored);
        }
    }
    return rc;
}
Also used : ArrayList(java.util.ArrayList) Activity(io.syndesis.server.endpoint.v1.handler.activity.Activity) JsonNode(com.fasterxml.jackson.databind.JsonNode) GetOptions(io.syndesis.server.jsondb.GetOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) ActivityStep(io.syndesis.server.endpoint.v1.handler.activity.ActivityStep) Map(java.util.Map)

Example 2 with ActivityStep

use of io.syndesis.server.endpoint.v1.handler.activity.ActivityStep in project syndesis by syndesisio.

the class PodLogMonitor method processLine.

@SuppressWarnings("PMD.CyclomaticComplexity")
private void processLine(byte[] line) throws IOException {
    // 2018-01-12T21:22:02.068338027Z { ..... }
    if (// not long enough
    line.length < 32 || // expecting space
    line[30] != ' ' || // expecting the json data starting here.
    line[31] != '{') {
        return;
    }
    String time = new String(line, 0, 30, StandardCharsets.US_ASCII);
    try {
        @SuppressWarnings("unchecked") Map<String, Object> // NOPMD
        json = Json.reader().forType(HashMap.class).readValue(line, 31, line.length - 31);
        // are the required fields set?
        String id = validate((String) json.remove("id"));
        String exchange = validate((String) json.remove("exchange"));
        InflightData inflightData = getInflightData(exchange, time);
        String step = (String) json.remove("step");
        if (step == null) {
            // Looks like an exchange level logging event.
            Boolean failed = (Boolean) json.remove("failed");
            if (failed != null) {
                inflightData.activity.setFailed(failed);
            }
            String status = (String) json.remove("status");
            inflightData.metadata.putAll(json);
            if (status != null) {
                inflightData.activity.setStatus(status);
                if ("done".equals(status)) {
                    inflightData.activity.setSteps(new ArrayList<>(inflightData.steps.values()));
                    if (!inflightData.metadata.isEmpty()) {
                        inflightData.activity.setMetadata(toJsonNode(inflightData.metadata));
                    }
                    String activityAsString = Json.writer().writeValueAsString(inflightData.activity);
                    String transactionPath = format("/exchanges/%s/%s", integrationId, exchange);
                    inflightActivities.remove(exchange);
                    logsController.eventQueue.put(batch -> {
                        // Do as little as possible in here, single thread processes the event queue.
                        batch.put(transactionPath, activityAsString);
                        trackState(time, batch);
                    });
                }
            }
        } else {
            // Looks like a step level logging event.
            ActivityStep as = inflightData.getStep(step, id);
            String message = (String) json.remove("message");
            if (message != null) {
                if (as.getMessages() == null) {
                    as.setMessages(new ArrayList<>());
                }
                as.getMessages().add(message);
            }
            String failure = (String) json.remove("failure");
            if (failure != null) {
                as.setFailure(failure);
            }
            Number duration = (Number) json.remove("duration");
            if (duration != null) {
                as.setDuration(duration.longValue());
            }
            if (!json.isEmpty()) {
                if (as.getEvents() == null) {
                    as.setEvents(new ArrayList<>());
                }
                as.getEvents().add(toJsonNode(json));
            }
        }
    } catch (JsonDBException | ClassCastException | IOException ignored) {
    // / log record not in the expected format.
    } catch (InterruptedException e) {
        final InterruptedIOException rethrow = new InterruptedIOException(e.getMessage());
        rethrow.initCause(e);
        throw rethrow;
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ActivityStep(io.syndesis.server.endpoint.v1.handler.activity.ActivityStep) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

ActivityStep (io.syndesis.server.endpoint.v1.handler.activity.ActivityStep)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Activity (io.syndesis.server.endpoint.v1.handler.activity.Activity)1 GetOptions (io.syndesis.server.jsondb.GetOptions)1 JsonDBException (io.syndesis.server.jsondb.JsonDBException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1