From 1e63391ebc1ea0b59286e3a8308e6afacb45c3e8 Mon Sep 17 00:00:00 2001 From: zhaoke Date: Wed, 26 Jul 2023 15:01:08 +0800 Subject: [PATCH] * Finish task #98672,#98671. --- lib/zin/wg/floattoolbar/v1.php | 4 +++- module/action/model.php | 2 ++ module/instance/config.php | 2 ++ module/instance/control.php | 22 ++++++++++++++++-- module/instance/css/view.ui.css | 13 +++-------- module/instance/js/view.ui.js | 22 ++++++++++++++++-- module/instance/model.php | 12 +++++----- module/instance/ui/view.html.php | 17 ++++++++++---- module/space/config/dtable.php | 8 ++++--- module/space/js/browse.ui.js | 40 ++++++++++++++++++++++++++++++++ module/space/ui/browse.html.php | 2 ++ 11 files changed, 115 insertions(+), 29 deletions(-) diff --git a/lib/zin/wg/floattoolbar/v1.php b/lib/zin/wg/floattoolbar/v1.php index 2316e1c1aa..af9815db4c 100644 --- a/lib/zin/wg/floattoolbar/v1.php +++ b/lib/zin/wg/floattoolbar/v1.php @@ -41,7 +41,9 @@ class floatToolbar extends wg if(!empty($item['url'])) $item['url'] = preg_replace_callback('/\{(\w+)\}/', array($this, 'getObjectValue'), $item['url']); if(!empty($item['data-url'])) $item['data-url'] = preg_replace_callback('/\{(\w+)\}/', array($this, 'getObjectValue'), $item['data-url']); - $btns[] = btn(set($item), setClass('ghost text-white')); + $className = 'ghost text-white'; + if(!empty($item['className'])) $className .= ' ' . $item['className']; + $btns[] = btn(set($item), setClass($className)); } return $btns; } diff --git a/module/action/model.php b/module/action/model.php index ead60ce9f6..65c9afc67a 100755 --- a/module/action/model.php +++ b/module/action/model.php @@ -938,6 +938,8 @@ class actionModel extends model if($actionType == 'saveautobackupsettings' && $enableAutoBackup) $desc = $this->lang->action->desc->closeautobackupsettings; $action->extra = ''; + + if(!empty($extra->result->code) && $extra->result->code != 200 && !empty($extra->result->message)) $action->comment = $extra->result->message; } } elseif(isset($this->lang->action->desc->$actionType)) diff --git a/module/instance/config.php b/module/instance/config.php index 81a443ef86..fbb4db9dc9 100644 --- a/module/instance/config.php +++ b/module/instance/config.php @@ -22,10 +22,12 @@ global $lang, $app; $app->loadLang('space'); $config->instance->actionList['start']['icon'] = 'play'; +$config->instance->actionList['start']['className'] = 'ajax-submit'; $config->instance->actionList['start']['hint'] = $lang->instance->start; $config->instance->actionList['start']['url'] = array('module' => 'instance', 'method' => 'ajaxStart', 'params' => 'id={id}'); $config->instance->actionList['stop']['icon'] = 'off'; +$config->instance->actionList['stop']['className'] = 'ajax-submit'; $config->instance->actionList['stop']['hint'] = $lang->instance->stop; $config->instance->actionList['stop']['data-confirm'] = $lang->instance->notices['confirmStop']; $config->instance->actionList['stop']['url'] = array('module' => 'instance', 'method' => 'ajaxStop', 'params' => 'id={id}'); diff --git a/module/instance/control.php b/module/instance/control.php index fd26f81916..484ae86c7a 100644 --- a/module/instance/control.php +++ b/module/instance/control.php @@ -456,6 +456,24 @@ class instance extends control $this->display(); } + /** + * 刷新应用状态。 + * Refresh status of an instance. + * + * @param int $instanceID + * @access public + * @return void + */ + public function ajaxRefresh($instanceID) + { + $instance = $this->instance->getByID($instanceID); + if(empty($instance)) return $this->send(array('status' => '')); + + $instance = $this->instance->freshStatus($instance); + + return $this->send(array('status' => $instance->status)); + } + /** * Uninstall app instance. * @@ -492,7 +510,7 @@ class instance extends control if($result->code == 200) return $this->send(array('result' => 'success', 'load' => true, 'message' => zget($this->lang->instance->notices, 'startSuccess'))); - return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'startFail'))); + return $this->send(array('result' => 'fail', 'message' => !empty($result->message) ? $result->message : zget($this->lang->instance->notices, 'startFail'))); } /** @@ -511,7 +529,7 @@ class instance extends control $this->action->create('instance', $instance->id, 'stop', '', json_encode(array('result' => $result, 'app' => array('alias' => $instance->appName, 'app_version' => $instance->version)))); if($result->code == 200) return $this->send(array('result' => 'success', 'load' => true, 'message' => zget($this->lang->instance->notices, 'stopSuccess'))); - return $this->send(array('result' => 'fail', 'message' => zget($this->lang->instance->notices, 'stopFail'))); + return $this->send(array('result' => 'fail', 'message' => !empty($result->message) ? $result->message : zget($this->lang->instance->notices, 'stopFail'))); } /** diff --git a/module/instance/css/view.ui.css b/module/instance/css/view.ui.css index b9d1410d3f..449c86ff4e 100644 --- a/module/instance/css/view.ui.css +++ b/module/instance/css/view.ui.css @@ -2,14 +2,7 @@ #dynamicPager {justify-content: flex-end;} #dynamicPager .ghost {color: rgb(131, 138, 157);} #dynamicTable a {color: inherit;} +.progress-container {word-break: keep-all; white-space: nowrap;} .progress-container i {margin: auto; margin-right: 6px;} -.progress-container .progress {margin: auto; width: 200px; height: .7rem; margin-left: 6px; margin-right: 15px;} -.label-dot { - display: inline-block; - width: 8px; - height: 8px; - padding: 0; - line-height: 20px; - text-indent: -9999em; - border-radius: 50%; - } \ No newline at end of file +.progress-container .progress {margin: auto; width: 100%; height: .7rem; margin-left: 6px; margin-right: 15px;} +.label-dot {display: inline-block; width: 8px; height: 8px; padding: 0; line-height: 20px; text-indent: -9999em; border-radius: 50%;} \ No newline at end of file diff --git a/module/instance/js/view.ui.js b/module/instance/js/view.ui.js index 9c17355f6c..b3d81c330f 100644 --- a/module/instance/js/view.ui.js +++ b/module/instance/js/view.ui.js @@ -1,4 +1,4 @@ -window.openAdminer = function(e) +window.openAdminer = function() { var dbName = $(this).data('dbname'); var dbType = $(this).data('dbtype'); @@ -22,4 +22,22 @@ window.openAdminer = function(e) } } ); -} \ No newline at end of file +} + +var reloadTimes = 0; +window.afterPageUpdate = function() +{ + setTimeout(function() + { + if(reloadTimes > 10) return; + if($('#statusTD').data('reload') === true || $('#memoryRate').data('load') == true) + { + reloadTimes++; + fetchContent({url: $.createLink('instance', 'view', 'id=' + instanceID), + selector: '#instanceInfoContainer', + id: 'instanceInfoContainer', + target: '#instanceInfoContainer', + }); + } + }, 1000); +} diff --git a/module/instance/model.php b/module/instance/model.php index 879b36b356..276687f2e4 100644 --- a/module/instance/model.php +++ b/module/instance/model.php @@ -468,9 +468,9 @@ class InstanceModel extends model switch($action) { case 'start': - return !($busy or in_array($instance->status, array('running', 'abnormal', 'destroyed'))); + return $instance->status === 'stopped'; case 'stop': - return !in_array($instance->status, array('stopped', 'stopping', 'destroying', 'suspending', 'suspended')); + return in_array($instance->status, array('abnormal', 'running')); case 'uninstall': return !in_array($instance->status, array('destroying')); case 'visit': @@ -1711,7 +1711,7 @@ class InstanceModel extends model */ public static function printCpuUsage($instance, $metrics, $type = 'bar') { - $rate = $metrics->rate; + $rate = $instance->status == 'stopped' ? 0 : $metrics->rate; $tip = "{$rate}% = {$metrics->usage} / {$metrics->limit}"; if(empty($color) && $rate == 0) $color = 'gray'; @@ -1739,9 +1739,9 @@ class InstanceModel extends model * @access public * @return mixed */ - public static function printMemUsage($instnace, $metrics, $type = 'bar') + public static function printMemUsage($instance, $metrics, $type = 'bar') { - $rate = $metrics->rate; + $rate = $instance->status == 'stopped' ? 0 : $metrics->rate; $tip = "{$rate}% = " . helper::formatKB($metrics->usage) . ' / ' . helper::formatKB($metrics->limit); if(empty($color) && $rate == 0) $color = 'gray'; @@ -1754,7 +1754,7 @@ class InstanceModel extends model if(strtolower($type) == 'pie') commonModel::printProgressPie($rate, '', $tip); $valueType = 'tip'; - if($instnace->status == 'stopped') $valueType = ''; + if($instance->status == 'stopped') $valueType = ''; commonModel::printProgressBar($rate, '', $tip, $valueType); } diff --git a/module/instance/ui/view.html.php b/module/instance/ui/view.html.php index c9dc93cffb..ea3dad97e9 100644 --- a/module/instance/ui/view.html.php +++ b/module/instance/ui/view.html.php @@ -13,6 +13,8 @@ declare(strict_types=1); namespace zin; +jsVar('instanceID', $instance->id); + $setting = usePager('pager'); $cpuInfo = $this->instance->printCpuUsage($instance, $instanceMetric->cpu, 'array'); $memoryInfo = $this->instance->printMemUsage($instance, $instanceMetric->memory, 'array'); @@ -53,10 +55,11 @@ detailHeader( ); div ( - setClass('flex flex-wrap gap-x-5'), + setClass('flex flex-normal gap-x-5'), div ( setClass('basis-2/3'), + setID('instanceInfoContainer'), detailBody ( sectionList @@ -69,11 +72,11 @@ div setClass('flex justify-between'), div ( - setClass('flex'), + setClass('flex basis-full'), img(set::src($instance->logo), setStyle(array('width' => '50px', 'height' => '50px'))), div ( - setClass('ml-3 flex col gap-y-1'), + setClass('ml-3 flex col gap-y-1 basis-full'), div ( $instance->name, setClass('text-xl'), @@ -110,8 +113,10 @@ div setStyle('background', "var(--color-{$memoryInfo['color']}-50)"), div ( - setClass('progress-bar ' . $memoryInfo['color']), + setID('memoryRate'), set::role('progressbar'), + setData('load', $instance->status == 'running' && $memoryInfo['rate'] == '0%'), + setClass('progress-bar ' . $memoryInfo['color']), setStyle('width', $memoryInfo['rate']) ) ) @@ -144,6 +149,8 @@ div ( h::td ( + setID('statusTD'), + setData('reload', in_array($instance->status, array('creating', 'initializing', 'pulling', 'startup', 'starting', 'suspending', 'installing', 'uninstalling', 'stopping', 'destroying', 'upgrading'))), span ( setClass('label label-dot mr-1 ' . zget($this->lang->instance->htmlStatusesClass, $instance->status, '')) @@ -190,7 +197,7 @@ div ), div ( - setStyle('width', '400px'), + setClass('basis-auto'), history() ) ); \ No newline at end of file diff --git a/module/space/config/dtable.php b/module/space/config/dtable.php index f5a420654b..9ab90a3eac 100644 --- a/module/space/config/dtable.php +++ b/module/space/config/dtable.php @@ -38,11 +38,13 @@ $config->space->dtable->fieldList['createdAt']['group'] = 'created'; $config->space->dtable->fieldList['actions']['type'] = 'actions'; $config->space->dtable->fieldList['actions']['menu'] = array('visit', 'start|stop', 'edit', 'bindUser', 'uninstall', 'upgrade'); -$config->space->dtable->fieldList['actions']['list']['start']['icon'] = 'play'; -$config->space->dtable->fieldList['actions']['list']['start']['hint'] = $lang->instance->start; -$config->space->dtable->fieldList['actions']['list']['start']['url'] = helper::createLink('instance', 'ajaxStart', "id={id}"); +$config->space->dtable->fieldList['actions']['list']['start']['icon'] = 'play'; +$config->space->dtable->fieldList['actions']['list']['start']['className'] = 'ajax-submit'; +$config->space->dtable->fieldList['actions']['list']['start']['hint'] = $lang->instance->start; +$config->space->dtable->fieldList['actions']['list']['start']['url'] = helper::createLink('instance', 'ajaxStart', "id={id}"); $config->space->dtable->fieldList['actions']['list']['stop']['icon'] = 'off'; +$config->space->dtable->fieldList['actions']['list']['stop']['className'] = 'ajax-submit'; $config->space->dtable->fieldList['actions']['list']['stop']['hint'] = $lang->instance->stop; $config->space->dtable->fieldList['actions']['list']['stop']['url'] = helper::createLink('instance', 'ajaxStop', "id={id}"); $config->space->dtable->fieldList['actions']['list']['stop']['data-confirm'] = $lang->instance->notices['confirmStop']; diff --git a/module/space/js/browse.ui.js b/module/space/js/browse.ui.js index c7479d5df7..85dbf54a1e 100644 --- a/module/space/js/browse.ui.js +++ b/module/space/js/browse.ui.js @@ -31,3 +31,43 @@ window.renderInstanceList = function (result, {col, row, value}) return result; } + +var refreshTimes = 0; +window.afterPageUpdate = function() +{ + if(refreshTimes > 0) return; + setTimeout(function() + { + refreshTimes++; + $.each(instances, function(index, instance) + { + if(!instance.externalID) refreshStatus(instance, index); + }); + + }, 1000); +} + +/** + * 刷新应用状态。 + * Refresh status of an instance. + * + * @param object instance + * @param int index + * @access public + * @return void + */ +function refreshStatus(instance, index) +{ + setTimeout(function() + { + $.ajaxSubmit({ + url: $.createLink('instance', 'ajaxRefresh', 'instanceID=' + instance.id), + method: 'GET', + onComplete: function(res) + { + if(res.status != instance.status) loadPage(); + } + }); + + }, 500 * index); +} \ No newline at end of file diff --git a/module/space/ui/browse.html.php b/module/space/ui/browse.html.php index 8826bc6d01..4db83f1328 100644 --- a/module/space/ui/browse.html.php +++ b/module/space/ui/browse.html.php @@ -10,6 +10,8 @@ declare(strict_types=1); */ namespace zin; +jsVar('instances', $instances); + $instances = initTableData($instances, $config->space->dtable->fieldList, $this->instance); featureBar