From mboxrd@z Thu Jan 1 00:00:00 1970 From: kupcevic@sourceware.org Date: 6 Oct 2006 22:08:17 -0000 Subject: [Cluster-devel] conga/luci site/luci/Extensions/HelperFunction ... Message-ID: <20061006220817.3993.qmail@sourceware.org> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit CVSROOT: /cvs/cluster Module name: conga Changes by: kupcevic at sourceware.org 2006-10-06 22:08:13 Modified files: luci/site/luci/Extensions: HelperFunctions.py StorageReport.py luci/storage : form-macros validate_html Log message: luci storage: display size in appropriate units (instead of bytes), taking preferred size into account Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/site/luci/Extensions/HelperFunctions.py.diff?cvsroot=cluster&r1=1.3&r2=1.4 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/site/luci/Extensions/StorageReport.py.diff?cvsroot=cluster&r1=1.9&r2=1.10 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/storage/form-macros.diff?cvsroot=cluster&r1=1.5&r2=1.6 http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/storage/validate_html.diff?cvsroot=cluster&r1=1.1&r2=1.2 --- conga/luci/site/luci/Extensions/HelperFunctions.py 2006/10/05 23:21:39 1.3 +++ conga/luci/site/luci/Extensions/HelperFunctions.py 2006/10/06 22:08:13 1.4 @@ -90,3 +90,49 @@ expires='Tue, 30 Jun 2060 12:00:00 GMT') return value + + + + +# returns (str(float), units) that fits best, +# ignores prefered units +def bytes_to_value_units(bytes): + units = 'TB' + if float(convert_bytes(bytes, 'GB')) < 1024: + units = 'GB' + if float(convert_bytes(bytes, 'MB')) < 1024: + units = 'MB' + if float(convert_bytes(bytes, 'KB')) < 1024: + units = 'KB' + if float(convert_bytes(bytes, 'bytes')) < 1024: + units = 'bytes' + return (convert_bytes(bytes, units), units) + +# returns (str(float), units) that fits best, +# takes prefered units into account +def bytes_to_value_prefunits(self, bytes): + p_units = self.REQUEST.SESSION.get('preferred_size_units') + dummy, units = bytes_to_value_units(bytes) + if get_units_multiplier(units) > get_units_multiplier(p_units): + units = p_units + return (convert_bytes(bytes, units), units) + +def get_units_multiplier(units): + if units.lower() == 'bytes': + return 1.0 + elif units.lower() == 'kb': + return 1024.0 + elif units.lower() == 'mb': + return 1024*1024.0 + elif units.lower() == 'gb': + return 1024*1024*1024.0 + elif units.lower() == 'tb': + return 1024*1024*1024*1024.0 + else: + raise "invalid size unit" + +def convert_bytes(bytes, units): + c = int(bytes) / get_units_multiplier(units) + c = str(c) + c = c[:c.find('.') + 3] + return c --- conga/luci/site/luci/Extensions/StorageReport.py 2006/10/05 23:21:39 1.9 +++ conga/luci/site/luci/Extensions/StorageReport.py 2006/10/06 22:08:13 1.10 @@ -232,7 +232,7 @@ try: rep = get_storage_report(ricci_comm, session) if rep == None: - raise Exception, 'Unable to communicate to host' + raise Exception, 'Unable to communicate with host (either system down or ricci not running on it)' else: return True except Exception, e: @@ -300,7 +300,7 @@ if module_r_status != '0': #raise Exception, 'error retrieving storage report' if module_r_status == '3': - raise Exception, 'Unable to find storage module: reinstall it' + raise Exception, 'Unable to find storage module on host (reinstall it)' resp_r = None for node in module_r.childNodes: if node.nodeType == xml.dom.Node.ELEMENT_NODE: @@ -334,9 +334,9 @@ if succ_v.get_value() != True: # error if err_code_v.get_value() == -1: - raise Exception, 'Generic error:\n\n' + err_desc_v.get_value() + raise Exception, 'Generic error on host:\n\n' + err_desc_v.get_value() else: - raise Exception, err_desc_v.get_value() + raise Exception, 'Host responded: ' + err_desc_v.get_value() #xml_report = fr_r.toxml() xml_report = fr_r @@ -765,17 +765,17 @@ pass if props != None: - res = check_props(props, request) + res = check_props(self, props, request) if res[0] == False: return res[1] + ' ' + res[2] if content_props != None: - res = check_props(content_props, request) + res = check_props(self, content_props, request) if res[0] == False: return res[1] + ' ' + res[2] return 'OK' -def check_props(props, request): +def check_props(self, props, request): valid = True var_name = '' msg = 'no message - BUG :(' @@ -785,23 +785,43 @@ prop = props[prop_name] req_value = request[prop_name] if prop['type'] == 'int': - try: - req_value = int(req_value) - except: - msg = prop['pretty_name'] + ' is missing an integer value' - var_name = prop_name - valid = False - break - min = int(prop['validation']['min']) - max = int(prop['validation']['max']) - step = int(prop['validation']['step']) - r_val = (req_value / step) * step - if r_val > max or r_val < min: - msg = prop['pretty_name'] + ' has to be within range ' + str(min) + ' - ' + str(max) + ' ' + prop['units'] - var_name = prop_name - valid = False - break - + if prop['units'] != 'bytes': + try: + req_value = int(req_value) + except: + msg = prop['pretty_name'] + ' is missing an integer value' + var_name = prop_name + valid = False + break + min = int(prop['validation']['min']) + max = int(prop['validation']['max']) + step = int(prop['validation']['step']) + r_val = (req_value / step) * step + if r_val > max or r_val < min: + msg = prop['pretty_name'] + ' has to be within range ' + str(min) + ' - ' + str(max) + ' ' + prop['units'] + var_name = prop_name + valid = False + break + else: + try: + req_value = float(req_value) + except: + msg = prop['pretty_name'] + ' is missing a float value' + var_name = prop_name + valid = False + break + dummy, units = bytes_to_value_prefunits(self, prop['value']) + min = float(convert_bytes(prop['validation']['min'], units)) + max = float(convert_bytes(prop['validation']['max'], units)) + step = float(convert_bytes(prop['validation']['step'], units)) + if step < 0.000001: + step = 0.000001 + r_val = (req_value / step) * step + if r_val > max or r_val < min: + msg = prop['pretty_name'] + ' has to be within range ' + str(min) + ' - ' + str(max) + ' ' + units + var_name = prop_name + valid = False + break elif prop['type'] == 'text': if len(req_value) < int(prop['validation']['min_length']): msg = prop['pretty_name'] + ' has to have minimum length of ' + prop['validation']['min_length'] @@ -893,9 +913,23 @@ var_name = node.getAttribute('name') if var_name in request: if bd_data['props'][var_name]['type'] == 'int': - val = int(request[var_name]) - step = int(bd_data['props'][var_name]['validation']['step']) - val = (val / step) * step + if bd_data['props'][var_name]['units'] != 'bytes': + val = int(request[var_name]) + step = int(bd_data['props'][var_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + bd_data['props'][var_name]['value']) + min = int(bd_data['props'][var_name]['validation']['min']) + max = int(bd_data['props'][var_name]['validation']['max']) + step = int(bd_data['props'][var_name]['validation']['step']) + val_units = float(request[var_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[var_name]) @@ -925,9 +959,23 @@ req_name = 'content_variable_' + selected_content_id + '_' + var_name if req_name in request: if selected_content_data['props'][req_name]['type'] == 'int': - val = int(request[req_name]) - step = int(selected_content_data['props'][req_name]['validation']['step']) - val = (val / step) * step + if selected_content_data['props'][req_name]['units'] != 'bytes': + val = int(request[req_name]) + step = int(selected_content_data['props'][req_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + selected_content_data['props'][req_name]['value']) + min = int(selected_content_data['props'][req_name]['validation']['min']) + max = int(selected_content_data['props'][req_name]['validation']['max']) + step = int(selected_content_data['props'][req_name]['validation']['step']) + val_units = float(request[req_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[req_name]) @@ -1035,9 +1083,23 @@ var_name = node.getAttribute('name') if var_name in request: if bd_data['props'][var_name]['type'] == 'int': - val = int(request[var_name]) - step = int(bd_data['props'][var_name]['validation']['step']) - val = (val / step) * step + if bd_data['props'][var_name]['units'] != 'bytes': + val = int(request[var_name]) + step = int(bd_data['props'][var_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + bd_data['props'][var_name]['value']) + min = int(bd_data['props'][var_name]['validation']['min']) + max = int(bd_data['props'][var_name]['validation']['max']) + step = int(bd_data['props'][var_name]['validation']['step']) + val_units = float(request[var_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[var_name]) @@ -1068,9 +1130,23 @@ req_name = 'content_variable_' + selected_content_id + '_' + var_name if req_name in request: if selected_content_data['props'][req_name]['type'] == 'int': - val = int(request[req_name]) - step = int(selected_content_data['props'][req_name]['validation']['step']) - val = (val / step) * step + if selected_content_data['props'][req_name]['units'] != 'bytes': + val = int(request[req_name]) + step = int(selected_content_data['props'][req_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + selected_content_data['props'][req_name]['value']) + min = int(selected_content_data['props'][req_name]['validation']['min']) + max = int(selected_content_data['props'][req_name]['validation']['max']) + step = int(selected_content_data['props'][req_name]['validation']['step']) + val_units = float(request[req_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[req_name]) @@ -1200,9 +1276,23 @@ var_name = node.getAttribute('name') if var_name in request: if mapper_data['props'][var_name]['type'] == 'int': - val = int(request[var_name]) - step = int(mapper_data['props'][var_name]['validation']['step']) - val = (val / step) * step + if mapper_data['props'][var_name]['units'] != 'bytes': + val = int(request[var_name]) + step = int(mapper_data['props'][var_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + mapper_data['props'][var_name]['value']) + min = int(mapper_data['props'][var_name]['validation']['min']) + max = int(mapper_data['props'][var_name]['validation']['max']) + step = int(mapper_data['props'][var_name]['validation']['step']) + val_units = float(request[var_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[var_name]) @@ -1288,9 +1378,23 @@ var_name = node.getAttribute('name') if request.has_key(var_name): if mapper_data['props'][var_name]['type'] == 'int': - val = int(request[var_name]) - step = int(mapper_data['props'][var_name]['validation']['step']) - val = (val / step) * step + if mapper_data['props'][var_name]['units'] != 'bytes': + val = int(request[var_name]) + step = int(mapper_data['props'][var_name]['validation']['step']) + val = (val / step) * step + else: + dummy, units = bytes_to_value_prefunits(self, + mapper_data['props'][var_name]['value']) + min = int(mapper_data['props'][var_name]['validation']['min']) + max = int(mapper_data['props'][var_name]['validation']['max']) + step = int(mapper_data['props'][var_name]['validation']['step']) + val_units = float(request[var_name]) + val = int(val_units * get_units_multiplier(units)) + val = (val / step) * step + if val < min: + val = min + if val > max: + val = max node.setAttribute('value', str(val)) else: node.setAttribute('value', request[var_name]) @@ -1422,7 +1526,8 @@ type = bd_xml.getAttribute('mapper_type') pretty_name = path.replace('/dev/','') pretty_type = 'Block Device' - description = props['size']['value'] + ' ' + props['size']['units'] + size_in_units, units = bytes_to_value_units(props['size']['value']) + description = str(size_in_units) + ' ' + units icon_name = '' color = 'black' if type == MAPPER_SYS_TYPE: @@ -1553,10 +1658,6 @@ min = var.getAttribute('min') max = var.getAttribute('max') step = var.getAttribute('step') - if d_units == 'bytes': - # modify units - - pass validation_data['min'] = str(min) validation_data['max'] = str(max) validation_data['step'] = str(step) --- conga/luci/storage/form-macros 2006/10/05 23:21:40 1.5 +++ conga/luci/storage/form-macros 2006/10/06 22:08:13 1.6 @@ -189,10 +189,16 @@ @@ -201,7 +207,9 @@ Display Devices by -