<commit_before>package com.griddynamics.jagger.master;

import com.griddynamics.jagger.coordinator.NodeContext;
import com.griddynamics.jagger.coordinator.NodeId;
import com.griddynamics.jagger.dbapi.entity.DecisionPerMetricEntity;
import com.griddynamics.jagger.dbapi.entity.DecisionPerTaskEntity;
import com.griddynamics.jagger.dbapi.entity.MetricDescriptionEntity;
import com.griddynamics.jagger.dbapi.entity.TaskData;
import com.griddynamics.jagger.dbapi.util.SessionMatchingSetup;
import com.griddynamics.jagger.engine.e1.ProviderUtil;
import com.griddynamics.jagger.engine.e1.collector.limits.*;
import com.griddynamics.jagger.engine.e1.collector.testgroup.TestGroupDecisionMakerInfo;
import com.griddynamics.jagger.engine.e1.scenario.WorkloadTask;
import com.griddynamics.jagger.engine.e1.services.DataService;
import com.griddynamics.jagger.engine.e1.services.DefaultDataService;
import com.griddynamics.jagger.engine.e1.services.JaggerPlace;
import com.griddynamics.jagger.engine.e1.services.data.service.MetricEntity;
import com.griddynamics.jagger.engine.e1.services.data.service.MetricSummaryValueEntity;
import com.griddynamics.jagger.engine.e1.services.data.service.TestEntity;
import com.griddynamics.jagger.util.Decision;
import com.griddynamics.jagger.engine.e1.collector.testgroup.TestGroupDecisionMakerListener;
import com.griddynamics.jagger.engine.e1.sessioncomparation.WorstCaseDecisionMaker;
import com.griddynamics.jagger.master.configuration.Task;
import org.hibernate.*;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.*;

public class DecisionMakerDistributionListener extends HibernateDaoSupport implements DistributionListener {
    private static final Logger log = LoggerFactory.getLogger(DecisionMakerDistributionListener.class);

    private NodeContext nodeContext;
    private WorstCaseDecisionMaker worstCaseDecisionMaker = new WorstCaseDecisionMaker();

    public DecisionMakerDistributionListener() {}

    public void setNodeContext(NodeContext nodeContext) {
        this.nodeContext = nodeContext;
    }

    @Override
    public void onDistributionStarted(String sessionId, String taskId, Task task, Collection<NodeId> capableNodes) {
        //do nothing
    }

