Skip to content

Commit

Permalink
dcache-frontend: provide default sorting on RESTful admin data fields
Browse files Browse the repository at this point in the history
Motivation:

Requested by admin users at dCache User Workshop (2018).

Modification:

Sorting by comparator on streams is added in most
pertinent cases.  Where there are backend sort lists
generated from parameters on the REST API, the
default fields are added when the sort parameter is
undefined.

Sorted results and default sorting on parameters
have been indicated via Swagger annotation.

Result:

Lists of pools, groups, units, etc., are now
sorted by default.

Target: master
Request: 4.2
Issue: dCache/dcache-view#90
Acked-by: Paul
  • Loading branch information
alrossi committed Jun 6, 2018
1 parent 2b7af29 commit 4928eff
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 40 deletions.
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -105,14 +106,17 @@ public Link(SelectionLink link) {
preferences = new UnitPreferences(link.getPreferences());
pools = link.getPools()
.stream()
.sorted(Comparator.comparing(SelectionPool::getName))
.map(SelectionPool::getName)
.collect(Collectors.toList());
poolGroups = link.getPoolGroupsPointingTo()
.stream()
.sorted(Comparator.comparing(SelectionPoolGroup::getName))
.map(SelectionPoolGroup::getName)
.collect(Collectors.toList());
unitGroups = link.getUnitGroupsTargetedBy()
.stream()
.sorted(Comparator.comparing(SelectionUnitGroup::getName))
.map(SelectionUnitGroup::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -129,6 +130,7 @@ protected List<String> extractLinks(PoolSelectionUnit psu) {
return psu.getLinkGroupByName(name)
.getLinks()
.stream()
.sorted(Comparator.comparing(SelectionLink::getName))
.map(SelectionLink::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -95,6 +96,7 @@ protected List<String> extractLinks(PoolSelectionUnit psu) {
.map(SelectionPoolGroup::getName)
.map(psu::getLinksPointingToPoolGroup)
.flatMap(links -> links.stream())
.sorted(Comparator.comparing(SelectionLink::getName))
.map(SelectionLink::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -84,6 +85,7 @@ public PoolGroup(String group, PoolSelectionUnit psu) {
super(group, psu);
pools = psu.getPoolsByPoolGroup(name)
.stream()
.sorted(Comparator.comparing(SelectionPool::getName))
.map(SelectionPool::getName)
.collect(Collectors.toList());
}
Expand All @@ -96,6 +98,7 @@ public List<String> getPools() {
protected List<String> extractLinks(PoolSelectionUnit psu) {
return psu.getLinksPointingToPoolGroup(name)
.stream()
.sorted(Comparator.comparing(SelectionLink::getName))
.map(SelectionLink::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -64,6 +64,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import diskCacheV111.poolManager.PoolPreferenceLevel;

Expand All @@ -88,7 +89,8 @@ public PreferenceResult(PoolPreferenceLevel poolPreferenceLevel) {
}

public List<String> getPools() {
return pools;
return pools == null? null :
pools.stream().sorted().collect(Collectors.toList());
}

public String getTag() {
Expand Down
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -90,6 +91,7 @@ public Unit(SelectionUnit unit) {
type = unit.getType().name();
groups = unit.getMemberOfUnitGroups()
.stream()
.sorted(Comparator.comparing(SelectionUnitGroup::getName))
.map(SelectionUnitGroup::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -62,6 +62,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -86,6 +87,7 @@ public UnitGroup(SelectionUnitGroup group, PoolSelectionUnit psu) {
super(group.getName(), psu);
units = group.getMemeberUnits()
.stream()
.sorted(Comparator.comparing(SelectionUnit::getName))
.map(SelectionUnit::getName)
.collect(Collectors.toList());
}
Expand All @@ -99,6 +101,7 @@ protected List<String> extractLinks(PoolSelectionUnit psu) {
return psu.getUnitGroups().get(name)
.getLinksPointingTo()
.stream()
.sorted(Comparator.comparing(SelectionLink::getName))
.map(SelectionLink::getName)
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -151,6 +151,7 @@ public List<DoorTransferRecord> getReads(@ApiParam("The file to list.")
@ApiParam("Only select reads requested by the client.")
@QueryParam("client") String client,
@ApiParam("How to sort responses.")
@DefaultValue("date")
@QueryParam("sort") String sort) {
try {
if (!HttpServletRequests.isAdmin(request)) {
Expand Down Expand Up @@ -210,6 +211,7 @@ public List<DoorTransferRecord> getWrites(@ApiParam("The file to list.")
@ApiParam("Only select writes requested by the client.")
@QueryParam("client") String client,
@ApiParam("How to sort responses.")
@DefaultValue("date")
@QueryParam("sort") String sort) {
try {
if (!HttpServletRequests.isAdmin(request)) {
Expand Down Expand Up @@ -269,6 +271,7 @@ public List<P2PTransferRecord> getP2ps(@ApiParam("The file to list.")
@ApiParam("Only select transfers triggered by the specified client.")
@QueryParam("client") String client,
@ApiParam("How to sort responses.")
@DefaultValue("date")
@QueryParam("sort") String sort) {
try {
if (!HttpServletRequests.isAdmin(request)) {
Expand Down Expand Up @@ -324,6 +327,7 @@ public List<HSMTransferRecord> getStores(@ApiParam("The file to list.")
@ApiParam("Only select tape writes involving the specified pool.")
@QueryParam("pool") String pool,
@ApiParam("How to sort responses.")
@DefaultValue("date")
@QueryParam("sort") String sort) {
try {
if (!HttpServletRequests.isAdmin(request)) {
Expand Down Expand Up @@ -377,6 +381,7 @@ public List<HSMTransferRecord> getRestores(@ApiParam("The file to list.")
@ApiParam("Only select tape reads involving the specified pool.")
@QueryParam("pool") String pool,
@ApiParam("How to sort responses.")
@DefaultValue("date")
@QueryParam("sort") String sort) {
try {
if (!HttpServletRequests.isAdmin(request)) {
Expand Down Expand Up @@ -406,7 +411,8 @@ public List<HSMTransferRecord> getRestores(@ApiParam("The file to list.")


@GET
@ApiOperation("Provide the full \"grid\" of time series data in one pass.")
@ApiOperation("Provide the full \"grid\" of time series data in one pass. "
+ "Data is sorted lexicographically by key.")
@ApiResponses({
@ApiResponse(code = 500, message = "Internal Server Error"),
})
Expand All @@ -420,6 +426,7 @@ public List<Histogram> getGridData() {
.getDataGrid()
.keySet()
.stream()
.sorted()
.forEach((key) -> {
try {
gridData.add(service.getHistogram(key));
Expand Down
Expand Up @@ -78,6 +78,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.ws.rs.core.MediaType;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

import diskCacheV111.util.CacheException;
Expand Down Expand Up @@ -138,7 +139,8 @@ public CellData getCellData(


@GET
@ApiOperation("Provide information about all cells. Requires admin role.")
@ApiOperation("Provide information about all cells. Requires admin role. "
+ "Results sorted lexicographically by cell name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Cell info service only accessible to admin users."),
})
Expand All @@ -151,6 +153,10 @@ public CellData[] getCellData()throws CacheException {

return Arrays.stream(service.getAddresses())
.map(service::getCellData)
.collect(Collectors.toList()).toArray(new CellData[0]);
.collect(Collectors.toList())
.stream()
.sorted(Comparator.comparing(CellData::getCellName))
.collect(Collectors.toList())
.toArray(new CellData[0]);
}
}
Expand Up @@ -77,10 +77,12 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPoolGroup;

import org.dcache.poolmanager.PoolMonitor;
import org.dcache.restful.providers.pool.PoolGroupInfo;
Expand All @@ -107,7 +109,8 @@ public final class PoolGroupInfoResources {
private PoolMonitor poolMonitor;

@GET
@ApiOperation("Get a list of poolgroups. Requires admin role.")
@ApiOperation("Get a list of poolgroups. Requires admin role."
+ " Results sorted lexicographically by group name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool group info only accessible to admin users."),
})
Expand All @@ -122,6 +125,7 @@ public List<PoolGroup> getPoolGroups() {

return psu.getPoolGroups().values()
.stream()
.sorted(Comparator.comparing(SelectionPoolGroup::getName))
.map((g) -> new PoolGroup(g.getName(), psu))
.collect(Collectors.toList());
}
Expand All @@ -147,7 +151,8 @@ public PoolGroup getPoolGroup(@ApiParam("The poolgroup to be described.")
@GET
@Path("/{group}/pools")
@ApiOperation("Get a list of pools that are a member of a poolgroup. If no "
+ "poolgroup is specified then all pools are listed.")
+ "poolgroup is specified then all pools are listed. "
+ "Results sorted lexicographically by pool name.")
@Produces(MediaType.APPLICATION_JSON)
public String[] getPoolsOfGroup(@ApiParam("The poolgroup to be described.")
@PathParam("group") String group) {
Expand Down
Expand Up @@ -92,10 +92,12 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool;
import diskCacheV111.pools.PoolV2Mode;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.PnfsId;
Expand Down Expand Up @@ -158,7 +160,7 @@ public final class PoolInfoResources {

@GET
@ApiOperation("Get information about all pools (name, group membership, links). "
+ "Requires admin role.")
+ "Requires admin role. Results sorted lexicographically by pool name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Pool info only accessible to admin users."),
})
Expand All @@ -173,6 +175,7 @@ public List<Pool> getPools() throws CacheException {

return psu.getPools().values()
.stream()
.sorted(Comparator.comparing(SelectionPool::getName))
.map((p) -> new Pool(p.getName(), psu))
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -316,6 +319,7 @@ public List<MoverData> getMovers(@ApiParam("The pool to be described.")
@ApiParam("Select movers with a specific storage class.")
@QueryParam("storageClass") String storageClass,
@ApiParam("How returned items should be sorted.")
@DefaultValue("door,startTime")
@QueryParam("sort") String sort) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
Expand Down Expand Up @@ -396,6 +400,7 @@ public List<NearlineData> getNearlineQueues(@ApiParam("The pool to be described.
@ApiParam("Select only operations of this storage class.")
@QueryParam("storageClass") String storageClass,
@ApiParam("How the returned values should be sorted.")
@DefaultValue("class,created")
@QueryParam("sort") String sort) {
if (!HttpServletRequests.isAdmin(request)) {
throw new ForbiddenException(
Expand Down
Expand Up @@ -68,6 +68,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -130,6 +131,7 @@ public SnapshotList<RestoreInfo> getRestores(@ApiParam("Use the snapshot "
@ApiParam("Select only restores with this status.")
@QueryParam("status") String status,
@ApiParam("A comma-seperated list of fields on which to sort the results.")
@DefaultValue("pool,started")
@QueryParam("sort") String sort) {
try {
return service.get(token,
Expand Down
Expand Up @@ -75,10 +75,13 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import diskCacheV111.poolManager.PoolSelectionUnit;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionLink;
import diskCacheV111.poolManager.PoolSelectionUnit.SelectionLinkGroup;

import org.dcache.poolmanager.PoolMonitor;
import org.dcache.restful.providers.selection.Link;
Expand All @@ -103,7 +106,8 @@ public final class LinkResources {


@GET
@ApiOperation("Get information about all links. Requires admin role.")
@ApiOperation("Get information about all links. Requires admin role."
+ " Results sorted lexicographically by link name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Link info only accessible to admin users."),
})
Expand All @@ -116,12 +120,14 @@ public List<Link> getLinks() {

return poolMonitor.getPoolSelectionUnit().getLinks().values()
.stream()
.sorted(Comparator.comparing(SelectionLink::getName))
.map(Link::new)
.collect(Collectors.toList());
}

@GET
@ApiOperation("Get information about all linkgroups. Requires admin role.")
@ApiOperation("Get information about all linkgroups. Requires admin role."
+ " Results sorted lexicographically by link group name.")
@ApiResponses({
@ApiResponse(code = 403, message = "Link group info only accessible to admin users."),
})
Expand All @@ -137,6 +143,7 @@ public List<LinkGroup> getLinkGroups() {

return psu.getLinkGroups().values()
.stream()
.sorted(Comparator.comparing(SelectionLinkGroup::getName))
.map((g) -> new LinkGroup(g, psu))
.collect(Collectors.toList());
}
Expand Down

0 comments on commit 4928eff

Please sign in to comment.