From 048d2734084aac785bd867fc653b6fc8d38c9ab4 Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Fri, 11 Sep 2015 17:35:29 +0900 Subject: [PATCH] Cat: cat health supports ts=0 option If ts=0, cat health disable epoch and timestamp Be Constant String timestamp and epoch Move timestamp and epoch to Table Add rest-api test and test Closes #10109 --- .../java/org/elasticsearch/common/Table.java | 23 ++++++++++++++++ .../rest/action/cat/RestCountAction.java | 13 +-------- .../rest/action/cat/RestHealthAction.java | 12 +-------- .../rest/action/support/RestTable.java | 18 +++++++++++-- .../org/elasticsearch/common/TableTests.java | 27 +++++++++++++++++++ .../rest/action/support/RestTableTests.java | 24 ++++++++++++++--- .../test/cat.health/10_basic.yaml | 27 +++++++++++++++++++ 7 files changed, 116 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/Table.java b/core/src/main/java/org/elasticsearch/common/Table.java index 6156cc2e79f..0d4a827202d 100644 --- a/core/src/main/java/org/elasticsearch/common/Table.java +++ b/core/src/main/java/org/elasticsearch/common/Table.java @@ -19,10 +19,14 @@ package org.elasticsearch.common; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import static java.util.Collections.emptyMap; @@ -36,6 +40,9 @@ public class Table { private Map headerMap = new HashMap<>(); private List currentCells; private boolean inHeaders = false; + private boolean withTime = false; + public static final String EPOCH = "epoch"; + public static final String TIMESTAMP = "timestamp"; public Table startHeaders() { inHeaders = true; @@ -43,6 +50,15 @@ public class Table { return this; } + public Table startHeadersWithTimestamp() { + startHeaders(); + this.withTime = true; + addCell("epoch", "alias:t,time;desc:seconds since 1970-01-01 00:00:00"); + addCell("timestamp", "alias:ts,hms,hhmmss;desc:time in HH:MM:SS"); + return this; + } + + public Table endHeaders() { if (currentCells == null || currentCells.isEmpty()) { throw new IllegalStateException("no headers added..."); @@ -69,11 +85,18 @@ public class Table { return this; } + private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("HH:mm:ss"); + public Table startRow() { if (headers.isEmpty()) { throw new IllegalStateException("no headers added..."); } currentCells = new ArrayList<>(headers.size()); + if (withTime) { + long time = System.currentTimeMillis(); + addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); + addCell(dateFormat.print(time)); + } return this; } diff --git a/core/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java b/core/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java index e4d291b5fc8..6fd64430c20 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/cat/RestCountAction.java @@ -37,10 +37,6 @@ import org.elasticsearch.rest.action.support.RestActions; import org.elasticsearch.rest.action.support.RestResponseListener; import org.elasticsearch.rest.action.support.RestTable; import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; - -import java.util.concurrent.TimeUnit; import static org.elasticsearch.rest.RestRequest.Method.GET; @@ -88,22 +84,15 @@ public class RestCountAction extends AbstractCatAction { @Override protected Table getTableWithHeader(final RestRequest request) { Table table = new Table(); - table.startHeaders(); - table.addCell("epoch", "alias:t,time;desc:seconds since 1970-01-01 00:00:00, that the count was executed"); - table.addCell("timestamp", "alias:ts,hms;desc:time that the count was executed"); + table.startHeadersWithTimestamp(); table.addCell("count", "alias:dc,docs.count,docsCount;desc:the document count"); table.endHeaders(); return table; } - private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("HH:mm:ss"); - private Table buildTable(RestRequest request, SearchResponse response) { Table table = getTableWithHeader(request); - long time = System.currentTimeMillis(); table.startRow(); - table.addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); - table.addCell(dateFormat.print(time)); table.addCell(response.getHits().totalHits()); table.endRow(); diff --git a/core/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java b/core/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java index bc5920b7db9..7dc7954a3a4 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/cat/RestHealthAction.java @@ -31,11 +31,8 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.support.RestResponseListener; import org.elasticsearch.rest.action.support.RestTable; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import java.util.Locale; -import java.util.concurrent.TimeUnit; import static org.elasticsearch.rest.RestRequest.Method.GET; @@ -67,9 +64,7 @@ public class RestHealthAction extends AbstractCatAction { @Override protected Table getTableWithHeader(final RestRequest request) { Table t = new Table(); - t.startHeaders(); - t.addCell("epoch", "alias:t,time;desc:seconds since 1970-01-01 00:00:00"); - t.addCell("timestamp", "alias:ts,hms,hhmmss;desc:time in HH:MM:SS"); + t.startHeadersWithTimestamp(); t.addCell("cluster", "alias:cl;desc:cluster name"); t.addCell("status", "alias:st;desc:health status"); t.addCell("node.total", "alias:nt,nodeTotal;text-align:right;desc:total number of nodes"); @@ -87,14 +82,9 @@ public class RestHealthAction extends AbstractCatAction { return t; } - private DateTimeFormatter dateFormat = DateTimeFormat.forPattern("HH:mm:ss"); - private Table buildTable(final ClusterHealthResponse health, final RestRequest request) { - long time = System.currentTimeMillis(); Table t = getTableWithHeader(request); t.startRow(); - t.addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); - t.addCell(dateFormat.print(time)); t.addCell(health.getClusterName()); t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT)); t.addCell(health.getNumberOfNodes()); diff --git a/core/src/main/java/org/elasticsearch/rest/action/support/RestTable.java b/core/src/main/java/org/elasticsearch/rest/action/support/RestTable.java index ae721707e97..4e06e5da2ef 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/support/RestTable.java +++ b/core/src/main/java/org/elasticsearch/rest/action/support/RestTable.java @@ -133,7 +133,7 @@ public class RestTable { } } - if (dispHeader != null) { + if (dispHeader != null && checkOutputTimestamp(dispHeader, request)) { // We know we need the header asked for: display.add(dispHeader); @@ -153,7 +153,7 @@ public class RestTable { } else { for (Table.Cell cell : table.getHeaders()) { String d = cell.attr.get("default"); - if (Booleans.parseBoolean(d, true)) { + if (Booleans.parseBoolean(d, true) && checkOutputTimestamp(cell.value.toString(), request)) { display.add(new DisplayHeader(cell.value.toString(), cell.value.toString())); } } @@ -161,6 +161,20 @@ public class RestTable { return display; } + + static boolean checkOutputTimestamp(DisplayHeader dispHeader, RestRequest request) { + return checkOutputTimestamp(dispHeader.name, request); + } + + static boolean checkOutputTimestamp(String disp, RestRequest request) { + if (Table.TIMESTAMP.equals(disp) || Table.EPOCH.equals(disp)) { + return request.paramAsBoolean("ts", true); + } else { + return true; + } + } + + /** * 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 diff --git a/core/src/test/java/org/elasticsearch/common/TableTests.java b/core/src/test/java/org/elasticsearch/common/TableTests.java index 46da20190dc..7e624cb749d 100644 --- a/core/src/test/java/org/elasticsearch/common/TableTests.java +++ b/core/src/test/java/org/elasticsearch/common/TableTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.test.ESTestCase; import java.util.List; import java.util.Map; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; public class TableTests extends ESTestCase { @@ -173,6 +174,32 @@ public class TableTests extends ESTestCase { assertNull(cell); } + public void testWithTimestamp() { + Table table = new Table(); + table.startHeadersWithTimestamp(); + table.endHeaders(); + + List headers = table.getHeaders(); + assertEquals(2, headers.size()); + assertEquals(Table.EPOCH, headers.get(0).value.toString()); + assertEquals(Table.TIMESTAMP, headers.get(1).value.toString()); + assertEquals(2, headers.get(0).attr.size()); + assertEquals("t,time", headers.get(0).attr.get("alias")); + assertEquals("seconds since 1970-01-01 00:00:00", headers.get(0).attr.get("desc")); + assertEquals(2, headers.get(1).attr.size()); + assertEquals("ts,hms,hhmmss", headers.get(1).attr.get("alias")); + assertEquals("time in HH:MM:SS", headers.get(1).attr.get("desc")); + + // check row's timestamp + table.startRow(); + table.endRow(); + List> rows = table.getRows(); + assertEquals(1, rows.size()); + assertEquals(2, rows.get(0).size()); + assertThat(rows.get(0).get(0).value, instanceOf(Long.class)); + + } + private Table getTableWithHeaders() { Table table = new Table(); table.startHeaders(); diff --git a/core/src/test/java/org/elasticsearch/rest/action/support/RestTableTests.java b/core/src/test/java/org/elasticsearch/rest/action/support/RestTableTests.java index c7fb993479c..89b3b6fd239 100644 --- a/core/src/test/java/org/elasticsearch/rest/action/support/RestTableTests.java +++ b/core/src/test/java/org/elasticsearch/rest/action/support/RestTableTests.java @@ -48,17 +48,19 @@ public class RestTableTests extends ESTestCase { private static final String CONTENT_TYPE = "Content-Type"; private static final String ACCEPT = "Accept"; private static final String TEXT_PLAIN = "text/plain; charset=UTF-8"; - private static final String TEXT_TABLE_BODY = "foo foo foo foo foo foo\n"; + private static final String TEXT_TABLE_BODY = "foo foo foo foo foo foo foo foo\n"; private static final String JSON_TABLE_BODY = "[{\"bulk.foo\":\"foo\",\"bulk.bar\":\"foo\",\"aliasedBulk\":\"foo\"," + "\"aliasedSecondBulk\":\"foo\",\"unmatched\":\"foo\"," + - "\"invalidAliasesBulk\":\"foo\"}]"; + "\"invalidAliasesBulk\":\"foo\",\"timestamp\":\"foo\",\"epoch\":\"foo\"}]"; private static final String YAML_TABLE_BODY = "---\n" + "- bulk.foo: \"foo\"\n" + " bulk.bar: \"foo\"\n" + " aliasedBulk: \"foo\"\n" + " aliasedSecondBulk: \"foo\"\n" + " unmatched: \"foo\"\n" + - " invalidAliasesBulk: \"foo\"\n"; + " invalidAliasesBulk: \"foo\"\n" + + " timestamp: \"foo\"\n" + + " epoch: \"foo\"\n"; private Table table = new Table(); private FakeRestRequest restRequest = new FakeRestRequest(); @@ -74,6 +76,9 @@ public class RestTableTests extends ESTestCase { table.addCell("unmatched", "alias:un.matched;desc:bar"); // invalid alias table.addCell("invalidAliasesBulk", "alias:,,,;desc:bar"); + // timestamp + table.addCell("timestamp", "alias:ts"); + table.addCell("epoch", "alias:t"); table.endHeaders(); } @@ -129,6 +134,17 @@ public class RestTableTests extends ESTestCase { TEXT_TABLE_BODY); } + public void testThatDisplayHeadersWithoutTimestamp() throws Exception { + restRequest.params().put("h", "timestamp,epoch,bulk*"); + restRequest.params().put("ts", "0"); + List headers = buildDisplayHeaders(table, restRequest); + + List headerNames = getHeaderNames(headers); + assertThat(headerNames, contains("bulk.foo", "bulk.bar", "aliasedBulk", "aliasedSecondBulk")); + assertThat(headerNames, not(hasItem("timestamp"))); + assertThat(headerNames, not(hasItem("epoch"))); + } + private RestResponse assertResponseContentType(Map headers, String mediaType) throws Exception { FakeRestRequest requestWithAcceptHeader = new FakeRestRequest(headers); table.startRow(); @@ -138,6 +154,8 @@ public class RestTableTests extends ESTestCase { table.addCell("foo"); table.addCell("foo"); table.addCell("foo"); + table.addCell("foo"); + table.addCell("foo"); table.endRow(); RestResponse response = buildResponse(table, new AbstractRestChannel(requestWithAcceptHeader, true) { @Override diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.health/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.health/10_basic.yaml index 0692df28a08..9225ad69ea4 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cat.health/10_basic.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cat.health/10_basic.yaml @@ -50,3 +50,30 @@ \n )+ $/ + + +--- +"With ts parameter": + + - do: + cat.health: + ts: 0 + + - match: + $body: | + /^ + ( \S+ \s+ # cluster + \w+ \s+ # status + \d+ \s+ # node.total + \d+ \s+ # node.data + \d+ \s+ # shards + \d+ \s+ # pri + \d+ \s+ # relo + \d+ \s+ # init + \d+ \s+ # unassign + \d+ \s+ # pending_tasks + (-|\d+[.]\d+ms|s) \s+ # max task waiting time + \d+\.\d+% # active shards percent + \n + )+ + $/