Skip to content

Commit

Permalink
#13 Globally use RunTemplates instead of Applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Apr 24, 2024
1 parent 6d5c755 commit 80cfe85
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 134 deletions.
27 changes: 27 additions & 0 deletions db/migrations/20240423212740_configuration_runtemplate.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class ConfigurationRuntemplate 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 {
$configs = $this->table('configuration');
$configs
->addColumn('runtemplate_id', 'integer', ['null' => true])
->addIndex(['runtemplate_id'])
->save();
}
}
27 changes: 27 additions & 0 deletions db/migrations/20240424083402_job_runtemplate.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class JobRuntemplate 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 {
$configs = $this->table('job');
$configs
->addColumn('runtemplate_id', 'integer', ['null' => true])
->addIndex(['runtemplate_id'])
->save();
}
}
4 changes: 2 additions & 2 deletions debian/changelog
@@ -1,12 +1,12 @@
multiflexi (1.12.0) UNRELEASED; urgency=medium
multiflexi (1.12.0) jammy; urgency=medium

[ CyberVitexus ]
* Dedicated multiflexi user created during installation

[ Vítězslav Dvořák ]
* App topics itroduced

-- Vítězslav Dvořák <info@vitexsoftware.cz> Fri, 12 Apr 2024 11:44:12 +0200
-- Vítězslav Dvořák <info@vitexsoftware.cz> Tue, 23 Apr 2024 21:03:11 +0200

multiflexi (1.10.5) UNRELEASED; urgency=medium

