Skip to content

Commit

Permalink
feat(tagstatistics): add endpoint to get statistics by environment an…
Browse files Browse the repository at this point in the history
…d countries
  • Loading branch information
lucashimpens committed Jul 26, 2024
1 parent 888265c commit f4bfddd
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.BadRequestException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.cerberus.core.crud.service.ITagService;
Expand All @@ -58,11 +57,11 @@ public class CampaignExecutionPrivateController {
@GetMapping(path = "/statistics", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getTagStatistics(
HttpServletRequest request,
@RequestParam(name = "systemsFilter", required = false) String[] systemsParam,
@RequestParam(name = "applicationsFilter", required = false) String[] applicationsParam,
@RequestParam(name = "group1Filter", required = false) String[] group1Param,
@RequestParam(name = "from", required = false) String fromParam,
@RequestParam(name = "to", required = false) String toParam
@RequestParam(name = "systems") String[] systemsParam,
@RequestParam(name = "applications") String[] applicationsParam,
@RequestParam(name = "group1") String[] group1Param,
@RequestParam(name = "from") String fromParam,
@RequestParam(name = "to") String toParam
) {
//Retrieve and format URL parameter
fromParam = ParameterParserUtil.parseStringParamAndDecode(fromParam, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'").format(new Date()), "UTF8");
Expand All @@ -79,11 +78,15 @@ public ResponseEntity<String> getTagStatistics(

try {
systemsAllowed = tagStatisticService.getSystemsAllowedForUser(request.getUserPrincipal().getName());
applicationsAllowed = tagStatisticService.getApplicationsSystems(systemsAllowed);
} catch (CerberusException e) {
throw new BadRequestException();
} catch (CerberusException exception) {
LOG.error("Unable to get allowed systems: ", exception);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Unable to get allowed systems: " + exception.getMessage());
}

applicationsAllowed = tagStatisticService.getApplicationsSystems(systemsAllowed);

if (systems.isEmpty() && applications.isEmpty()) {
tagStatistics = new ArrayList<>();
} else {
Expand All @@ -94,8 +97,8 @@ public ResponseEntity<String> getTagStatistics(
}

try {
Map<String, Map<String, JSONObject>> aggregateByTag = tagStatisticService.createMapAggregateByTag(tagStatistics);
Map<String, JSONObject> aggregateByCampaign = tagStatisticService.createMapAggregateByCampaign(aggregateByTag);
Map<String, Map<String, JSONObject>> aggregateByTag = tagStatisticService.createMapGroupedByTag(tagStatistics, "CAMPAIGN");
Map<String, JSONObject> aggregateByCampaign = tagStatisticService.createMapAggregatedStatistics(aggregateByTag, "CAMPAIGN");
List<JSONObject> aggregateListByCampaign = new ArrayList<>();
aggregateByCampaign.forEach((key, value) -> {
if (!Objects.equals(key, "globalGroup1List")) {
Expand All @@ -106,11 +109,58 @@ public ResponseEntity<String> getTagStatistics(
response.put("campaignStatistics", aggregateListByCampaign);
return ResponseEntity.ok(response.toString());
} catch (JSONException ex) {
LOG.error("Internal server error: ", ex);
LOG.error("Error when JSON processing: ", ex);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Internal server error: " + ex.getMessage());
.body("Error when JSON processing: " + ex.getMessage());
}
}

@GetMapping(path = "/statistics/{campaign}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getCampaignStatisticsByCountryEnv(
HttpServletRequest request,
@PathVariable("campaign") String campaign,
@RequestParam(name = "countries", required = false) String[] countriesParam,
@RequestParam(name = "environments", required = false) String[] environmentsParam,
@RequestParam(name = "from") String fromParam,
@RequestParam(name = "to") String toParam
) throws JSONException {
fromParam = ParameterParserUtil.parseStringParamAndDecode(fromParam, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'").format(new Date()), "UTF8");
toParam = ParameterParserUtil.parseStringParamAndDecode(toParam, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'").format(new Date()), "UTF8");
List<String> countries = ParameterParserUtil.parseListParamAndDecode(countriesParam, new ArrayList<>(), "UTF8");
List<String> environments = ParameterParserUtil.parseListParamAndDecode(environmentsParam, new ArrayList<>(), "UTF8");
String fromDateFormatted = tagStatisticService.formatDateForDb(fromParam);
String toDateFormatted = tagStatisticService.formatDateForDb(toParam);
List<TagStatistic> tagStatistics;
JSONObject response = new JSONObject();

tagStatistics = tagStatisticService.readByCriteria(campaign, countries, environments, fromDateFormatted, toDateFormatted).getDataList();

boolean userHasRightSystems = tagStatisticService.userHasRightSystems(request.getUserPrincipal().getName(), tagStatistics);
if (!userHasRightSystems) {
response.put("message", "User has no access to required systems.");
return ResponseEntity
.status(HttpStatus.FORBIDDEN)
.body(response.toString());
}

Map<String, Map<String, JSONObject>> aggregateByTag = tagStatisticService.createMapGroupedByTag(tagStatistics, "ENV_COUNTRY");
Map<String, JSONObject> aggregateByCampaign = tagStatisticService.createMapAggregatedStatistics(aggregateByTag, "ENV_COUNTRY");
List<JSONObject> aggregateListByCampaign = new ArrayList<>();
countries.clear();
environments.clear();
aggregateByCampaign.forEach((key, value) -> {
aggregateListByCampaign.add(value);
String environment = key.split("_")[0];
String country = key.split("_")[1];
if (!environments.contains(environment)) environments.add(environment);
if (!countries.contains(country)) countries.add(country);
});

response.put("campaignStatistics", aggregateListByCampaign);
response.put("environments", environments);
response.put("countries", countries);
return ResponseEntity.ok(response.toString());
}

@PostMapping("{executionId}/declareFalseNegative")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public interface ITagStatisticDAO {
* @return
*/
Answer read(TagStatistic object);

AnswerList<TagStatistic> readByCriteria(List<String> systems, List<String> applications, List<String> groups1, String minDate, String maxDate);

AnswerList<TagStatistic> readByCriteria(String campaign, List<String> countries, List<String> environments, String minDate, String maxDate);

/**
* Get a TagStatistics list by tag from database
* @param tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,83 @@ public AnswerList<TagStatistic> readByCriteria(List<String> systems, List<String
return response;
}

@Override
public AnswerList<TagStatistic> readByCriteria(String campaign, List<String> countries, List<String> environments, String minDate, String maxDate) {
AnswerList<TagStatistic> response = new AnswerList<>();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
List<TagStatistic> tagStatistics = new ArrayList<>();

StringBuilder query = new StringBuilder();

query.append("SELECT `Id`, `Tag`, `Country`, `Environment`, `Campaign`, `CampaignGroup1`, `SystemList`, `ApplicationList`, `DateStartExe`, `DateEndExe`, `NbExe`, `NbExeUsefull`, `NbOK`, `NbKO`, `NbFA`, `NbNA`, `NbNE`, `NbPE`, `NbWE`, `NbPE`, `NbQU`, `NbQE`, `NbCA` from tagstatistic WHERE `Campaign` = ? AND `DateStartExe` >= ? AND `DateEndExe` <= ?");

if (!countries.isEmpty()) {
query.append(" AND ").append(SqlUtil.generateInClause("`Country`", countries));
}

if (!environments.isEmpty()) {
query.append(" AND ").append(SqlUtil.generateInClause("`Environment`", environments));
}

try (Connection connection = this.databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query.toString());
Statement stm = connection.createStatement()) {

int i = 1;

preStat.setString(i++, campaign);
preStat.setString(i++, minDate);
preStat.setString(i++, maxDate);

if (!countries.isEmpty()) {
for (String country : countries) {
preStat.setString(i++, country);
}
}

if (!environments.isEmpty()) {
for (String environment : environments) {
preStat.setString(i++, environment);
}
}

try (ResultSet resultSet = preStat.executeQuery();
ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {

LOG.info("Execute SQL Statement: {} ", preStat);

while (resultSet.next()) {
tagStatistics.add(this.loadFromResultSet(resultSet));
}

int nrTotalRows = 0;
if (rowSet != null && rowSet.next()) {
nrTotalRows = rowSet.getInt(1);
}

if (tagStatistics.size() >= MAX_ROW_SELECTED) { // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
LOG.error("Partial Result in the query.");
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
} else if (tagStatistics.isEmpty()) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
}
response = new AnswerList<>(tagStatistics, nrTotalRows);
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : {}", exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
}
response.setResultMessage(msg);
response.setDataList(tagStatistics);
return response;
}

/**
* Get a TagStatistics list by tag from database
* @param tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public interface ITagStatisticService {
*/
void populateTagStatisticsMap(Map<String, TagStatistic> tagStatistics, List<TestCaseExecution> executions, Tag tag);

Map<String, Map<String, JSONObject>> createMapAggregateByTag(List<TagStatistic> tagStatistics) throws JSONException;
Map<String, JSONObject> createMapAggregateByCampaign(Map<String, Map<String, JSONObject>> aggregateByTag) throws JSONException;
AnswerList<TagStatistic> readByCriteria(String campaign, List<String> countries, List<String> environment, String minDate, String maxDate);
Map<String, Map<String, JSONObject>> createMapGroupedByTag(List<TagStatistic> tagStatistics, String aggregateType) throws JSONException;
Map<String, JSONObject> createMapAggregatedStatistics(Map<String, Map<String, JSONObject>> aggregateByTag, String aggregateType) throws JSONException;
List<String> getSystemsAllowedForUser(String user) throws CerberusException;
List<String> getApplicationsSystems(List<String> systems);
String formatDateForDb(String date);

boolean userHasRightSystems(String user, List<TagStatistic> tagStatistics);
}
Loading

0 comments on commit f4bfddd

Please sign in to comment.