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

CLDR-17287 basic performance test for the back end #3418

Merged
merged 4 commits into from
Dec 14, 2023
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 @@ -261,6 +261,15 @@ public static CookieSession retrieveUser(String email) {
}
}

/** only for tests. */
public static CookieSession getTestSession(User user) {
final CookieSession extant = retrieveUser(user);
if (extant != null) {
return extant;
}
return newSession(user, "TEST.TEST.TEST.TEST");
}

/**
* Retrieve the session for a user
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum ErrorCode {
E_BAD_VALUE,
E_BAD_XPATH,
E_NO_OLD_VOTES,
E_PERMANENT_VOTE_NO_FORUM;
E_PERMANENT_VOTE_NO_FORUM,
E_VOTE_NOT_ACCEPTED;
}

private final ErrorCode err_code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.unicode.cldr.web.api;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.unicode.cldr.web.SurveyException;
import org.unicode.cldr.web.SurveyException.ErrorCode;
Expand Down Expand Up @@ -64,7 +65,27 @@ public void setMessage(String message) {
* @return
*/
public Response build() {
return Response.serverError().entity(this).build();
return Response.status(getStatus()).entity(this).build();
}

/** the error as a Status */
public Status getStatus() {
switch (code) {
case E_BAD_LOCALE:
case E_BAD_SECTION:
case E_BAD_XPATH:
return Response.Status.NOT_FOUND;
case E_NO_PERMISSION:
return Response.Status.FORBIDDEN;
case E_BAD_VALUE:
case E_VOTE_NOT_ACCEPTED:
return Response.Status.NOT_ACCEPTABLE;
case E_SESSION_DISCONNECTED:
case E_NOT_LOGGED_IN:
return Response.Status.UNAUTHORIZED;
default:
return Response.Status.INTERNAL_SERVER_ERROR;
}
}

