<commit_before>package edu.kit.ipd.crowdcontrol.workerservice.query;

import edu.kit.ipd.crowdcontrol.workerservice.InternalServerErrorException;
import edu.kit.ipd.crowdcontrol.workerservice.RequestHelper;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.enums.ExperimentsPlatformModeMode;
import edu.kit.ipd.crowdcontrol.workerservice.*;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.CalibrationAnswerOptionRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.CalibrationRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.ExperimentRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.operations.*;
import edu.kit.ipd.crowdcontrol.workerservice.objectservice.Communication;
import edu.kit.ipd.crowdcontrol.workerservice.proto.Experiments;
import edu.kit.ipd.crowdcontrol.workerservice.proto.View;
import org.jooq.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.handling.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * The class query is responsible for the query-part of the CQRS-pattern. Therefore it provides the getNext-method used
 * for the /next query and getPreview used for the /preview-query.
 *
 * @author LeanderK
 * @version 1.0
 */
public class Queries implements RequestHelper {
    private static final Logger logger = LoggerFactory.getLogger(Queries.class);
    private final HashMap<String, TaskChooserAlgorithm> strategies = new HashMap<>();
    private final CalibrationsOperations calibrationsOperations;
    private final ExperimentOperations experimentOperations;
    private final PlatformOperations platformOperations;
    private final ExperimentsPlatformOperations experimentsPlatformOperations;
    private final WorkerOperations workerOperations;
    private final Communication communication;
    private final PreviewTaskChooser previewTaskChooser;
    private final JWTHelper jwtHelper;

