@Test
  public void testGroupByWithSum1()
      throws JsonGenerationException, JsonMappingException, IOException, InterruptedException {
    Object[][] rows1 = {{0}, {2}, {2}, {5}, {10}, {100}};
    Block block = new ArrayBlock(Arrays.asList(rows1), new String[] {"a"}, 1);

    TupleOperator operator = new GroupByOperator();
    Map<String, Block> input = new HashMap<String, Block>();
    input.put("first", block);

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode json = mapper.createObjectNode();
    json.put("input", "first");
    ArrayNode anode = mapper.createArrayNode();
    anode.add("a");
    json.put("groupBy", anode);
    anode = mapper.createArrayNode();
    ObjectNode onode = mapper.createObjectNode();
    onode.put("type", "SUM");
    onode.put("input", "a");
    onode.put("output", "sum");
    anode.add(onode);
    json.put("aggregates", anode);

    BlockProperties props =
        new BlockProperties(null, new BlockSchema("INT a, INT sum"), (BlockProperties) null);
    operator.setInput(input, json, props);

    Block output = new TupleOperatorBlock(operator, props);

    ArrayBlock.assertData(
        output,
        new Object[][] {{0, 0}, {2, 4}, {5, 5}, {10, 10}, {100, 100}},
        new String[] {"a", "sum"});
  }
  /**
   * Generates the JSON code for the raw log entry collection.
   *
   * @param model the model data to output
   * @param locale the locale indicating the language in which localizable data should be shown
   * @return a string containing the JSON code for the raw log entry collection
   * @throws MonitorInterfaceException
   *     <ul>
   *       <li>the model data is invalid
   *       <li>an error occurred while converting the raw log entry collection to JSON
   *     </ul>
   */
  @SuppressWarnings("unchecked")
  @Override
  protected JsonNode getResponseData(Map<String, ?> model, Locale locale)
      throws MonitorInterfaceException {

    if (model.containsKey("rawLogsCollection") && model.containsKey("addQueryId")) {

      if (model.containsKey("getExport")
          && model.containsKey("Slaname")
          && model.containsKey("Jobname")
          && model.containsKey("Queryname")) {
        final Set<RawLogEntry> logsCollection = (Set<RawLogEntry>) model.get("rawLogsCollection");
        final Boolean addQueryId = (Boolean) model.get("addQueryId");
        final Boolean getExport = (Boolean) model.get("getExport");
        final Boolean isSummary = (Boolean) model.get("isSummary");
        final String slaName = (String) model.get("Slaname");
        final String jobName = (String) model.get("Jobname");
        final String queryName = (String) model.get("Queryname");
        final ObjectMapper mapper = this.getObjectMapper();
        final ArrayNode rowsCollection = mapper.createArrayNode();
        List<RawLogEntry> sortLogs = new ArrayList<RawLogEntry>(logsCollection);
        Collections.sort(sortLogs, new MyLogComparable());

        for (RawLogEntry logEntry : sortLogs) {
          rowsCollection.add(
              RawLogSerializer.serialize(
                  logEntry,
                  addQueryId,
                  getExport,
                  slaName,
                  jobName,
                  queryName,
                  locale,
                  mapper,
                  isSummary,
                  null));
        }
        return rowsCollection;
      } else {
        final Set<RawLogEntry> logsCollection = (Set<RawLogEntry>) model.get("rawLogsCollection");
        final Boolean addQueryId = (Boolean) model.get("addQueryId");
        final Boolean isSummary = (Boolean) model.get("isSummary");
        final Long noPagingCount = (Long) model.get("noPagingCount");
        final ObjectMapper mapper = this.getObjectMapper();
        this.getRootObjectNode().put("noPagingCount", noPagingCount);
        final ArrayNode rowsCollection = mapper.createArrayNode();
        for (RawLogEntry logEntry : logsCollection) {
          rowsCollection.add(
              RawLogSerializer.serialize(logEntry, addQueryId, locale, mapper, isSummary));
        }
        return rowsCollection;
      }
    }
    throw new MonitorInterfaceException("An internal error occurred", "internal.error");
  }
