<commit_before><?php

namespace Symbiote\QueuedJobs\Controllers;

use ReflectionClass;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\TextareaField;
use SilverStripe\ORM\DataList;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
use Symbiote\QueuedJobs\Forms\GridFieldQueuedJobExecute;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;

/**
 * @author <NAME> <<EMAIL>>
 * @license BSD http://silverstripe.org/bsd-license/
 */
class QueuedJobsAdmin extends ModelAdmin
{
    /**
     * @var string
     */
    private static $url_segment = 'queuedjobs';

    /**
     * @var string
     */
    private static $menu_title = 'Jobs';

    /**
     * @var string
     */
    private static $menu_icon = "symbiote/silverstripe-queuedjobs:images/clipboard.png";

    /**
     * @var array
     */
    private static $managed_models = [
        QueuedJobDescriptor::class
    ];

    /**
     * @var array
     */
    private static $dependencies = [
        'jobQueue' => '%$' . QueuedJobService::class,
    ];

    /**
     * @var array
     */
    private static $allowed_actions = [
        'EditForm'
    ];

    /**
     * European date format
     * @var string
     */
    private static $date_format_european = 'dd/MM/yyyy';

    /**
     * @var QueuedJobService
     */
    public $jobQueue;

    /**
     * @config The number of seconds to include jobs that have finished
     * default: 300 (5 minutes), examples: 3600(1h), 86400(1d)
     */
    private static $max_finished_jobs_age = 300;

    /**
     * @param int $id
     * @param FieldList $fields
     * @return Form
     */
    public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);

        $filter = $this->jobQueue->getJobListFilter(null, self::config()->max_finished_jobs_age);

        $list = DataList::create(QueuedJobDescriptor::class);
        $list = $list->where($filter)->sort('Created', 'DESC');

        $gridFieldConfig = GridFieldConfig_RecordEditor::create()
            ->addComponent(new GridFieldQueuedJobExecute('execute'))
            ->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) {
                return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN;
            }))
            ->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) {
                return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN;
            }))
            ->removeComponentsByType(GridFieldAddNewButton::class);


        // Set messages to HTML display format
        $formatting = array(
            'Messages' => function ($val, $obj) {
                return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>$obj->Messages</div>";
            },
        );
        $gridFieldConfig->getComponentByType(GridFieldDataColumns::class)
            ->setFieldFormatting($formatting);

        // Replace gridfield
        /** @skipUpgrade */
        $grid = GridField::create(
            QueuedJobDescriptor::class,
            _t(__CLASS__ . '.JobsFieldTitle', 'Jobs'),
            $list,
            $gridFieldConfig
        );
        $grid->setForm($form);
        /** @skipUpgrade */
        $form->Fields()->replaceField('QueuedJobDescriptor', $grid);

        if (Permission::check('ADMIN')) {
            $types = ClassInfo::subclassesFor(AbstractQueuedJob::class);
            $types = array_combine($types, $types);
            unset($types[AbstractQueuedJob::class]);
            $jobType = DropdownField::create('JobType', _t(__CLASS__ . '.CREATE_JOB_TYPE', 'Create job of type'), $types);
            $jobType->setEmptyString('(select job to create)');
            $form->Fields()->push($jobType);

            $jobParams = TextareaField::create(
                'JobParams',
                _t(__CLASS__ . '.JOB_TYPE_PARAMS', 'Constructor parameters for job creation (one per line)')
            );
            $form->Fields()->push($jobParams);
<commit_msg>filter out non-instantiable classes from QJ admin dropdown<commit_after><?php

namespace Symbiote\QueuedJobs\Controllers;

use ReflectionClass;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\TextareaField;
use SilverStripe\ORM\DataList;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
use Symbiote\QueuedJobs\Forms\GridFieldQueuedJobExecute;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;

/**
 * @author <NAME> <<EMAIL>>
 * @license BSD http://silverstripe.org/bsd-license/
 */
class QueuedJobsAdmin extends ModelAdmin
{
    /**
     * @var string
     */
    private static $url_segment = 'queuedjobs';

    /**
     * @var string
     */
    private static $menu_title = 'Jobs';

    /**
     * @var string
     */
    private static $menu_icon = "symbiote/silverstripe-queuedjobs:images/clipboard.png";

    /**
     * @var array
     */
    private static $managed_models = [
        QueuedJobDescriptor::class
    ];

    /**
     * @var array
     */
    private static $dependencies = [
        'jobQueue' => '%$' . QueuedJobService::class,
    ];

    /**
     * @var array
     */
    private static $allowed_actions = [
        'EditForm'
    ];

    /**
     * European date format
     * @var string
     */
    private static $date_format_european = 'dd/MM/yyyy';

    /**
     * @var QueuedJobService
     */
    public $jobQueue;

    /**
     * @config The number of seconds to include jobs that have finished
     * default: 300 (5 minutes), examples: 3600(1h), 86400(1d)
     */
    private static $max_finished_jobs_age = 300;

    /**
     * @param int $id
     * @param FieldList $fields
     * @return Form
     */
    public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);

        $filter = $this->jobQueue->getJobListFilter(null, self::config()->max_finished_jobs_age);

        $list = DataList::create(QueuedJobDescriptor::class);
        $list = $list->where($filter)->sort('Created', 'DESC');

        $gridFieldConfig = GridFieldConfig_RecordEditor::create()
            ->addComponent(new GridFieldQueuedJobExecute('execute'))
            ->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) {
                return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN;
            }))
            ->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) {
                return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN;
            }))
            ->removeComponentsByType(GridFieldAddNewButton::class);


        // Set messages to HTML display format
        $formatting = array(
            'Messages' => function ($val, $obj) {
                return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>$obj->Messages</div>";
            },
        );
        $gridFieldConfig->getComponentByType(GridFieldDataColumns::class)
            ->setFieldFormatting($formatting);

        // Replace gridfield
        /** @skipUpgrade */
        $grid = GridField::create(
            QueuedJobDescriptor::class,
            _t(__CLASS__ . '.JobsFieldTitle', 'Jobs'),
            $list,
            $gridFieldConfig
        );
        $grid->setForm($form);
        /** @skipUpgrade */
        $form->Fields()->replaceField('QueuedJobDescriptor', $grid);

        if (Permission::check('ADMIN')) {
            $types = ClassInfo::subclassesFor(AbstractQueuedJob::class);
            $types = array_combine($types, $types);
            foreach ($types as $class) {
                $reflection = new ReflectionClass($class);
                if (!$reflection->isInstantiable()) {
                    unset($types[$class]);
                }
            }
            $jobType = DropdownField::create('JobType', _t(__CLASS__ . '.CREATE_JOB_TYPE', 'Create job of type'), $types);
            $jobType->setEmptyString('(select job to create)');
            $form->Fields()->push($jobType);

            $jobParams = TextareaField::create(
                'JobParams',
                _t(__CLASS__ . '.JOB_TYPE_PARAMS', 'Constructor parameters for job creation (one per line)')
            );
            $form->Fields()->push($jobParams);

{'fraction_non_alphanumeric': 0.13178703215603585, 'fraction_numerical': 0.005060622034791776, 'mean_word_length': 2.7583201267828845, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 66, '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': '7040760', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2789, 'n_words': 508}