Package net.opentsdb.tree

Examples of net.opentsdb.tree.TreeRule


   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query to work with
   * @throws BadRequestException if the request was invalid.
   */
  private void handleRule(TSDB tsdb, HttpQuery query) {
    final TreeRule rule;
    if (query.hasContent()) {
      rule = query.serializer().parseTreeRuleV1();
    } else {
      rule = parseRule(query);
    }
   
    try {
     
      // no matter what, we'll need a tree to work with, so make sure it exists
      Tree tree = null;
        tree = Tree.fetchTree(tsdb, rule.getTreeId())
          .joinUninterruptibly();
 
      if (tree == null) {
        throw new BadRequestException(HttpResponseStatus.NOT_FOUND,
            "Unable to locate tree: " + rule.getTreeId());
      }
     
      // if get, then we're just returning a rule from a tree
      if (query.getAPIMethod() == HttpMethod.GET) {
       
        final TreeRule tree_rule = tree.getRule(rule.getLevel(),
            rule.getOrder());
        if (tree_rule == null) {
          throw new BadRequestException(HttpResponseStatus.NOT_FOUND,
              "Unable to locate rule: " + rule);
        }
        query.sendReply(query.serializer().formatTreeRuleV1(tree_rule));
       
      } else if (query.getAPIMethod() == HttpMethod.POST || query.getAPIMethod() == HttpMethod.PUT) {
 
        if (rule.syncToStorage(tsdb, (query.getAPIMethod() == HttpMethod.PUT))
            .joinUninterruptibly()) {
          final TreeRule stored_rule = TreeRule.fetchRule(tsdb,
              rule.getTreeId(), rule.getLevel(), rule.getOrder())
              .joinUninterruptibly();
          query.sendReply(query.serializer().formatTreeRuleV1(stored_rule));
        } else {
          throw new RuntimeException("Unable to save rule " + rule +
View Full Code Here


   * @param query The HTTP query to work with
   * @return A rule object filled in with changes
   * @throws BadRequestException if some of the data was invalid
   */
  private TreeRule parseRule(HttpQuery query) {
    final TreeRule rule = new TreeRule(parseTreeId(query, true));
   
    if (query.hasQueryStringParam("type")) {
      try {
        rule.setType(TreeRule.stringToType(query.getQueryStringParam("type")));
      } catch (IllegalArgumentException e) {
        throw new BadRequestException("Unable to parse the 'type' parameter", e);
      }
    }
    if (query.hasQueryStringParam("field")) {
      rule.setField(query.getQueryStringParam("field"));
    }
    if (query.hasQueryStringParam("custom_field")) {
      rule.setCustomField(query.getQueryStringParam("custom_field"));
    }
    if (query.hasQueryStringParam("regex")) {
      try {
        rule.setRegex(query.getQueryStringParam("regex"));
      } catch (PatternSyntaxException e) {
        throw new BadRequestException(
            "Unable to parse the 'regex' parameter", e);
      }
    }
    if (query.hasQueryStringParam("separator")) {
      rule.setSeparator(query.getQueryStringParam("separator"));
    }
    if (query.hasQueryStringParam("description")) {
      rule.setDescription(query.getQueryStringParam("description"));
    }
    if (query.hasQueryStringParam("notes")) {
      rule.setNotes(query.getQueryStringParam("notes"));
    }
    if (query.hasQueryStringParam("regex_group_idx")) {
      try {
        rule.setRegexGroupIdx(Integer.parseInt(
            query.getQueryStringParam("regex_group_idx")));
      } catch (NumberFormatException e) {
        throw new BadRequestException(
            "Unable to parse the 'regex_group_idx' parameter", e);
      }
    }
    if (query.hasQueryStringParam("display_format")) {
      rule.setDisplayFormat(query.getQueryStringParam("display_format"));
    }
    //if (query.hasQueryStringParam("level")) {
      try {
        rule.setLevel(Integer.parseInt(
            query.getRequiredQueryStringParam("level")));
      } catch (NumberFormatException e) {
        throw new BadRequestException(
            "Unable to parse the 'level' parameter", e);
      }
    //}
    //if (query.hasQueryStringParam("order")) {
      try {
        rule.setOrder(Integer.parseInt(
            query.getRequiredQueryStringParam("order")));
      } catch (NumberFormatException e) {
        throw new BadRequestException(
            "Unable to parse the 'order' parameter", e);
      }
View Full Code Here

    // store the first tree
    byte[] key = new byte[] { 0, 1 };
    storage.addColumn(key, Tree.TREE_FAMILY(), "tree".getBytes(MockBase.ASCII()),
        (byte[])TreetoStorageJson.invoke(TestTree.buildTestTree()));
   
    TreeRule rule = new TreeRule(1);
    rule.setField("host");
    rule.setDescription("Hostname rule");
    rule.setType(TreeRuleType.TAGK);
    rule.setDescription("Host Name");
    storage.addColumn(key, Tree.TREE_FAMILY(),
        "tree_rule:0:0".getBytes(MockBase.ASCII()),
        JSON.serializeToBytes(rule));

    rule = new TreeRule(1);
    rule.setField("");
    rule.setLevel(1);
    rule.setNotes("Metric rule");
    rule.setType(TreeRuleType.METRIC);
    storage.addColumn(key, Tree.TREE_FAMILY(),
        "tree_rule:1:0".getBytes(MockBase.ASCII()),
        JSON.serializeToBytes(rule));
   
    root = new Branch(1);
    root.setDisplayName("ROOT");
    root_path = new TreeMap<Integer, String>();
    root_path.put(0, "ROOT");
    root.prependParentPath(root_path);
    storage.addColumn(key, Tree.TREE_FAMILY(),
        "branch".getBytes(MockBase.ASCII()),
        (byte[])branchToStorageJson.invoke(root));
   
    // tree 2
    key = new byte[] { 0, 2 };

    Tree tree2 = new Tree();
    tree2.setTreeId(2);
    tree2.setName("2nd Tree");
    tree2.setDescription("Other Tree");
    storage.addColumn(key, Tree.TREE_FAMILY(), "tree".getBytes(MockBase.ASCII()),
        (byte[])TreetoStorageJson.invoke(tree2));
   
    rule = new TreeRule(2);
    rule.setField("host");
    rule.setType(TreeRuleType.TAGK);
    storage.addColumn(key, Tree.TREE_FAMILY(),
        "tree_rule:0:0".getBytes(MockBase.ASCII()),
        JSON.serializeToBytes(rule));
   
    rule = new TreeRule(2);
    rule.setField("");
    rule.setLevel(1);
    rule.setType(TreeRuleType.METRIC);
    storage.addColumn(key, Tree.TREE_FAMILY(),
        "tree_rule:1:0".getBytes(MockBase.ASCII()),
        JSON.serializeToBytes(rule));
   
    root = new Branch(2);
View Full Code Here

  }
 
  @Test (expected = IllegalArgumentException.class)
  public void storeRuleInvalidMissingCustomFieldMetricCustom() throws Exception {
    setupStorage();
    final TreeRule rule = new TreeRule(1);
    rule.setLevel(1);
    rule.setOrder(0);
    rule.setType(TreeRuleType.METRIC_CUSTOM);
    rule.setNotes("Just some notes");
    rule.setField("foo");
    rule.syncToStorage(storage.getTSDB(), false).joinUninterruptibly();
  }
View Full Code Here

  }
 
  @Test (expected = IllegalArgumentException.class)
  public void storeRuleInvalidRegexIdx() throws Exception {
    setupStorage();
    final TreeRule rule = new TreeRule(1);
    rule.setLevel(1);
    rule.setOrder(0);
    rule.setType(TreeRuleType.TAGK);
    rule.setRegex("^.*$");
    rule.setRegexGroupIdx(-1);
    rule.syncToStorage(storage.getTSDB(), false).joinUninterruptibly();
  }
View Full Code Here

   * Mocks classes for testing the storage calls
   */
  private void setupStorage() throws Exception {
    storage = new MockBase(true, true, true, true);

    final TreeRule stored_rule = new TreeRule(1);
    stored_rule.setLevel(2);
    stored_rule.setOrder(1);
    stored_rule.setType(TreeRuleType.METRIC_CUSTOM);
    stored_rule.setField("host");
    stored_rule.setCustomField("owner");
    stored_rule.setDescription("Host owner");
    stored_rule.setNotes("Owner of the host machine");
   
    // pretend there's a tree definition in the storage row
    storage.addColumn(new byte[] { 0, 1 }, "tree".getBytes(MockBase.ASCII()),
        new byte[] { 1 });
   
View Full Code Here

  private MockBase storage;
  private TreeRule rule;
 
  @Before
  public void before() {
    rule = new TreeRule();
  }
View Full Code Here

    rule = new TreeRule();
  }
 
  @Test
  public void copyConstructor() {
    rule = new TreeRule(1);
    rule.setCustomField("Custom");   
    rule.setDescription("Hello World!");
    rule.setDisplayFormat("Display");
    rule.setField("Field");
    rule.setLevel(1);
    rule.setNotes("Notes");
    rule.setOrder(2);
    rule.setRegexGroupIdx(4);
    rule.setSeparator("\\.");
   
    final TreeRule copy = new TreeRule(rule);
    assertEquals(1, copy.getTreeId());
    assertEquals("Custom", copy.getCustomField());
    assertEquals("Hello World!", copy.getDescription());
    assertEquals("Display", copy.getDisplayFormat());
    assertEquals("Field", copy.getField());
    assertEquals(1, copy.getLevel());
    assertEquals("Notes", copy.getNotes());
    assertEquals(2, copy.getOrder());
    assertEquals(4, copy.getRegexGroupIdx());
    assertEquals("\\.", copy.getSeparator());
  }
View Full Code Here

  }

  @Test
  public void fetchRule() throws Exception {
    setupStorage();
    final TreeRule rule = TreeRule.fetchRule(storage.getTSDB(), 1, 2, 1)
      .joinUninterruptibly();
    assertNotNull(rule);
    assertEquals(1, rule.getTreeId());
    assertEquals(2, rule.getLevel());
    assertEquals(1, rule.getOrder());
    assertEquals("Host owner", rule.getDescription());
  }
View Full Code Here

  }
 
  @Test
  public void fetchRuleDoesNotExist() throws Exception {
    setupStorage();
    final TreeRule rule = TreeRule.fetchRule(storage.getTSDB(), 1, 2, 2)
      .joinUninterruptibly();
    assertNull(rule);
  }
View Full Code Here

TOP

Related Classes of net.opentsdb.tree.TreeRule

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.