Skip to content

Commit

Permalink
(un)active runtemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Feb 10, 2025
1 parent 6188339 commit a636973
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 14 deletions.
26 changes: 26 additions & 0 deletions db/migrations/20250210124736_runtemplate_active.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class RuntemplateActive extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change(): void
{
$table = $this->table('runtemplate');
$table->addColumn('active', 'boolean', ['default' => true])
->update();
}
}
7 changes: 4 additions & 3 deletions lib/zabbixcompanylld.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
$server = str_replace('.'.$companyCode, '', $venue);

$lldData = [];
$ap2c = new \MultiFlexi\RunTemplate();
$rumtemplate = new \MultiFlexi\RunTemplate();
$companer = new Company(['code' => $companyCode], ['autoload' => true]);
$ca = new \MultiFlexi\CompanyApp($companer);
$apper = new Application();

$companyData = $companer->getData();

$appsAssigned = $ca->getAll()->leftJoin('apps ON apps.id = companyapp.app_id')->select(['apps.name', 'apps.description', 'apps.id', 'apps.image, apps.code, apps.uuid'], true)->fetchAll('id');
$runtemplates = $ap2c->listingQuery()->leftJoin('company ON company.id = runtemplate.company_id')->select(['runtemplate.id', 'interv', 'company.code AS company_code', 'company.name AS company_name']);
$runtemplates = $rumtemplate->listingQuery()->where('runtemplate.active',true)->leftJoin('company ON company.id = runtemplate.company_id')->select(['runtemplate.id', 'interv', 'company.code AS company_code', 'company.name AS company_name']);

