Skip to content

Commit

Permalink
feat(tagstatistics): add nbExe and nbExeUseful on screen 2, improvmen…
Browse files Browse the repository at this point in the history
…ts on errors displaying in the front.
  • Loading branch information
lucashimpens committed Oct 18, 2024
1 parent 4e92b4e commit ce7b7a3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,43 @@ public ResponseEntity<String> getTagStatistics(
String fromDateFormatted = tagStatisticService.formatDateForDb(fromParam);
String toDateFormatted = tagStatisticService.formatDateForDb(toParam);
List<TagStatistic> tagStatistics;
AnswerList<TagStatistic> daoAnswer;
List<String> systemsAllowed;
List<String> applicationsAllowed;
JSONObject response = new JSONObject();

try {
systemsAllowed = tagStatisticService.getSystemsAllowedForUser(request.getUserPrincipal().getName());
} 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 (request.getUserPrincipal() == null) {
MessageEvent message = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNAUTHORISED);
message.setDescription(message.getDescription().replace("%ITEM%", "Campaign Statistics"));
message.setDescription(message.getDescription().replace("%OPERATION%", "'Get statistics'"));
message.setDescription(message.getDescription().replace("%REASON%", "No user provided in the request, please refresh the page or login again."));
response.put("message", message.getDescription());
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED)
.body(response.toString());
}

if (systems.isEmpty() && applications.isEmpty()) {
tagStatistics = new ArrayList<>();
} else {
//If user put in filter a system that is has no access, we delete from the list.
systems.removeIf(param -> !systemsAllowed.contains(param));
applications.removeIf(param -> !applicationsAllowed.contains(param));
tagStatistics = tagStatisticService.readByCriteria(systems, applications, group1List, fromDateFormatted, toDateFormatted).getDataList();
}
systemsAllowed = tagStatisticService.getSystemsAllowedForUser(request.getUserPrincipal().getName());
applicationsAllowed = tagStatisticService.getApplicationsSystems(systemsAllowed);

if (systems.isEmpty() && applications.isEmpty()) {
tagStatistics = new ArrayList<>();
} else {
//If user put in filter a system that is has no access, we delete from the list.
systems.removeIf(param -> !systemsAllowed.contains(param));
applications.removeIf(param -> !applicationsAllowed.contains(param));
daoAnswer = tagStatisticService.readByCriteria(systems, applications, group1List, fromDateFormatted, toDateFormatted);

if (daoAnswer.getDataList().isEmpty()) {
response.put("message", daoAnswer.getResultMessage().getDescription());
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(response.toString());
}
tagStatistics = daoAnswer.getDataList();
}

try {
Map<String, Map<String, JSONObject>> aggregateByTag = tagStatisticService.createMapGroupedByTag(tagStatistics, "CAMPAIGN");
Map<String, JSONObject> aggregateByCampaign = tagStatisticService.createMapAggregatedStatistics(aggregateByTag, "CAMPAIGN");
List<JSONObject> aggregateListByCampaign = new ArrayList<>();
Expand All @@ -116,6 +128,11 @@ public ResponseEntity<String> getTagStatistics(
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error when JSON processing: " + exception.getMessage());
} 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());
}
}