public static Response surveyNotQuiteReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.logging.Logger;
import javax.json.bind.spi.JsonbProvider;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.unicode.cldr.test.CheckCLDR;
import org.unicode.cldr.test.CheckCLDR.CheckStatus;
import org.unicode.cldr.test.CheckCLDR.CheckStatus.Subtype;
Expand Down Expand Up @@ -62,11 +61,11 @@ public VoteEntry(User u, Integer override, boolean redacted) {

static final Logger logger = SurveyLog.forClass(VoteAPIHelper.class);

private static class ArgsForGet {
public static class ArgsForGet {
String localeId;
String sessionId;
String page = null;
String xpstrid = null;
public String xpstrid = null;
Boolean getDashboard = false;

public ArgsForGet(String loc, String session) {
Expand Down Expand Up @@ -119,79 +118,85 @@ private static Response handleGetRows(ArgsForGet args) {
/** if true, hide emails. TODO: CLDR-16829 remove this parameter */
final boolean redacted =
((mySession.user == null) || (!mySession.user.getLevel().isGuest()));
final RowResponse r = new RowResponse();
XPathMatcher matcher = null;
PageId pageId = null;
String xp = null;

if (args.xpstrid == null && args.page != null) {
try {
pageId = PageId.valueOf(args.page);
} catch (IllegalArgumentException iae) {
return Response.status(404)
.entity(new STError(ErrorCode.E_BAD_SECTION))
.build();
}
if (pageId.getSectionId() == org.unicode.cldr.util.PathHeader.SectionId.Special) {
return new STError(
ErrorCode.E_SPECIAL_SECTION,
"Items not visible - page "
+ pageId
+ " section "
+ pageId.getSectionId())
.build();
}
r.pageId = pageId.name();
} else if (args.xpstrid != null && args.page == null) {
xp = sm.xpt.getByStringID(args.xpstrid);
if (xp == null) {
return Response.status(404).entity(new STError(ErrorCode.E_BAD_XPATH)).build();
}
matcher = XPathMatcher.exactMatcherForString(xp);
} else {
// Should not get here.
return new STError(
ErrorCode.E_INTERNAL,
"handleGetRows: need xpstrid or page, but not both")
.build();
}
final DataPage pageData = DataPage.make(pageId, mySession, locale, xp, matcher);
pageData.setUserForVotelist(mySession.user);
r.page = new RowResponse.Page();
if (args.xpstrid != null) {
r.setOneRowPath(args.xpstrid);
final RowResponse r = getRowsResponse(args, sm, locale, mySession, redacted);
return Response.ok(r).build();
} catch (Throwable t) {
t.printStackTrace();
if (!(t instanceof SurveyException
&& ((SurveyException) t).getErrCode() != ErrorCode.E_INTERNAL)) {
// log unknown or internal errs
SurveyLog.logException(
logger, t, "Trying to load " + args.localeId + " / " + args.xpstrid);
}
return new STError(t).build();
}
}

// don't return default content
CLDRLocale dcParent =
SupplementalDataInfo.getInstance().getBaseFromDefaultContent(locale);
if (dcParent != null) {
r.dcParent = dcParent.getBaseName();
r.page.nocontent = true;
} else {
r.canModify = UserRegistry.userCanModifyLocale(mySession.user, locale);
r.localeDisplayName = locale.getDisplayName();
r.page.nocontent = false;
Collection<DataRow> dataRows = pageData.getAll();
r.page.rows = makePageRows(dataRows, redacted);
if (args.page != null) {
r.displaySets = makeDisplaySets(dataRows);
}
public static RowResponse getRowsResponse(
ArgsForGet args,
final SurveyMain sm,
final CLDRLocale locale,
final CookieSession mySession,
final boolean redacted)
throws SurveyException {
final RowResponse r = new RowResponse();
XPathMatcher matcher = null;
PageId pageId = null;
String xp = null;

if (args.xpstrid == null && args.page != null) {
try {
pageId = PageId.valueOf(args.page);
} catch (IllegalArgumentException iae) {
throw new SurveyException(ErrorCode.E_BAD_SECTION);
}
if (args.getDashboard) {
r.notifications =
getOnePathDash(XPathTable.xpathToBaseXpath(xp), locale, mySession);
if (pageId.getSectionId() == org.unicode.cldr.util.PathHeader.SectionId.Special) {
throw new SurveyException(
ErrorCode.E_SPECIAL_SECTION,
"Items not visible - page " + pageId + " section " + pageId.getSectionId());
}
if (DEBUG_SERIALIZATION) {
debugSerialization(r, args);
r.pageId = pageId.name();
} else if (args.xpstrid != null && args.page == null) {
xp = sm.xpt.getByStringID(args.xpstrid);
if (xp == null) {
throw new SurveyException(ErrorCode.E_BAD_XPATH);
}
return Response.ok(r).build();
} catch (Throwable t) {
t.printStackTrace();
SurveyLog.logException(
logger, t, "Trying to load " + args.localeId + " / " + args.xpstrid);
return new STError(t).build(); // 500
matcher = XPathMatcher.exactMatcherForString(xp);
} else {
// Should not get here. but could be a 'not acceptable'
throw new SurveyException(
ErrorCode.E_INTERNAL, // or E_BAD_XPATH?
"handleGetRows: need xpstrid or page, but not both");
}
final DataPage pageData = DataPage.make(pageId, mySession, locale, xp, matcher);
pageData.setUserForVotelist(mySession.user);
r.page = new RowResponse.Page();
if (args.xpstrid != null) {
r.setOneRowPath(args.xpstrid);
}

// don't return default content
CLDRLocale dcParent = SupplementalDataInfo.getInstance().getBaseFromDefaultContent(locale);
if (dcParent != null) {
r.dcParent = dcParent.getBaseName();
r.page.nocontent = true;
} else {
r.canModify = UserRegistry.userCanModifyLocale(mySession.user, locale);
r.localeDisplayName = locale.getDisplayName();
r.page.nocontent = false;
Collection<DataRow> dataRows = pageData.getAll();
r.page.rows = makePageRows(dataRows, redacted);
if (args.page != null) {
r.displaySets = makeDisplaySets(dataRows);
}
}
if (args.getDashboard) {
r.notifications = getOnePathDash(XPathTable.xpathToBaseXpath(xp), locale, mySession);
}
if (DEBUG_SERIALIZATION) {
debugSerialization(r, args);
}
return r;
}

private static void debugSerialization(RowResponse r, ArgsForGet args) {
Expand Down Expand Up @@ -372,18 +377,40 @@ private static Dashboard.ReviewNotification[] getOnePathDash(
// separated from
// the HTTP response concerns, so that VoteAPI and XPathAlt can share code without the
// awkwardness of forbiddenIsOk.
static Response handleVote(
public static Response handleVote(
String loc,
String xp,
String value,
int voteLevelChanged,
final CookieSession mySession,
boolean forbiddenIsOk) {
// translate this call into jax-rs Response
try {
final VoteResponse r =
getHandleVoteResponse(
loc, xp, value, voteLevelChanged, mySession, forbiddenIsOk);
return Response.ok(r).build();
} catch (Throwable se) {
return new STError(se).build();
}
}
/**
* this function is the implementation of handleVote() but does not use any jax-rs, for unit
* tests
*/
public static VoteResponse getHandleVoteResponse(
String loc,
String xp,
String value,
int voteLevelChanged,
final CookieSession mySession,
boolean forbiddenIsOk)
throws SurveyException {
VoteResponse r = new VoteResponse();
mySession.userDidAction();
CLDRLocale locale = CLDRLocale.getInstance(loc);
if (!UserRegistry.userCanModifyLocale(mySession.user, locale)) {
return Response.status(Status.FORBIDDEN).build();
throw new SurveyException(ErrorCode.E_NO_PERMISSION, "Not allowed to modify " + locale);
}
loc = locale.getBaseName(); // sanitized
final SurveyMain sm = CookieSession.sm;
Expand Down Expand Up @@ -445,15 +472,15 @@ static Response handleVote(
}
} catch (Throwable t) {
SurveyLog.logException(logger, t, "Processing submission " + locale + ":" + xp);
return (new STError(t).build());
throw new SurveyException(
ErrorCode.E_INTERNAL, "Processing submission " + locale + ":" + xp);
}
}
if (!forbiddenIsOk && r.statusAction.isForbidden()) {
return Response.status(Response.Status.NOT_ACCEPTABLE)
.entity(new STError("Status action is forbidden: " + r.statusAction))
.build();
throw new SurveyException(
ErrorCode.E_VOTE_NOT_ACCEPTED, "Status action is forbidden: " + r.statusAction);
}
return Response.ok(r).build();
return r;
}

private static void normalizedToZeroLengthError(VoteResponse r, List<CheckStatus> result) {
Expand Down
Loading
Loading