Skip to content

Commit

Permalink
Merge pull request #147 from DemocracyDevelopers/73-generate-assertio…
Browse files Browse the repository at this point in the history
…ns-endpoint

73 generate assertions endpoint
  • Loading branch information
vteague authored Jul 2, 2024
2 parents dcbc281 + 4cc18d7 commit 035da55
Showing 1 changed file with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public String endpointName() {
@Override
public String endpointBody(final Request the_request, final Response the_response) {
final String prefix = "[endpointBody]";
LOGGER.debug(String.format("%s %s.", prefix, "Received Generate Assertions request."));
LOGGER.debug(String.format("%s %s.", prefix, "Received Generate Assertions request"));

final List<GenerateAssertionsResponseWithErrors> responseData;

Expand Down Expand Up @@ -152,17 +152,17 @@ public String endpointBody(final Request the_request, final Response the_respons

okJSON(the_response, Main.GSON.toJson(responseData));

LOGGER.debug(String.format("%s %s.", prefix, "Completed Generate Assertions request."));
LOGGER.debug(String.format("%s %s.", prefix, "Completed Generate Assertions request"));
} else {
final String msg = "Blank contest name or invalid time limit in Generate Assertions request.";
final String msg = "Blank contest name or invalid time limit in Generate Assertions request";
LOGGER.debug(String.format("%s %s %s.", prefix, msg, the_request.body()));
badDataContents(the_response, msg);
}
} catch (IllegalArgumentException e) {
LOGGER.debug(String.format("%s %s.", prefix, "Bad Generate Assertions request."));
LOGGER.debug(String.format("%s %s.", prefix, "Bad Generate Assertions request"));
badDataContents(the_response, e.getMessage());
} catch (RuntimeException e) {
LOGGER.debug(String.format("%s %s.", prefix, "Error processing Generate Assertions request."));
LOGGER.debug(String.format("%s %s.", prefix, "Error processing Generate Assertions request"));
serverError(the_response, e.getMessage());
}

Expand All @@ -181,7 +181,7 @@ public String endpointBody(final Request the_request, final Response the_respons
protected List<GenerateAssertionsResponseWithErrors> generateAllAssertions(List<ContestResult> IRVContestResults,
double timeLimitSeconds, String raireUrl) {
final String prefix = "[generateAllAssertions]";
LOGGER.debug(String.format("%s %s.", prefix, "Generating assertions for all IRV contests."));
LOGGER.debug(String.format("%s %s.", prefix, "Generating assertions for all IRV contests"));

final List<GenerateAssertionsResponseWithErrors> responseData = new ArrayList<>();

Expand All @@ -191,7 +191,7 @@ protected List<GenerateAssertionsResponseWithErrors> generateAllAssertions(List<
responseData.add(response);
}

LOGGER.debug(String.format("%s %s.", prefix, "Completed assertion generation for all IRV contests."));
LOGGER.debug(String.format("%s %s.", prefix, "Completed assertion generation for all IRV contests"));
return responseData;
}

Expand All @@ -214,14 +214,16 @@ protected List<GenerateAssertionsResponseWithErrors> generateAllAssertions(List<
*/
protected GenerateAssertionsResponseWithErrors generateAssertionsUpdateWinners(List<ContestResult> IRVContestResults,
String contestName, double timeLimitSeconds, String raireUrl) {
final String prefix = "[generateAssertions]";
final String prefix = "[generateAssertionsUpdateWinners]";
LOGGER.debug(String.format("%s %s %s.", prefix, "Generating assertions for contest ", contestName));

try {
final ContestResult cr = IRVContestResults.stream()
.filter(r -> r.getContestName().equalsIgnoreCase(contestName)).findAny().orElseThrow();
final List<String> candidates = cr.getContests().stream().findAny().orElseThrow().choices().stream()
.map(Choice::name).toList();
.filter(r -> r.getContestName().equalsIgnoreCase(contestName)).findAny().orElseThrow(
() -> new NoSuchElementException("Non-existent or non-IRV contest in Generate Assertions request for contest:"));
final List<String> candidates = cr.getContests().stream().findAny().orElseThrow(
() -> new NoSuchElementException("No contests in Contest Result for contest:"))
.choices().stream().map(Choice::name).toList();

// Make the request.
final GenerateAssertionsRequest generateAssertionsRequest = new GenerateAssertionsRequest(
Expand All @@ -238,8 +240,8 @@ protected GenerateAssertionsResponseWithErrors generateAssertionsUpdateWinners(L

// Send it to the RAIRE service.
final HttpResponse raireResponse = httpClient.execute(requestToRaire);
LOGGER.debug(String.format("%s %s.", prefix, "Sent Assertion Request to Raire service for "
+ contestName));
LOGGER.debug(String.format("%s %s %s.", prefix,
"Sent Assertion Request to Raire service for contest", contestName));

// Interpret the response.
final int statusCode = raireResponse.getStatusLine().getStatusCode();
Expand All @@ -248,15 +250,15 @@ protected GenerateAssertionsResponseWithErrors generateAssertionsUpdateWinners(L
if (statusCode == HttpStatus.SC_OK && !gotRaireError) {
// OK response. Update the stored winner and return it.

LOGGER.debug(String.format("%s %s.", prefix, "OK response received from RAIRE for "
+ contestName));
LOGGER.debug(String.format("%s %s %s.", prefix, "OK response received from RAIRE for",
contestName));
GenerateAssertionsResponse responseFromRaire = Main.GSON.fromJson(EntityUtils.toString(raireResponse.getEntity()),
GenerateAssertionsResponse.class);

updateWinnersAndLosers(cr, candidates, responseFromRaire.winner);

LOGGER.debug(String.format("%s %s %s.", prefix,
"Completed assertion generation for contest ", contestName));
"Completed assertion generation for contest", contestName));
return new GenerateAssertionsResponseWithErrors(contestName, responseFromRaire.winner, "");

} else if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR && gotRaireError) {
Expand Down Expand Up @@ -287,10 +289,10 @@ protected GenerateAssertionsResponseWithErrors generateAssertionsUpdateWinners(L
LOGGER.error(String.format("%s %s %s", prefix, msg, e.getMessage()));
throw new RuntimeException(msg);
} catch (NoSuchElementException e ) {
// This happens if the contest name is not in the IRVContestResults.
final String msg = "Non-existent or non-IRV contest in Generate Assertions request:";
LOGGER.error(String.format("%s %s %s %s", prefix, msg, contestName, e.getMessage()));
throw new IllegalArgumentException(msg + contestName);
// This happens if the contest name is not in the IRVContestResults, or if the Contest Result
// does not actually contain any contests (the latter should never happen).
LOGGER.error(String.format("%s %s %s.", prefix, e.getMessage(), contestName));
throw new IllegalArgumentException(e.getMessage() + " " + contestName);
} catch (JsonSyntaxException e) {
// This happens if the raire service returns something that isn't interpretable as json,
// so gson throws a syntax exception when trying to parse raireResponse.
Expand All @@ -313,7 +315,7 @@ protected GenerateAssertionsResponseWithErrors generateAssertionsUpdateWinners(L
// This also shouldn't happen - it would indicate an unexpected problem such as the httpClient
// returning a null response.
final String msg = "Error requesting or receiving assertions for contest ";
LOGGER.error(String.format("%s %s %s", prefix, msg, contestName));
LOGGER.error(String.format("%s %s %s.", prefix, msg, contestName));
throw new RuntimeException(msg + contestName);
} catch (IOException e) {
// Generic error that can be thrown by the httpClient if the connection attempt fails.
Expand Down

0 comments on commit 035da55

Please sign in to comment.