Skip to content

Commit

Permalink
Merge pull request #149 from fredden/input-with-datalist
Browse files Browse the repository at this point in the history
Admin grids usability improvements
  • Loading branch information
Ethan3600 committed Jul 18, 2020
2 parents 0346ebb + 91d70db commit d2b039f
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 46 deletions.
40 changes: 40 additions & 0 deletions Model/Schedule/Source/Group.php
@@ -0,0 +1,40 @@
<?php

namespace EthanYehuda\CronjobManager\Model\Schedule\Source;

use Magento\Cron\Model\Config;
use Magento\Framework\Data\OptionSourceInterface;

class Group implements OptionSourceInterface
{
/**
* @var Config
*/
protected $cronConfig;

public function __construct(
Config $config
) {
$this->cronConfig = $config;
}

/**
* Return array of options as value-label pairs
*
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
*/
public function toOptionArray()
{
$groups = \array_keys($this->cronConfig->getJobs());
\natsort($groups);

$options = [];
foreach ($groups as $group) {
$options[] = [
'value' => $group,
'label' => $group,
];
}
return $options;
}
}
62 changes: 57 additions & 5 deletions Plugin/Cron/Model/ScheduleResourcePlugin.php
Expand Up @@ -4,8 +4,10 @@
namespace EthanYehuda\CronjobManager\Plugin\Cron\Model;

use EthanYehuda\CronjobManager\Model\ErrorNotification;
use Magento\Cron\Model\ConfigInterface;
use Magento\Cron\Model\ResourceModel\Schedule as ScheduleResource;
use Magento\Cron\Model\Schedule;
use Magento\Cron\Model\ResourceModel;
use Magento\Framework\DataObject;