Expand Down
24 changes: 10 additions & 14 deletions src/MultiFlexi/Configuration.php
Expand Up @@ -13,12 +13,11 @@
*
* @author vitex
*/
class Configuration extends \Ease\SQL\Engine
{
class Configuration extends \Ease\SQL\Engine {

public $myTable = 'configuration';

public function __construct($identifier = null, $options = array())
{
public function __construct($identifier = null, $options = array()) {
parent::__construct($identifier, $options);
}

Expand All @@ -30,17 +29,17 @@ public function __construct($identifier = null, $options = array())
*
* @return int|null ID záznamu nebo null v případě neůspěchu
*/
public function saveToSQL($data = null, $searchForID = false)
{
public function saveToSQL($data = null, $searchForID = false) {
if (is_null($data)) {
$data = $this->getData();
}
$result = 0;
unset($data['app_id']);
unset($data['company_id']);
$this->deleteFromSQL(['app_id' => $this->getDataValue('app_id'), 'company_id' => $this->getDataValue('company_id')]);
unset($data['runtemplate_id']);
$this->deleteFromSQL(['app_id' => $this->getDataValue('app_id'), 'company_id' => $this->getDataValue('company_id'), 'runtemplate_id' => $this->getDataValue('runtemplate_id')]);
foreach ($data as $column => $value) {
$result += $this->insertToSQL(['app_id' => $this->getDataValue('app_id'), 'company_id' => $this->getDataValue('company_id'), 'name' => $column, 'value' => $value]);
$result += $this->insertToSQL(['app_id' => $this->getDataValue('app_id'), 'company_id' => $this->getDataValue('company_id'), 'runtemplate_id' => $this->getDataValue('runtemplate_id'), 'name' => $column, 'value' => $value]);
}
return $result;
}
Expand All @@ -52,8 +51,7 @@ public function saveToSQL($data = null, $searchForID = false)
*
* @return int
*/
public function takeData($data)
{
public function takeData($data) {
$cfgs = new Conffield();
foreach ($cfgs->appConfigs($this->getDataValue('app_id')) as $cfg) {
if ($cfg['type'] == 'checkbox') {
Expand All @@ -69,8 +67,7 @@ public function takeData($data)
* @param int $companyId
* @param int $appId
*/
public function setEnvironment($companyId, $appId)
{
public function setEnvironment($companyId, $appId) {
foreach ($this->getAppConfig($companyId, $appId) as $cfgRaw) {
$this->addStatusMessage(sprintf(_('Setting Environment %s to %s'), $cfgRaw['name'], $cfgRaw['value']), 'debug');
putenv($cfgRaw['name'] . '=' . $cfgRaw['value']);
Expand All @@ -85,8 +82,7 @@ public function setEnvironment($companyId, $appId)
*
* @return array
*/
public function getAppConfig($companyId, $appId)
{
public function getAppConfig($companyId, $appId) {
return $this->getColumnsFromSQL(['name', 'value'], ['company_id' => $companyId, 'app_id' => $appId]);
}
}
111 changes: 41 additions & 70 deletions src/MultiFlexi/RunTemplate.php
Expand Up @@ -17,16 +17,20 @@
*
* @author vitex
*/
class RunTemplate extends Engine
{
class RunTemplate extends Engine {

/**
*
* @var string
*/
public $nameColumn = 'name';

/**
*
* @param mixed $identifier
* @param array $options
*/
public function __construct($identifier = null, $options = [])
{
public function __construct($identifier = null, $options = []) {
$this->myTable = 'runtemplate';
parent::__construct($identifier, $options);
}
Expand All @@ -43,8 +47,7 @@ public function __construct($identifier = null, $options = [])
*
* @return int
*/
public function runTemplateID(int $appId, int $companyId)
{
public function runTemplateID(int $appId, int $companyId) {
$runTemplateId = intval($this->listingQuery()->where('company_id=' . $companyId . ' AND app_id=' . $appId)->select('id', true)->fetchColumn());
return $runTemplateId ? $runTemplateId : $this->dbsync(['app_id' => $appId, 'company_id' => $companyId, 'interv' => 'n']);
}
Expand All @@ -56,16 +59,14 @@ public function runTemplateID(int $appId, int $companyId)
*
* @return bool
*/
public function setState(bool $state)
{
public function setState(bool $state) {
if (\Ease\Shared::cfg('ZABBIX_SERVER')) {
$this->notifyZabbix($this->getData());
}
return $state ? $this->dbsync() : $this->deleteFromSQL();
}

public function performInit()
{
public function performInit() {
$app = new Application((int) $this->getDataValue('app_id'));
// $this->setEnvironment();
if (empty($app->getDataValue('setup')) == false) {
Expand All @@ -82,16 +83,14 @@ public function performInit()
*
* @return int
*/
public function deleteFromSQL($data = null)
{
public function deleteFromSQL($data = null) {
if (is_null($data)) {
$data = $this->getData();
}
return parent::deleteFromSQL($data);
}

public function getCompanyEnvironment()
{
public function getCompanyEnvironment() {
$connectionData = $this->getAppInfo();
$platformHelperClass = '\\MultiFlexi\\' . $connectionData['type'] . '\\Company';
$platformHelper = new $platformHelperClass($connectionData['company_id'], $connectionData);
Expand All @@ -104,9 +103,8 @@ public function getCompanyEnvironment()
*
* @return \Envms\FluentPDO\Query
*/
public function getCompanyTemplates($companyId)
{
return $this->listingQuery()->where('company_id', $companyId);
public function getCompanyTemplates($companyId) {
return $this->listingQuery()->select(['apps.name AS app_name', 'apps.description', 'apps.homepage', 'apps.image'])->leftJoin('apps ON apps.id = runtemplate.app_id')->where('company_id', $companyId);
}

/**
Expand All @@ -116,9 +114,8 @@ public function getCompanyTemplates($companyId)
*
* @return array<array>
*/
public function getCompanyAppsByInterval(int $companyId)
{
$companyApps = [
public function getCompanyRunTemplatesByInterval(int $companyId) {
$runtemplates = [
'i' => [],
'h' => [],
'd' => [],
Expand All @@ -127,9 +124,9 @@ public function getCompanyAppsByInterval(int $companyId)
'y' => []
];
foreach ($this->getCompanyTemplates($companyId)->fetchAll() as $template) {
$companyApps[$template['interv']][$template['app_id']] = $template;
$runtemplates[$template['interv']][$template['id']] = $template;
}
return $companyApps;
return $runtemplates;
}

/**
Expand All @@ -138,8 +135,7 @@ public function getCompanyAppsByInterval(int $companyId)
*
* @return array
*/
public function getAppEnvironment()
{
public function getAppEnvironment() {
$appInfo = $this->getAppInfo();
$jobber = new Job();
$jobber->company = new Company(intval($appInfo['company_id']));
Expand All @@ -151,8 +147,7 @@ public function getAppEnvironment()
*
* @return array
*/
public function getAppInfo()
{
public function getAppInfo() {
return $this->listingQuery()
->select('apps.*')
->select('apps.id as apps_id')
Expand All @@ -173,8 +168,7 @@ public function getAppInfo()
*
* @return array
*/
public function getPeriodAppsForCompany($companyID)
{
public function getPeriodAppsForCompany($companyID) {
return $this->getColumnsFromSQL(['app_id', 'interv', 'id'], ['company_id' => $companyID], 'id', 'app_id');
}

Expand All @@ -185,50 +179,31 @@ public function getPeriodAppsForCompany($companyID)
*
* @return boolean save status
*/
public function setProvision($status)
{
public function setProvision($status) {
return $this->dbsync(['prepared' => $status]);
}

/**
* Assign Apps in company to given interval
*
* @param int $companyId
* @param array $appIds
*
* @param int $companyId
* @param array $runtemplateIds
* @param string $interval
*/
public function assignAppsToCompany(int $companyId, array $appIds, string $interval)
{
$actions = new \MultiFlexi\ActionConfig();
$companyAppsInInterval = $this->listingQuery()->where(['company_id' => $companyId, 'interv' => $interval])->fetchAll('app_id');
foreach ($companyAppsInInterval as $appId => $runtempalte) {
if (array_key_exists($appId, $appIds) === false) {
$actionConfigsDeleted = $actions->deleteFromSQL(['runtemplate_id' => $runtempalte['id']]);
if (\Ease\Shared::cfg('ZABBIX_SERVER')) {
$this->notifyZabbix(['app_id' => $appId, 'company_id' => $companyId, 'interv' => 'n']);
}
$actions->addStatusMessage(strval($actionConfigsDeleted) . ' ' . _('action configurations deleted'));
$runtempaltesDeletd = $this->deleteFromSQL(['company_id' => $companyId, 'app_id' => $runtempalte['app_id']]);
$this->addStatusMessage(strval($runtempaltesDeletd) . ' ' . _('runtemplate deleted'));
}
}
foreach ($appIds as $appId) {
if (array_key_exists($appId, $companyAppsInInterval) === false) {
$appInserted = $this->insertToSQL(['app_id' => $appId, 'company_id' => $companyId, 'interv' => $interval]);
if (\Ease\Shared::cfg('ZABBIX_SERVER')) {
$this->notifyZabbix(['id' => $appInserted, 'app_id' => $appId, 'company_id' => $companyId, 'interv' => $interval]);
}
$this->addStatusMessage(sprintf(_('Application %s in company %s assigned to interval %s'), $appId, $companyId, $interval));
}
public function setPeriods(int $companyId, array $runtemplateIds, string $interval) {
foreach ($runtemplateIds as $runtemplateId) {
$this->updateToSQL(['interv' => $interval], ['id' => $runtemplateId]);
// if (\Ease\Shared::cfg('ZABBIX_SERVER')) {
// $this->notifyZabbix(['id' => $appInserted, 'app_id' => $appId, 'company_id' => $companyId, 'interv' => $interval]);
// }
// $this->addStatusMessage(sprintf(_('Application %s in company %s assigned to interval %s'), $appId, $companyId, $interval));
}
}

/**
*
* @param array $jobInterval
*/
public function notifyZabbix(array $jobInterval)
{
public function notifyZabbix(array $jobInterval) {
$zabbixSender = new ZabbixSender(\Ease\Shared::cfg('ZABBIX_SERVER'));
$hostname = \Ease\Shared::cfg('ZABBIX_HOST');
$company = new Company($jobInterval['company_id']);
Expand All @@ -250,33 +225,29 @@ public function notifyZabbix(array $jobInterval)
*
* @return array
*/
public static function stripToValues(array $envData)
{
public static function stripToValues(array $envData) {
$env = [];
foreach ($envData as $key => $data) {
$env[$key] = $data['value'];
}
return $env;
}



/**
* Actions Availble with flag when performed in case of success of failure
*
* @return array<array>
*/
public function getPostActions()
{
public function getPostActions() {
$actions = [];
$s = $this->getDataValue('success') ? unserialize($this->getDataValue('success')): [];
$f = $this->getDataValue('fail') ? unserialize($this->getDataValue('fail')): [];
foreach ($s as $action => $enabled){
$s = $this->getDataValue('success') ? unserialize($this->getDataValue('success')) : [];
$f = $this->getDataValue('fail') ? unserialize($this->getDataValue('fail')) : [];
foreach ($s as $action => $enabled) {
$actions[$action]['success'] = $enabled;
}
foreach ($s as $action => $enabled){
foreach ($s as $action => $enabled) {
$actions[$action]['fail'] = $enabled;
}
return $actions;
}

}
2 changes: 1 addition & 1 deletion src/MultiFlexi/Ui/ApplicationPanel.php
Expand Up @@ -40,7 +40,7 @@ public function __construct($application, $content = null, $footer = null)
$cid = $application->getMyKey();
$this->headRow = new Row();
$this->headRow->addColumn(2, [new AppLogo($application, ['style' => 'height: 60px']), '&nbsp;', $application->getRecordName()]);
$this->headRow->addColumn(4, [new LinkButton('app.php?id=' . $cid, '🛠️&nbsp;' . _('Application'), 'primary btn-lg'),
$this->headRow->addColumn(4, [new LinkButton('app.php?id=' . $cid, '🧩&nbsp;' . _('Application'), 'primary btn-lg'),
new LinkButton('joblist.php?app_id=' . $cid, '🧑‍💻&nbsp;' . _('Jobs history'), 'secondary btn-lg')]);

$ca = new \MultiFlexi\CompanyApp(null);
Expand Down
2 changes: 1 addition & 1 deletion src/MultiFlexi/Ui/AppsSelector.php
Expand Up @@ -30,7 +30,7 @@ public function __construct($identifier = null, $enabled = [], $optionsPage = 'a
'searchField' => ['name', 'description', 'homepage']
];

$properties['render']['item'] = 'function (item, escape) { return "<div class=container><div class=row> <div class=col-md-2><a href=app.php?id=" + escape(item.id) + "><img height=40 align=left src=\"" + escape(item.image) + "\"></a></div><div class=col-md-7>&nbsp;" + escape(item.name) + "</div><div class=col-md-3><a href=' . $optionsPage . '?app=" + escape(item.id) + "&interval=' . $identifier . ' style=\"font-size: 30px; padding: 5px;\" >🛠️️</a></div> </div></div>" }';
$properties['render']['item'] = 'function (item, escape) { return "<div class=container><div class=row> <div class=col-md-2><a href=app.php?id=" + escape(item.id) + "><img height=40 align=left src=\"" + escape(item.image) + "\"></a></div><div class=col-md-7>&nbsp;" + escape(item.name) + "</div><div class=col-md-3><a href=' . $optionsPage . '?id=" + escape(item.id) + "&interval=' . $identifier . ' style=\"font-size: 30px; padding: 5px;\" >🛠️️</a></div> </div></div>" }';
$properties['render']['option'] = 'function (item, escape) { return "<div><img height=40 align=right src=\"" + escape(item.image) + "\">" + escape(item.name) + "<br><small>" + escape(item.description) + "</small></div>" }';
$properties['plugins'] = ['remove_button'];

Expand Down

0 comments on commit 80cfe85

Please sign in to comment.