Example #1
0
    @Override
    public void serialize(
        final GraphSONVertex directionalVertex,
        final JsonGenerator jsonGenerator,
        final SerializerProvider serializerProvider)
        throws IOException, JsonGenerationException {
      final Vertex vertex = directionalVertex.getVertexToSerialize();
      jsonGenerator.writeStartObject();
      jsonGenerator.writeObjectField(GraphSONTokens.ID, vertex.getId());
      jsonGenerator.writeStringField(GraphSONTokens.LABEL, vertex.getLabel());
      jsonGenerator.writeStringField(GraphSONTokens.TYPE, GraphSONTokens.VERTEX);

      if (normalize) {
        jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES);
        vertex
            .getProperties()
            .values()
            .stream()
            .sorted(Comparators.PROPERTY_COMPARATOR)
            .forEachOrdered(
                FunctionUtils.wrapConsumer(
                    e -> jsonGenerator.writeObjectField(e.getKey(), e.get())));
        jsonGenerator.writeEndObject();
      } else {
        jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES);
        vertex
            .getProperties()
            .values()
            .forEach(
                FunctionUtils.wrapConsumer(
                    e -> jsonGenerator.writeObjectField(e.getKey(), e.get())));
        jsonGenerator.writeEndObject();
      }

      if (directionalVertex.getDirection() == Direction.BOTH
          || directionalVertex.getDirection() == Direction.OUT) {
        jsonGenerator.writeArrayFieldStart(GraphSONTokens.OUT);
        if (normalize)
          vertex
              .outE()
              .order(Comparators.HELD_EDGE_COMPARATOR)
              .forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject));
        else vertex.outE().forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject));
        jsonGenerator.writeEndArray();
      }

      if (directionalVertex.getDirection() == Direction.BOTH
          || directionalVertex.getDirection() == Direction.IN) {
        jsonGenerator.writeArrayFieldStart(GraphSONTokens.IN);
        if (normalize)
          vertex
              .inE()
              .order(Comparators.HELD_EDGE_COMPARATOR)
              .forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject));
        else vertex.inE().forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject));
        jsonGenerator.writeEndArray();
      }
      jsonGenerator.writeEndObject();
    }
  @Test
  public void writeJSONViaLowerlevelLibs() throws IOException {
    final StringWriter writer = new StringWriter();

    final ObjectMapper mapper = new ObjectMapper();

    final JsonGenerator jgen = mapper.getFactory().createGenerator(writer);

    jgen.writeStartObject();

    jgen.writeStringField(
        "odata.type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer");

    jgen.writeStringField("*****@*****.**", "Edm.String");
    jgen.writeStringField("Name", "A name");

    jgen.writeStringField("*****@*****.**", "Edm.Int32");
    jgen.writeNumberField("CustomerId", 0);

    jgen.writeArrayFieldStart("BackupContactInfo");

    jgen.writeStartObject();

    jgen.writeArrayFieldStart("AlternativeNames");
    jgen.writeString("myname");
    jgen.writeEndArray();

    jgen.writeArrayFieldStart("EmailBag");
    jgen.writeString("*****@*****.**");
    jgen.writeEndArray();

    jgen.writeObjectFieldStart("ContactAlias");
    jgen.writeStringField(
        "odata.type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases");
    jgen.writeArrayFieldStart("AlternativeNames");
    jgen.writeString("myAlternativeName");
    jgen.writeEndArray();
    jgen.writeEndObject();

    jgen.writeEndObject();

    jgen.writeEndArray();

    jgen.writeEndObject();

    jgen.flush();

    assertFalse(writer.toString().isEmpty());
  }
  public void writeServiceDocument(final JsonGenerator gen) throws IOException {
    gen.writeStartObject();

    if (!isODataMetadataNone) {
      final String metadataUri =
          (serviceRoot == null ? "" : serviceRoot.endsWith("/") ? serviceRoot : (serviceRoot + "/"))
              + Constants.METADATA;
      gen.writeObjectField(Constants.JSON_CONTEXT, metadataUri);

      if (metadata != null
          && metadata.getServiceMetadataETagSupport() != null
          && metadata.getServiceMetadataETagSupport().getMetadataETag() != null) {
        gen.writeStringField(
            Constants.JSON_METADATA_ETAG,
            metadata.getServiceMetadataETagSupport().getMetadataETag());
      }
    }

    gen.writeArrayFieldStart(Constants.VALUE);

    final EdmEntityContainer container = metadata.getEdm().getEntityContainer();
    writeEntitySets(gen, container);
    writeFunctionImports(gen, container);
    writeSingletons(gen, container);
  }
  @Override
  public void serialize(Request request, JsonGenerator jgen, SerializerProvider provider)
      throws IOException {
    jgen.writeStartObject();
    jgen.writeStringField("uri", request.getUri());
    jgen.writeStringField("method", request.getMethod().name());

    if (request.getHeaders() != null && !request.getHeaders().isEmpty()) {
      jgen.writeArrayFieldStart("headers");

      request
          .getHeaders()
          .forEach(
              httpHeader -> {
                try {
                  jgen.writeStartObject();
                  jgen.writeStringField("name", httpHeader.getName());
                  jgen.writeStringField("value", httpHeader.getValue());
                  jgen.writeEndObject();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              });
      jgen.writeEndArray();
    }
    if (request.getBody() != null && !request.getBody().isEmpty()) {
      jgen.writeStringField("body", request.getBody());
    }
    jgen.writeEndObject();
  }
  @Override
  public void serialize(IndexInput input, JsonGenerator jg, SerializerProvider sp)
      throws IOException {
    jg.writeStartObject();

    // Riak bug ... explicitly specifying "default" as the type breaks things
    if (!input.getNamespace().getBucketTypeAsString().equals(Namespace.DEFAULT_BUCKET_TYPE)) {
      jg.writeArrayFieldStart("bucket");
      jg.writeString(input.getNamespace().getBucketTypeAsString());
      jg.writeString(input.getNamespace().getBucketNameAsString());
      jg.writeEndArray();
    } else {
      jg.writeStringField("bucket", input.getNamespace().getBucketNameAsString());
    }

    jg.writeStringField("index", input.getIndex());
    IndexInput.IndexCriteria criteria = input.getCriteria();
    if (criteria instanceof IndexInput.MatchCriteria) {
      IndexInput.MatchCriteria<?> match = (IndexInput.MatchCriteria) criteria;
      jg.writeObjectField("key", match.getValue());
    } else if (criteria instanceof IndexInput.RangeCriteria) {
      IndexInput.RangeCriteria range = (IndexInput.RangeCriteria) criteria;
      jg.writeObjectField("start", range.getBegin());
      jg.writeObjectField("end", range.getEnd());
    }
    jg.writeEndObject();
  }
Example #6
0
  @Override
  public void serialize(Item item, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonProcessingException {
    jgen.writeStartArray();

    jgen.writeStartObject();
    jgen.writeStringField("id", "i_" + String.valueOf(item.getId()));
    jgen.writeObjectField("title", item.getName());
    jgen.writeObjectField("type", item.getType());

    String imagePath = "i_image_" + item.getId() + "." + item.getImage().getExtention();

    jgen.writeObjectFieldStart("image");
    jgen.writeObjectField("type", item.getImage().getType());
    jgen.writeObjectField("path", imagePath);
    jgen.writeEndObject();

    jgen.writeObjectFieldStart("values");
    jgen.writeStringField("productName", item.getName());
    jgen.writeStringField("imageFullPath", imagePath);
    for (Description description : item.getDescription()) {
      if (description.getDescription().size() > 1) {
        jgen.writeArrayFieldStart(description.getValue().getKey());
        for (String desc : description.getDescription()) jgen.writeString(desc);
        jgen.writeEndArray();
      } else
        jgen.writeStringField(
            description.getValue().getKey(),
            (description.getDescription().size() == 0) ? "" : description.getDescription().get(0));
    }
    jgen.writeEndObject();

    jgen.writeEndObject();
    jgen.writeEndArray();
  }
  @GET("/backend/aggregate/points")
  String getPoints(String content) throws IOException {
    logger.debug("getPoints(): content={}", content);
    PointsRequest request = ObjectMappers.readRequiredValue(mapper, content, PointsRequest.class);
    List<AggregatePoint> points = aggregateDao.readAggregates(request.getFrom(), request.getTo());

    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    jg.writeStartObject();
    jg.writeArrayFieldStart("points");
    for (AggregatePoint point : points) {
      jg.writeStartArray();
      jg.writeNumber(point.getCaptureTime());
      long durationAverage;
      if (point.getTraceCount() == 0) {
        durationAverage = 0;
      } else {
        durationAverage = point.getDurationTotal() / point.getTraceCount();
      }
      jg.writeNumber(durationAverage / 1000000000.0);
      jg.writeNumber(point.getTraceCount());
      jg.writeEndArray();
    }
    jg.writeEndArray();
    jg.writeNumberField("fixedAggregateIntervalSeconds", fixedAggregateIntervalSeconds);
    jg.writeEndObject();
    jg.close();
    return sb.toString();
  }
  @Override
  public String handleRequest(ExecutionJobVertex jobVertex, Map<String, String> params)
      throws Exception {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

    gen.writeStartObject();
    gen.writeStringField("id", jobVertex.getJobVertexId().toString());
    gen.writeNumberField("parallelism", jobVertex.getParallelism());

    gen.writeArrayFieldStart("subtasks");

    int num = 0;
    for (ExecutionVertex vertex : jobVertex.getTaskVertices()) {

      InstanceConnectionInfo location = vertex.getCurrentAssignedResourceLocation();
      String locationString = location == null ? "(unassigned)" : location.getHostname();

      gen.writeStartObject();

      gen.writeNumberField("subtask", num++);
      gen.writeNumberField("attempt", vertex.getCurrentExecutionAttempt().getAttemptNumber());
      gen.writeStringField("host", locationString);

      StringifiedAccumulatorResult[] accs =
          vertex.getCurrentExecutionAttempt().getUserAccumulatorsStringified();
      gen.writeArrayFieldStart("user-accumulators");
      for (StringifiedAccumulatorResult acc : accs) {
        gen.writeStartObject();
        gen.writeStringField("name", acc.getName());
        gen.writeStringField("type", acc.getType());
        gen.writeStringField("value", acc.getValue());
        gen.writeEndObject();
      }
      gen.writeEndArray();

      gen.writeEndObject();
    }
    gen.writeEndArray();

    gen.writeEndObject();
    gen.close();
    return writer.toString();
  }
Example #9
0
  public InputStream verifyDatatype2(Profile p, Profile baseP, String id, String type) {
    String result = "";
    // Datatype dt = p.getDatatypeLibrary().findOne(id);

    try {
      // Create temporary file
      File tmpJsonFile = File.createTempFile("resultTmp", ".json");

      // Generate json file
      JsonFactory factory = new JsonFactory();
      JsonGenerator generator = factory.createGenerator(new FileWriter(tmpJsonFile));
      generator.setPrettyPrinter(new DefaultPrettyPrinter());

      generator.writeStartObject();
      generator.writeStringField("id", id);
      generator.writeStringField("type", type);

      generator.writeArrayFieldStart("eltVerification");

      // for (Component c : dt.getComponents()){
      // generator.writeStartObject();
      // generator.writeStringField("eltName", "usage");
      // generator.writeStringField("eltAtt", c.getUsage().value());
      // result = this.validateChangeUsage(p.getMetaData().getHl7Version(),
      // baseP.getDatatypeLibrary().findOneComponent(c.getId()).getUsage(),
      // p.getDatatypeLibrary().findOneComponent(c.getId()).getUsage());
      // generator.writeStringField("result", result);
      // generator.writeEndObject();
      //
      // generator.writeStartObject();
      // generator.writeStringField("eltName", "minLength");
      // generator.writeStringField("eltAtt", String.valueOf(c.getMinLength()));
      // result = this.validateChangeLength(String.valueOf(c.getMinLength()), c.getMaxLength());
      // generator.writeStringField("result", result);
      // generator.writeEndObject();
      //
      // generator.writeStartObject();
      // generator.writeStringField("eltName", "maxLength");
      // generator.writeStringField("eltAtt", String.valueOf(c.getMaxLength()));
      // result = this.validateChangeLength(String.valueOf(c.getMinLength()), c.getMaxLength());
      // generator.writeStringField("result", result);
      // generator.writeEndObject();
      //
      // }

      generator.writeEndArray();
      generator.writeEndObject();

      generator.close();

      return FileUtils.openInputStream(tmpJsonFile);
    } catch (IOException e) {
      return new NullInputStream(1L);
    }
  }
 @Override
 public void serialize(
     final GeoJsonPoint value, final JsonGenerator gen, final SerializerProvider serializers)
     throws IOException {
   gen.writeStartObject();
   gen.writeStringField("type", value.getType());
   gen.writeArrayFieldStart("coordinates");
   gen.writeNumber(value.getX());
   gen.writeNumber(value.getY());
   gen.writeEndArray();
   gen.writeEndObject();
 }
  @Override
  public void serialize(LoadCurve curve, JsonGenerator jGenerator, SerializerProvider provider)
      throws IOException {

    jGenerator.writeStartObject();
    jGenerator.writeArrayFieldStart("loadByHoursInMW");

    do {
      jGenerator.writeNumber(curve.getPowerOnTimeInMW(pointer));
      pointer = pointer.plusHours(1);
    } while (pointer != LocalTime.MIDNIGHT);

    jGenerator.writeEndArray();
    jGenerator.writeEndObject();

    logger.debug("Serialized: {} to JSON.", curve);
  }
 private void executeQuery(JsonGenerator jg, Query query) throws IOException {
   query.setStart(System.currentTimeMillis());
   try (Transaction ignored = database.beginTx();
       Result result = database.execute(query.toCypher())) {
     query.setEnd(System.currentTimeMillis());
     jg.writeNumberField("duration", query.getDuration());
     if (!query.isCountQuery()) jg.writeArrayFieldStart(query.getResultHeader());
     while (result.hasNext()) {
       Map<String, Object> row = result.next();
       if (!query.isCountQuery()) jg.writeStartObject();
       for (Entry<String, Object> column : row.entrySet()) {
         writeField(column, jg);
       }
       if (!query.isCountQuery()) jg.writeEndObject();
     }
     if (!query.isCountQuery()) jg.writeEndArray();
   }
 }
  private void writeActionDescriptors(
      JsonGenerator jgen, String currentVocab, List<ActionDescriptor> actionDescriptors)
      throws IOException, IntrospectionException {
    for (ActionDescriptor actionDescriptor : actionDescriptors) {
      jgen.writeStartObject(); // begin a hydra:Operation

      final String semanticActionType = actionDescriptor.getSemanticActionType();
      if (semanticActionType != null) {
        jgen.writeStringField("@type", semanticActionType);
      }
      jgen.writeStringField("hydra:method", actionDescriptor.getHttpMethod());

      final ActionInputParameter requestBodyInputParameter = actionDescriptor.getRequestBody();
      if (requestBodyInputParameter != null) {

        jgen.writeObjectFieldStart("hydra:expects"); // begin hydra:expects

        final Class<?> clazz = requestBodyInputParameter.getParameterType();
        final Expose classExpose = clazz.getAnnotation(Expose.class);
        final String typeName;
        if (classExpose != null) {
          typeName = classExpose.value();
        } else {
          typeName = requestBodyInputParameter.getParameterType().getSimpleName();
        }
        jgen.writeStringField("@type", typeName);

        jgen.writeArrayFieldStart("hydra:supportedProperty"); // begin hydra:supportedProperty
        // TODO check need for actionDescriptor and requestBodyInputParameter here:
        recurseSupportedProperties(
            jgen,
            currentVocab,
            clazz,
            actionDescriptor,
            requestBodyInputParameter,
            requestBodyInputParameter.getCallValue());
        jgen.writeEndArray(); // end hydra:supportedProperty

        jgen.writeEndObject(); // end hydra:expects
      }

      jgen.writeEndObject(); // end hydra:Operation
    }
  }
Example #14
0
  public InputStream verifyValueSet2(Profile p, Profile baseP, String id, String type) {
    // Type is ValueSet (or Table)
    String result = "";
    // Table t = p.getTableLibrary().findOneTableById(id);

    try {
      // Create temporary file
      File tmpJsonFile = File.createTempFile("resultTmp", ".json");

      // Generate json file
      JsonFactory factory = new JsonFactory();
      JsonGenerator generator = factory.createGenerator(new FileWriter(tmpJsonFile));
      generator.setPrettyPrinter(new DefaultPrettyPrinter());

      generator.writeStartObject();
      generator.writeStringField("id", id);
      generator.writeStringField("type", type);

      generator.writeArrayFieldStart("eltVerification");

      // for (Code c : t.getCodes()){
      //
      // generator.writeStartObject();
      // generator.writeStringField("eltName", "usage");
      // generator.writeStringField("eltAtt", c.getCodeUsage());
      // result = this.validateChangeUsage(p.getMetaData().getHl7Version(),
      // Usage.fromValue(baseP.getTableLibrary().findOneCodeById(id).getCodeUsage()),
      // Usage.fromValue(p.getTableLibrary().findOneCodeById(id).getCodeUsage()));
      // generator.writeStringField("result", result);
      // generator.writeEndObject();
      //
      // }

      generator.writeEndArray();
      generator.writeEndObject();

      generator.close();

      return FileUtils.openInputStream(tmpJsonFile);
    } catch (IOException e) {
      return new NullInputStream(1L);
    }
  }
Example #15
0
  @Override
  public void toJson(JsonGenerator gen) throws IOException {
    gen.writeStartObject();

    // common tags
    gen.writeObjectFieldStart("tags");
    for (Tag tag : tags) {
      gen.writeStringField(
          ValidCharacters.toValidCharset(tag.getKey()),
          ValidCharacters.toValidCharset(tag.getValue()));
    }
    gen.writeEndObject();

    gen.writeArrayFieldStart("metrics");
    for (AtlasMetric m : metrics) {
      m.toJson(gen);
    }
    gen.writeEndArray();

    gen.writeEndObject();
    gen.flush();
  }
Example #16
0
  public String getGroups(@Name("filter") String filter) {
    StringWriter resultWriter = new StringWriter();
    JsonFactory f = new JsonFactory();
    f.setCodec(om);
    JsonGenerator g;
    try {
      g = f.createGenerator(resultWriter);

      final Node target = DB.getIndex().get("id", "agents").getSingle();
      TraversalDescription td =
          Traversal.description()
              .relationships(RelTypes.SUBGROUP, Direction.OUTGOING)
              .uniqueness(Uniqueness.NODE_PATH);

      Traverser results = td.traverse(target);
      g.writeStartArray();
      Iterator<Path> paths = results.iterator();
      while (paths.hasNext()) {
        Path path = paths.next();
        if (!path.endNode().hasProperty("name")) continue;
        g.writeStartObject();
        g.writeStringField("name", (String) path.endNode().getProperty("name"));
        g.writeArrayFieldStart("path");
        for (Node pathStep : path.nodes()) {
          if (pathStep.hasProperty("name")) g.writeString((String) pathStep.getProperty("name"));
        }
        g.writeEndArray();
        g.writeEndObject();
      }
      g.writeEndArray();
      g.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    resultWriter.flush();
    return resultWriter.toString();
  }
  @Override
  public void serialize(CyNetworkView networkView, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonProcessingException {
    jgen.useDefaultPrettyPrinter();

    jgen.writeStartObject();
    jgen.writeStringField(
        CytoscapeJsNetworkModule.FORMAT_VERSION_TAG, CytoscapeJsNetworkModule.FORMAT_VERSION);
    jgen.writeStringField(CytoscapeJsNetworkModule.GENERATED_BY_TAG, "cytoscape-" + version);
    jgen.writeStringField(
        CytoscapeJsNetworkModule.TARGET_CYJS_VERSION_TAG,
        CytoscapeJsNetworkModule.CYTOSCAPEJS_VERSION);

    final CyNetwork network = networkView.getModel();

    // Serialize network data table
    jgen.writeObjectFieldStart(DATA.getTag());
    jgen.writeObject(network.getRow(network));
    jgen.writeEndObject();

    jgen.writeObjectFieldStart(ELEMENTS.getTag());

    // Write array
    final List<CyNode> nodes = network.getNodeList();
    final List<CyEdge> edges = network.getEdgeList();

    jgen.writeArrayFieldStart(NODES.getTag());
    for (final CyNode node : nodes) {
      jgen.writeStartObject();

      // Data field
      jgen.writeObjectFieldStart(DATA.getTag());
      jgen.writeStringField(ID.getTag(), node.getSUID().toString());
      // Write CyRow in "data" field
      jgen.writeObject(network.getRow(node));
      jgen.writeEndObject();

      // Position and other visual props
      jgen.writeObject(networkView.getNodeView(node));

      // Special case for cytoscape.js format:
      // - Selected
      jgen.writeBooleanField(
          CyNetwork.SELECTED, network.getRow(node).get(CyNetwork.SELECTED, Boolean.class));

      jgen.writeEndObject();
    }
    jgen.writeEndArray();

    jgen.writeArrayFieldStart(EDGES.getTag());
    for (final CyEdge edge : edges) {
      jgen.writeStartObject();

      jgen.writeObjectFieldStart(DATA.getTag());
      jgen.writeStringField(ID.getTag(), edge.getSUID().toString());
      jgen.writeStringField(SOURCE.getTag(), edge.getSource().getSUID().toString());
      jgen.writeStringField(TARGET.getTag(), edge.getTarget().getSUID().toString());

      // Write CyRow in "data" field
      jgen.writeObject(network.getRow(edge));

      jgen.writeEndObject();

      // Special case for cytoscape.js format:
      // - Selected
      jgen.writeBooleanField(
          CyNetwork.SELECTED, network.getRow(edge).get(CyNetwork.SELECTED, Boolean.class));

      jgen.writeEndObject();
    }
    jgen.writeEndArray();

    jgen.writeEndObject();
  }
  @Override
  public String handleRequest(ExecutionJobVertex jobVertex, Map<String, String> params)
      throws Exception {
    // Build a map that groups tasks by TaskManager
    Map<String, List<ExecutionVertex>> taskManagerVertices = new HashMap<>();

    for (ExecutionVertex vertex : jobVertex.getTaskVertices()) {
      TaskManagerLocation location = vertex.getCurrentAssignedResourceLocation();
      String taskManager =
          location == null ? "(unassigned)" : location.getHostname() + ":" + location.dataPort();

      List<ExecutionVertex> vertices = taskManagerVertices.get(taskManager);

      if (vertices == null) {
        vertices = new ArrayList<ExecutionVertex>();
        taskManagerVertices.put(taskManager, vertices);
      }

      vertices.add(vertex);
    }

    // Build JSON response
    final long now = System.currentTimeMillis();

    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

    gen.writeStartObject();

    gen.writeStringField("id", jobVertex.getJobVertexId().toString());
    gen.writeStringField("name", jobVertex.getJobVertex().getName());
    gen.writeNumberField("now", now);

    gen.writeArrayFieldStart("taskmanagers");
    for (Entry<String, List<ExecutionVertex>> entry : taskManagerVertices.entrySet()) {
      String host = entry.getKey();
      List<ExecutionVertex> taskVertices = entry.getValue();

      int[] tasksPerState = new int[ExecutionState.values().length];

      long startTime = Long.MAX_VALUE;
      long endTime = 0;
      boolean allFinished = true;

      LongCounter tmReadBytes = new LongCounter();
      LongCounter tmWriteBytes = new LongCounter();
      LongCounter tmReadRecords = new LongCounter();
      LongCounter tmWriteRecords = new LongCounter();

      for (ExecutionVertex vertex : taskVertices) {
        final ExecutionState state = vertex.getExecutionState();
        tasksPerState[state.ordinal()]++;

        // take the earliest start time
        long started = vertex.getStateTimestamp(ExecutionState.DEPLOYING);
        if (started > 0) {
          startTime = Math.min(startTime, started);
        }

        allFinished &= state.isTerminal();
        endTime = Math.max(endTime, vertex.getStateTimestamp(state));

        Map<AccumulatorRegistry.Metric, Accumulator<?, ?>> metrics =
            vertex.getCurrentExecutionAttempt().getFlinkAccumulators();

        if (metrics != null) {
          LongCounter readBytes =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_BYTES_IN);
          tmReadBytes.merge(readBytes);

          LongCounter writeBytes =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_BYTES_OUT);
          tmWriteBytes.merge(writeBytes);

          LongCounter readRecords =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_RECORDS_IN);
          tmReadRecords.merge(readRecords);

          LongCounter writeRecords =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_RECORDS_OUT);
          tmWriteRecords.merge(writeRecords);
        }
      }

      long duration;
      if (startTime < Long.MAX_VALUE) {
        if (allFinished) {
          duration = endTime - startTime;
        } else {
          endTime = -1L;
          duration = now - startTime;
        }
      } else {
        startTime = -1L;
        endTime = -1L;
        duration = -1L;
      }

      ExecutionState jobVertexState =
          ExecutionJobVertex.getAggregateJobVertexState(tasksPerState, taskVertices.size());

      gen.writeStartObject();

      gen.writeStringField("host", host);
      gen.writeStringField("status", jobVertexState.name());

      gen.writeNumberField("start-time", startTime);
      gen.writeNumberField("end-time", endTime);
      gen.writeNumberField("duration", duration);

      gen.writeObjectFieldStart("metrics");
      gen.writeNumberField("read-bytes", tmReadBytes.getLocalValuePrimitive());
      gen.writeNumberField("write-bytes", tmWriteBytes.getLocalValuePrimitive());
      gen.writeNumberField("read-records", tmReadRecords.getLocalValuePrimitive());
      gen.writeNumberField("write-records", tmWriteRecords.getLocalValuePrimitive());
      gen.writeEndObject();

      gen.writeObjectFieldStart("status-counts");
      for (ExecutionState state : ExecutionState.values()) {
        gen.writeNumberField(state.name(), tasksPerState[state.ordinal()]);
      }
      gen.writeEndObject();

      gen.writeEndObject();
    }
    gen.writeEndArray();

    gen.writeEndObject();

    gen.close();
    return writer.toString();
  }
