Search in sources :

Example 26 with Table

use of org.opensearch.common.Table in project OpenSearch by opensearch-project.

the class RestTableTests method testReversedSort.

public void testReversedSort() {
    Table table = new Table();
    table.startHeaders();
    table.addCell("reversed");
    table.endHeaders();
    List<Integer> comparisonList = Arrays.asList(0, 1, 2);
    for (int i = 0; i < comparisonList.size(); i++) {
        table.startRow();
        table.addCell(comparisonList.get(i));
        table.endRow();
    }
    restRequest.params().put("s", "reversed:desc");
    List<Integer> rowOrder = RestTable.getRowOrder(table, restRequest);
    assertEquals(Arrays.asList(2, 1, 0), rowOrder);
}
Also used : Table(org.opensearch.common.Table)

Example 27 with Table

use of org.opensearch.common.Table in project OpenSearch by opensearch-project.

the class RestTableTests method testCompareRow.

public void testCompareRow() {
    Table table = new Table();
    table.startHeaders();
    table.addCell("compare");
    table.endHeaders();
    for (Integer i : Arrays.asList(1, 2, 1)) {
        table.startRow();
        table.addCell(i);
        table.endRow();
    }
    RestTable.TableIndexComparator comparator = new RestTable.TableIndexComparator(table, Collections.singletonList(new RestTable.ColumnOrderElement("compare", false)));
    assertTrue(comparator.compare(0, 1) < 0);
    assertTrue(comparator.compare(0, 2) == 0);
    assertTrue(comparator.compare(1, 2) > 0);
    RestTable.TableIndexComparator reverseComparator = new RestTable.TableIndexComparator(table, Collections.singletonList(new RestTable.ColumnOrderElement("compare", true)));
    assertTrue(reverseComparator.compare(0, 1) > 0);
    assertTrue(reverseComparator.compare(0, 2) == 0);
    assertTrue(reverseComparator.compare(1, 2) < 0);
}
Also used : Table(org.opensearch.common.Table)

Example 28 with Table

use of org.opensearch.common.Table in project OpenSearch by opensearch-project.

the class RestShardsActionTests method testBuildTable.

public void testBuildTable() {
    final int numShards = randomIntBetween(1, 5);
    DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
    List<ShardRouting> shardRoutings = new ArrayList<>(numShards);
    Map<ShardRouting, ShardStats> shardStatsMap = new HashMap<>();
    String index = "index";
    for (int i = 0; i < numShards; i++) {
        ShardRoutingState shardRoutingState = ShardRoutingState.fromValue((byte) randomIntBetween(2, 3));
        ShardRouting shardRouting = TestShardRouting.newShardRouting(index, i, localNode.getId(), randomBoolean(), shardRoutingState);
        Path path = createTempDir().resolve("indices").resolve(shardRouting.shardId().getIndex().getUUID()).resolve(String.valueOf(shardRouting.shardId().id()));
        ShardStats shardStats = new ShardStats(shardRouting, new ShardPath(false, path, path, shardRouting.shardId()), null, null, null, null);
        shardStatsMap.put(shardRouting, shardStats);
        shardRoutings.add(shardRouting);
    }
    IndexStats indexStats = mock(IndexStats.class);
    when(indexStats.getPrimaries()).thenReturn(new CommonStats());
    when(indexStats.getTotal()).thenReturn(new CommonStats());
    IndicesStatsResponse stats = mock(IndicesStatsResponse.class);
    when(stats.asMap()).thenReturn(shardStatsMap);
    DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
    when(discoveryNodes.get(localNode.getId())).thenReturn(localNode);
    ClusterStateResponse state = mock(ClusterStateResponse.class);
    RoutingTable routingTable = mock(RoutingTable.class);
    when(routingTable.allShards()).thenReturn(shardRoutings);
    ClusterState clusterState = mock(ClusterState.class);
    when(clusterState.routingTable()).thenReturn(routingTable);
    when(clusterState.nodes()).thenReturn(discoveryNodes);
    when(state.getState()).thenReturn(clusterState);
    final RestShardsAction action = new RestShardsAction();
    final Table table = action.buildTable(new FakeRestRequest(), state, stats);
    // now, verify the table is correct
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(0).value, equalTo("index"));
    assertThat(headers.get(1).value, equalTo("shard"));
    assertThat(headers.get(2).value, equalTo("prirep"));
    assertThat(headers.get(3).value, equalTo("state"));
    assertThat(headers.get(4).value, equalTo("docs"));
    assertThat(headers.get(5).value, equalTo("store"));
    assertThat(headers.get(6).value, equalTo("ip"));
    assertThat(headers.get(7).value, equalTo("id"));
    assertThat(headers.get(8).value, equalTo("node"));
    final List<List<Table.Cell>> rows = table.getRows();
    assertThat(rows.size(), equalTo(numShards));
    Iterator<ShardRouting> shardRoutingsIt = shardRoutings.iterator();
    for (final List<Table.Cell> row : rows) {
        ShardRouting shardRouting = shardRoutingsIt.next();
        ShardStats shardStats = shardStatsMap.get(shardRouting);
        assertThat(row.get(0).value, equalTo(shardRouting.getIndexName()));
        assertThat(row.get(1).value, equalTo(shardRouting.getId()));
        assertThat(row.get(2).value, equalTo(shardRouting.primary() ? "p" : "r"));
        assertThat(row.get(3).value, equalTo(shardRouting.state()));
        assertThat(row.get(6).value, equalTo(localNode.getHostAddress()));
        assertThat(row.get(7).value, equalTo(localNode.getId()));
        assertThat(row.get(69).value, equalTo(shardStats.getDataPath()));
        assertThat(row.get(70).value, equalTo(shardStats.getStatePath()));
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) CommonStats(org.opensearch.action.admin.indices.stats.CommonStats) ShardPath(org.opensearch.index.shard.ShardPath) ArrayList(java.util.ArrayList) List(java.util.List) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) ShardPath(org.opensearch.index.shard.ShardPath) Path(java.nio.file.Path) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.opensearch.cluster.ClusterState) Table(org.opensearch.common.Table) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) IndexStats(org.opensearch.action.admin.indices.stats.IndexStats)