Expand All @@ -137,17 +154,29 @@ public ResponseEntity<String> getCampaignStatisticsByCountryEnv(
List<TagStatistic> tagStatistics;
JSONObject response = new JSONObject();

AnswerList<TagStatistic> answer = tagStatisticService.readByCriteria(campaign, countries, environments, fromDateFormatted, toDateFormatted);
tagStatistics = answer.getDataList();

try {
if (tagStatistics.isEmpty()) {
response.put("message", answer.getResultMessage().getDescription());
if (request.getUserPrincipal() == null) {
MessageEvent message = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNAUTHORISED);
message.setDescription(message.getDescription().replace("%ITEM%", "Campaign Statistics"));
message.setDescription(message.getDescription().replace("%OPERATION%", "'Get statistics'"));
message.setDescription(message.getDescription().replace("%REASON%", "No user provided in the request, please refresh the page or login again."));
response.put("message", message.getDescription());
return ResponseEntity
.status(HttpStatus.UNAUTHORIZED)
.body(response.toString());
}

AnswerList<TagStatistic> daoAnswer = tagStatisticService.readByCriteria(campaign, countries, environments, fromDateFormatted, toDateFormatted);

if (daoAnswer.getDataList().isEmpty()) {
response.put("message", daoAnswer.getResultMessage().getDescription());
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(response.toString());
}

tagStatistics = daoAnswer.getDataList();

boolean userHasRightSystems = tagStatisticService.userHasRightSystems(request.getUserPrincipal().getName(), tagStatistics);
if (!userHasRightSystems) {
response.put("message", new MessageEvent(MessageEventEnum.DATA_OPERATION_NOT_FOUND_OR_NOT_AUTHORIZE).getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Answer createWithMap(Map<String, TagStatistic> tagStatistics) {
preStat.setTimestamp(parameterIndex++, tagStatisticEntry.getValue().getDateStartExe());
preStat.setTimestamp(parameterIndex++, tagStatisticEntry.getValue().getDateEndExe());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbExe());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbExeUsefull());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbExeUseful());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbOK());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbKO());
preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbFA());
Expand Down Expand Up @@ -390,7 +390,7 @@ private TagStatistic loadFromResultSet(ResultSet resultSet) throws SQLException
Timestamp dateStartExe = resultSet.getTimestamp("DateStartExe");
Timestamp dateEndExe = resultSet.getTimestamp("DateEndExe");
int nbExe = resultSet.getInt("nbExe");
int nbExeUsefull = resultSet.getInt("nbExeUsefull");
int nbExeUseful = resultSet.getInt("nbExeUsefull");
int nbOK = resultSet.getInt("nbOK");
int nbKO = resultSet.getInt("nbKO");
int nbFA = resultSet.getInt("nbFA");
Expand All @@ -414,7 +414,7 @@ private TagStatistic loadFromResultSet(ResultSet resultSet) throws SQLException
.dateStartExe(dateStartExe)
.dateEndExe(dateEndExe)
.nbExe(nbExe)
.nbExeUsefull(nbExeUsefull)
.nbExeUseful(nbExeUseful)
.nbOK(nbOK)
.nbKO(nbKO)
.nbFA(nbFA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TagStatistic {
private Timestamp dateStartExe;
private Timestamp dateEndExe;
private int nbExe;
private int nbExeUsefull;
private int nbExeUseful;
private int nbOK;
private int nbKO;
private int nbFA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void calculateExecutionDates(TagStatistic tagStatistic, TestCaseExecutio
private void calculateNumberExecutionsByStatus(TagStatistic tagStatistic, TestCaseExecution execution) {
int nbRetries = execution.getNbExecutions() - 1;
tagStatistic.setNbExe(tagStatistic.getNbExe() + execution.getNbExecutions());
tagStatistic.setNbExeUsefull(tagStatistic.getNbExeUsefull() + (execution.getNbExecutions() - nbRetries));
tagStatistic.setNbExeUseful(tagStatistic.getNbExeUseful() + (execution.getNbExecutions() - nbRetries));

switch(execution.getControlStatus()) {
case "OK":
Expand Down Expand Up @@ -240,7 +240,7 @@ public AnswerList<TagStatistic> readByCriteria(String campaign, List<String> cou
public Map<String, Map<String, JSONObject>> createMapGroupedByTag(List<TagStatistic> tagStatistics, String aggregateType) throws JSONException {
Map<String, Map<String, JSONObject>> aggregateByTag = new HashMap<>();
for (TagStatistic tagStatistic : tagStatistics) {
int nbExeUsefull = 0;
int nbExeUseful = 0;
int nbExe = 0;
int nbOK = 0;
long duration = 0;
Expand All @@ -267,7 +267,7 @@ public Map<String, Map<String, JSONObject>> createMapGroupedByTag(List<TagStatis
}

if (!aggregateByTag.get(key).containsKey(tag)) {
statistics = createJsonTagStat(systemsInTagMap, applicationsInTagMap, group1, minTagDateStartExe, maxTagDateEndExe, duration, nbExeUsefull, nbExe, nbOK);
statistics = createJsonTagStat(systemsInTagMap, applicationsInTagMap, group1, minTagDateStartExe, maxTagDateEndExe, duration, nbExeUseful, nbExe, nbOK);
aggregateByTag.get(key).put(tag, statistics);
}

Expand All @@ -293,18 +293,18 @@ public Map<String, Map<String, JSONObject>> createMapGroupedByTag(List<TagStatis
msMinTagDateStart = minTagDateStartExe.getTime();
msMaxTagDateEnd = maxTagDateEndExe.getTime();
duration = (msMaxTagDateEnd - msMinTagDateStart) / 1000;
nbExeUsefull += tagStatistic.getNbExeUsefull() + mapTag.getInt("nbExeUsefull");
nbExeUseful += tagStatistic.getNbExeUseful() + mapTag.getInt("nbExeUseful");
nbExe += tagStatistic.getNbExe() + mapTag.getInt("nbExe");
nbOK += tagStatistic.getNbOK() + mapTag.getInt("nbOK");

statistics = createJsonTagStat(systemsInTagMap, applicationsInTagMap, group1, minTagDateStartExe, maxTagDateEndExe, duration, nbExeUsefull, nbExe, nbOK);
statistics = createJsonTagStat(systemsInTagMap, applicationsInTagMap, group1, minTagDateStartExe, maxTagDateEndExe, duration, nbExeUseful, nbExe, nbOK);
aggregateByTag.get(key).put(tag, statistics);
}
return aggregateByTag;
}

public Map<String, JSONObject> createMapAggregatedStatistics(Map<String, Map<String, JSONObject>> aggregateByTag, String aggregateType) throws JSONException {
Map<String, JSONObject> aggregateByCampaign = new HashMap<>();
Map<String, JSONObject> aggregatedStatistics = new HashMap<>();
JSONArray globalGroup1List = new JSONArray();
for (Map.Entry<String, Map<String, JSONObject>> aggregateByTagEntry : aggregateByTag.entrySet()) {
String key = aggregateByTagEntry.getKey();
Expand All @@ -313,8 +313,9 @@ public Map<String, JSONObject> createMapAggregatedStatistics(Map<String, Map<Str
String maxDateEnd = "";
double sumPercOK = 0;
double sumPercReliability = 0;
int sumNumberExeUsefull = 0;
int numberCampaignExecutions = 0;
int sumNumberExeUseful = 0;
int sumNumberExe = 0;
int nbCampaignExecutions = 0;
String campaignGroup1 = "";
JSONArray systemsByCampaign = new JSONArray();
JSONArray applicationsByCampaign = new JSONArray();
Expand All @@ -332,11 +333,12 @@ public Map<String, JSONObject> createMapAggregatedStatistics(Map<String, Map<Str

minDateStart = updateMinCampaignDateStart(minDateStart, mapTagEntry);
maxDateEnd = updateMaxCampaignDateEnd(maxDateEnd, mapTagEntry);
sumPercOK += ((double) mapTagEntry.getInt("nbOK") / mapTagEntry.getInt("nbExeUsefull"));
sumPercReliability += ((double) mapTagEntry.getInt("nbExeUsefull") / mapTagEntry.getInt("nbExe"));
sumNumberExeUsefull += mapTagEntry.getInt("nbExeUsefull");
sumPercOK += ((double) mapTagEntry.getInt("nbOK") / mapTagEntry.getInt("nbExeUseful"));
sumPercReliability += ((double) mapTagEntry.getInt("nbExeUseful") / mapTagEntry.getInt("nbExe"));
sumNumberExeUseful += mapTagEntry.getInt("nbExeUseful");
sumNumberExe += mapTagEntry.getInt("nbExe");
totalDuration += mapTagEntry.getLong("duration");
numberCampaignExecutions++;
nbCampaignExecutions++;

if (aggregateType.equals("CAMPAIGN")) {
campaignGroup1 = mapTagEntry.getString("campaignGroup1");
Expand All @@ -350,24 +352,24 @@ public Map<String, JSONObject> createMapAggregatedStatistics(Map<String, Map<Str
double avgDuration = totalDuration / aggregateByTagEntry.getValue().size();
double avgOK = (sumPercOK * 100.0) / aggregateByTagEntry.getValue().size();
double avgReliability = (sumPercReliability * 100) / aggregateByTagEntry.getValue().size();
int avgNbExeUsefull = sumNumberExeUsefull / aggregateByTagEntry.getValue().size();
//int avgNbExeUsefull = sumNumberExeUseful / aggregateByTagEntry.getValue().size();

if (aggregateType.equals("ENV_COUNTRY")) {
String environment = key.split("_")[0];
String country = key.split("_")[1];
statistics = createJsonCampaignStatByEnvCountry(systemsByCampaign, applicationsByCampaign, environment, country, avgDuration, minDateStart, maxDateEnd, avgOK, avgReliability, avgNbExeUsefull);
statistics = createJsonCampaignStatByEnvCountry(systemsByCampaign, applicationsByCampaign, environment, country, avgDuration, minDateStart, maxDateEnd, avgOK, avgReliability, sumNumberExeUseful, sumNumberExe);
} else {
statistics = createJsonCampaignStat(key, systemsByCampaign, applicationsByCampaign, campaignGroup1, avgDuration, minDateStart, maxDateEnd, avgOK, avgReliability, avgNbExeUsefull, numberCampaignExecutions);
statistics = createJsonCampaignStat(key, systemsByCampaign, applicationsByCampaign, campaignGroup1, avgDuration, minDateStart, maxDateEnd, avgOK, avgReliability, nbCampaignExecutions);
}

aggregateByCampaign.put(key, statistics);
aggregatedStatistics.put(key, statistics);
}

if (aggregateType.equals("CAMPAIGN")) {
aggregateByCampaign.put("globalGroup1List", new JSONObject().put("array", globalGroup1List));
aggregatedStatistics.put("globalGroup1List", new JSONObject().put("array", globalGroup1List));
}

return aggregateByCampaign;
return aggregatedStatistics;
}

public List<String> getSystemsAllowedForUser(String user) throws CerberusException {
Expand Down Expand Up @@ -439,20 +441,20 @@ private String updateMinCampaignDateStart(String minDateStart, JSONObject mapTag
return minDateStart;
}

private JSONObject createJsonTagStat(JSONArray systems, JSONArray applications, String group1, Timestamp minDateStart, Timestamp maxDateEnd, long duration, int nbExeUsefull, int nbExe, int nbOK) throws JSONException {
private JSONObject createJsonTagStat(JSONArray systems, JSONArray applications, String group1, Timestamp minDateStart, Timestamp maxDateEnd, long duration, int nbExeUseful, int nbExe, int nbOK) throws JSONException {
return new JSONObject(new LinkedHashMap<>())
.put("systemList", systems)
.put("applicationList", applications)
.put("campaignGroup1", group1)
.put("minTagDateStart", minDateStart)
.put("maxTagDateEnd", maxDateEnd)
.put("duration", duration)
.put("nbExeUsefull", nbExeUsefull)
.put("nbExeUseful", nbExeUseful)
.put("nbExe", nbExe)
.put("nbOK",nbOK);
}

private JSONObject createJsonCampaignStatByEnvCountry(JSONArray systems, JSONArray applications, String environment, String country, double avgDuration, String minDateStart, String maxDateEnd, double avgOK, double avgReliability, int avgNbExeUsefull) throws JSONException {
private JSONObject createJsonCampaignStatByEnvCountry(JSONArray systems, JSONArray applications, String environment, String country, double avgDuration, String minDateStart, String maxDateEnd, double avgOK, double avgReliability, int sumNbExeUseful, int sumNbExe) throws JSONException {
return new JSONObject(new LinkedHashMap<>())
.put("systemList", systems)
.put("applicationList", applications)
Expand All @@ -463,10 +465,11 @@ private JSONObject createJsonCampaignStatByEnvCountry(JSONArray systems, JSONArr
.put("maxDateEnd", maxDateEnd)
.put("avgOK", avgOK)
.put("avgReliability",avgReliability)
.put("avgNbExeUsefull", avgNbExeUsefull);
.put("nbExeUseful", sumNbExeUseful)
.put("nbExe", sumNbExe);
}

private JSONObject createJsonCampaignStat(String campaign, JSONArray systems, JSONArray applications, String group1, double avgDuration, String minDateStart, String maxDateEnd, double avgOK, double avgReliability, int avgNbExeUsefull, int numberCampaignExecutions) throws JSONException {
private JSONObject createJsonCampaignStat(String campaign, JSONArray systems, JSONArray applications, String group1, double avgDuration, String minDateStart, String maxDateEnd, double avgOK, double avgReliability, int nbCampaignExecutions) throws JSONException {
return new JSONObject(new LinkedHashMap<>())
.put("campaign", campaign)
.put("systemList", systems)
Expand All @@ -477,8 +480,7 @@ private JSONObject createJsonCampaignStat(String campaign, JSONArray systems, JS
.put("maxDateEnd", maxDateEnd)
.put("avgOK", avgOK)
.put("avgReliability",avgReliability)
.put("avgNbExeUsefull", avgNbExeUsefull)
.put("numberCampaignExecutions", numberCampaignExecutions);
.put("nbCampaignExecutions", nbCampaignExecutions);
}

private String setKeyAccordingToAggregateType(String aggregateType, TagStatistic tagStatistic) {
Expand Down
Loading

0 comments on commit ce7b7a3

Please sign in to comment.