<commit_before><?php

namespace SynchWeb\Page\EM;

use SynchWeb\Page\EM\SchemaValidator;
use SynchWeb\Page\EM\RelionParameterTransformer;
use SynchWeb\Page\EM\RelionSchema;

trait Relion
{
    /**
     * Output the schema for client-side use
     *
     * @SuppressWarnings(PHPMD.StaticAccess)
     */
    public function relionSchema()
    {
        $schema = new RelionSchema();
        $this->_output($schema->clientSchema());
    }

    /**
     * Create a data collection and a processing job then enqueue the job
     *
     * @SuppressWarnings(PHPMD.LongVariable)
     */
    public function relionStart()
    {
        global $zocalo_mx_reprocess_queue;

        $this->configExitIfNoMicroscopes();

        $proposalCode = $this->arg('prop');
        $dataCollectionId = $this->arg('id');

        $session = $this->sessionFromDataCollection(
            $proposalCode,
            $dataCollectionId
        );

        $this->sessionExitIfNotActive($session);

        $dataCollection = $this->dataCollectionForProcessing(
            $proposalCode,
            $dataCollectionId
        );

        if ($dataCollection['runningJobs'] > 0) {
            $message = 'A Relion job is already running on this data collection!';
            error_log("$message - $proposalCode $dataCollectionId");
            $this->_error($message, 400);
        }

        $preparedData = $this->relionPreparePostData($dataCollection, $session);

        $processingJobId = $this->relionAddJob($dataCollectionId, $preparedData);

        // Send job to processing queue
        $message = array(
            'parameters' => array('ispyb_process' => $processingJobId)
        );

        $this->_send_zocalo_message($zocalo_mx_reprocess_queue, $message);

        $output = array(
            'timestamp' => gmdate('c', time()),
            'message' => $message
        );

        $this->_output($output);
    }

    /**
     * Request that a currently running job be stopped
     *
     * @SuppressWarnings(PHPMD.LongVariable)
     */
    public function relionStop()
    {
        global $zocalo_mx_reprocess_queue;

        $rows = $this->db->pq(
            "SELECT ProcessingJob.processingJobId
            FROM ProcessingJob
            INNER JOIN DataCollection
                ON DataCollection.dataCollectionId = ProcessingJob.dataCollectionId
            INNER JOIN DataCollectionGroup
                ON DataCollectionGroup.dataCollectionGroupId = DataCollection.dataCollectionGroupId
            INNER JOIN BLSession
                ON BLSession.sessionId = DataCollectionGroup.sessionId
            INNER JOIN Proposal
                ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(Proposal.proposalCode, Proposal.proposalNumber) = :1
            AND processingJobId = :2",
            array($this->arg('prop'), $this->arg('id')),
            false
        );

        if (count($rows) == 0) {
            $message = 'Processing job not found!';
            error_log($message);
            $this->_error($message, 400);
        }

        $result = $rows[0];

        $parameters = array(
            'ispyb_process' => $result['processingJobId'],
        );
        $recipe = ['relion-stop'];
        $this->_submit_zocalo_recipe($recipe, $parameters);

        $this->_output(array(
            'timestamp' => gmdate('c', time()),
            'message' => array(
                'recipe' => $recipe,
                'parameters' => $parameters,
            )<commit_msg>Making relion-stop work with new zocalo queueing mechanism<commit_after><?php

namespace SynchWeb\Page\EM;

use SynchWeb\Page\EM\SchemaValidator;
use SynchWeb\Page\EM\RelionParameterTransformer;
use SynchWeb\Page\EM\RelionSchema;

trait Relion
{
    /**
     * Output the schema for client-side use
     *
     * @SuppressWarnings(PHPMD.StaticAccess)
     */
    public function relionSchema()
    {
        $schema = new RelionSchema();
        $this->_output($schema->clientSchema());
    }

    /**
     * Create a data collection and a processing job then enqueue the job
     *
     * @SuppressWarnings(PHPMD.LongVariable)
     */
    public function relionStart()
    {
        global $zocalo_mx_reprocess_queue;

        $this->configExitIfNoMicroscopes();

        $proposalCode = $this->arg('prop');
        $dataCollectionId = $this->arg('id');

        $session = $this->sessionFromDataCollection(
            $proposalCode,
            $dataCollectionId
        );

        $this->sessionExitIfNotActive($session);

        $dataCollection = $this->dataCollectionForProcessing(
            $proposalCode,
            $dataCollectionId
        );

        if ($dataCollection['runningJobs'] > 0) {
            $message = 'A Relion job is already running on this data collection!';
            error_log("$message - $proposalCode $dataCollectionId");
            $this->_error($message, 400);
        }

        $preparedData = $this->relionPreparePostData($dataCollection, $session);

        $processingJobId = $this->relionAddJob($dataCollectionId, $preparedData);

        // Send job to processing queue
        $message = array(
            'parameters' => array('ispyb_process' => $processingJobId)
        );

        $this->_send_zocalo_message($zocalo_mx_reprocess_queue, $message);

        $output = array(
            'timestamp' => gmdate('c', time()),
            'message' => $message
        );

        $this->_output($output);
    }

    /**
     * Request that a currently running job be stopped
     *
     * @SuppressWarnings(PHPMD.LongVariable)
     */
    public function relionStop()
    {
        global $zocalo_mx_reprocess_queue;

        $rows = $this->db->pq(
            "SELECT ProcessingJob.processingJobId
            FROM ProcessingJob
            INNER JOIN DataCollection
                ON DataCollection.dataCollectionId = ProcessingJob.dataCollectionId
            INNER JOIN DataCollectionGroup
                ON DataCollectionGroup.dataCollectionGroupId = DataCollection.dataCollectionGroupId
            INNER JOIN BLSession
                ON BLSession.sessionId = DataCollectionGroup.sessionId
            INNER JOIN Proposal
                ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(Proposal.proposalCode, Proposal.proposalNumber) = :1
            AND processingJobId = :2",
            array($this->arg('prop'), $this->arg('id')),
            false
        );

        if (count($rows) == 0) {
            $message = 'Processing job not found!';
            error_log($message);
            $this->_error($message, 400);
        }

        $result = $rows[0];

        $parameters = array(
            'ispyb_process' => $result['processingJobId'],
        );
        $recipe = 'relion-stop';
        $this->_submit_zocalo_recipe($recipe, $parameters);

        $this->_output(array(
            'timestamp' => gmdate('c', time()),
            'message' => array(
                'recipe' => $recipe,
                'parameters' => $parameters,
            )
{'fraction_non_alphanumeric': 0.11561923677117322, 'fraction_numerical': 0.003121010072350688, 'mean_word_length': 2.140311804008909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '1651496', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1926, 'n_words': 381}