Example 29 with Table

use of org.opensearch.common.Table in project OpenSearch by opensearch-project.

the class ExampleCatAction method getTableWithHeader.

@Override
protected Table getTableWithHeader(RestRequest request) {
    final Table table = new Table();
    table.startHeaders();
    table.addCell("test", "desc:test");
    table.endHeaders();
    return table;
}
Also used : RestTable(org.opensearch.rest.action.cat.RestTable) Table(org.opensearch.common.Table)

Example 30 with Table

use of org.opensearch.common.Table in project OpenSearch by opensearch-project.

the class ExampleCatAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final String message = request.param("message", "Hello from Cat Example action");
    Table table = getTableWithHeader(request);
    table.startRow();
    table.addCell(message);
    table.endRow();
    return channel -> {
        try {
            channel.sendResponse(RestTable.buildResponse(table, channel));
        } catch (final Exception e) {
            channel.sendResponse(new BytesRestResponse(channel, e));
        }
    };
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) RestTable(org.opensearch.rest.action.cat.RestTable) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) AbstractCatAction(org.opensearch.rest.action.cat.AbstractCatAction) Table(org.opensearch.common.Table) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestTable(org.opensearch.rest.action.cat.RestTable) Table(org.opensearch.common.Table) BytesRestResponse(org.opensearch.rest.BytesRestResponse)

Aggregations

Table (org.opensearch.common.Table)56 List (java.util.List)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)10 NodeClient (org.opensearch.client.node.NodeClient)9 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)9 RestRequest (org.opensearch.rest.RestRequest)9 GET (org.opensearch.rest.RestRequest.Method.GET)8 Arrays.asList (java.util.Arrays.asList)7 Collections.unmodifiableList (java.util.Collections.unmodifiableList)7 RestResponse (org.opensearch.rest.RestResponse)7 RestResponseListener (org.opensearch.rest.action.RestResponseListener)7 Map (java.util.Map)6 NodeStats (org.opensearch.action.admin.cluster.node.stats.NodeStats)5 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)5 CommonStats (org.opensearch.action.admin.indices.stats.CommonStats)5 Strings (org.opensearch.common.Strings)5 TimeValue (org.opensearch.common.unit.TimeValue)5 NodeInfo (org.opensearch.action.admin.cluster.node.info.NodeInfo)4 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)4 ShardRouting (org.opensearch.cluster.routing.ShardRouting)4