class ScheduleResourcePlugin
{
Expand All @@ -14,17 +16,67 @@ class ScheduleResourcePlugin
*/
private $errorNotification;

public function __construct(ErrorNotification $errorNotification)
{
public function __construct(
ConfigInterface $config,
ErrorNotification $errorNotification
) {
$this->config = $config;
$this->errorNotification = $errorNotification;
}

public function beforeSave(
ScheduleResource $subject,
DataObject $dataObject
) {
$this->recordJobDuration($dataObject);
$this->recordJobGroup($dataObject);
}

protected function recordJobDuration(DataObject $dataObject): void
{
if ($dataObject->dataHasChangedFor('duration')) {
// avoid loops
return;
}
$executedAt = $dataObject->getData('executed_at');
$finishedAt = $dataObject->getData('finished_at');
$executedTimestamp = \strtotime($executedAt ?: 'now') ?: \time();
$finishedTimestamp = \strtotime($finishedAt ?: 'now') ?: \time();

if (!$executedAt) {
// Job has not yet started. Nothing to do.
return;
}

$dataObject->setData('duration', $finishedTimestamp - $executedTimestamp);
}

protected function recordJobGroup(DataObject $dataObject): void
{
if ($dataObject->dataHasChangedFor('group')) {
// avoid loops
return;
}
if ($dataObject->getData('group')) {
// already have recorded group. Nothing to do.
return;
}

$jobCode = $dataObject->getData('job_code');
foreach ($this->config->getJobs() as $group => $jobs) {
if (\in_array($jobCode, \array_keys($jobs))) {
$dataObject->setData('group', $group);
return;
}
}
}

/**
* Email notification if status has been set to ERROR
*/
public function afterSave(
ResourceModel\Schedule $subject,
ResourceModel\Schedule $result,
ScheduleResource $subject,
ScheduleResource $result,
Schedule $object
) {
if ($object->getOrigData('status') !== $object->getStatus()
Expand Down
50 changes: 50 additions & 0 deletions Setup/UpgradeSchema.php
Expand Up @@ -50,6 +50,11 @@ public function upgrade(
$this->addHostnameToSchedule();
}

if (version_compare($context->getVersion(), '1.10.0') < 0) {
$this->addGroupToSchedule();
$this->addDurationToSchedule();
}

$this->setup->endSetup();
}

Expand Down Expand Up @@ -77,6 +82,51 @@ public function addPidToSchedule()
);
}

/**
* Add column to cron_schedule to keep track of which a job's duration
*/
public function addDurationToSchedule()
{
if (version_compare($this->magentoMetaData->getVersion(), '2.3.0', '>=')) {
// For Magento 2.3+, db_schema.xml is used instead
return;
}
$this->setup->getConnection()->addColumn(
$this->setup->getTable('cron_schedule'),
'duration',
[
'type' => Table::TYPE_INTEGER,
'comment' => 'Number of seconds job ran for',
'nullable' => true,
'default' => null,
'after' => 'group',
]
);
}

/**
* Add column to cron_schedule to keep track of which group a job belongs to
*/
public function addGroupToSchedule()
{
if (version_compare($this->magentoMetaData->getVersion(), '2.3.0', '>=')) {
// For Magento 2.3+, db_schema.xml is used instead
return;
}
$this->setup->getConnection()->addColumn(
$this->setup->getTable('cron_schedule'),
'group',
[
'type' => Table::TYPE_TEXT,
'length' => 255,
'comment' => 'Cron group for this job',
'nullable' => true,
'default' => null,
'after' => 'pid',
]
);
}

/**
* Add column to cron_schedule to keep track of which server is running each process
*/
Expand Down
19 changes: 19 additions & 0 deletions Ui/Component/Filters.php
@@ -0,0 +1,19 @@
<?php

namespace EthanYehuda\CronjobManager\Ui\Component;

use Magento\Ui\Component\Filters as MageFilters;

class Filters extends MageFilters
{
protected $filterMap = [
// Original list
'dateRange' => 'filterDate',
'select' => 'filterSelect',
'text' => 'filterInput',
'textRange' => 'filterRange',

// Added
'textWithDatalist' => 'filterInput',
];
}
4 changes: 4 additions & 0 deletions etc/adminhtml/di.xml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Ui\Component\Filters" type="EthanYehuda\CronjobManager\Ui\Component\Filters" />
</config>
2 changes: 2 additions & 0 deletions etc/db_schema.xml
Expand Up @@ -2,7 +2,9 @@
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="cron_schedule">
<column xsi:type="varchar" name="group" nullable="true" length="255" comment="Cron group for this job"/>
<column xsi:type="varchar" name="hostname" nullable="true" length="255" comment="Hostname of the server running this job"/>
<column xsi:type="int" name="duration" padding="10" unsigned="true" nullable="true" comment="Number of seconds job ran for"/>
<column xsi:type="int" name="pid" padding="10" unsigned="true" nullable="true" comment="Process Id"/>
<column xsi:type="timestamp" name="kill_request" on_update="false" nullable="true" comment="Kill Request"/>
</table>
Expand Down
2 changes: 2 additions & 0 deletions etc/db_schema_whitelist.json
@@ -1,6 +1,8 @@
{
"cron_schedule": {
"column": {
"duration": true,
"group": true,
"hostname": true,
"pid": true,
"kill_request": true
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="EthanYehuda_CronjobManager" setup_version="1.9.0" >
<module name="EthanYehuda_CronjobManager" setup_version="1.10.0" >
<sequence>
<module name="Magento_Cron"/>
</sequence>
Expand Down
7 changes: 7 additions & 0 deletions view/adminhtml/requirejs-config.js 100755 → 100644
@@ -1,4 +1,11 @@
var config = {
config: {
mixins: {
'Magento_Ui/js/grid/filters/filters': {
'EthanYehuda_CronjobManager/js/grid/filters-mixin': true
}
}
},
shim: {
'EthanYehuda_CronjobManager/js/timeline/timeline': {
'deps': ['EthanYehuda_CronjobManager/js/lib/knockout/bindings/virtual-foreach']
Expand Down
65 changes: 33 additions & 32 deletions view/adminhtml/ui_component/cronjobmanager_config_grid.xml
Expand Up @@ -76,40 +76,40 @@
<paging name="listing_paging" />
<bookmark name="bookmarks" />
<columnsControls name="columns_controls" />
</listingToolbar>
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
</item>
</argument>
<action name="schedule_now">
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Schedule Now</item>
<item name="message" xsi:type="string" translate="true">Schedule selected cron jobs to run now?</item>
</item>
<item name="type" xsi:type="string">schedule_now</item>
<item name="label" xsi:type="string" translate="true">Schedule Now</item>
<item name="url" xsi:type="url" path="cronjobmanager/config_job/massScheduleNow" />
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
</item>
</argument>
</action>
<action name="restore">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Restore System Defaults</item>
<item name="message" xsi:type="string" translate="true">Restore job configurations to system defaults?</item>
<action name="schedule_now">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Schedule Now</item>
<item name="message" xsi:type="string" translate="true">Schedule selected cron jobs to run now?</item>
</item>
<item name="type" xsi:type="string">schedule_now</item>
<item name="label" xsi:type="string" translate="true">Schedule Now</item>
<item name="url" xsi:type="url" path="cronjobmanager/config_job/massScheduleNow" />
</item>
<item name="type" xsi:type="string">restore</item>
<item name="label" xsi:type="string" translate="true">Restore System Defaults</item>
<item name="url" xsi:type="url" path="cronjobmanager/config_job/massRestoreSystemDefault" />
</item>
</argument>
</action>
</massaction>
</argument>
</action>
<action name="restore">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Restore System Defaults</item>
<item name="message" xsi:type="string" translate="true">Restore job configurations to system defaults?</item>
</item>
<item name="type" xsi:type="string">restore</item>
<item name="label" xsi:type="string" translate="true">Restore System Defaults</item>
<item name="url" xsi:type="url" path="cronjobmanager/config_job/massRestoreSystemDefault" />
</item>
</argument>
</action>
</massaction>
</listingToolbar>
<columns name="cronjobmanager_config_grid_columns">
<selectionsColumn name="ids">
<argument name="data" xsi:type="array">
Expand All @@ -124,9 +124,8 @@
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">EthanYehuda\CronjobManager\Model\Schedule\Source\Schedule</item>
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">select</item>
<item name="filter" xsi:type="string">textWithDatalist</item>
<item name="dataType" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="sorting" xsi:type="string">asc</item>
<item name="label" xsi:type="string" translate="true">Job Code</item>
<item name="sortOrder" xsi:type="number">20</item>
Expand All @@ -135,8 +134,10 @@
</column>
<column name="group">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">EthanYehuda\CronjobManager\Model\Schedule\Source\Group</item>
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="dataType" xsi:type="string">select</item>
<item name="filter" xsi:type="string">textWithDatalist</item>
<item name="label" xsi:type="string" translate="true">Group</item>
<item name="sortOrder" xsi:type="number">30</item>
<item name="sortable" xsi:type="boolean">true</item>
Expand Down

0 comments on commit d2b039f

Please sign in to comment.