diff --git a/source/src/main/java/org/cerberus/core/crud/dao/ITagStatisticDAO.java b/source/src/main/java/org/cerberus/core/crud/dao/ITagStatisticDAO.java new file mode 100644 index 000000000..97dd093af --- /dev/null +++ b/source/src/main/java/org/cerberus/core/crud/dao/ITagStatisticDAO.java @@ -0,0 +1,78 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.crud.dao; + +import org.cerberus.core.crud.entity.TagStatistic; +import org.cerberus.core.util.answer.Answer; +import org.cerberus.core.util.answer.AnswerList; + +import java.util.Map; + +/** + * Interface that defines the public methods to manage Application data on table + * Create Read Update Delete + * + * @author lhimpens + */ +public interface ITagStatisticDAO { + + /** + * Insert a unique line of TagStatistic in database + * @param object + * @return + */ + Answer create(TagStatistic object); + + /** + * Insert a list of tagStatistics in only one INSERT statement + * @param tagStatistics + * @return + */ + Answer createWithMap(Map tagStatistics); + + /** + * Get a TagStatistic object from database + * @param object + * @return + */ + Answer read(TagStatistic object); + + /** + * Get a TagStatistics list by tag from database + * @param tag + * @return + */ + AnswerList readByTag(String tag); + + /** + * Update a TagStatistic + * @param object + * @return + */ + Answer update(TagStatistic object); + + /** + * Delete a TagStatistic object in database + * @param tag + * @param object + * @return + */ + Answer delete(String tag, TagStatistic object); +} diff --git a/source/src/main/java/org/cerberus/core/crud/dao/impl/TagStatisticDAO.java b/source/src/main/java/org/cerberus/core/crud/dao/impl/TagStatisticDAO.java new file mode 100644 index 000000000..7fafdca6a --- /dev/null +++ b/source/src/main/java/org/cerberus/core/crud/dao/impl/TagStatisticDAO.java @@ -0,0 +1,267 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.crud.dao.impl; + +import lombok.AllArgsConstructor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.cerberus.core.crud.dao.ITagStatisticDAO; +import org.cerberus.core.crud.entity.TagStatistic; +import org.cerberus.core.database.DatabaseSpring; +import org.cerberus.core.engine.entity.MessageEvent; +import org.cerberus.core.enums.MessageEventEnum; +import org.cerberus.core.util.ParameterParserUtil; +import org.cerberus.core.util.answer.Answer; +import org.cerberus.core.util.answer.AnswerList; +import org.springframework.stereotype.Repository; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@AllArgsConstructor +@Repository +public class TagStatisticDAO implements ITagStatisticDAO { + private static final int MAX_ROW_SELECTED = 100000; + + private final DatabaseSpring databaseSpring; + + private static final String OBJECT_NAME = "TagStatistic"; + + private static final Logger LOG = LogManager.getLogger(TagStatisticDAO.class); + + /** + * Insert a unique line of TagStatistic in database + * @param object + * @return + */ + @Override + public Answer create(TagStatistic object) { + throw new UnsupportedOperationException(); + } + + /** + * Insert a list of tagStatistics in only one INSERT statement + * @param tagStatistics + * @return + */ + @Override + public Answer createWithMap(Map tagStatistics) { + MessageEvent msg; + StringBuilder placeholders = new StringBuilder(); + String baseQuery ="INSERT INTO tagstatistic (`Tag`, `Country`, `Environment`, `Campaign`, `CampaignGroup1`, `SystemList`, `ApplicationList`, `DateStartExe`, `DateEndExe`, `NbExe`, `NbExeUsefull`, `NbOK`, `nbKO`, `nbFA`, `nbNA`, `nbNE`, `nbWE`, `nbPE`, `nbQU`, `nbQE`, `nbCA`, `UsrCreated`) VALUES "; + placeholders.append("(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); + if (tagStatistics.size() > 1) { + for (int i = 1; i < tagStatistics.size(); i++) { + placeholders.append(", (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); + } + } + String query = baseQuery + placeholders.toString(); + + try (Connection connection = this.databaseSpring.connect(); + PreparedStatement preStat = connection.prepareStatement(query)) { + int parameterIndex = 1; + for (Map.Entry tagStatisticEntry : tagStatistics.entrySet()) { + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getTag()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getCountry()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getEnvironment()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getCampaign()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getCampaignGroup1()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getSystemList()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getApplicationList()); + 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().getNbOK()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbKO()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbFA()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbNA()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbNE()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbWE()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbPE()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbQU()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbQE()); + preStat.setInt(parameterIndex++, tagStatisticEntry.getValue().getNbCA()); + preStat.setString(parameterIndex++, tagStatisticEntry.getValue().getUsrCreated()); + } + + preStat.executeUpdate(); + msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK); + msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT")); + + } catch (SQLException exception) { + LOG.error("Unable to execute query : {}", exception.toString()); + msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_DUPLICATE); + msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString())); + } + return new Answer(msg); + } + + /** + * Get a TagStatistic object from database + * @param object + * @return + */ + @Override + public Answer read(TagStatistic object) { + return null; + } + + /** + * Get a TagStatistics list by tag from database + * @param tag + * @return + */ + @Override + public AnswerList readByTag(String tag) { + AnswerList response = new AnswerList<>(); + MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED); + msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "")); + List tagStatistics = new ArrayList<>(); + + String query ="SELECT * FROM `tagstatistic` WHERE `tag` = ?"; + LOG.debug("SQL : {}", query); + + try (Connection connection = this.databaseSpring.connect(); + PreparedStatement preStat = connection.prepareStatement(query); + Statement stm = connection.createStatement()) { + preStat.setString(1, tag); + + try (ResultSet resultSet = preStat.executeQuery(); + ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) { + + 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; + } + + /** + * Update a TagStatistic + * @param object + * @return + */ + @Override + public Answer update(TagStatistic object) { + throw new UnsupportedOperationException(); + } + + /** + * Delete a TagStatistic object in database + * @param tag + * @param object + * @return + */ + @Override + public Answer delete(String tag, TagStatistic object) { + throw new UnsupportedOperationException(); + } + + /** + * Convert a database result set into entity + * @param resultSet + * @return + * @throws SQLException + */ + private TagStatistic loadFromResultSet(ResultSet resultSet) throws SQLException { + long id = ParameterParserUtil.parseLongParam(resultSet.getString("Id"), 0); + String tag = ParameterParserUtil.parseStringParam(resultSet.getString("Tag"), ""); + String country = ParameterParserUtil.parseStringParam(resultSet.getString("Country"), ""); + String environment = ParameterParserUtil.parseStringParam(resultSet.getString("Environment"), ""); + String campaign = ParameterParserUtil.parseStringParam(resultSet.getString("Campaign"), ""); + String campaignGroup1 = ParameterParserUtil.parseStringParam(resultSet.getString("CampaignGroup1"), ""); + String systemList = ParameterParserUtil.parseStringParam(resultSet.getString("SystemList"), ""); + String applicationList = ParameterParserUtil.parseStringParam(resultSet.getString("ApplicationList"), ""); + Timestamp dateStartExe = resultSet.getTimestamp("DateStartExe"); + Timestamp dateEndExe = resultSet.getTimestamp("DateEndExe"); + int nbExe = resultSet.getInt("nbExe"); + int nbExeUsefull = resultSet.getInt("nbExeUsefull"); + int nbOK = resultSet.getInt("nbOK"); + int nbKO = resultSet.getInt("nbKO"); + int nbFA = resultSet.getInt("nbFA"); + int nbNA = resultSet.getInt("nbNA"); + int nbNE = resultSet.getInt("nbNE"); + int nbWE = resultSet.getInt("nbWE"); + int nbPE = resultSet.getInt("nbPE"); + int nbQU = resultSet.getInt("nbQU"); + int nbQE = resultSet.getInt("nbQE"); + int nbCA = resultSet.getInt("nbCA"); + String usrModif = ParameterParserUtil.parseStringParam(resultSet.getString("UsrModif"), ""); + String usrCreated = ParameterParserUtil.parseStringParam(resultSet.getString("UsrCreated"), ""); + Timestamp dateModif = resultSet.getTimestamp("DateModif"); + Timestamp dateCreated = resultSet.getTimestamp("DateCreated"); + + return TagStatistic.builder() + .id(id) + .tag(tag) + .country(country) + .environment(environment) + .campaign(campaign) + .campaignGroup1(campaignGroup1) + .systemList(systemList) + .applicationList(applicationList) + .dateStartExe(dateStartExe) + .dateEndExe(dateEndExe) + .nbExe(nbExe) + .nbExeUsefull(nbExeUsefull) + .nbOK(nbOK) + .nbKO(nbKO) + .nbFA(nbFA) + .nbNA(nbNA) + .nbNE(nbNE) + .nbWE(nbWE) + .nbPE(nbPE) + .nbQU(nbQU) + .nbQE(nbQE) + .nbCA(nbCA) + .usrCreated(usrCreated) + .usrModif(usrModif) + .dateCreated(dateCreated) + .dateModif(dateModif) + .build(); + } +} diff --git a/source/src/main/java/org/cerberus/core/crud/entity/TagStatistic.java b/source/src/main/java/org/cerberus/core/crud/entity/TagStatistic.java new file mode 100644 index 000000000..f2fbc2694 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/crud/entity/TagStatistic.java @@ -0,0 +1,105 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.crud.entity; + +import lombok.*; + +import java.sql.Timestamp; +import java.util.List; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +public class TagStatistic { + private long id; + private String tag; + private String country; + private String environment; + private String campaign; + private String campaignGroup1; + private String systemList; + private String applicationList; + private Timestamp dateStartExe; + private Timestamp dateEndExe; + private int nbExe; + private int nbExeUsefull; + private int nbOK; + private int nbKO; + private int nbFA; + private int nbNA; + private int nbNE; + private int nbWE; + private int nbPE; + private int nbQU; + private int nbQE; + private int nbCA; + @EqualsAndHashCode.Exclude + private String usrCreated; + @EqualsAndHashCode.Exclude + private Timestamp dateCreated; + @EqualsAndHashCode.Exclude + private String usrModif; + @EqualsAndHashCode.Exclude + private Timestamp dateModif; + + private List executions; + + public void incrementNbOK() { + this.nbOK++; + } + + public void incrementNbFA() { + this.nbFA++; + } + + public void incrementNbKO() { + this.nbKO++; + } + + public void incrementNbNA() { + this.nbNA++; + } + + public void incrementNbNE() { + this.nbNE++; + } + + public void incrementNbWE() { + this.nbWE++; + } + + public void incrementNbPE() { + this.nbPE++; + } + + public void incrementNbQU() { + this.nbQU++; + } + + public void incrementNbQE() { + this.nbQE++; + } + + public void incrementNbCA() { + this.nbCA++; + } +} diff --git a/source/src/main/java/org/cerberus/core/crud/service/ITagStatisticService.java b/source/src/main/java/org/cerberus/core/crud/service/ITagStatisticService.java new file mode 100644 index 000000000..7a25f947b --- /dev/null +++ b/source/src/main/java/org/cerberus/core/crud/service/ITagStatisticService.java @@ -0,0 +1,53 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.crud.service; + +import org.cerberus.core.crud.entity.Tag; +import org.cerberus.core.crud.entity.TagStatistic; +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.util.answer.AnswerList; + +import java.util.List; +import java.util.Map; +public interface ITagStatisticService { + + /** + * @param tag + * @return AnswerList that contains data from database + */ + AnswerList readByTag(String tag); + + /** + * Initialize TagStatistics objects + * @param tag + * @param executions + * @return Map with TagStatistics initialized + */ + Map initTagStatistics(Tag tag, List executions); + + /** + * Populate TagStatistics objects with aggregated data + * @param tagStatistics + * @param executions + * @param tag + */ + void populateTagStatisticsMap(Map tagStatistics, List executions, Tag tag); + +} diff --git a/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java b/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java index 9b433a50a..b022a1172 100644 --- a/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java +++ b/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java @@ -22,15 +22,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.cerberus.core.crud.dao.ITagDAO; -import org.cerberus.core.crud.entity.Campaign; -import org.cerberus.core.crud.entity.EventHook; -import org.cerberus.core.crud.entity.Tag; -import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.crud.entity.*; import org.cerberus.core.crud.factory.IFactoryTag; -import org.cerberus.core.crud.service.ICampaignService; -import org.cerberus.core.crud.service.ITagService; -import org.cerberus.core.crud.service.ITestCaseExecutionQueueService; -import org.cerberus.core.crud.service.ITestCaseExecutionService; +import org.cerberus.core.crud.service.*; import org.cerberus.core.engine.entity.MessageGeneral; import org.cerberus.core.enums.MessageEventEnum; import org.cerberus.core.enums.MessageGeneralEnum; @@ -81,6 +75,10 @@ public class TagService implements ITagService { private IEventService eventService; @Autowired private ICampaignService campaignService; + @Autowired + private IParameterService parameterService; + @Autowired + private ITagStatisticService tagStatisticService; private static final Logger LOG = LogManager.getLogger("TagService"); @@ -221,6 +219,17 @@ public Answer updateEndOfQueueData(String tag) { } } + //TagStatistics, only if it's a campaign and if parameter is activated + if (StringUtil.isNotEmpty(mytag.getCampaign()) && parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_featureflipping_tagstatistics_enable, "", false )) { + LOG.info("TagStatistics creation for tag {} started.", tag); + tagStatisticService.populateTagStatisticsMap( + tagStatisticService.initTagStatistics(mytag, executions), + executions, + mytag + ); + LOG.info("TagStatistics creation for tag {} finished.", tag); + } + return tagDAO.updateDateEndQueue(mytag); } catch (CerberusException ex) { @@ -309,7 +318,7 @@ public Answer createAuto(String tagS, String campaign, String user, JSONArray re @Override public String enrichTagWithCloudProviderBuild(String provider, String system, String tagS, String user, String pass) { - LOG.debug("Trying to enrish tag '" + tagS + "' with Cloud service provider Build (" + provider + ")."); + LOG.debug("Trying to enrish tag '{}' with Cloud service provider Build ({}).", tagS, provider); if (StringUtil.isEmpty(tagS)) { return null; } @@ -320,10 +329,10 @@ public String enrichTagWithCloudProviderBuild(String provider, String system, St case TestCaseExecution.ROBOTPROVIDER_BROWSERSTACK: if ((tag != null) && (StringUtil.isEmpty(tag.getBrowserstackBuildHash()) || "BSHash".equalsIgnoreCase(tag.getBrowserstackBuildHash()))) { String newBuildHash = browserstackService.getBrowserStackBuildHashFromEndpoint(system, tagS, user, pass, "api.browserstack.com/automate/builds.json"); - LOG.debug("Result : " + newBuildHash); + LOG.debug("Result : {}", newBuildHash); tag.setBrowserstackBuildHash(newBuildHash); newBuildHash = browserstackService.getBrowserStackBuildHashFromEndpoint(system, tagS, user, pass, "api.browserstack.com/app-automate/builds.json"); - LOG.debug("Result : " + newBuildHash); + LOG.debug("Result : {}", newBuildHash); tag.setBrowserstackAppBuildHash(newBuildHash); Answer ans = tagDAO.updateBrowserStackBuild(tagS, tag); return newBuildHash; @@ -385,13 +394,13 @@ public void manageCampaignEndOfExecution(String tag) throws CerberusException { AnswerList answerListQueue = new AnswerList<>(); answerListQueue = executionQueueService.readQueueOpen(tag); if (answerListQueue.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && (answerListQueue.getDataList().isEmpty())) { - LOG.debug("No More executions (in queue or running) on tag : " + tag + " - " + answerListQueue.getDataList().size() + " " + answerListQueue.getMessageCodeString() + " - "); + LOG.debug("No More executions (in queue or running) on tag : {} - {} {}", tag, answerListQueue.getDataList().size(), answerListQueue.getMessageCodeString()); this.updateEndOfQueueData(tag); } else { - LOG.debug("Still executions in queue on tag : " + tag + " - " + answerListQueue.getDataList().size() + " " + answerListQueue.getMessageCodeString()); + LOG.debug("Still executions in queue on tag : {} - {} {}", tag, answerListQueue.getDataList().size(), answerListQueue.getMessageCodeString()); } } else { - LOG.debug("Tag is already flaged with recent timestamp. " + currentTag.getDateEndQueue()); + LOG.debug("Tag is already flagged with recent timestamp. {}", currentTag.getDateEndQueue()); } } @@ -413,7 +422,7 @@ public void manageCampaignStartOfExecution(String tag, Timestamp startOfExecutio currentTag.setDateStartExe(startOfExecution); tagDAO.updateDateStartExe(currentTag); } else { - LOG.debug("Tag is already flaged with recent start of exe timestamp. " + currentTag.getDateStartExe()); + LOG.debug("Tag is already flaged with recent start of exe timestamp. {}", currentTag.getDateStartExe()); } } @@ -474,8 +483,7 @@ public String formatResult(Tag tag) { } JSONObject getJSONEnriched() { - JSONObject result = new JSONObject(); - return result; + return new JSONObject(); } } diff --git a/source/src/main/java/org/cerberus/core/crud/service/impl/TagStatisticService.java b/source/src/main/java/org/cerberus/core/crud/service/impl/TagStatisticService.java new file mode 100644 index 000000000..aeb4bc774 --- /dev/null +++ b/source/src/main/java/org/cerberus/core/crud/service/impl/TagStatisticService.java @@ -0,0 +1,211 @@ +/** + * Cerberus Copyright (C) 2013 - 2017 cerberustesting + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This file is part of Cerberus. + * + * Cerberus is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cerberus is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cerberus. If not, see . + */ +package org.cerberus.core.crud.service.impl; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.cerberus.core.crud.dao.ITagStatisticDAO; +import org.cerberus.core.crud.entity.Tag; +import org.cerberus.core.crud.entity.TagStatistic; +import org.cerberus.core.crud.entity.TestCaseExecution; +import org.cerberus.core.crud.service.ICampaignService; +import org.cerberus.core.crud.service.ITagStatisticService; +import org.cerberus.core.exception.CerberusException; +import org.cerberus.core.util.answer.AnswerList; +import org.json.JSONArray; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class TagStatisticService implements ITagStatisticService { + + private static final Logger LOG = LogManager.getLogger("TagStatisticService"); + + @Autowired + private ITagStatisticDAO tagStatisticDAO; + + @Autowired + private ICampaignService campaignService; + + /** + * @param tag + * @return AnswerList that contains data from database + */ + public AnswerList readByTag(String tag) { + return tagStatisticDAO.readByTag(tag); + } + + /** + * Initialize TagStatistics objects + * @param tag + * @param executions + * @return Map with TagStatistics initialized + */ + public HashMap initTagStatistics(Tag tag, List executions) { + + String campaignGroup1 = getCampaignGroup1(tag.getCampaign()); + HashMap tagStatistics = new HashMap<>(); + + for (TestCaseExecution execution : executions) { + String key = String.format("%s_%s", execution.getEnvironment(), execution.getCountry()); + tagStatistics.computeIfAbsent(key, k -> TagStatistic.builder() + .tag(tag.getTag()) + .country(execution.getCountry()) + .environment(execution.getEnvironment()) + .campaign(tag.getCampaign()) + .dateStartExe(new Timestamp(0)) + .dateEndExe(new Timestamp(0)) + .campaignGroup1(campaignGroup1) + .executions(new ArrayList<>()) + .build()); + tagStatistics.get(key).getExecutions().add(execution); + } + return tagStatistics; + } + + /** + * Populate TagStatistics objects with aggregated data + * @param tagStatistics + * @param executions + * @param tag + */ + public void populateTagStatisticsMap(Map tagStatistics, List executions, Tag tag) { + for (Map.Entry tagStatisticEntry : tagStatistics.entrySet()) { + TagStatistic tagStatistic = tagStatisticEntry.getValue(); + List systems = new ArrayList<>(); + List applications = new ArrayList<>(); + + for (TestCaseExecution execution : tagStatistic.getExecutions()) { + calculateExecutionDates(tagStatistic, execution); + calculateNumberExecutionsByStatus(tagStatistic, execution); + populateSystemList(systems, execution); + populateApplicationList(applications, execution); + } + tagStatistic.setSystemList(new JSONArray(systems).toString()); + tagStatistic.setApplicationList(new JSONArray(applications).toString()); + tagStatistic.setUsrCreated(tag.getUsrCreated()); + tagStatistic.setExecutions(null); + } + tagStatisticDAO.createWithMap(tagStatistics); + } + + /** + * Used to calculate the start and end date for each TagStatistic + * @param tagStatistic + * @param execution + */ + private void calculateExecutionDates(TagStatistic tagStatistic, TestCaseExecution execution) { + if (tagStatistic.getDateStartExe().getTime() == 0 || (execution.getStart() < tagStatistic.getDateStartExe().getTime())) { + tagStatistic.setDateStartExe(new Timestamp(execution.getStart())); + } + if (tagStatistic.getDateEndExe().getTime() == 0 ||(execution.getEnd() > tagStatistic.getDateEndExe().getTime())) { + tagStatistic.setDateEndExe(new Timestamp(execution.getEnd())); + } + } + + /** + * Used to calculate number of executions by status for each TagStatistics + * @param tagStatistic + * @param execution + */ + 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)); + + switch(execution.getControlStatus()) { + case "OK": + tagStatistic.incrementNbOK(); + break; + case "KO": + tagStatistic.incrementNbKO(); + break; + case "FA": + tagStatistic.incrementNbFA(); + break; + case "NA": + tagStatistic.incrementNbNA(); + break; + case "NE": + tagStatistic.incrementNbNE(); + break; + case "WE": + tagStatistic.incrementNbWE(); + break; + case "PE": + tagStatistic.incrementNbPE(); + break; + case "QU": + tagStatistic.incrementNbQU(); + break; + case "QE": + tagStatistic.incrementNbQE(); + break; + case "CA": + tagStatistic.incrementNbCA(); + break; + default: + break; + } + } + + /** + * Populate the systems list for each TagStatistic object + * @param systems + * @param execution + */ + private void populateSystemList(List systems, TestCaseExecution execution) { + if (!systems.contains(execution.getSystem())) { + systems.add(execution.getSystem()); + } + } + + /** + * Populate the applications list for each TagStatistic object + * @param applications + * @param execution + */ + private void populateApplicationList(List applications, TestCaseExecution execution) { + if (!applications.contains(execution.getApplication())) { + applications.add(execution.getApplication()); + } + } + + /** + * Retrieve campaign group 1 + * @param campaign + * @return + */ + private String getCampaignGroup1(String campaign) { + String campaignGroup1 = ""; + try { + campaignGroup1 = campaignService.convert(campaignService.readByKey(campaign)).getGroup1(); + } catch (CerberusException exception) { + LOG.error("Unable to get campaign owner: ", exception); + } + return campaignGroup1; + } +} diff --git a/source/src/main/resources/database.sql b/source/src/main/resources/database.sql index 3b1daf790..de9a16038 100644 --- a/source/src/main/resources/database.sql +++ b/source/src/main/resources/database.sql @@ -6369,3 +6369,35 @@ ALTER TABLE testdatalib ADD IgnoreFirstLine BOOLEAN NOT NULL AFTER `Separator`; INSERT INTO `parameter` (`system`, `param`, `value`, `description`) VALUES ('', 'cerberus_homepage_nbdisplayedscheduledtag', '3', 'Number of scheduled not yet executed tag displayed inside homepage.'); +-- 1800 +CREATE TABLE `tagstatistic` ( + `Id` INT NOT NULL AUTO_INCREMENT, + `Tag` VARCHAR(255) NOT NULL, + `Country` VARCHAR(45) NOT NULL, + `Environment` VARCHAR(45) NOT NULL, + `Campaign` VARCHAR(200) NOT NULL, + `CampaignGroup1` VARCHAR(45), + `SystemList` text NOT NULL, + `ApplicationList` text NOT NULL, + `DateStartExe` TIMESTAMP NOT NULL, + `DateEndExe` TIMESTAMP NOT NULL, + `NbExe` INT DEFAULT 0, + `NbExeUsefull` INT DEFAULT 0, + `NbOK` int DEFAULT 0, + `NbKO` int DEFAULT 0, + `NbFA` int DEFAULT 0, + `NbNA` int DEFAULT 0, + `NbNE` int DEFAULT 0, + `NbWE` int DEFAULT 0, + `NbPE` int DEFAULT 0, + `NbQU` int DEFAULT 0, + `NbQE` int DEFAULT 0, + `NbCA` int DEFAULT 0, + `UsrCreated` VARCHAR(45) NOT NULL DEFAULT '', + `DateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `UsrModif` VARCHAR(45) NOT NULL DEFAULT '', + `DateModif` timestamp NOT NULL DEFAULT '1970-01-01 01:01:01', + PRIMARY KEY (`Id`), + UNIQUE KEY `tag_stat_unique` (`Tag`, `Country`, `Environment`)) + ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; +