Skip to content

POL-293 Add paging to the history of a single policy #119

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

Merged
merged 5 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/main/java/com/redhat/cloud/policies/app/PolicyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,7 @@ void disableTrigger(@PathParam("triggerId") UUID triggerId,
@Consumes("application/json")
String findLastTriggered(@QueryParam("triggerIds") String triggerIds,
@QueryParam("thin") boolean thin,
@QueryParam("page") int page,
@QueryParam("per_page") int per_page,
@HeaderParam("Hawkular-Tenant") String customerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.hibernate.exception.ConstraintViolationException;

import static java.lang.Integer.min;

/**
* @author hrupp
*/
Expand Down Expand Up @@ -128,7 +130,7 @@ public class PolicyCrudService {
@Parameter(
name = "limit",
in = ParameterIn.QUERY,
description = "Number of items per page, if not specified uses 10. " + Pager.NO_LIMIT + " can be used to specify an unlimited page, when specified it ignores the offset" ,
description = "Number of items per page, if not specified uses 50. " + Pager.NO_LIMIT + " can be used to specify an unlimited page, when specified it ignores the offset" ,
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
Expand Down Expand Up @@ -786,12 +788,26 @@ public Response getPolicy(@PathParam("id") UUID policyId) {
@APIResponse(responseCode = "403", description = "Individual permissions missing to complete action")
@APIResponse(responseCode = "404", description = "Policy not found")
@APIResponse(responseCode = "500", description = "Retrieval of History failed")
@Parameter(name = "id", description = "UUID of the policy")
@Parameters({
@Parameter(
name = "offset",
in = ParameterIn.QUERY,
description = "Page number, starts 0, if not specified uses 0.",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(
name = "limit",
in = ParameterIn.QUERY,
description = "Number of items per page, if not specified uses 50. Maximum value is 200.",
schema = @Schema(type = SchemaType.INTEGER)
),
@Parameter(name = "id", description = "UUID of the policy")
})
@GET
@Path("/{id}/history/trigger")
public Response getTriggerHistoryForPolicy(@PathParam("id") UUID policyId) {
if (!user.canReadAll()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to retrieve policies")).build();
return Response.status(Response.Status.FORBIDDEN).entity(new Msg("Missing permissions to retrieve the policy history")).build();
}

Policy policy = Policy.findById(user.getAccount(), policyId);
Expand All @@ -800,8 +816,16 @@ public Response getTriggerHistoryForPolicy(@PathParam("id") UUID policyId) {
if (policy==null) {
builder = Response.status(Response.Status.NOT_FOUND);
} else {

try {
String alerts = engine.findLastTriggered(policyId.toString(), false, user.getAccount());
Pager pager = PagingUtils.extractPager(uriInfo);

int limit = pager.getLimit();
limit = min(limit,200);
int pageNum = pager.getOffset() / limit;

String alerts = engine.findLastTriggered(policyId.toString(), false, pageNum, limit,
Copy link
Member

Choose a reason for hiding this comment

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

I'll need a "paginated response", e.g. to know the total number of elements to decide if "next page" button is disabled or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I am not sure yet how to get that from the engine

user.getAccount());

List<HistoryItem> items = new ArrayList<>();
DocumentContext jp = JsonPath.parse(alerts);
Expand All @@ -816,8 +840,9 @@ public Response getTriggerHistoryForPolicy(@PathParam("id") UUID policyId) {
}
builder = Response.ok(items);
} catch (Exception e) {
log.warning("Retrieval of history failed: " + e.getMessage());
builder = Response.serverError();
String msg = "Retrieval of history failed with: " + e.getMessage();
log.warning(msg);
builder = Response.serverError().entity(msg);
}
}
return builder.build();
Expand Down