$actions = new \MultiFlexi\ActionConfig();
$succesActions = ActionsChooser::toggles('success');
Expand All @@ -70,13 +70,14 @@
'{#INTERVAL}' => RunTemplate::codeToInterval($runtemplateData['interv']),
'{#INTERVAL_SECONDS}' => Job::codeToSeconds($runtemplateData['interv']),
'{#RUNTEMPLATE}' => $runtemplateData['id'],
'{#RUNTEMPLATE_NAME}' => $runtemplateData['name'],
'{#COMPANY_NAME}' => $runtemplateData['company_name'],
'{#COMPANY_CODE}' => $runtemplateData['company_code'],
'{#COMPANY_SERVER}' => \Ease\Shared::cfg('ZABBIX_HOST'),
'{#DATA_ITEM}' => false, // TODO
];
} else {
$ap2c->addStatusMessage('Application '.$runtemplateData['app_id'].' is not assigned with company ?');
$rumtemplate->addStatusMessage('Application '.$runtemplateData['app_id'].' is not assigned with company ?');
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/zabbixlld.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
$mode = \array_key_exists(1, $argv) ? $argv[1] : 'n/a';

$lldData = [];
$ap2c = new \MultiFlexi\RunTemplate();
$rumtemplate = new \MultiFlexi\RunTemplate();
$companer = new Company();
$apper = new Application();

foreach ($companer->listingQuery()->where('enabled', 1) as $companyData) {
if ($mode === '-a') {
$companer->setData($companyData);
$appsForCompany = $ap2c->getPeriodAppsForCompany($companyData['id']);
$appsForCompany = $rumtemplate->getActiveRunTemplatesForCompany($companyData['id']);

foreach ($appsForCompany as $companyAppData) {
$apper->loadFromSQL($companyAppData['app_id']);
Expand Down
2 changes: 1 addition & 1 deletion lib/zabbixlldactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$runTemplater = new \MultiFlexi\RunTemplate();
$runTemplater->logBanner();

$enabled = $runTemplater->listingQuery()->disableSmartJoin()->where("success LIKE '%\"Zabbix\";b:1;%'")->whereOr("fail LIKE '%\"Zabbix\";b:1;%'")
$enabled = $runTemplater->listingQuery()->disableSmartJoin()->where('runtemplate.active',true)->where("success LIKE '%\"Zabbix\";b:1;%'")->whereOr("fail LIKE '%\"Zabbix\";b:1;%'")
->select(['runtemplate.id AS runtemplate_id', 'runtemplate.name AS runtemplate_name'])
->leftJoin('company ON company.id = runtemplate.company_id')
->select(['company.id AS company_id', 'company.name AS company_name'])
Expand Down
16 changes: 14 additions & 2 deletions src/MultiFlexi/RunTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,23 @@ public function getAppInfo()
*
* @return array
*/
public function getPeriodAppsForCompany($companyID)
public function getRunTemplatesForCompany($companyID)
{
return $this->getColumnsFromSQL(['app_id', 'interv', 'id'], ['company_id' => $companyID], 'id', 'app_id');
}


/**
* All RunTemplates for GivenCompany.
*
* @param int $companyID
*
* @return array
*/
public function getActiveRunTemplatesForCompany($companyID)
{
return $this->getRunTemplatesForCompany($companyID)->where('runtemplate.active',true);
}

/**
* Set Provision state.
*
Expand Down
2 changes: 1 addition & 1 deletion src/MultiFlexi/Ui/CompanyApplicationPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(\MultiFlexi\CompanyApp $companyApp, $content = null,

$crls = new \MultiFlexi\Ui\CompanyRuntemplatesLinks($company, $this->application, [], ['class' => 'btn btn-outline-secondary btn-sm']);

$usedByCompany->addItem(new \Ease\TWB4\Card([new \Ease\Html\DivTag([new \Ease\Html\H5Tag([_('Runtemplates') . ': ' , ' <small>' . $crls->count() . '</small>'], ['class' => 'card-title']), $crls], ['class' => 'card-body'])], ['style' => 'width: 6rem;']));
$usedByCompany->addItem(new \Ease\TWB4\Card([new \Ease\Html\DivTag([new \Ease\Html\H5Tag([_('Active RunTemplates') . ': ' , ' <small>' . $crls->count() . '</small>'], ['class' => 'card-title']), $crls], ['class' => 'card-body'])], ['style' => 'width: 6rem;']));

$this->headRow->addColumn(6, $usedByCompany);

Expand Down
2 changes: 1 addition & 1 deletion src/MultiFlexi/Ui/CompanyRuntemplatesLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CompanyRuntemplatesLinks extends \Ease\Html\DivTag
public function __construct(\MultiFlexi\Company $company, \MultiFlexi\Application $application, array $properties = [], array $linkProperties = [])
{
$runTemplater = new \MultiFlexi\RunTemplate();
$runtemplatesRaw = $runTemplater->listingQuery()->where('app_id', $application->getMyKey())->where('company_id', $company->getMyKey());
$runtemplatesRaw = $runTemplater->listingQuery()->where('active',true)->where('app_id', $application->getMyKey())->where('company_id', $company->getMyKey());
$jobber = new \MultiFlexi\Job();

$runtemplatesDiv = new \Ease\Html\DivTag();
Expand Down
29 changes: 28 additions & 1 deletion src/MultiFlexi/Ui/RunTemplatePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(\MultiFlexi\RunTemplate $runtemplate)
$executorChooser = new AppExecutorSelect($runtemplate->getApplication(), [], (string) $runtemplate->getDataValue('executor'), ['id' => $runtemplateId.'_executor', 'data-runtemplate' => $runtemplateId]);

$scheduleButton = new \Ease\TWB4\LinkButton('schedule.php?id='.$runtemplateId, [_('Manual Schedule').'&nbsp;&nbsp;', new \Ease\Html\ImgTag('images/launchinbackground.svg', _('Launch'), ['height' => '30px'])], 'primary btn-lg');
$runtemplateOptions->addColumn(4, '');
$runtemplateOptions->addColumn(4, [ _('Status'), new \Ease\TWB4\Widgets\Toggle('active', $runtemplate->getDataValue('active') ? true : false, $runtemplate->getDataValue('active') ? 'false' : 'true', ['title' => $runtemplate->getDataValue('active') ? _('Enabled') : _('Disabled'), 'data-runtemplate'=> $runtemplateId, 'id'=>'enabler', 'data-on'=>_('Enabled'), 'data-off'=>_('Disabled')]), new \Ease\Html\SpanTag('', ['id'=>'deactivated']) ]);
$runtemplateOptions->addColumn(4, $scheduleButton);

if (WebPage::getRequestValue('delete', 'int') === 1) {
Expand Down Expand Up @@ -184,6 +184,33 @@ public function finalize(): void

EOD);



$this->addJavaScript(<<<'EOD'
$('#enabler').change( function(event, state) {
$.ajax({
url: 'rtactive.php',
data: {
runtemplate: $(this).attr("data-runtemplate"),
active: $(this).val()
},
error: function() {
$('#deactivated').before( "⚰️" );
console.log("not saved");
},
success: function(data) {
$('#deactivated').before( "💾" );
console.log("saved");
},
type: 'POST'
});
});

EOD);

parent::finalize();
}
}
2 changes: 1 addition & 1 deletion src/MultiFlexi/Ui/ServicesForCompanyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($company, $tagProperties = [])
// (new Application())->listingQuery()->select('id AS app_id')->select('name AS app_name')->where('enabled', 1)->fetchAll();

$glue = new RunTemplate();
$assigned = $glue->getPeriodAppsForCompany($companyID);
$assigned = $glue->getActiveRunTemplatesForCompany($companyID);
parent::__construct($tagProperties);
$jobber = new \MultiFlexi\Job();
$appTabs = new \Ease\TWB4\Tabs();
Expand Down
2 changes: 1 addition & 1 deletion src/companyapp.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
$successIcons = new ATag('actions.php?id='.$runtemplateData['id'].'#SuccessActions', RunTemplate::actionIcons($runtemplateData['success'] ? unserialize($runtemplateData['success']) : null, ['style' => 'border-bottom: 4px solid green;']));
$failIcons = new ATag('actions.php?id='.$runtemplateData['id'].'#FailActions', RunTemplate::actionIcons($runtemplateData['fail'] ? unserialize($runtemplateData['fail']) : null, ['style' => 'border-bottom: 4px solid red;']));

$runtemplateRow->addColumn(8, ['<span title="'._(RunTemplate::codeToInterval($runtemplateData['interv'])).'">'.RunTemplate::getIntervalEmoji($runtemplateData['interv']).'</span>&nbsp;', '&nbsp;<a href="schedule.php?id='.$runtemplateData['id'].'&when=now&executor=Native" title="'._('Launch now').'"><span style="color: green; font-weight: xx-large;">▶</span></a> ', new ATag('runtemplate.php?id='.$runtemplateData['id'], $runtemplateData['name']), '&nbsp;&nbsp;', $successIcons, '&nbsp;', $failIcons]);
$runtemplateRow->addColumn(8, [ '<span title="'._(RunTemplate::codeToInterval($runtemplateData['interv'])).'">'.RunTemplate::getIntervalEmoji($runtemplateData['interv']).'</span>&nbsp;', $runtemplateData['active'] ? '&nbsp;<a href="schedule.php?id='.$runtemplateData['id'].'&when=now&executor=Native" title="'._('Launch now').'"><span style="color: green; font-weight: xx-large;">▶</span></a> ' : '<span style="color: lightgray; font-weight: xx-large;" title="'._('Disabled').'">🚧</span>', '&nbsp;' , new ATag('runtemplate.php?id='.$runtemplateData['id'], $runtemplateData['name']), '&nbsp;&nbsp;', $successIcons, '&nbsp;', $failIcons]);
$runtemplatesDiv->addItem($runtemplateRow);
$runtemplates[$runtemplateData['id']] = $runtemplateData['name'];
}
Expand Down
31 changes: 31 additions & 0 deletions src/rtactive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* This file is part of the MultiFlexi package
*
* https://multiflexi.eu/
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MultiFlexi\Ui;

require_once './init.php';

WebPage::singleton()->onlyForLogged();

$runtemplate_id = \Ease\TWB4\WebPage::getRequestValue('runtemplate', 'int');
$active = \Ease\TWB4\WebPage::getRequestValue('active','bool');

if (null !== $runtemplate_id) {
$switcher = new \MultiFlexi\RunTemplate();
$switcher->setData(['id' => $runtemplate_id, 'active' => $active]);
http_response_code($switcher->dbsync() ? 201 : 400);
} else {
http_response_code(404);
}
2 changes: 1 addition & 1 deletion tests/src/MultiFlexi/RunTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testsetEnvironment(): void
*/
public function testgetAppsForCompany(): void
{
$this->assertEquals('', $this->object->getPeriodAppsForCompany());
$this->assertEquals('', $this->object->getActiveRunTemplatesForCompany());
// Remove the following lines when you implement this test.
$this->markTestIncomplete('This test has not been implemented yet.');
}
Expand Down

0 comments on commit a636973

Please sign in to comment.