    public Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
                   PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
                   WorkerOperations workerOperations, JWTHelper jwtHelper) {
        this(calibrationsOperations, experimentOperations, platformOperations, communication, experimentsPlatformOperations, workerOperations, false, jwtHelper);
    }

    public Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
                   PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
                   WorkerOperations workerOperations, boolean disableRegistering, JWTHelper jwtHelper) {
        this.calibrationsOperations = calibrationsOperations;
        this.experimentOperations = experimentOperations;
        this.platformOperations = platformOperations;
        this.communication = communication;
        this.experimentsPlatformOperations = experimentsPlatformOperations;
        this.workerOperations = workerOperations;
        this.jwtHelper = jwtHelper;
        previewTaskChooser = new PreviewTaskChooser(experimentOperations, experimentsPlatformOperations);
        if (!disableRegistering) {
            registerTaskChooser(new AntiSpoof(experimentOperations, experimentsPlatformOperations));
        }
    }

    Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
            PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
            TaskChooserAlgorithm mockUp, WorkerOperations workerOperations, JWTHelper jwtHelper) {
        this(calibrationsOperations, experimentOperations, platformOperations, communication, experimentsPlatformOperations, workerOperations, false, jwtHelper);
        if (mockUp != null)
            registerTaskChooser(mockUp);
    }

    /**
     * used to register a new TaskStrategy.
     *
     * @param taskChooserAlgorithm the TaskChooserAlgorithm to register
     */
    private void registerTaskChooser(TaskChooserAlgorithm taskChooserAlgorithm) {
        strategies.put(taskChooserAlgorithm.getName(), taskChooserAlgorithm);
        experimentOperations.insertTaskChooserOrIgnore(taskChooserAlgorithm.getName(), taskChooserAlgorithm.getDescription());
        taskChooserAlgorithm.getParameters()
                .forEach(parameter -> experimentOperations.insertTaskChooserParamOrIgnore(
                        taskChooserAlgorithm.getName(),
                        parameter.getDescription(),
                        parameter.getRegex(),
                        parameter.getData())
                );
    }

    /**
     * this method returns an overview over the assignment, an instance of view, where containing only information
     * about the experiment.
     * @param context the Context of the Request
     * @return the JSON-Representation of View
     */
    public View preview(Context context) {
        checkForExperimentAndPlatform(context);
        int experimentId = assertParameterInt(context, "experiment");
        logger.debug("generating preview for experiment: {}", experimentId);
        return previewTaskChooser.next(View.newBuilder(), context, experimentId, "", false, false)
                .orElseThrow(() -> new InternalServerErrorException("Unable to create Preview!"));
    }

    /**
     * this method returns an instance of View, determining what the worker should see next.
     * <p>
     * it is indented to be called when the Router gets an /next-Request.
     *
     * @param context the Context of the Request
     * @return the JSON-Representation of View
     */
    public View getNext(Context context) {<commit_msg>preview: delete check for experiment, this fixes coolcrowd/worker-ui#43<commit_after>package edu.kit.ipd.crowdcontrol.workerservice.query;

import edu.kit.ipd.crowdcontrol.workerservice.InternalServerErrorException;
import edu.kit.ipd.crowdcontrol.workerservice.RequestHelper;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.enums.ExperimentsPlatformModeMode;
import edu.kit.ipd.crowdcontrol.workerservice.*;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.CalibrationAnswerOptionRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.CalibrationRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.model.tables.records.ExperimentRecord;
import edu.kit.ipd.crowdcontrol.workerservice.database.operations.*;
import edu.kit.ipd.crowdcontrol.workerservice.objectservice.Communication;
import edu.kit.ipd.crowdcontrol.workerservice.proto.Experiments;
import edu.kit.ipd.crowdcontrol.workerservice.proto.View;
import org.jooq.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.handling.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * The class query is responsible for the query-part of the CQRS-pattern. Therefore it provides the getNext-method used
 * for the /next query and getPreview used for the /preview-query.
 *
 * @author LeanderK
 * @version 1.0
 */
public class Queries implements RequestHelper {
    private static final Logger logger = LoggerFactory.getLogger(Queries.class);
    private final HashMap<String, TaskChooserAlgorithm> strategies = new HashMap<>();
    private final CalibrationsOperations calibrationsOperations;
    private final ExperimentOperations experimentOperations;
    private final PlatformOperations platformOperations;
    private final ExperimentsPlatformOperations experimentsPlatformOperations;
    private final WorkerOperations workerOperations;
    private final Communication communication;
    private final PreviewTaskChooser previewTaskChooser;
    private final JWTHelper jwtHelper;

    public Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
                   PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
                   WorkerOperations workerOperations, JWTHelper jwtHelper) {
        this(calibrationsOperations, experimentOperations, platformOperations, communication, experimentsPlatformOperations, workerOperations, false, jwtHelper);
    }

    public Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
                   PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
                   WorkerOperations workerOperations, boolean disableRegistering, JWTHelper jwtHelper) {
        this.calibrationsOperations = calibrationsOperations;
        this.experimentOperations = experimentOperations;
        this.platformOperations = platformOperations;
        this.communication = communication;
        this.experimentsPlatformOperations = experimentsPlatformOperations;
        this.workerOperations = workerOperations;
        this.jwtHelper = jwtHelper;
        previewTaskChooser = new PreviewTaskChooser(experimentOperations, experimentsPlatformOperations);
        if (!disableRegistering) {
            registerTaskChooser(new AntiSpoof(experimentOperations, experimentsPlatformOperations));
        }
    }

    Queries(CalibrationsOperations calibrationsOperations, ExperimentOperations experimentOperations,
            PlatformOperations platformOperations, Communication communication, ExperimentsPlatformOperations experimentsPlatformOperations,
            TaskChooserAlgorithm mockUp, WorkerOperations workerOperations, JWTHelper jwtHelper) {
        this(calibrationsOperations, experimentOperations, platformOperations, communication, experimentsPlatformOperations, workerOperations, false, jwtHelper);
        if (mockUp != null)
            registerTaskChooser(mockUp);
    }

    /**
     * used to register a new TaskStrategy.
     *
     * @param taskChooserAlgorithm the TaskChooserAlgorithm to register
     */
    private void registerTaskChooser(TaskChooserAlgorithm taskChooserAlgorithm) {
        strategies.put(taskChooserAlgorithm.getName(), taskChooserAlgorithm);
        experimentOperations.insertTaskChooserOrIgnore(taskChooserAlgorithm.getName(), taskChooserAlgorithm.getDescription());
        taskChooserAlgorithm.getParameters()
                .forEach(parameter -> experimentOperations.insertTaskChooserParamOrIgnore(
                        taskChooserAlgorithm.getName(),
                        parameter.getDescription(),
                        parameter.getRegex(),
                        parameter.getData())
                );
    }

    /**
     * this method returns an overview over the assignment, an instance of view, where containing only information
     * about the experiment.
     * @param context the Context of the Request
     * @return the JSON-Representation of View
     */
    public View preview(Context context) {
        int experiment = assertParameterInt(context, "experiment");
        try {
            experimentOperations.getExperiment(experiment);
        } catch (ExperimentNotFoundException e) {
            throw new NotFoundException(String.format("Experiment %d not found", experiment));
        }
        int experimentId = assertParameterInt(context, "experiment");
        logger.debug("generating preview for experiment: {}", experimentId);
        return previewTaskChooser.next(View.newBuilder(), context, experimentId, "", false, false)
                .orElseThrow(() -> new InternalServerErrorException("Unable to create Preview!"));
    }

    /**
     * this method returns an instance of View, determining what the worker should see next.
     * <p>
     * it is indented to be called when the Router gets an /next-Request.
     *
     * @param context the Context of the Request
     * @return the JSON-Representation of View
     */
    public View getNext(Context context) {
{'fraction_non_alphanumeric': 0.07035458711908016, 'fraction_numerical': 0.000804052424218059, 'mean_word_length': 4.110106820049301, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5564866', 'n_tokens_mistral': 3029, 'n_tokens_neox': 3019, 'n_words': 712}