Beispiel #3
0
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String thisUsersId = req.getParameter("userId");
    if ("true".equals(req.getParameter("pingAlive"))) {
      updateLastAliveTime(thisUsersId);
    } else {
      ObjectMapper mapper = new ObjectMapper();

      ArrayNode usersArray = mapper.createArrayNode();

      for (Map.Entry<String, User> userEntry : users.entrySet()) {
        if (!thisUsersId.equals(userEntry.getKey())) {
          User user = userEntry.getValue();
          Date now = new Date();
          if ((now.getTime() - user.getLastAliveTime().getTime()) / 1000 <= 10) {
            ObjectNode userJson = mapper.createObjectNode();
            userJson.put("user_id", userEntry.getKey());
            userJson.put("user_name", user.getName());
            usersArray.add(userJson);
          }
        }
      }

      ObjectNode usersJson = mapper.createObjectNode();
      usersJson.put("opponents", usersArray);

      resp.setContentType("application/json; charset=UTF-8");
      mapper.writeValue(resp.getWriter(), usersJson);
    }
  }
  @Test
  public void testSortOperator()
      throws JsonGenerationException, JsonMappingException, IOException, InterruptedException {
    System.out.println("Testing SORT operator");

    Object[][] rows1 = {{0, 10, 0}, {2, 5, 2}, {2, 8, 5}, {5, 9, 6}, {10, 11, 1}, {100, 6, 10}};
    Block block = new ArrayBlock(Arrays.asList(rows1), new String[] {"a", "b", "c"}, 1);

    TupleOperator operator = new SortOperator();
    Map<String, Block> input = new HashMap<String, Block>();
    input.put("unsorted", block);

    ObjectMapper mapper = new ObjectMapper();

    ObjectNode json = mapper.createObjectNode();
    json.put("input", "unsorted");
    ArrayNode anode = mapper.createArrayNode();
    anode.add("b");
    anode.add("c");
    json.put("sortBy", anode);

    BlockProperties props =
        new BlockProperties(null, new BlockSchema("INT a, INT b, INT c"), (BlockProperties) null);
    operator.setInput(input, json, props);

    Block output = new TupleOperatorBlock(operator, props);

    System.out.println("output is " + output);
    ArrayBlock.assertData(
        output,
        new Object[][] {{2, 5, 2}, {100, 6, 10}, {2, 8, 5}, {5, 9, 6}, {0, 10, 0}, {10, 11, 1}},
        new String[] {"a", "b", "c"});
  }
 private ArrayNode createSerializedTimestamp(int[] vector, ObjectMapper mapper) {
   ArrayNode ret = mapper.createArrayNode();
   for (int val : vector) {
     ret.add(val);
   }
   return ret;
 }
  /**
   * Calculate the results graph data. The graph includes final rank graph and final vs provisional
   * score graph.
   *
   * @param resultInfos the results
   */
  private void calculateResultsGraphData(List<ResultInfo> resultInfos) {
    // Sort the results info by final score.
    Collections.sort(
        resultInfos,
        new Comparator<ResultInfo>() {
          public int compare(ResultInfo resultInfo, ResultInfo resultInfo2) {
            if (resultInfo.getFinalRank() < resultInfo2.getFinalRank()) {
              return 0;
            }
            if (resultInfo.getFinalRank() == resultInfo2.getFinalRank()) {
              return String.CASE_INSENSITIVE_ORDER.compare(
                  resultInfo.getHandle(), resultInfo2.getHandle());
            }
            return 1;
          }
        });
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode finalScoreRankingData = objectMapper.createObjectNode();
    ObjectNode finalVsProvisionalScoreData = objectMapper.createObjectNode();
    ArrayNode rating = objectMapper.createArrayNode();
    ArrayNode score = objectMapper.createArrayNode();
    for (ResultInfo result : resultInfos) {
      ObjectNode finalScoreNode = objectMapper.createObjectNode();
      finalScoreNode.put("handle", result.getHandle());
      finalScoreNode.put("number", result.getFinalScore());
      finalScoreNode.put("rank", result.getFinalRank());
      rating.add(finalScoreNode);

      ObjectNode finalProvisionalScoreNode = objectMapper.createObjectNode();
      finalProvisionalScoreNode.put("handle", result.getHandle());
      finalProvisionalScoreNode.put("finalScore", result.getFinalScore());
      finalProvisionalScoreNode.put("provisionalScore", result.getProvisionalScore());
      score.add(finalProvisionalScoreNode);
    }
    finalScoreRankingData.put("rating", rating);
    finalVsProvisionalScoreData.put("score", score);

    viewData.setFinalScoreRankingData(finalScoreRankingData.toString());
    viewData.setFinalVsProvisionalScoreData(finalVsProvisionalScoreData.toString());
  }
  @Test
  // testing multiple join keys
  public void testMergeJoinMultipleJoinKeys()
      throws JsonGenerationException, JsonMappingException, IOException, InterruptedException {
    Object[][] rows1 = {{0, 1}, {2, 1}, {2, 2}, {5, 1}, {10, 1}, {100, 1}};
    Object[][] rows2 = {{1, 1}, {2, 0}, {2, 1}, {5, 1}, {100, 2}, {100, 3}};
    Object[][] expected = {{2, 1, 2, 1}, {5, 1, 5, 1}};

    Block block1 = new ArrayBlock(Arrays.asList(rows1), new String[] {"a", "b"});
    Block block2 = new ArrayBlock(Arrays.asList(rows2), new String[] {"c", "a"});

    TupleOperator operator = new MergeJoinOperator();
    Map<String, Block> input = new HashMap<String, Block>();
    input.put("block1", block1);
    input.put("block2", block2);

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode node = mapper.createObjectNode();
    ArrayNode lkeys = mapper.createArrayNode();
    lkeys.add("a");
    lkeys.add("b");
    node.put("leftCubeColumns", lkeys);
    ArrayNode rkeys = mapper.createArrayNode();
    rkeys.add("c");
    rkeys.add("a");
    node.put("rightCubeColumns", rkeys);
    node.put("leftBlock", "block1");

    BlockProperties props =
        new BlockProperties(
            null,
            new BlockSchema("INT block1___a, INT block1___b, INT block2___c, INT block2___a"),
            (BlockProperties) null);
    operator.setInput(input, node, props);

    Block output = new TupleOperatorBlock(operator, props);

    ArrayBlock.assertData(output, expected, new String[] {"block1.a", "block2.a"});
  }
