Esempio n. 1
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnLessCellsThanDeclared() {
   Table table = this.getTableWithHeaders();
   table.startRow();
   table.addCell("foo");
   table.endRow(true);
 }
Esempio n. 2
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out);
        out.append(" ");
      }
      out.append("\n");
    }

    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out);
        out.append(" ");
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Esempio n. 3
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", true);
    int[] width = buildWidths(table, request, verbose);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      // print the headers
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(table.getHeaders().get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (List<Table.Cell> row : table.getRows()) {
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(row.get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Esempio n. 4
0
  /**
   * Extracts all the required fields from the RestRequest 'h' parameter. In order to support
   * wildcards like 'bulk.*' this needs potentially parse all the configured headers and its aliases
   * and needs to ensure that everything is only added once to the returned headers, even if
   * 'h=bulk.*.bulk.*' is specified or some headers are contained twice due to matching aliases
   */
  private static Set<String> expandHeadersFromRequest(Table table, RestRequest request) {
    Set<String> headers = new LinkedHashSet<>(table.getHeaders().size());

    // check headers and aliases
    for (String header : Strings.splitStringByCommaToArray(request.param("h"))) {
      if (Regex.isSimpleMatchPattern(header)) {
        for (Table.Cell tableHeaderCell : table.getHeaders()) {
          String configuredHeader = tableHeaderCell.value.toString();
          if (Regex.simpleMatch(header, configuredHeader)) {
            headers.add(configuredHeader);
          } else if (tableHeaderCell.attr.containsKey("alias")) {
            String[] aliases = Strings.splitStringByCommaToArray(tableHeaderCell.attr.get("alias"));
            for (String alias : aliases) {
              if (Regex.simpleMatch(header, alias)) {
                headers.add(configuredHeader);
                break;
              }
            }
          }
        }
      } else {
        headers.add(header);
      }
    }

    return headers;
  }
 @Override
 public void handleRequest(
     final RestRequest request, final RestChannel channel, final NodeClient client)
     throws Exception {
   boolean helpWanted = request.paramAsBoolean("help", false);
   if (helpWanted) {
     Table table = getTableWithHeader(request);
     int[] width = buildHelpWidths(table, request);
     BytesStreamOutput bytesOutput = channel.bytesOutput();
     UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOutput);
     for (Table.Cell cell : table.getHeaders()) {
       // need to do left-align always, so create new cells
       pad(new Table.Cell(cell.value), width[0], request, out);
       out.append(" | ");
       pad(
           new Table.Cell(cell.attr.containsKey("alias") ? cell.attr.get("alias") : ""),
           width[1],
           request,
           out);
       out.append(" | ");
       pad(
           new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"),
           width[2],
           request,
           out);
       out.append("\n");
     }
     out.close();
     channel.sendResponse(
         new BytesRestResponse(
             RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOutput.bytes()));
   } else {
     doRequest(request, channel, client);
   }
 }
Esempio n. 6
0
 @Test
 public void testOnLessCellsThanDeclaredUnchecked() {
   Table table = this.getTableWithHeaders();
   table.startRow();
   table.addCell("foo");
   table.endRow(false);
 }
Esempio n. 7
0
 public Table addColumn(String headerName, String attrs, List<Object> values) {
   Table t = new Table();
   t.startHeaders().addCell(headerName, attrs).endHeaders();
   for (Object val : values) {
     t.startRow().addCell(val).endRow();
   }
   return this.addTable(t);
 }
Esempio n. 8
0
 private Table getTableWithHeaders() {
   Table table = new Table();
   table.startHeaders();
   table.addCell("foo", "alias:f;desc:foo");
   table.addCell("bar", "alias:b;desc:bar");
   table.endHeaders();
   return table;
 }
 @Override
 Table getTableWithHeader(final RestRequest request) {
   Table table = new Table();
   table
       .startHeaders()
       .addCell("id", "desc:node id")
       .addCell("host", "alias:h;desc:host name")
       .addCell("ip", "desc:ip address ")
       .addCell("node", "alias:n;desc:node name")
       .endHeaders();
   return table;
 }
Esempio n. 10
0
  static List<DisplayHeader> buildDisplayHeaders(Table table, RestRequest request) {
    List<DisplayHeader> display = new ArrayList<>();
    if (request.hasParam("h")) {
      Set<String> headers = expandHeadersFromRequest(table, request);

      for (String possibility : headers) {
        DisplayHeader dispHeader = null;

        if (table.getAsMap().containsKey(possibility)) {
          dispHeader = new DisplayHeader(possibility, possibility);
        } else {
          for (Table.Cell headerCell : table.getHeaders()) {
            String aliases = headerCell.attr.get("alias");
            if (aliases != null) {
              for (String alias : Strings.splitStringByCommaToArray(aliases)) {
                if (possibility.equals(alias)) {
                  dispHeader = new DisplayHeader(headerCell.value.toString(), alias);
                  break;
                }
              }
            }
          }
        }

        if (dispHeader != null && checkOutputTimestamp(dispHeader, request)) {
          // We know we need the header asked for:
          display.add(dispHeader);

          // Look for accompanying sibling column
          Table.Cell hcell = table.getHeaderMap().get(dispHeader.name);
          String siblingFlag = hcell.attr.get("sibling");
          if (siblingFlag != null) {
            // ...link the sibling and check that its flag is set
            String sibling = siblingFlag + "." + dispHeader.name;
            Table.Cell c = table.getHeaderMap().get(sibling);
            if (c != null && request.paramAsBoolean(siblingFlag, false)) {
              display.add(
                  new DisplayHeader(c.value.toString(), siblingFlag + "." + dispHeader.display));
            }
          }
        }
      }
    } else {
      for (Table.Cell cell : table.getHeaders()) {
        String d = cell.attr.get("default");
        if (Booleans.parseBoolean(d, true)
            && checkOutputTimestamp(cell.value.toString(), request)) {
          display.add(new DisplayHeader(cell.value.toString(), cell.value.toString()));
        }
      }
    }
    return display;
  }
Esempio n. 11
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", false);
    boolean help = request.paramAsBoolean("h", false);
    if (help) {
      int[] width = buildHelpWidths(table, request, verbose);
      StringBuilder out = new StringBuilder();
      for (Table.Cell cell : table.getHeaders()) {
        // need to do left-align always, so create new cells
        pad(new Table.Cell(cell.value), width[0], request, out);
        out.append(" ");
        pad(
            new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"),
            width[1],
            request,
            out);
        out.append("\n");
      }
      return new StringRestResponse(RestStatus.OK, out.toString());
    }

    int[] width = buildWidths(table, request, verbose);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      // print the headers
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(table.getHeaders().get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (List<Table.Cell> row : table.getRows()) {
      for (int i = 0; i < width.length; i++) {
        String headerName = table.getHeaders().get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          pad(row.get(i), width[i], request, out);
          out.append(" ");
        }
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Esempio n. 12
0
  private static int[] buildWidths(
      Table table, RestRequest request, boolean verbose, List<DisplayHeader> headers) {
    int[] width = new int[headers.size()];
    int i;

    if (verbose) {
      i = 0;
      for (DisplayHeader hdr : headers) {
        int vWidth = hdr.display.length();
        if (width[i] < vWidth) {
          width[i] = vWidth;
        }
        i++;
      }
    }

    i = 0;
    for (DisplayHeader hdr : headers) {
      for (Table.Cell cell : table.getAsMap().get(hdr.name)) {
        String v = renderValue(request, cell.value);
        int vWidth = v == null ? 0 : v.length();
        if (width[i] < vWidth) {
          width[i] = vWidth;
        }
      }
      i++;
    }
    return width;
  }
Esempio n. 13
0
  public static int[] buildHelpWidths(Table table, RestRequest request, boolean verbose) {
    int[] width = new int[3];
    for (Table.Cell cell : table.getHeaders()) {
      String v = renderValue(request, cell.value);
      int vWidth = v == null ? 0 : v.length();
      if (width[0] < vWidth) {
        width[0] = vWidth;
      }

      v = renderValue(request, cell.attr.containsKey("alias") ? cell.attr.get("alias") : "");
      vWidth = v == null ? 0 : v.length();
      if (width[1] < vWidth) {
        width[1] = vWidth;
      }

      v =
          renderValue(
              request, cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available");
      vWidth = v == null ? 0 : v.length();
      if (width[2] < vWidth) {
        width[2] = vWidth;
      }
    }
    return width;
  }
Esempio n. 14
0
  public static RestResponse buildXContentBuilder(
      Table table, RestRequest request, RestChannel channel) throws Exception {
    XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
    List<DisplayHeader> displayHeaders = buildDisplayHeaders(table, request);

    builder.startArray();
    for (int row = 0; row < table.getRows().size(); row++) {
      builder.startObject();
      for (DisplayHeader header : displayHeaders) {
        builder.field(
            header.display, renderValue(request, table.getAsMap().get(header.name).get(row).value));
      }
      builder.endObject();
    }
    builder.endArray();
    return new XContentRestResponse(request, RestStatus.OK, builder);
  }
Esempio n. 15
0
  public static RestResponse buildTextPlainResponse(Table table, RestChannel channel)
      throws IOException {
    RestRequest request = channel.request();
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    BytesStreamOutput bytesOut = channel.bytesOutput();
    UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
    int lastHeader = headers.size() - 1;
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out,
            isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    out.close();
    return new BytesRestResponse(
        RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
  }
Esempio n. 16
0
  public static RestResponse buildXContentBuilder(
      Table table, RestRequest request, RestChannel channel) throws Exception {
    XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
    Set<String> displayHeaders = buildDisplayHeaders(table, request);

    List<Table.Cell> headers = table.getHeaders();
    builder.startArray();
    for (List<Table.Cell> row : table.getRows()) {
      builder.startObject();
      for (int i = 0; i < headers.size(); i++) {
        String headerName = headers.get(i).value.toString();
        if (displayHeaders.contains(headerName)) {
          builder.field(headerName, renderValue(request, row.get(i).value));
        }
      }
      builder.endObject();
    }
    builder.endArray();
    return new XContentRestResponse(request, RestStatus.OK, builder);
  }
Esempio n. 17
0
  private static int[] buildWidths(Table table, RestRequest request, boolean verbose) {
    int[] width = new int[table.getHeaders().size()];

    if (verbose) {
      for (int col = 0; col < width.length; col++) {
        String v = renderValue(request, table.getHeaders().get(col).value);
        int vWidth = v == null ? 0 : v.length();
        if (width[col] < vWidth) {
          width[col] = vWidth;
        }
      }
    }

    for (List<Table.Cell> row : table.getRows()) {
      for (int col = 0; col < width.length; col++) {
        String v = renderValue(request, row.get(col).value);
        int vWidth = v == null ? 0 : v.length();
        if (width[col] < vWidth) {
          width[col] = vWidth;
        }
      }
    }
    return width;
  }
  private Table buildTable(RestRequest request, ClusterStateResponse state) {
    Table table = getTableWithHeader(request);
    DiscoveryNodes nodes = state.getState().nodes();

    table.startRow();
    DiscoveryNode master = nodes.get(nodes.masterNodeId());
    if (master == null) {
      table.addCell("-");
      table.addCell("-");
      table.addCell("-");
      table.addCell("-");
    } else {
      table.addCell(master.getId());
      table.addCell(master.getHostName());
      table.addCell(master.getHostAddress());
      table.addCell(master.getName());
    }
    table.endRow();

    return table;
  }
Esempio n. 19
0
 private static Set<String> buildDisplayHeaders(Table table, RestRequest request) {
   String pHeaders = request.param("headers");
   Set<String> display;
   if (pHeaders != null) {
     display = Strings.commaDelimitedListToSet(pHeaders);
   } else {
     display = new HashSet<String>();
     for (Table.Cell cell : table.getHeaders()) {
       String d = cell.attr.get("default");
       if (Booleans.parseBoolean(d, true)) {
         display.add(cell.value.toString());
       }
     }
   }
   return display;
 }
Esempio n. 20
0
  @Test
  public void testSimple() {
    Table table = this.getTableWithHeaders();
    table.startRow();
    table.addCell("foo1");
    table.addCell("bar1");
    table.endRow();
    table.startRow();
    table.addCell("foo2");
    table.addCell("bar2");
    table.endRow();

    // Check headers
    List<Table.Cell> headers = table.getHeaders();
    assertEquals(2, headers.size());
    assertEquals("foo", headers.get(0).value.toString());
    assertEquals(2, headers.get(0).attr.size());
    assertEquals("f", headers.get(0).attr.get("alias"));
    assertEquals("foo", headers.get(0).attr.get("desc"));
    assertEquals("bar", headers.get(1).value.toString());
    assertEquals(2, headers.get(1).attr.size());
    assertEquals("b", headers.get(1).attr.get("alias"));
    assertEquals("bar", headers.get(1).attr.get("desc"));

    // Check rows
    List<List<Table.Cell>> rows = table.getRows();
    assertEquals(2, rows.size());
    List<Table.Cell> row = rows.get(0);
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("bar1", row.get(1).value.toString());
    row = rows.get(1);
    assertEquals("foo2", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getAsMap
    Map<String, List<Table.Cell>> map = table.getAsMap();
    assertEquals(2, map.size());
    row = map.get("foo");
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("foo2", row.get(1).value.toString());
    row = map.get("bar");
    assertEquals("bar1", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getHeaderMap
    Map<String, Table.Cell> headerMap = table.getHeaderMap();
    assertEquals(2, headerMap.size());
    Table.Cell cell = headerMap.get("foo");
    assertEquals("foo", cell.value.toString());
    cell = headerMap.get("bar");
    assertEquals("bar", cell.value.toString());

    // Check findHeaderByName
    cell = table.findHeaderByName("foo");
    assertEquals("foo", cell.value.toString());
    cell = table.findHeaderByName("missing");
    assertNull(cell);
  }
Esempio n. 21
0
  public Table addTable(Table t2) {
    Table t1 = this;
    Table t = new Table();

    t.startHeaders();

    for (Cell c : t1.getHeaders()) {
      t.addCell(c);
    }

    for (Cell c : t2.getHeaders()) {
      t.addCell(c);
    }

    t.endHeaders();

    if (t1.rows.size() != t2.rows.size()) {
      StringBuilder sb = new StringBuilder();
      sb.append("cannot add a table with ");
      sb.append(t2.rows.size());
      sb.append(" rows to table with ");
      sb.append(t1.rows.size());
      sb.append(" rows");
      throw new ElasticSearchIllegalArgumentException(sb.toString());
    }

    for (int i = 0; i < t1.rows.size(); i++) {
      t.startRow();
      for (Cell c : t1.rows.get(i)) {
        t.addCell(c);
      }
      for (Cell c : t2.rows.get(i)) {
        t.addCell(c);
      }
      t.endRow(false);
    }

    return t;
  }
Esempio n. 22
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnAddCellWithoutRow() {
   Table table = this.getTableWithHeaders();
   table.addCell("error");
 }
  private Table buildTable(
      RestRequest req,
      ClusterStateResponse state,
      NodesInfoResponse nodesInfo,
      NodesStatsResponse nodesStats) {
    boolean fullId = req.paramAsBoolean("full_id", false);

    DiscoveryNodes nodes = state.getState().nodes();
    String masterId = nodes.masterNodeId();
    Table table = getTableWithHeader(req);

    for (DiscoveryNode node : nodes) {
      NodeInfo info = nodesInfo.getNodesMap().get(node.id());
      NodeStats stats = nodesStats.getNodesMap().get(node.id());

      JvmInfo jvmInfo = info == null ? null : info.getJvm();
      JvmStats jvmStats = stats == null ? null : stats.getJvm();
      FsInfo fsInfo = stats == null ? null : stats.getFs();
      OsStats osStats = stats == null ? null : stats.getOs();
      ProcessStats processStats = stats == null ? null : stats.getProcess();
      NodeIndicesStats indicesStats = stats == null ? null : stats.getIndices();

      table.startRow();

      table.addCell(fullId ? node.id() : Strings.substring(node.getId(), 0, 4));
      table.addCell(info == null ? null : info.getProcess().getId());
      table.addCell(node.getHostName());
      table.addCell(node.getHostAddress());
      if (node.address() instanceof InetSocketTransportAddress) {
        table.addCell(((InetSocketTransportAddress) node.address()).address().getPort());
      } else {
        table.addCell("-");
      }

      table.addCell(node.getVersion().number());
      table.addCell(info == null ? null : info.getBuild().shortHash());
      table.addCell(jvmInfo == null ? null : jvmInfo.version());
      table.addCell(fsInfo == null ? null : fsInfo.getTotal().getAvailable());
      table.addCell(jvmStats == null ? null : jvmStats.getMem().getHeapUsed());
      table.addCell(jvmStats == null ? null : jvmStats.getMem().getHeapUsedPercent());
      table.addCell(jvmInfo == null ? null : jvmInfo.getMem().getHeapMax());
      table.addCell(
          osStats == null ? null : osStats.getMem() == null ? null : osStats.getMem().getUsed());
      table.addCell(
          osStats == null
              ? null
              : osStats.getMem() == null ? null : osStats.getMem().getUsedPercent());
      table.addCell(
          osStats == null ? null : osStats.getMem() == null ? null : osStats.getMem().getTotal());
      table.addCell(processStats == null ? null : processStats.getOpenFileDescriptors());
      table.addCell(
          processStats == null
              ? null
              : calculatePercentage(
                  processStats.getOpenFileDescriptors(), processStats.getMaxFileDescriptors()));
      table.addCell(processStats == null ? null : processStats.getMaxFileDescriptors());

      table.addCell(
          osStats == null ? null : String.format(Locale.ROOT, "%.2f", osStats.getLoadAverage()));
      table.addCell(jvmStats == null ? null : jvmStats.getUptime());
      table.addCell(node.clientNode() ? "c" : node.dataNode() ? "d" : "-");
      table.addCell(
          masterId == null
              ? "x"
              : masterId.equals(node.id()) ? "*" : node.masterNode() ? "m" : "-");
      table.addCell(node.name());

      CompletionStats completionStats =
          indicesStats == null ? null : stats.getIndices().getCompletion();
      table.addCell(completionStats == null ? null : completionStats.getSize());

      FieldDataStats fdStats = indicesStats == null ? null : stats.getIndices().getFieldData();
      table.addCell(fdStats == null ? null : fdStats.getMemorySize());
      table.addCell(fdStats == null ? null : fdStats.getEvictions());

      QueryCacheStats fcStats = indicesStats == null ? null : indicesStats.getQueryCache();
      table.addCell(fcStats == null ? null : fcStats.getMemorySize());
      table.addCell(fcStats == null ? null : fcStats.getEvictions());

      RequestCacheStats qcStats = indicesStats == null ? null : indicesStats.getRequestCache();
      table.addCell(qcStats == null ? null : qcStats.getMemorySize());
      table.addCell(qcStats == null ? null : qcStats.getEvictions());
      table.addCell(qcStats == null ? null : qcStats.getHitCount());
      table.addCell(qcStats == null ? null : qcStats.getMissCount());

      FlushStats flushStats = indicesStats == null ? null : indicesStats.getFlush();
      table.addCell(flushStats == null ? null : flushStats.getTotal());
      table.addCell(flushStats == null ? null : flushStats.getTotalTime());

      GetStats getStats = indicesStats == null ? null : indicesStats.getGet();
      table.addCell(getStats == null ? null : getStats.current());
      table.addCell(getStats == null ? null : getStats.getTime());
      table.addCell(getStats == null ? null : getStats.getCount());
      table.addCell(getStats == null ? null : getStats.getExistsTime());
      table.addCell(getStats == null ? null : getStats.getExistsCount());
      table.addCell(getStats == null ? null : getStats.getMissingTime());
      table.addCell(getStats == null ? null : getStats.getMissingCount());

      IndexingStats indexingStats = indicesStats == null ? null : indicesStats.getIndexing();
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getDeleteCurrent());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getDeleteTime());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getDeleteCount());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getIndexCurrent());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getIndexTime());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getIndexCount());
      table.addCell(indexingStats == null ? null : indexingStats.getTotal().getIndexFailedCount());

      MergeStats mergeStats = indicesStats == null ? null : indicesStats.getMerge();
      table.addCell(mergeStats == null ? null : mergeStats.getCurrent());
      table.addCell(mergeStats == null ? null : mergeStats.getCurrentNumDocs());
      table.addCell(mergeStats == null ? null : mergeStats.getCurrentSize());
      table.addCell(mergeStats == null ? null : mergeStats.getTotal());
      table.addCell(mergeStats == null ? null : mergeStats.getTotalNumDocs());
      table.addCell(mergeStats == null ? null : mergeStats.getTotalSize());
      table.addCell(mergeStats == null ? null : mergeStats.getTotalTime());

      PercolateStats percolateStats = indicesStats == null ? null : indicesStats.getPercolate();
      table.addCell(percolateStats == null ? null : percolateStats.getCurrent());
      table.addCell(percolateStats == null ? null : percolateStats.getMemorySize());
      table.addCell(percolateStats == null ? null : percolateStats.getNumQueries());
      table.addCell(percolateStats == null ? null : percolateStats.getTime());
      table.addCell(percolateStats == null ? null : percolateStats.getCount());

      RefreshStats refreshStats = indicesStats == null ? null : indicesStats.getRefresh();
      table.addCell(refreshStats == null ? null : refreshStats.getTotal());
      table.addCell(refreshStats == null ? null : refreshStats.getTotalTime());

      ScriptStats scriptStats = stats == null ? null : stats.getScriptStats();
      table.addCell(scriptStats == null ? null : scriptStats.getCompilations());
      table.addCell(scriptStats == null ? null : scriptStats.getCacheEvictions());

      SearchStats searchStats = indicesStats == null ? null : indicesStats.getSearch();
      table.addCell(searchStats == null ? null : searchStats.getTotal().getFetchCurrent());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getFetchTime());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getFetchCount());
      table.addCell(searchStats == null ? null : searchStats.getOpenContexts());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getQueryCurrent());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getQueryTime());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getQueryCount());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getScrollCurrent());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getScrollTime());
      table.addCell(searchStats == null ? null : searchStats.getTotal().getScrollCount());

      SegmentsStats segmentsStats = indicesStats == null ? null : indicesStats.getSegments();
      table.addCell(segmentsStats == null ? null : segmentsStats.getCount());
      table.addCell(segmentsStats == null ? null : segmentsStats.getMemory());
      table.addCell(segmentsStats == null ? null : segmentsStats.getIndexWriterMemory());
      table.addCell(segmentsStats == null ? null : segmentsStats.getIndexWriterMaxMemory());
      table.addCell(segmentsStats == null ? null : segmentsStats.getVersionMapMemory());
      table.addCell(segmentsStats == null ? null : segmentsStats.getBitsetMemory());

      SuggestStats suggestStats = indicesStats == null ? null : indicesStats.getSuggest();
      table.addCell(suggestStats == null ? null : suggestStats.getCurrent());
      table.addCell(suggestStats == null ? null : suggestStats.getTime());
      table.addCell(suggestStats == null ? null : suggestStats.getCount());

      table.endRow();
    }

    return table;
  }
  private Table buildTable(
      RestRequest req,
      ClusterStateResponse state,
      NodesInfoResponse nodesInfo,
      NodesStatsResponse nodesStats) {
    final String[] threadPools = req.paramAsStringArray("thread_pool_patterns", new String[] {"*"});
    final DiscoveryNodes nodes = state.getState().nodes();
    final Table table = getTableWithHeader(req);

    // collect all thread pool names that we see across the nodes
    final Set<String> candidates = new HashSet<>();
    for (final NodeStats nodeStats : nodesStats.getNodes()) {
      for (final ThreadPoolStats.Stats threadPoolStats : nodeStats.getThreadPool()) {
        candidates.add(threadPoolStats.getName());
      }
    }

    // collect all thread pool names that match the specified thread pool patterns
    final Set<String> included = new HashSet<>();
    for (final String candidate : candidates) {
      if (Regex.simpleMatch(threadPools, candidate)) {
        included.add(candidate);
      }
    }

    for (final DiscoveryNode node : nodes) {
      final NodeInfo info = nodesInfo.getNodesMap().get(node.getId());
      final NodeStats stats = nodesStats.getNodesMap().get(node.getId());

      final Map<String, ThreadPoolStats.Stats> poolThreadStats;
      final Map<String, ThreadPool.Info> poolThreadInfo;

      if (stats == null) {
        poolThreadStats = Collections.emptyMap();
        poolThreadInfo = Collections.emptyMap();
      } else {
        // we use a sorted map to ensure that thread pools are sorted by name
        poolThreadStats = new TreeMap<>();
        poolThreadInfo = new HashMap<>();

        ThreadPoolStats threadPoolStats = stats.getThreadPool();
        for (ThreadPoolStats.Stats threadPoolStat : threadPoolStats) {
          poolThreadStats.put(threadPoolStat.getName(), threadPoolStat);
        }
        if (info != null) {
          for (ThreadPool.Info threadPoolInfo : info.getThreadPool()) {
            poolThreadInfo.put(threadPoolInfo.getName(), threadPoolInfo);
          }
        }
      }
      for (Map.Entry<String, ThreadPoolStats.Stats> entry : poolThreadStats.entrySet()) {

        if (!included.contains(entry.getKey())) continue;

        table.startRow();

        table.addCell(node.getName());
        table.addCell(node.getId());
        table.addCell(node.getEphemeralId());
        table.addCell(info == null ? null : info.getProcess().getId());
        table.addCell(node.getHostName());
        table.addCell(node.getHostAddress());
        table.addCell(node.getAddress().address().getPort());
        final ThreadPoolStats.Stats poolStats = entry.getValue();
        final ThreadPool.Info poolInfo = poolThreadInfo.get(entry.getKey());

        Long maxQueueSize = null;
        String keepAlive = null;
        Integer minThreads = null;
        Integer maxThreads = null;

        if (poolInfo != null) {
          if (poolInfo.getQueueSize() != null) {
            maxQueueSize = poolInfo.getQueueSize().singles();
          }
          if (poolInfo.getKeepAlive() != null) {
            keepAlive = poolInfo.getKeepAlive().toString();
          }
          if (poolInfo.getMin() >= 0) {
            minThreads = poolInfo.getMin();
          }
          if (poolInfo.getMax() >= 0) {
            maxThreads = poolInfo.getMax();
          }
        }

        table.addCell(entry.getKey());
        table.addCell(poolInfo == null ? null : poolInfo.getThreadPoolType().getType());
        table.addCell(poolStats == null ? null : poolStats.getActive());
        table.addCell(poolStats == null ? null : poolStats.getThreads());
        table.addCell(poolStats == null ? null : poolStats.getQueue());
        table.addCell(maxQueueSize);
        table.addCell(poolStats == null ? null : poolStats.getRejected());
        table.addCell(poolStats == null ? null : poolStats.getLargest());
        table.addCell(poolStats == null ? null : poolStats.getCompleted());
        table.addCell(minThreads);
        table.addCell(maxThreads);
        table.addCell(keepAlive);

        table.endRow();
      }
    }

    return table;
  }
  @Override
  protected Table getTableWithHeader(final RestRequest request) {
    Table table = new Table();
    table.startHeaders();
    table.addCell("id", "default:false;alias:id,nodeId;desc:unique node id");
    table.addCell("pid", "default:false;alias:p;desc:process id");
    table.addCell("host", "alias:h;desc:host name");
    table.addCell("ip", "alias:i;desc:ip address");
    table.addCell("port", "default:false;alias:po;desc:bound transport port");

    table.addCell("version", "default:false;alias:v;desc:es version");
    table.addCell("build", "default:false;alias:b;desc:es build hash");
    table.addCell("jdk", "default:false;alias:j;desc:jdk version");
    table.addCell(
        "disk.avail",
        "default:false;alias:d,disk,diskAvail;text-align:right;desc:available disk space");
    table.addCell(
        "heap.current", "default:false;alias:hc,heapCurrent;text-align:right;desc:used heap");
    table.addCell("heap.percent", "alias:hp,heapPercent;text-align:right;desc:used heap ratio");
    table.addCell(
        "heap.max", "default:false;alias:hm,heapMax;text-align:right;desc:max configured heap");
    table.addCell(
        "ram.current",
        "default:false;alias:rc,ramCurrent;text-align:right;desc:used machine memory");
    table.addCell(
        "ram.percent", "alias:rp,ramPercent;text-align:right;desc:used machine memory ratio");
    table.addCell(
        "ram.max", "default:false;alias:rm,ramMax;text-align:right;desc:total machine memory");
    table.addCell(
        "file_desc.current",
        "default:false;alias:fdc,fileDescriptorCurrent;text-align:right;desc:used file descriptors");
    table.addCell(
        "file_desc.percent",
        "default:false;alias:fdp,fileDescriptorPercent;text-align:right;desc:used file descriptor ratio");
    table.addCell(
        "file_desc.max",
        "default:false;alias:fdm,fileDescriptorMax;text-align:right;desc:max file descriptors");

    table.addCell("load", "alias:l;text-align:right;desc:most recent load avg");
    table.addCell("uptime", "default:false;alias:u;text-align:right;desc:node uptime");
    table.addCell("node.role", "alias:r,role,dc,nodeRole;desc:d:data node, c:client node");
    table.addCell("master", "alias:m;desc:m:master-eligible, *:current master");
    table.addCell("name", "alias:n;desc:node name");

    table.addCell(
        "completion.size",
        "alias:cs,completionSize;default:false;text-align:right;desc:size of completion");

    table.addCell(
        "fielddata.memory_size",
        "alias:fm,fielddataMemory;default:false;text-align:right;desc:used fielddata cache");
    table.addCell(
        "fielddata.evictions",
        "alias:fe,fielddataEvictions;default:false;text-align:right;desc:fielddata evictions");

    table.addCell(
        "query_cache.memory_size",
        "alias:fcm,queryCacheMemory;default:false;text-align:right;desc:used query cache");
    table.addCell(
        "query_cache.evictions",
        "alias:fce,queryCacheEvictions;default:false;text-align:right;desc:query cache evictions");

    table.addCell(
        "request_cache.memory_size",
        "alias:qcm,requestCacheMemory;default:false;text-align:right;desc:used request cache");
    table.addCell(
        "request_cache.evictions",
        "alias:qce,requestCacheEvictions;default:false;text-align:right;desc:request cache evictions");
    table.addCell(
        "request_cache.hit_count",
        "alias:qchc,requestCacheHitCount;default:false;text-align:right;desc:request cache hit counts");
    table.addCell(
        "request_cache.miss_count",
        "alias:qcmc,requestCacheMissCount;default:false;text-align:right;desc:request cache miss counts");

    table.addCell(
        "flush.total", "alias:ft,flushTotal;default:false;text-align:right;desc:number of flushes");
    table.addCell(
        "flush.total_time",
        "alias:ftt,flushTotalTime;default:false;text-align:right;desc:time spent in flush");

    table.addCell(
        "get.current",
        "alias:gc,getCurrent;default:false;text-align:right;desc:number of current get ops");
    table.addCell(
        "get.time", "alias:gti,getTime;default:false;text-align:right;desc:time spent in get");
    table.addCell(
        "get.total", "alias:gto,getTotal;default:false;text-align:right;desc:number of get ops");
    table.addCell(
        "get.exists_time",
        "alias:geti,getExistsTime;default:false;text-align:right;desc:time spent in successful gets");
    table.addCell(
        "get.exists_total",
        "alias:geto,getExistsTotal;default:false;text-align:right;desc:number of successful gets");
    table.addCell(
        "get.missing_time",
        "alias:gmti,getMissingTime;default:false;text-align:right;desc:time spent in failed gets");
    table.addCell(
        "get.missing_total",
        "alias:gmto,getMissingTotal;default:false;text-align:right;desc:number of failed gets");

    table.addCell(
        "indexing.delete_current",
        "alias:idc,indexingDeleteCurrent;default:false;text-align:right;desc:number of current deletions");
    table.addCell(
        "indexing.delete_time",
        "alias:idti,indexingDeleteTime;default:false;text-align:right;desc:time spent in deletions");
    table.addCell(
        "indexing.delete_total",
        "alias:idto,indexingDeleteTotal;default:false;text-align:right;desc:number of delete ops");
    table.addCell(
        "indexing.index_current",
        "alias:iic,indexingIndexCurrent;default:false;text-align:right;desc:number of current indexing ops");
    table.addCell(
        "indexing.index_time",
        "alias:iiti,indexingIndexTime;default:false;text-align:right;desc:time spent in indexing");
    table.addCell(
        "indexing.index_total",
        "alias:iito,indexingIndexTotal;default:false;text-align:right;desc:number of indexing ops");
    table.addCell(
        "indexing.index_failed",
        "alias:iif,indexingIndexFailed;default:false;text-align:right;desc:number of failed indexing ops");

    table.addCell(
        "merges.current",
        "alias:mc,mergesCurrent;default:false;text-align:right;desc:number of current merges");
    table.addCell(
        "merges.current_docs",
        "alias:mcd,mergesCurrentDocs;default:false;text-align:right;desc:number of current merging docs");
    table.addCell(
        "merges.current_size",
        "alias:mcs,mergesCurrentSize;default:false;text-align:right;desc:size of current merges");
    table.addCell(
        "merges.total",
        "alias:mt,mergesTotal;default:false;text-align:right;desc:number of completed merge ops");
    table.addCell(
        "merges.total_docs",
        "alias:mtd,mergesTotalDocs;default:false;text-align:right;desc:docs merged");
    table.addCell(
        "merges.total_size",
        "alias:mts,mergesTotalSize;default:false;text-align:right;desc:size merged");
    table.addCell(
        "merges.total_time",
        "alias:mtt,mergesTotalTime;default:false;text-align:right;desc:time spent in merges");

    table.addCell(
        "percolate.current",
        "alias:pc,percolateCurrent;default:false;text-align:right;desc:number of current percolations");
    table.addCell(
        "percolate.memory_size",
        "alias:pm,percolateMemory;default:false;text-align:right;desc:memory used by percolations");
    table.addCell(
        "percolate.queries",
        "alias:pq,percolateQueries;default:false;text-align:right;desc:number of registered percolation queries");
    table.addCell(
        "percolate.time",
        "alias:pti,percolateTime;default:false;text-align:right;desc:time spent percolating");
    table.addCell(
        "percolate.total",
        "alias:pto,percolateTotal;default:false;text-align:right;desc:total percolations");

    table.addCell(
        "refresh.total",
        "alias:rto,refreshTotal;default:false;text-align:right;desc:total refreshes");
    table.addCell(
        "refresh.time",
        "alias:rti,refreshTime;default:false;text-align:right;desc:time spent in refreshes");

    table.addCell(
        "script.compilations",
        "alias:scrcc,scriptCompilations;default:false;text-align:right;desc:script compilations");
    table.addCell(
        "script.cache_evictions",
        "alias:scrce,scriptCacheEvictions;default:false;text-align:right;desc:script cache evictions");

    table.addCell(
        "search.fetch_current",
        "alias:sfc,searchFetchCurrent;default:false;text-align:right;desc:current fetch phase ops");
    table.addCell(
        "search.fetch_time",
        "alias:sfti,searchFetchTime;default:false;text-align:right;desc:time spent in fetch phase");
    table.addCell(
        "search.fetch_total",
        "alias:sfto,searchFetchTotal;default:false;text-align:right;desc:total fetch ops");
    table.addCell(
        "search.open_contexts",
        "alias:so,searchOpenContexts;default:false;text-align:right;desc:open search contexts");
    table.addCell(
        "search.query_current",
        "alias:sqc,searchQueryCurrent;default:false;text-align:right;desc:current query phase ops");
    table.addCell(
        "search.query_time",
        "alias:sqti,searchQueryTime;default:false;text-align:right;desc:time spent in query phase");
    table.addCell(
        "search.query_total",
        "alias:sqto,searchQueryTotal;default:false;text-align:right;desc:total query phase ops");
    table.addCell(
        "search.scroll_current",
        "alias:scc,searchScrollCurrent;default:false;text-align:right;desc:open scroll contexts");
    table.addCell(
        "search.scroll_time",
        "alias:scti,searchScrollTime;default:false;text-align:right;desc:time scroll contexts held open");
    table.addCell(
        "search.scroll_total",
        "alias:scto,searchScrollTotal;default:false;text-align:right;desc:completed scroll contexts");

    table.addCell(
        "segments.count",
        "alias:sc,segmentsCount;default:false;text-align:right;desc:number of segments");
    table.addCell(
        "segments.memory",
        "alias:sm,segmentsMemory;default:false;text-align:right;desc:memory used by segments");
    table.addCell(
        "segments.index_writer_memory",
        "alias:siwm,segmentsIndexWriterMemory;default:false;text-align:right;desc:memory used by index writer");
    table.addCell(
        "segments.index_writer_max_memory",
        "alias:siwmx,segmentsIndexWriterMaxMemory;default:false;text-align:right;desc:maximum memory index writer may use before it must write buffered documents to a new segment");
    table.addCell(
        "segments.version_map_memory",
        "alias:svmm,segmentsVersionMapMemory;default:false;text-align:right;desc:memory used by version map");
    table.addCell(
        "segments.fixed_bitset_memory",
        "alias:sfbm,fixedBitsetMemory;default:false;text-align:right;desc:memory used by fixed bit sets for nested object field types and type filters for types referred in _parent fields");

    table.addCell(
        "suggest.current",
        "alias:suc,suggestCurrent;default:false;text-align:right;desc:number of current suggest ops");
    table.addCell(
        "suggest.time",
        "alias:suti,suggestTime;default:false;text-align:right;desc:time spend in suggest");
    table.addCell(
        "suggest.total",
        "alias:suto,suggestTotal;default:false;text-align:right;desc:number of suggest ops");

    table.endHeaders();
    return table;
  }
 @Override
 protected Table getTableWithHeader(final RestRequest request) {
   final Table table = new Table();
   table.startHeaders();
   table.addCell("node_name", "default:true;alias:nn;desc:node name");
   table.addCell("node_id", "default:false;alias:id;desc:persistent node id");
   table.addCell("ephemeral_node_id", "default:false;alias:eid;desc:ephemeral node id");
   table.addCell("pid", "default:false;alias:p;desc:process id");
   table.addCell("host", "default:false;alias:h;desc:host name");
   table.addCell("ip", "default:false;alias:i;desc:ip address");
   table.addCell("port", "default:false;alias:po;desc:bound transport port");
   table.addCell("name", "default:true;alias:n;desc:thread pool name");
   table.addCell("type", "alias:t;default:false;desc:thread pool type");
   table.addCell("active", "alias:a;default:true;text-align:right;desc:number of active threads");
   table.addCell("size", "alias:s;default:false;text-align:right;desc:number of threads");
   table.addCell(
       "queue", "alias:q;default:true;text-align:right;desc:number of tasks currently in queue");
   table.addCell(
       "queue_size",
       "alias:qs;default:false;text-align:right;desc:maximum number of tasks permitted in queue");
   table.addCell(
       "rejected", "alias:r;default:true;text-align:right;desc:number of rejected tasks");
   table.addCell(
       "largest",
       "alias:l;default:false;text-align:right;desc:highest number of seen active threads");
   table.addCell(
       "completed", "alias:c;default:false;text-align:right;desc:number of completed tasks");
   table.addCell("min", "alias:mi;default:false;text-align:right;desc:minimum number of threads");
   table.addCell("max", "alias:ma;default:false;text-align:right;desc:maximum number of threads");
   table.addCell(
       "keep_alive", "alias:ka;default:false;text-align:right;desc:thread keep alive time");
   table.endHeaders();
   return table;
 }
Esempio n. 27
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnAddCellWithoutHeader() {
   Table table = new Table();
   table.addCell("error");
 }
Esempio n. 28
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnEndRowWithoutStart() {
   Table table = this.getTableWithHeaders();
   table.endRow();
 }
Esempio n. 29
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnStartRowWithoutHeader() {
   Table table = new Table();
   table.startRow();
 }
Esempio n. 30
0
 @Test(expected = IllegalStateException.class)
 public void testFailOnEndHeadersWithoutStart() {
   Table table = new Table();
   table.endHeaders();
 }