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

feat: add support for date ranges in statistic endpoint #1575 #1653

Merged
merged 3 commits into from
Jul 4, 2022
Merged
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
Expand Up @@ -16,12 +16,20 @@
public interface StatService {
@GET
@Produces({MediaType.APPLICATION_JSON})
JsonNode stat(@HeaderParam("Authorization") String authorization, @QueryParam("month") String month, @QueryParam("format") String format);
JsonNode stat(@HeaderParam("Authorization") String authorization,
@QueryParam("month") String month,
@QueryParam("start-month") String startMonth,
@QueryParam("end-month") String endMonth,
@QueryParam("format") String format);

@GET
String statOpenMetrics(@HeaderParam("Authorization") String authorization, @QueryParam("month") String month, @QueryParam("format") String format);

@POST
@Produces({MediaType.APPLICATION_JSON})
JsonNode statPost(@HeaderParam("Authorization") String authorization, @FormParam("month") String month, @FormParam("format") String format);
JsonNode statPost(@HeaderParam("Authorization") String authorization,
@FormParam("month") String month,
@FormParam("start-month") String startMonth,
@FormParam("end-month") String endMonth,
@FormParam("format") String format);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public void stat(final String umaPatClientId, final String umaPatClientSecret) t
final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret);

final StatService service = ClientFactory.instance().createStatService(issuer + STAT_PATH);
final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), "202101", null);
final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), "202101", null, null, null);
assertTrue(node != null && node.hasNonNull("response"));
}

@Test(enabled = false)
@Parameters({"umaPatClientId", "umaPatClientSecret"})
public void statWithDateRange(final String umaPatClientId, final String umaPatClientSecret) throws Exception {
final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret);

final StatService service = ClientFactory.instance().createStatService(issuer + STAT_PATH);
final JsonNode node = service.stat("Bearer " + authorization.getAccessToken(), null, "202201", "202204", null);
assertTrue(node != null && node.hasNonNull("response"));
}

Expand All @@ -33,7 +43,7 @@ public void stat(final String umaPatClientId, final String umaPatClientSecret) t
public void statPost(final String umaPatClientId, final String umaPatClientSecret) throws Exception {
final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret);
final StatService service = ClientFactory.instance().createStatService(issuer + "/jans-auth/restv1/internal/stat");
final JsonNode node = service.stat(authorization.getAccessToken(), "202101", null);
final JsonNode node = service.stat(authorization.getAccessToken(), "202101", null, null, null);
assertTrue(node != null && node.hasNonNull("response"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ public class StatResource extends ConfigBaseResource {
AuthService authService;

@GET
@ProtectedApi(scopes = { ApiAccessConstants.STATS_USER_READ_ACCESS, ApiAccessConstants.JANS_STAT })
@ProtectedApi(scopes = {ApiAccessConstants.STATS_USER_READ_ACCESS, ApiAccessConstants.JANS_STAT})
@Produces(MediaType.APPLICATION_JSON)
public Response getStatistics(@HeaderParam("Authorization") String authorization,
@QueryParam(value = "month") String month, @QueryParam(value = "format") String format) {
@QueryParam(value = "month") String month,
@QueryParam(value = "start-month") String startMonth,
@QueryParam(value = "end-month") String endMonth,
@QueryParam(value = "format") String format) {
if (StringUtils.isBlank(format)) {
format = "";
}
String url = getIssuer() + this.statUrl;
JsonNode jsonNode = this.authService.getStat(url, authorization, month, format);
logger.trace("StatResource::getUserStatistics() - jsonNode:{} ",jsonNode);
JsonNode jsonNode = this.authService.getStat(url, authorization, month, startMonth, endMonth, format);
logger.trace("StatResource::getUserStatistics() - jsonNode:{} ", jsonNode);
return Response.ok(jsonNode.get("response")).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package io.jans.configapi.security.client;

import static io.jans.as.model.util.Util.escapeLog;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

Expand All @@ -18,7 +19,9 @@
import io.jans.as.model.common.GrantType;
import io.jans.as.model.common.IntrospectionResponse;
import io.jans.as.model.jwk.JSONWebKeySet;

import static io.jans.as.model.jwk.JWKParameter.JSON_WEB_KEY_SET;

import io.jans.configapi.core.util.Jackson;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
Expand All @@ -41,18 +44,18 @@
@RegisterProvider(StatClient.class)
@ApplicationScoped
public class AuthClientFactory {


private static final String CONTENT_TYPE = "Content-Type";

private static Logger log = LoggerFactory.getLogger(AuthClientFactory.class);

public static IntrospectionService getIntrospectionService(String url, boolean followRedirects) {
return createIntrospectionService(url, followRedirects);
}

public static IntrospectionResponse getIntrospectionResponse(String url, String header, String token,
boolean followRedirects) {
boolean followRedirects) {
log.debug("Introspect Token - url:{}, header:{}, token:{} ,followRedirects:{} ", url, header, token,
followRedirects);
ResteasyWebTarget target = (ResteasyWebTarget) ClientBuilder.newClient()
Expand All @@ -61,15 +64,15 @@ public static IntrospectionResponse getIntrospectionResponse(String url, String
return proxy.introspectToken(header, token);
}

public static JsonNode getStatResponse(String url, String token, String month, String format) {
public static JsonNode getStatResponse(String url, String token, String month, String startMonth, String endMonth, String format) {
if (log.isDebugEnabled()) {
log.debug("Stat Response Token - url:{}, token:{}, month:{} ,format:{} ", escapeLog(url), escapeLog(token),
escapeLog(month), escapeLog(format));
log.debug("Stat Response Token - url:{}, token:{}, month:{}, startMonth:{}, endMonth:{}, format:{} ", escapeLog(url), escapeLog(token),
escapeLog(month), escapeLog(startMonth), escapeLog(endMonth), escapeLog(format));
}
ResteasyWebTarget webTarget = (ResteasyWebTarget) ClientBuilder.newClient()
ResteasyWebTarget webTarget = (ResteasyWebTarget) ClientBuilder.newClient()
.target(url);
StatService statService = webTarget.proxy(StatService.class);
return statService.stat(token, month, format);
return statService.stat(token, month, startMonth, endMonth, format);
}

public static JsonNode getHealthCheckResponse(String url) {
Expand All @@ -86,7 +89,7 @@ public static JsonNode getHealthCheckResponse(String url) {
}

public static TokenResponse requestAccessToken(final String tokenUrl, final String clientId,
final String clientSecret, final String scope) {
final String clientSecret, final String scope) {
log.debug("Request for Access Token - tokenUrl:{}, clientId:{}, clientSecret:{}, scope:{} ", tokenUrl,
clientId, clientSecret, scope);
Response response = null;
Expand Down Expand Up @@ -186,7 +189,7 @@ public static JSONWebKeySet getJSONWebKeys(String jwksUri) {
return null;
}


private static Builder getClientBuilder(String url) {
return ClientBuilder.newClient().target(url).request();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class AuthService {
@Inject
AuthClientFactory authClientFactory;

public JsonNode getStat(String url, String token, String month, String format) {
return AuthClientFactory.getStatResponse(url, token, month, format);
public JsonNode getStat(String url, String token, String month, String startMonth, String endMonth, String format) {
return AuthClientFactory.getStatResponse(url, token, month, startMonth, endMonth, format);
}

public JsonNode getHealthCheckResponse(String url) {
return AuthClientFactory.getHealthCheckResponse(url);
}
Expand Down