Example #19
0
  @Override
  public String handleJsonRequest(
      Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager)
      throws Exception {
    try {
      if (jobManager != null) {
        // whether one task manager's metrics are requested, or all task manager, we
        // return them in an array. This avoids unnecessary code complexity.
        // If only one task manager is requested, we only fetch one task manager metrics.
        final List<Instance> instances = new ArrayList<>();
        if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
          try {
            InstanceID instanceID =
                new InstanceID(StringUtils.hexStringToByte(pathParams.get(TASK_MANAGER_ID_KEY)));
            Future<Object> future =
                jobManager.ask(
                    new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout);
            TaskManagerInstance instance = (TaskManagerInstance) Await.result(future, timeout);
            if (instance.instance().nonEmpty()) {
              instances.add(instance.instance().get());
            }
          }
          // this means the id string was invalid. Keep the list empty.
          catch (IllegalArgumentException e) {
            // do nothing.
          }
        } else {
          Future<Object> future =
              jobManager.ask(JobManagerMessages.getRequestRegisteredTaskManagers(), timeout);
          RegisteredTaskManagers taskManagers =
              (RegisteredTaskManagers) Await.result(future, timeout);
          instances.addAll(taskManagers.asJavaCollection());
        }

        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

        gen.writeStartObject();
        gen.writeArrayFieldStart("taskmanagers");

        for (Instance instance : instances) {
          gen.writeStartObject();
          gen.writeStringField("id", instance.getId().toString());
          gen.writeStringField("path", instance.getActorGateway().path());
          gen.writeNumberField("dataPort", instance.getTaskManagerLocation().dataPort());
          gen.writeNumberField("timeSinceLastHeartbeat", instance.getLastHeartBeat());
          gen.writeNumberField("slotsNumber", instance.getTotalNumberOfSlots());
          gen.writeNumberField("freeSlots", instance.getNumberOfAvailableSlots());
          gen.writeNumberField("cpuCores", instance.getResources().getNumberOfCPUCores());
          gen.writeNumberField("physicalMemory", instance.getResources().getSizeOfPhysicalMemory());
          gen.writeNumberField("freeMemory", instance.getResources().getSizeOfJvmHeap());
          gen.writeNumberField("managedMemory", instance.getResources().getSizeOfManagedMemory());

          // only send metrics when only one task manager requests them.
          if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
            byte[] report = instance.getLastMetricsReport();
            if (report != null) {
              gen.writeFieldName("metrics");
              gen.writeRawValue(new String(report, "utf-8"));
            }
          }

          gen.writeEndObject();
        }

        gen.writeEndArray();
        gen.writeEndObject();

        gen.close();
        return writer.toString();
      } else {
        throw new Exception("No connection to the leading JobManager.");
      }
    } catch (Exception e) {
      throw new RuntimeException("Failed to fetch list of all task managers: " + e.getMessage(), e);
    }
  }
  private void recurseSupportedProperties(
      JsonGenerator jgen,
      String currentVocab,
      Class<?> beanType,
      ActionDescriptor actionDescriptor,
      ActionInputParameter actionInputParameter,
      Object currentCallValue)
      throws IntrospectionException, IOException {
    // TODO support Option provider by other method args?
    final BeanInfo beanInfo = Introspector.getBeanInfo(beanType);
    final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    // TODO collection and map

    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
      final Method writeMethod = propertyDescriptor.getWriteMethod();
      if (writeMethod == null) {
        continue;
      }
      final Class<?> propertyType = propertyDescriptor.getPropertyType();
      // TODO: the property name must be a valid URI - need to check context for terms?
      String propertyName = getWritableExposedPropertyOrPropertyName(propertyDescriptor);
      if (DataType.isSingleValueType(propertyType)) {

        final Property property =
            new Property(
                beanType,
                propertyDescriptor.getReadMethod(),
                propertyDescriptor.getWriteMethod(),
                propertyDescriptor.getName());

        Object propertyValue = PropertyUtils.getPropertyValue(currentCallValue, propertyDescriptor);

        MethodParameter methodParameter =
            new MethodParameter(propertyDescriptor.getWriteMethod(), 0);
        ActionInputParameter propertySetterInputParameter =
            new ActionInputParameter(methodParameter, propertyValue);
        final Object[] possiblePropertyValues =
            actionInputParameter.getPossibleValues(
                propertyDescriptor.getWriteMethod(), 0, actionDescriptor);

        writeSupportedProperty(
            jgen,
            currentVocab,
            propertySetterInputParameter,
            propertyName,
            property,
            possiblePropertyValues);
      } else {
        jgen.writeStartObject();
        jgen.writeStringField("hydra:property", propertyName);
        // TODO: is the property required -> for bean props we need the Access annotation to express
        // that
        jgen.writeObjectFieldStart(
            getPropertyOrClassNameInVocab(
                currentVocab, "rangeIncludes", LdContextFactory.HTTP_SCHEMA_ORG, "schema:"));
        Expose expose = AnnotationUtils.getAnnotation(propertyType, Expose.class);
        String subClass;
        if (expose != null) {
          subClass = expose.value();
        } else {
          subClass = propertyType.getSimpleName();
        }
        jgen.writeStringField(
            getPropertyOrClassNameInVocab(
                currentVocab, "subClassOf", "http://www.w3.org/2000/01/rdf-schema#", "rdfs:"),
            subClass);

        jgen.writeArrayFieldStart("hydra:supportedProperty");

        Object propertyValue = PropertyUtils.getPropertyValue(currentCallValue, propertyDescriptor);

        recurseSupportedProperties(
            jgen,
            currentVocab,
            propertyType,
            actionDescriptor,
            actionInputParameter,
            propertyValue);
        jgen.writeEndArray();

        jgen.writeEndObject();
        jgen.writeEndObject();
      }
    }
  }
  @Override
  public void serialize(List<Link> links, JsonGenerator jgen, SerializerProvider serializerProvider)
      throws IOException {

    try {
      Collection<Link> simpleLinks = new ArrayList<Link>();
      Collection<Affordance> affordances = new ArrayList<Affordance>();
      Collection<Link> templatedLinks = new ArrayList<Link>();
      Collection<Affordance> templatedAffordances = new ArrayList<Affordance>();
      Collection<Affordance> collectionAffordances = new ArrayList<Affordance>();
      Link selfRel = null;
      for (Link link : links) {
        if (link instanceof Affordance) {
          final Affordance affordance = (Affordance) link;
          final List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors();
          if (!actionDescriptors.isEmpty()) {
            if (affordance.isTemplated()) {
              templatedAffordances.add(affordance);
            } else {
              if (!affordance.isSelfRel()
                  && Cardinality.COLLECTION == affordance.getCardinality()) {
                collectionAffordances.add(affordance);
              } else {
                affordances.add(affordance);
              }
            }
          } else {
            if (affordance.isTemplated()) {
              templatedLinks.add(affordance);
            } else {
              simpleLinks.add(affordance);
            }
          }
        } else if (link.isTemplated()) {
          templatedLinks.add(link);
        } else {
          simpleLinks.add(link);
        }
        if ("self".equals(link.getRel())) {
          selfRel = link;
        }
      }

      for (Affordance templatedAffordance : templatedAffordances) {
        jgen.writeObjectFieldStart(templatedAffordance.getRel());

        jgen.writeStringField("@type", "hydra:IriTemplate");
        jgen.writeStringField("hydra:template", templatedAffordance.getHref());
        final List<ActionDescriptor> actionDescriptors = templatedAffordance.getActionDescriptors();
        ActionDescriptor actionDescriptor = actionDescriptors.get(0);
        jgen.writeArrayFieldStart("hydra:mapping");
        writeHydraVariableMapping(jgen, actionDescriptor, actionDescriptor.getPathVariableNames());
        writeHydraVariableMapping(jgen, actionDescriptor, actionDescriptor.getRequestParamNames());
        jgen.writeEndArray();

        jgen.writeEndObject();
      }
      for (Link templatedLink : templatedLinks) {
        // we only have the template, no access to method params
        jgen.writeObjectFieldStart(templatedLink.getRel());

        jgen.writeStringField("@type", "hydra:IriTemplate");
        jgen.writeStringField("hydra:template", templatedLink.getHref());

        jgen.writeArrayFieldStart("hydra:mapping");
        writeHydraVariableMapping(jgen, null, templatedLink.getVariableNames());
        jgen.writeEndArray();

        jgen.writeEndObject();
      }

      @SuppressWarnings("unchecked")
      Deque<LdContext> contextStack =
          (Deque<LdContext>) serializerProvider.getAttribute(JacksonHydraSerializer.KEY_LD_CONTEXT);
      String currentVocab =
          (contextStack != null && !contextStack.isEmpty()) ? contextStack.peek().vocab : null;

      // related collections
      if (!collectionAffordances.isEmpty()) {
        jgen.writeArrayFieldStart("hydra:collection");

        for (Affordance collectionAffordance : collectionAffordances) {
          jgen.writeStartObject();
          jgen.writeStringField(JsonLdKeywords.AT_TYPE, "hydra:Collection");
          jgen.writeStringField(JsonLdKeywords.AT_ID, collectionAffordance.getHref());
          jgen.writeObjectFieldStart("hydra:manages");
          jgen.writeStringField("hydra:property", collectionAffordance.getRel());
          // a) in the case of </Alice> :knows /bob the subject must be /Alice
          // b) in the case of <> orderedItem /latte-1 the subject is an anonymous resource of type
          // Order
          // c) in the case of </order/1> :seller </store> the object must be the store
          // 1. where is the information *about* the subject or object: the type or @id
          // 2. how to decide if the collection manages items for a subject or object?
          // ad 1.)
          // ad a) self rel of the Resource that has the collection affordance: looking through link
          // list
          //       at serialization time is possible
          // ad b) a candidate is the class given as value of @ExposesResourceFor on the controller
          //       that defines the method which leads us to believe we have a collection:
          //       either a GET /orders handler having a collection return type or the POST /orders.
          //       Works only if the GET or POST has no request mapping path of its own
          //       an alternative is to use {} without type if @ExposesResourceFor is not present
          // ad c) like a
          // ad 2.)
          // we know the affordance is a collection
          // * we could pass information into build(rel), like
          // .rel().reverseRel("schema:seller").build()
          // * we could use build("myReversedTerm") and look at the @context to see if it is
          // reversed,
          //   if so, we know that the manages block must use object not subject. However weird if
          // the
          //   reverse term is never really used in the json-ld
          // * we could have a registry which says if a property is meant to be reversed in a
          // certain context
          //   and use that to find out if we need subject or object, like :seller ->
          if (selfRel != null) {
            // prefer rev over rel, assuming that rev exists to be used by RDF serialization
            if (collectionAffordance.getRev() != null) {
              jgen.writeStringField("hydra:object", selfRel.getHref());
            } else if (collectionAffordance.getRel() != null) {
              jgen.writeStringField("hydra:subject", selfRel.getHref());
            }
          } else {
            // prefer rev over rel, assuming that rev exists to be used by RDF serialization
            if (collectionAffordance.getRev() != null) {
              jgen.writeObjectFieldStart("hydra:object");
              jgen.writeEndObject();
            } else if (collectionAffordance.getRel() != null) {
              jgen.writeObjectFieldStart("hydra:subject");
              jgen.writeEndObject();
            }
          }
          jgen.writeEndObject(); // end manages

          List<ActionDescriptor> actionDescriptors = collectionAffordance.getActionDescriptors();
          if (!actionDescriptors.isEmpty()) {
            jgen.writeArrayFieldStart("hydra:operation");
          }
          writeActionDescriptors(jgen, currentVocab, actionDescriptors);
          if (!actionDescriptors.isEmpty()) {
            jgen.writeEndArray(); // end hydra:operation
          }

          jgen.writeEndObject(); // end collection
        }
        jgen.writeEndArray();
      }

      for (Affordance affordance : affordances) {
        final String rel = affordance.getRel();
        List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors();

        if (!actionDescriptors.isEmpty()) {
          if (!Link.REL_SELF.equals(rel)) {
            jgen.writeObjectFieldStart(rel); // begin rel
          }
          jgen.writeStringField(JsonLdKeywords.AT_ID, affordance.getHref());
          jgen.writeArrayFieldStart("hydra:operation");
        }

        writeActionDescriptors(jgen, currentVocab, actionDescriptors);

        if (!actionDescriptors.isEmpty()) {
          jgen.writeEndArray(); // end hydra:operation

          if (!Link.REL_SELF.equals(rel)) {
            jgen.writeEndObject(); // end rel
          }
        }
      }

      for (Link simpleLink : simpleLinks) {
        final String rel = simpleLink.getRel();
        if (Link.REL_SELF.equals(rel)) {
          jgen.writeStringField("@id", simpleLink.getHref());
        } else {
          String linkAttributeName = IanaRels.isIanaRel(rel) ? IANA_REL_PREFIX + rel : rel;
          jgen.writeObjectFieldStart(linkAttributeName);
          jgen.writeStringField("@id", simpleLink.getHref());
          jgen.writeEndObject();
        }
      }
    } catch (IntrospectionException e) {
      throw new RuntimeException(e);
    }
  }
