From a7bf5573fa94fac39d5bc1b4bc8ae6de174410fc Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Wed, 24 Oct 2018 08:04:04 +0200 Subject: [PATCH] baculum: Add new size directive control It also fixes a bug with saving size directives reported by Catin King. --- gui/baculum/protected/Web/Data/data_desc.json | 32 ++--- gui/baculum/protected/Web/JavaScript/misc.js | 59 ++++++-- .../Web/Portlets/BaculaConfigDirectives.php | 2 + .../Web/Portlets/BaculaConfigDirectives.tpl | 5 + .../protected/Web/Portlets/DirectiveSize.php | 133 ++++++++++++++++++ .../protected/Web/Portlets/DirectiveSize.tpl | 32 +++++ .../Web/Portlets/DirectiveTimePeriod.tpl | 2 + 7 files changed, 234 insertions(+), 31 deletions(-) create mode 100644 gui/baculum/protected/Web/Portlets/DirectiveSize.php create mode 100644 gui/baculum/protected/Web/Portlets/DirectiveSize.tpl diff --git a/gui/baculum/protected/Web/Data/data_desc.json b/gui/baculum/protected/Web/Data/data_desc.json index afbc5276b..6ee4ff38f 100644 --- a/gui/baculum/protected/Web/Data/data_desc.json +++ b/gui/baculum/protected/Web/Data/data_desc.json @@ -667,7 +667,7 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "ReRunFailedLevels": { "Required": false, @@ -1473,7 +1473,7 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "CatalogFiles": { "Required": false, @@ -1503,13 +1503,13 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MigrationLowBytes": { "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "NextPool": { "Required": false, @@ -2099,7 +2099,7 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "ReRunFailedLevels": { "Required": false, @@ -2767,49 +2767,49 @@ "Required": false, "ValueType": "size32", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MaximumBlockSize": { "Required": false, "ValueType": "maxblocksize", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "PaddingSize": { "Required": false, "ValueType": "size32", "DefaultValue": 4096, - "FieldType": "TextBox" + "FieldType": "Size" }, "FileAlignment": { "Required": false, "ValueType": "size32", "DefaultValue": 4096, - "FieldType": "TextBox" + "FieldType": "Size" }, "MaximumVolumeSize": { "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MaximumFileSize": { "Required": false, "ValueType": "size64", "DefaultValue": 1000000000, - "FieldType": "TextBox" + "FieldType": "Size" }, "VolumeCapacity": { "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MinimumFeeSpace": { "Required": false, "ValueType": "size64", "DefaultValue": 5000000, - "FieldType": "TextBox" + "FieldType": "Size" }, "MaximumConcurrentJobs": { "Required": false, @@ -2827,13 +2827,13 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MaximumJobSpoolSize": { "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "DriveIndex": { "Required": false, @@ -2845,7 +2845,7 @@ "Required": false, "ValueType": "size64", "DefaultValue": 0, - "FieldType": "TextBox" + "FieldType": "Size" }, "MountPoint": { "Required": false, diff --git a/gui/baculum/protected/Web/JavaScript/misc.js b/gui/baculum/protected/Web/JavaScript/misc.js index fdcfd57c7..85ac75855 100644 --- a/gui/baculum/protected/Web/JavaScript/misc.js +++ b/gui/baculum/protected/Web/JavaScript/misc.js @@ -1,16 +1,28 @@ var Units = { - formats: [ - {format: 'second', value: 1}, - {format: 'minute', value: 60}, - {format: 'hour', value: 60}, - {format: 'day', value: 24} - ], - units: ['K', 'M', 'G', 'T', 'P'], + units: { + size: [ + {short: '', long: 'byte', value: 1}, + {short: 'K', long: 'kilobyte', value: 1000}, + {short: 'M', long: 'megabyte', value: 1000}, + {short: 'G', long: 'gigabyte', value: 1000}, + {short: 'T', long: 'terabyte', value: 1000}, + {short: 'P', long: 'petabyte', value: 1000} + ], + time: [ + {long: 'second', value: 1}, + {long: 'minute', value: 60}, + {long: 'hour', value: 60}, + {long: 'day', value: 24} + ] + }, get_decimal_size: function(size) { var dec_size; var size_unit = 'B'; - var units = Units.units.slice(0); - + var units = []; + for (var u in Units.units.size) { + units.push(Units.units.size[u].short); + } + units.shift(); // remove "byte" unit if (size === null) { size = 0; } @@ -35,15 +47,32 @@ var Units = { } return dec_size; }, + format_size: function(size_bytes, format) { + var reminder; + var f = this.units.size[0].long; + for (var i = 0; i < this.units.size.length; i++) { + if (this.units.size[i].long != format && size_bytes) { + reminder = size_bytes % this.units.size[i].value + if (reminder === 0) { + size_bytes /= this.units.size[i].value; + f = this.units.size[i].long; + continue; + } + break; + } + } + var ret = {value: size_bytes, format: f}; + return ret; + }, format_time_period: function(time_seconds, format) { var reminder; - var f; - for (var i = 0; i < this.formats.length; i++) { - if (this.formats[i].format != format) { - reminder = time_seconds % this.formats[i].value; + var f = this.units.time[0].long; + for (var i = 0; i < this.units.time.length; i++) { + if (this.units.time[i].long != format && time_seconds) { + reminder = time_seconds % this.units.time[i].value; if (reminder === 0) { - time_seconds /= this.formats[i].value; - f = this.formats[i].format; + time_seconds /= this.units.time[i].value; + f = this.units.time[i].long; continue; } break; diff --git a/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php b/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php index 5eeacb22b..3a89fdb64 100644 --- a/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php +++ b/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php @@ -30,6 +30,7 @@ Prado::using('Application.Web.Portlets.DirectiveBoolean'); Prado::using('Application.Web.Portlets.DirectiveComboBox'); Prado::using('Application.Web.Portlets.DirectiveInteger'); Prado::using('Application.Web.Portlets.DirectiveListBox'); +Prado::using('Application.Web.Portlets.DirectiveSize'); Prado::using('Application.Web.Portlets.DirectiveText'); Prado::using('Application.Web.Portlets.DirectiveTimePeriod'); Prado::using('Application.Web.Portlets.DirectiveRunscript'); @@ -52,6 +53,7 @@ class BaculaConfigDirectives extends DirectiveListTemplate { 'DirectiveInteger', 'DirectiveListBox', 'DirectiveText', + 'DirectiveSize', 'DirectiveTimePeriod' ); diff --git a/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.tpl b/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.tpl index 38fdb2f64..1751c1564 100644 --- a/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.tpl +++ b/gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.tpl @@ -71,6 +71,11 @@ + + + + + diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.php b/gui/baculum/protected/Web/Portlets/DirectiveSize.php new file mode 100644 index 000000000..660533550 --- /dev/null +++ b/gui/baculum/protected/Web/Portlets/DirectiveSize.php @@ -0,0 +1,133 @@ + 'byte', 'value' => 1, 'label' => 'Bytes'), + array('format' => 'kilobyte', 'value' => 1000, 'label' => 'Kilobytes'), + array('format' => 'megabyte', 'value' => 1000, 'label' => 'Megabytes'), + array('format' => 'gigabyte', 'value' => 1000, 'label' => 'Gigabytes'), + array('format' => 'terabyte', 'value' => 1000, 'label' => 'Terabytes') + ); + + public function getValue() { + $value = $this->Directive->Text; + if (is_numeric($value)) { + settype($value, 'integer'); + $size_format = $this->SizeFormat->SelectedValue; + $value = $this->getValueBytes($value, $size_format); + } else { + $value = null; + } + return $value; + } + + public function getSizeFormat() { + return $this->getViewState(self::SIZE_FORMAT, self::DEFAULT_SIZE_FORMAT); + } + + public function setSizeFormat($format) { + $this->setViewState(self::SIZE_FORMAT, $format); + } + + public function getSizeFormats() { + $size_formats = array(); + for ($i = 0; $i < count($this->size_formats); $i++) { + $format = array( + 'label' => Prado::localize($this->size_formats[$i]['label']), + 'format' => $this->size_formats[$i]['format'] + ); + array_push($size_formats, $format); + } + return $size_formats; + } + + public function createDirective() { + $size_format = $this->getSizeFormat(); + $directive_value = $this->getDirectiveValue(); + $default_value = $this->getDefaultValue(); + if ($this->getInConfig() === false) { + if ($default_value !== 0) { + $directive_value = $default_value; + } else { + $directive_value = 0; + } + } + $formatted_value = $this->formatSize($directive_value, $size_format); + $this->Directive->Text = $formatted_value['value']; + $this->SizeFormat->DataSource = $this->size_formats; + $this->SizeFormat->SelectedValue = $formatted_value['format']; + $this->SizeFormat->dataBind(); + $this->Label->Text = $this->getLabel(); + $validate = $this->getRequired(); + $this->DirectiveValidator->setVisible($validate); + } + + /** + * Convert original size in bytes into given size format. + * + * Note, if there is not possible to convert size value into given format + * then there will be returned value converted by using as close format as possible. + * Example: + * size_value: 121000 + * given format: byte + * returned value: 121 + * returned format: kilobyte + */ + private function formatSize($size_bytes, $format) { + $value = $size_bytes; + for ($i = 0; $i < count($this->size_formats); $i++) { + if ($this->size_formats[$i]['format'] != $format) { + $remainder = $value % $this->size_formats[$i]['value']; + if ($remainder === 0) { + $value /= $this->size_formats[$i]['value']; + $format = $this->size_formats[$i]['format']; + continue; + } + break; + } + break; + } + $result = array('value' => $value, 'format' => $format); + return $result; + } + + private function getValueBytes($value, $size_format) { + for ($i = 0; $i < count($this->size_formats); $i++) { + $value *= $this->size_formats[$i]['value']; + if ($this->size_formats[$i]['format'] === $size_format) { + break; + } + } + return $value; + } +} +?> diff --git a/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl b/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl new file mode 100644 index 000000000..4ff363e14 --- /dev/null +++ b/gui/baculum/protected/Web/Portlets/DirectiveSize.tpl @@ -0,0 +1,32 @@ +
+
:
+
+ + <%=$this->getRequired() ? ' ' : ''%> + + + +
+
diff --git a/gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.tpl b/gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.tpl index 251fc2642..135244c04 100644 --- a/gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.tpl +++ b/gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.tpl @@ -14,6 +14,8 @@ DataValueField="format" Visible="<%=$this->display_directive%>" ActiveControl.EnableUpdate="false" + AutoPostBack="false" + OnSelectedIndexChanged="saveValue" /> <%=$this->getRequired() ? ' ' : ''%> -- 2.47.3