Beispiel #8
0
  public static void main(String args[]) {

    JacksonTester tester = new JacksonTester();

    try {
      ObjectMapper mapper = new ObjectMapper();

      JsonNode rootNode = mapper.createObjectNode();
      JsonNode marksNode = mapper.createArrayNode();

      ((ArrayNode) marksNode).add(100);
      ((ArrayNode) marksNode).add(90);
      ((ArrayNode) marksNode).add(85);

      ((ObjectNode) rootNode).put("name", "Mahesh Kumar");
      ((ObjectNode) rootNode).put("age", 21);
      ((ObjectNode) rootNode).put("verified", false);
      ((ObjectNode) rootNode).put("marks", marksNode);

      mapper.writeValue(new File("student.json"), rootNode);

      rootNode = mapper.readTree(new File("student.json"));

      JsonNode nameNode = rootNode.path("name");
      System.out.println("Name: " + nameNode.getTextValue());

      JsonNode ageNode = rootNode.path("age");
      System.out.println("Age: " + ageNode.getIntValue());

      JsonNode verifiedNode = rootNode.path("verified");
      System.out.println("Verified: " + (verifiedNode.getBooleanValue() ? "Yes" : "No"));

      JsonNode marksNode1 = rootNode.path("marks");
      Iterator<JsonNode> iterator = marksNode1.getElements();
      System.out.print("Marks: [ ");

      while (iterator.hasNext()) {
        JsonNode marks = iterator.next();
        System.out.print(marks.getIntValue() + " ");
      }

      System.out.println("]");
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (JsonMappingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
    // jquery-validation call
    @RequestMapping(value = "/validate", method = RequestMethod.GET)
    @ResponseBody
    public Object validateField(String fieldId, String fieldValue, String _) {

        ObjectMapper mapper = new ObjectMapper();
        ArrayNode arrayNode = mapper.createArrayNode();

        boolean result;

        if (fieldId.equals("email")) {
            arrayNode.add(fieldId);
            result = mAccountService.validateEmail(fieldValue);
            arrayNode.add(result);
        }

        return arrayNode;
    }
  /**
   * Generates the JSON code for the query collection.
   *
   * @param model the model data to output
   * @param locale the locale indicating the language in which localizable data should be shown
   * @return a string containing the JSON code for the query collection
   * @throws MonitorInterfaceException
   *     <ul>
   *       <li>the model data is invalid
   *       <li>an error occurred while converting the query collection to JSON
   *     </ul>
   */
  @SuppressWarnings("unchecked")
  @Override
  protected JsonNode getResponseData(Map<String, ?> model, Locale locale)
      throws MonitorInterfaceException {

    if (model.containsKey("queryCollection")) {
      final ObjectMapper mapper = this.getObjectMapper();
      final ArrayNode queriesList = mapper.createArrayNode();

      for (Query query : (Collection<Query>) model.get("queryCollection")) {

        queriesList.add(QuerySerializer.serialize(query, true, locale, mapper));
      }

      return queriesList;
    }

    throw new MonitorInterfaceException("An internal error occurred", "internal.error");
  }