    @Override
    public void onTaskDistributionCompleted(String sessionId, String taskId, Task task) {

        if (task instanceof CompositeTask) {
            TestGroupDecisionMakerListener decisionMakerListener = TestGroupDecisionMakerListener.Composer.compose(ProviderUtil.provideElements(((CompositeTask) task).getDecisionMakerListeners(),
                    sessionId,
                    taskId,
                    nodeContext,
                    JaggerPlace.TEST_GROUP_DECISION_MAKER_LISTENER));

            DataService dataService = new DefaultDataService(nodeContext);

            // Get tests in test group
            CompositeTask compositeTask = (CompositeTask) task;
            List<WorkloadTask> workloadTasks = new ArrayList<WorkloadTask>();
            for (CompositableTask compositableTask : compositeTask.getAttendant()) {
                if (compositableTask instanceof WorkloadTask) {
                    workloadTasks.add((WorkloadTask) compositableTask);
                }
            }
            for (CompositableTask compositableTask : compositeTask.getLeading()) {
                if (compositableTask instanceof WorkloadTask) {
                    workloadTasks.add((WorkloadTask) compositableTask);
                }
            }

            // Make decision per tests
            Set<DecisionPerTest> decisionsPerTest = new HashSet<DecisionPerTest>();

            for (WorkloadTask workloadTask : workloadTasks) {
                if (workloadTask.getLimits() != null) {
                    String testName = workloadTask.getName();

                    // Get data for current session
                    TestEntity testEntity = dataService.getTestByName(sessionId,testName);
                    Set<MetricEntity> metricEntitySet = dataService.getMetrics(testEntity);
                    Map<MetricEntity,MetricSummaryValueEntity> metricValues = dataService.getMetricSummary(metricEntitySet);

                    Map<String,MetricEntity> idToEntity = new HashMap<String, MetricEntity>();
                    for (MetricEntity metricEntity : metricValues.keySet()) {
                        idToEntity.put(metricEntity.getMetricId(),metricEntity);
                    }

                    // Get relation limit <-> metrics
                    boolean needBaselineSessionValue = false;
                    Map<Limit,Set<MetricEntity>> limitToEntity = new HashMap<Limit, Set<MetricEntity>>();
                    for (Limit limit : workloadTask.getLimits().getLimits()) {
                        limitToEntity.put(limit,getMetricsForLimit(limit, idToEntity));
                        if (!limitToEntity.get(limit).isEmpty()) {
                            if (limit.getRefValue() == null) {
                                needBaselineSessionValue = true;
                            }
                        }
                    }

                    // Get data for baseline session
                    Map<String,Double> metricIdToValuesBaseline = new HashMap<String, Double>();
                    if (needBaselineSessionValue) {
                        String baselineId = workloadTask.getLimits().getBaselineId();
                        TestEntity testEntityBaseline = null;

                        // Strategy to match sessions - we will use baseline only when all test parameters are matching
                        SessionMatchingSetup sessionMatchingSetup = new SessionMatchingSetup(true,
                                EnumSet.of(SessionMatchingSetup.MatchBy.ALL));
                        DefaultDataService defaultDataService = (DefaultDataService) dataService;
                        Map<String, Set<TestEntity>> matchingTestEntities =
                                defaultDataService.getTestsWithName(Arrays.asList(sessionId,baselineId), testName, sessionMatchingSetup);
                        if (!matchingTestEntities.get(baselineId).isEmpty()) {
                            testEntityBaseline = matchingTestEntities.get(baselineId).iterator().next();
                        }

                        if (testEntityBaseline != null) {
                            Set<MetricEntity> metricEntitySetBaseline = dataService.getMetrics(testEntityBaseline);
                            Map<MetricEntity,MetricSummaryValueEntity> metricValuesBaseline = dataService.getMetricSummary(metricEntitySetBaseline);
                            for (Map.Entry<MetricEntity,MetricSummaryValueEntity> entry : metricValuesBaseline.entrySet()) {
                                metricIdToValuesBaseline.put(entry.getKey().getMetricId(),entry.getValue().getValue());
                            }
                        }
                        else {
                            log.error("Was not able to find matching test {} in baseline session {}",testName,baselineId);
                        }
                    }
<commit_msg>changes after review<commit_after>package com.griddynamics.jagger.master;

import com.griddynamics.jagger.coordinator.NodeContext;
import com.griddynamics.jagger.coordinator.NodeId;
import com.griddynamics.jagger.dbapi.entity.DecisionPerMetricEntity;
import com.griddynamics.jagger.dbapi.entity.DecisionPerTaskEntity;
import com.griddynamics.jagger.dbapi.entity.MetricDescriptionEntity;
import com.griddynamics.jagger.dbapi.entity.TaskData;
import com.griddynamics.jagger.dbapi.util.SessionMatchingSetup;
import com.griddynamics.jagger.engine.e1.ProviderUtil;
import com.griddynamics.jagger.engine.e1.collector.limits.*;
import com.griddynamics.jagger.engine.e1.collector.testgroup.TestGroupDecisionMakerInfo;
import com.griddynamics.jagger.engine.e1.scenario.WorkloadTask;
import com.griddynamics.jagger.engine.e1.services.DataService;
import com.griddynamics.jagger.engine.e1.services.DefaultDataService;
import com.griddynamics.jagger.engine.e1.services.JaggerPlace;
import com.griddynamics.jagger.engine.e1.services.data.service.MetricEntity;
import com.griddynamics.jagger.engine.e1.services.data.service.MetricSummaryValueEntity;
import com.griddynamics.jagger.engine.e1.services.data.service.TestEntity;
import com.griddynamics.jagger.util.Decision;
import com.griddynamics.jagger.engine.e1.collector.testgroup.TestGroupDecisionMakerListener;
import com.griddynamics.jagger.engine.e1.sessioncomparation.WorstCaseDecisionMaker;
import com.griddynamics.jagger.master.configuration.Task;
import org.hibernate.*;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.*;

public class DecisionMakerDistributionListener extends HibernateDaoSupport implements DistributionListener {
    private static final Logger log = LoggerFactory.getLogger(DecisionMakerDistributionListener.class);

    private NodeContext nodeContext;
    private WorstCaseDecisionMaker worstCaseDecisionMaker = new WorstCaseDecisionMaker();

    public DecisionMakerDistributionListener() {}

    public void setNodeContext(NodeContext nodeContext) {
        this.nodeContext = nodeContext;
    }

    @Override
    public void onDistributionStarted(String sessionId, String taskId, Task task, Collection<NodeId> capableNodes) {
        //do nothing
    }

    @Override
    public void onTaskDistributionCompleted(String sessionId, String taskId, Task task) {

        if (task instanceof CompositeTask) {
            TestGroupDecisionMakerListener decisionMakerListener = TestGroupDecisionMakerListener.Composer.compose(ProviderUtil.provideElements(((CompositeTask) task).getDecisionMakerListeners(),
                    sessionId,
                    taskId,
                    nodeContext,
                    JaggerPlace.TEST_GROUP_DECISION_MAKER_LISTENER));

            DefaultDataService dataService = new DefaultDataService(nodeContext);

            // Get tests in test group
            CompositeTask compositeTask = (CompositeTask) task;
            List<WorkloadTask> workloadTasks = new ArrayList<WorkloadTask>();
            for (CompositableTask compositableTask : compositeTask.getAttendant()) {
                if (compositableTask instanceof WorkloadTask) {
                    workloadTasks.add((WorkloadTask) compositableTask);
                }
            }
            for (CompositableTask compositableTask : compositeTask.getLeading()) {
                if (compositableTask instanceof WorkloadTask) {
                    workloadTasks.add((WorkloadTask) compositableTask);
                }
            }

            // Make decision per tests
            Set<DecisionPerTest> decisionsPerTest = new HashSet<DecisionPerTest>();

            for (WorkloadTask workloadTask : workloadTasks) {
                if (workloadTask.getLimits() != null) {
                    String testName = workloadTask.getName();

                    // Get data for current session
                    TestEntity testEntity = dataService.getTestByName(sessionId,testName);
                    Set<MetricEntity> metricEntitySet = dataService.getMetrics(testEntity);
                    Map<MetricEntity,MetricSummaryValueEntity> metricValues = dataService.getMetricSummary(metricEntitySet);

                    Map<String,MetricEntity> idToEntity = new HashMap<String, MetricEntity>();
                    for (MetricEntity metricEntity : metricValues.keySet()) {
                        idToEntity.put(metricEntity.getMetricId(),metricEntity);
                    }

                    // Get relation limit <-> metrics
                    boolean needBaselineSessionValue = false;
                    Map<Limit,Set<MetricEntity>> limitToEntity = new HashMap<Limit, Set<MetricEntity>>();
                    for (Limit limit : workloadTask.getLimits().getLimits()) {
                        limitToEntity.put(limit,getMetricsForLimit(limit, idToEntity));
                        if (!limitToEntity.get(limit).isEmpty()) {
                            if (limit.getRefValue() == null) {
                                needBaselineSessionValue = true;
                            }
                        }
                    }

                    // Get data for baseline session
                    Map<String,Double> metricIdToValuesBaseline = new HashMap<String, Double>();
                    if (needBaselineSessionValue) {
                        String baselineId = workloadTask.getLimits().getBaselineId();
                        TestEntity testEntityBaseline = null;

                        // Strategy to match sessions - we will use baseline only when all test parameters are matching
                        SessionMatchingSetup sessionMatchingSetup = new SessionMatchingSetup(true,
                                EnumSet.of(SessionMatchingSetup.MatchBy.ALL));
                        Map<String, Set<TestEntity>> matchingTestEntities =
                                dataService.getTestsWithName(Arrays.asList(sessionId,baselineId), testName, sessionMatchingSetup);
                        if (!matchingTestEntities.get(baselineId).isEmpty()) {
                            testEntityBaseline = matchingTestEntities.get(baselineId).iterator().next();
                        }

                        if (testEntityBaseline != null) {
                            Set<MetricEntity> metricEntitySetBaseline = dataService.getMetrics(testEntityBaseline);
                            Map<MetricEntity,MetricSummaryValueEntity> metricValuesBaseline = dataService.getMetricSummary(metricEntitySetBaseline);
                            for (Map.Entry<MetricEntity,MetricSummaryValueEntity> entry : metricValuesBaseline.entrySet()) {
                                metricIdToValuesBaseline.put(entry.getKey().getMetricId(),entry.getValue().getValue());
                            }
                        }
                        else {
                            log.error("Was not able to find matching test {} in baseline session {}",testName,baselineId);
                        }
                    }

{'fraction_non_alphanumeric': 0.08009358012798459, 'fraction_numerical': 0.002201885364343219, 'mean_word_length': 2.4803639846743293, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4560582', 'n_tokens_mistral': 3669, 'n_tokens_neox': 3467, 'n_words': 608}