Skip to content

Commit

Permalink
EPMRPP-88736 code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx committed Jul 3, 2024
1 parent 7f7b5c4 commit 6b5b6b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package com.epam.ta.reportportal.core.analytics;

import com.epam.ta.reportportal.entity.analytics.AnalyticsData;

public interface DefectUpdateStatisticsService {

AnalyticsData saveAnalyzedDefectStatistics(int amountToAnalyze, int analyzedAmount, int userAnalyzedAmount,
void saveAnalyzedDefectStatistics(int amountToAnalyze, int analyzedAmount, int userAnalyzedAmount,
Long projectId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.epam.ta.reportportal.core.analyzer.auto.client.AnalyzerServiceClient;
import com.epam.ta.reportportal.dao.AnalyticsDataRepository;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.dao.ServerSettingsRepository;
import com.epam.ta.reportportal.entity.Metadata;
import com.epam.ta.reportportal.entity.analytics.AnalyticsData;
import com.epam.ta.reportportal.entity.project.Project;
Expand All @@ -47,23 +48,27 @@ public class DefectUpdateStatisticsServiceImpl implements DefectUpdateStatistics

private static final Logger LOGGER = LoggerFactory.getLogger(
DefectUpdateStatisticsServiceImpl.class);
public static final String SERVER_ANALYTICS_ALL = "server.analytics.all";

private final String buildVersion;

private final AnalyticsDataRepository analyticsDataRepository;
private final AnalyzerServiceClient analyzerServicesClient;
private final ProjectRepository projectRepository;
private final ServerSettingsRepository serverSettingsRepository;


@Autowired
public DefectUpdateStatisticsServiceImpl(
@Value("${info.build.version:unknown}") String buildVersion,
AnalyticsDataRepository analyticsDataRepository,
AnalyzerServiceClient analyzerServicesClient,
ProjectRepository projectRepository) {
ProjectRepository projectRepository, ServerSettingsRepository serverSettingsRepository) {
this.buildVersion = buildVersion;
this.analyzerServicesClient = analyzerServicesClient;
this.analyticsDataRepository = analyticsDataRepository;
this.projectRepository = projectRepository;
this.serverSettingsRepository = serverSettingsRepository;
}


Expand All @@ -74,21 +79,32 @@ public DefectUpdateStatisticsServiceImpl(
* @param analyzedAmount The amount of items that have been analyzed.
* @param userAnalyzedAmount The amount of items that have been analyzed by the user.
* @param projectId The ID of the project.
* @return The saved AnalyticsData instance.
*/
@Override
public AnalyticsData saveAnalyzedDefectStatistics(int amountToAnalyze, int analyzedAmount,
public void saveAnalyzedDefectStatistics(int amountToAnalyze, int analyzedAmount,
int userAnalyzedAmount, Long projectId) {
var map = getMapWithCommonParameters(projectId);
map.put("sentToAnalyze", amountToAnalyze);
map.put("analyzed", analyzedAmount);
map.put("userAnalyzed", userAnalyzedAmount);

AnalyticsData ad = new AnalyticsData();
ad.setType(DEFECT_UPDATE_STATISTICS.name());
ad.setCreatedAt(Instant.now());
ad.setMetadata(new Metadata(map));
return analyticsDataRepository.save(ad);
if (isAnalyticsGatheringAllowed()) {
var map = getMapWithCommonParameters(projectId);
map.put("sentToAnalyze", amountToAnalyze);
map.put("analyzed", analyzedAmount);
map.put("userAnalyzed", userAnalyzedAmount);

AnalyticsData ad = new AnalyticsData();
ad.setType(DEFECT_UPDATE_STATISTICS.name());
ad.setCreatedAt(Instant.now());
ad.setMetadata(new Metadata(map));
analyticsDataRepository.save(ad);
}

}

private boolean isAnalyticsGatheringAllowed() {
return serverSettingsRepository.selectServerSettings()
.stream()
.filter(setting -> setting.getKey().equals(SERVER_ANALYTICS_ALL))
.findFirst()
.map(serverSettings -> Boolean.parseBoolean(serverSettings.getValue()))
.orElse(false);
}

private Map<String, Object> getMapWithCommonParameters(Long projectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.epam.ta.reportportal.entity.AnalyzeMode.ALL_LAUNCHES;
import static com.epam.ta.reportportal.entity.enums.TestItemIssueGroup.PRODUCT_BUG;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.anyLong;
Expand All @@ -40,7 +39,6 @@
import com.epam.ta.reportportal.core.item.impl.IssueTypeHandler;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.dao.TestItemRepository;
import com.epam.ta.reportportal.entity.analytics.AnalyticsData;
import com.epam.ta.reportportal.entity.enums.LogLevel;
import com.epam.ta.reportportal.entity.enums.StatusEnum;
import com.epam.ta.reportportal.entity.item.TestItem;
Expand Down Expand Up @@ -128,9 +126,6 @@ void analyze() {
issuesAnalyzer.runAnalyzers(launch,
items.stream().map(TestItem::getItemId).collect(Collectors.toList()), analyzerConfig
);
when(defectUpdateStatisticsService.saveAnalyzedDefectStatistics(anyInt(), anyInt(), eq(0),
anyLong())).thenReturn(new AnalyticsData());


verify(analyzerServiceClient, times(1)).analyze(any());
verify(testItemRepository, times(itemsCount)).save(any());
Expand Down

0 comments on commit 6b5b6b1

Please sign in to comment.