Example #22
0
  public String getContacts(@Name("filter") String filter) {
    StringWriter resultWriter = new StringWriter();
    JsonFactory f = new JsonFactory();
    f.setCodec(om);
    JsonGenerator g;
    try {
      g = f.createGenerator(resultWriter);

      final Node target = DB.getIndex().get("id", "agents").getSingle();
      TraversalDescription td =
          Traversal.description()
              .relationships(RelTypes.GROUPMEMBER, Direction.OUTGOING)
              .relationships(RelTypes.SUBGROUP, Direction.OUTGOING)
              .uniqueness(Uniqueness.NODE_PATH)
              .evaluator(
                  new Evaluator() {
                    @Override
                    public Evaluation evaluate(Path path) {
                      if (path.endNode().hasProperty("name")
                          && path.lastRelationship().isType(RelTypes.GROUPMEMBER)) {
                        return Evaluation.INCLUDE_AND_PRUNE;
                      }
                      return Evaluation.EXCLUDE_AND_CONTINUE;
                    }
                  });

      Traverser results = td.traverse(target);

      Multimap<Long, Path> groupByNode = ArrayListMultimap.create();

      Iterator<Path> paths = results.iterator();
      while (paths.hasNext()) {
        Path path = paths.next();
        groupByNode.put(path.endNode().getId(), path);
      }
      g.writeStartArray();
      for (Long nodeId : groupByNode.keySet()) {
        List<Path> pathList = (List<Path>) groupByNode.get(nodeId);
        g.writeStartObject();
        Node node = pathList.get(0).endNode();
        g.writeStringField("name", (String) node.getProperty("name"));
        g.writeArrayFieldStart("groups");
        for (Path path : pathList) {
          g.writeStartObject();
          g.writeStringField(
              "name", (String) path.lastRelationship().getStartNode().getProperty("name"));
          g.writeArrayFieldStart("path");
          for (Node pathStep : path.nodes()) {
            if (!pathStep.equals(node) && pathStep.hasProperty("name"))
              g.writeString((String) pathStep.getProperty("name"));
          }
          g.writeEndArray();
          g.writeEndObject();
        }
        g.writeEndArray();
        g.writeEndObject();
      }
      g.writeEndArray();
      g.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    resultWriter.flush();
    return resultWriter.toString();
  }