All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.testvagrant.monitor.services.BuildsServiceImpl Maven / Gradle / Ivy

The newest version!
package com.testvagrant.monitor.services;

import com.google.gson.GsonBuilder;
import com.testvagrant.monitor.clients.BuildsClient;
import com.testvagrant.monitor.clients.DevicesClient;
import com.testvagrant.monitor.clients.ScenariosClient;
import com.testvagrant.monitor.entities.CrashData;
import com.testvagrant.monitor.requests.Build;
import com.testvagrant.monitor.requests.Device;
import com.testvagrant.monitor.requests.Scenario;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.testvagrant.monitor.constants.MongoDB.STATUS_PASSED;

public class BuildsServiceImpl extends OptimusServiceImpl implements BuildsService {
    @Override
    public void notifyBuildStart() {
        new BuildsClient().createNewBuild();
    }


    public void notifyBuildStart(String buildNo) {
        new BuildsClient().createNewBuild(buildNo);
    }

    @Override
    public void notifyBuildEnd() {
        Build buildToUpdate = new BuildsClient().findBuildById(getLatestBuild());
        Integer buildScenarioCount = new ScenariosClient().getBuildScenarioCount(getLatestBuild());
        Integer buildScenarioCountByStatus = new ScenariosClient().getBuildScenarioCountByStatus(getLatestBuild(), "passed");
        buildToUpdate.setBuildEndTime(new Date());
        buildToUpdate.setBuildScenarios(buildScenarioCount);
        buildToUpdate.setBuildSuccessRate(buildScenarioCountByStatus);
        new BuildsClient().updateBuildRecord(buildToUpdate);
    }

    @Override
    public void updateBuildWithUniqueScenarios() {
        List distinctScenarios = new ScenariosClient().getDistinctScenarios();
        int numberOfUniqueScenarios = distinctScenarios.size();
        int passedScenariosCount = distinctScenarios.stream().filter(scenario -> {
            boolean status = false;
            try {
                status = scenario.getStatus().equalsIgnoreCase(STATUS_PASSED);
            } catch (Exception e) {

            }
            return status;
        }).collect(Collectors.toList()).size();
        float pass_percentage = (passedScenariosCount * 100.0f) / numberOfUniqueScenarios;
        DecimalFormat df = new DecimalFormat("#.0");
        String passRate = df.format(pass_percentage);
        Build buildById = new BuildsClient().findBuildById(getLatestBuild());
        buildById.setScenariosCount(numberOfUniqueScenarios);
        buildById.setScenarioSuccessRate(passRate);

        new BuildsClient().updateBuildRecord(buildById);
    }

    @Override
    public void createCrashCollection() {
        List crashDataList = new ArrayList<>();
        List uniqueScenariosByDevice = new ScenariosClient().getDistinctScenariosByDeviceId(getLatestBuild());
        uniqueScenariosByDevice.forEach(scenario -> {
            if (scenario.getActivity() != null) {
                Map> stackTraceMap = new HashMap<>();
                CrashData crashData = new CrashData();
//                crashData.setUdid(scenario.getDeviceUdid());
                Device deviceById = new DevicesClient().getDeviceById(scenario.getDeviceId());
                String deviceId = deviceById.getId();
                crashData.setUdid(deviceById.getUdid());
                List scenariosByActivity = new ScenariosClient().getScenariosByActivity(getLatestBuild(), deviceId, scenario.getActivity());
                List stackTraceList = new ArrayList<>();
                scenariosByActivity.forEach(scenarioByActivity -> {
                    stackTraceList.add(scenarioByActivity.getStacktrace());
                });
                stackTraceMap.put(scenario.getActivity(), stackTraceList);
                crashData.setActivityStacktraceMap(stackTraceMap);
                crashDataList.add(crashData);
            }

        });
        if (crashDataList.size() > 0) {
            Build build = new BuildsClient().findBuildById(getLatestBuild());
            build.setCrashlytics(new GsonBuilder().disableHtmlEscaping().create().toJson(crashDataList));
            new BuildsClient().updateBuildRecord(build);
        }
    }

    public void updateBuildRunMode(String runMode) {
        Build buildById = new BuildsClient().findBuildById(getLatestBuild());
        buildById.setRunMode(runMode);
        new BuildsClient().updateBuildRecord(buildById);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy