Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination #17638

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"wlm_stats_list": {
"stability": "experimental",
"url": {
"paths": [
{
"path": "/_list/wlm_stats",
"methods": ["GET"]
}
]
},
"params": {
"size": {
"type": "int",
"required": false,
"description": "Number of results per page"
},
"next_token": {
"type": "string",
"required": false,
"description": "Pagination token for next page"
},
"sort": {
"type": "string",
"required": false,
"description": "Sort field"
},
"order": {
"type": "string",
"required": false,
"description": "Sort order (asc or desc)"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
"Basic usage returns expected table header":
- do:
wlm_stats_list: {}
- is_true: $body
- match:
$body: /(?s).*NODE_ID\s+\|\s+QUERY_GROUP_ID\s+\|\s+TOTAL_COMPLETIONS\s+\|\s+TOTAL_REJECTIONS\s+\|\s+TOTAL_CANCELLATIONS\s+\|\s+CPU_USAGE\s+\|\s+MEMORY_USAGE.*/

---
"Custom size param still returns headers":
- do:
wlm_stats_list:
size: 1
- is_true: $body
- match:
$body: /TOTAL_REJECTIONS/

---
"Sort by node_id asc does not error":
- do:
wlm_stats_list:
sort: node_id
order: asc
- is_true: $body
- match:
$body: /NODE_ID/

---
"Sort by query_group_id desc does not error":
- do:
wlm_stats_list:
sort: query_group_id
order: desc
- is_true: $body
- match:
$body: /QUERY_GROUP_ID/

---
"Invalid sort field returns error":
- do:
catch: bad_request
wlm_stats_list:
sort: memory_usage
order: desc
- match:
error.reason: "Invalid value for 'sort'. Allowed: 'node_id', 'query_group_id'."

---
"Invalid sort order returns error":
- do:
catch: bad_request
wlm_stats_list:
order: upside_down
- match:
error.reason: "Invalid value for 'order'. Allowed: 'asc', 'desc'."
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class WlmStatsResponse extends BaseNodesResponse<WlmStats> implements ToX
super(in);
}

WlmStatsResponse(ClusterName clusterName, List<WlmStats> nodes, List<FailedNodeException> failures) {
public WlmStatsResponse(ClusterName clusterName, List<WlmStats> nodes, List<FailedNodeException> failures) {
super(clusterName, nodes, failures);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.action.pagination;

import org.opensearch.wlm.stats.WlmStats;

import java.util.Comparator;

public enum SortBy {
QUERY_GROUP {
@Override
public Comparator<WlmStats> getComparator() {
return Comparator.comparing(
(WlmStats wlmStats) -> wlmStats.getQueryGroupStats().getStats().isEmpty()
? ""
: wlmStats.getQueryGroupStats().getStats().keySet().iterator().next()
).thenComparing(wlmStats -> wlmStats.getNode().getId());
}
},
NODE_ID {
@Override
public Comparator<WlmStats> getComparator() {
return Comparator.comparing(
(WlmStats wlmStats) -> wlmStats.getNode().getId()
).thenComparing(
wlmStats -> wlmStats.getQueryGroupStats().getStats().isEmpty()
? ""
: wlmStats.getQueryGroupStats().getStats().keySet().iterator().next()
);
}
};

public abstract Comparator<WlmStats> getComparator();

public static SortBy fromString(String input) {
switch (input.toLowerCase()) {
case "query_group":
return QUERY_GROUP;
case "node_id":
return NODE_ID;
default:
throw new IllegalArgumentException("Invalid sort field: " + input);
}
Comment on lines +42 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use this to avoid using switch? Same for SortOrder

try {
            return SortBy.valueOf(input.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid sort field: " + input + ". Allowed values: query_group, node_id");
        }

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.action.pagination;

import org.opensearch.wlm.stats.WlmStats;

import java.util.Comparator;

public enum SortOrder {
ASC {
@Override
public Comparator<WlmStats> apply(Comparator<WlmStats> baseComparator) {
return baseComparator;
}
},
DESC {
@Override
public Comparator<WlmStats> apply(Comparator<WlmStats> baseComparator) {
return baseComparator.reversed();
}
};

public abstract Comparator<WlmStats> apply(Comparator<WlmStats> baseComparator);

public static SortOrder fromString(String input) {
switch (input.toLowerCase()) {
case "asc":
return ASC;
case "desc":
return DESC;
default:
throw new IllegalArgumentException("Invalid sort order: " + input);
}
}
}
Loading
Loading