def test_size_format(self): """Test size_format function""" pat = 'abc' expect = 'N/A' result = size_format(pat) self.assertEqual(expect, result) pat = None expect = 'N/A' result = size_format(pat) self.assertEqual(expect, result) pat = 1024 expect = '1.0KB' result = size_format(pat) self.assertEqual(expect, result) pat = '1024' expect = '1.0KB' result = size_format(pat) self.assertEqual(expect, result)
def adjust_values(func, gen, links, pkey): """ Helper function to adjust values in UI. It groups values for identical key, make links for provided mapped function, represent "Number of" keys as integers and represents size values in GB format. The mapped function is the one from das_mapping_db which convert UI key into triplet of das key, das access key and link, see das_mapping_db:daskey_from_presentation """ rdict = {} uidict = {} for uikey, value, uilink, uidesc, uiexamples in [k for k, _g in groupby(gen)]: val = quote(value) if uikey in rdict: existing_val = rdict[uikey] if not isinstance(existing_val, list): existing_val = [existing_val] if val not in existing_val: rdict[uikey] = existing_val + [val] else: rdict[uikey] = val uidict[uikey] = (uilink, uidesc, uiexamples) page = "" to_show = [] green = 'style="color:green"' red = 'style="color:red"' for key, val in rdict.iteritems(): uilink, _uidesc, _uiexamples = uidict[key] if uilink and val: if not isinstance(val, list): val = [val] values = [] for elem in val: for ilink in uilink: dasquery = ilink['query'] % elem val = '<a href="/das/request?input=%s">%s</a>' \ % (dasquery, elem) values.append(val) to_show.append((key, ', '.join(values))) continue lookup = func(key) if key.lower() == 'reason' or key.lower() == 'qhash': continue if key.lower() == 'error': key = '<span %s>WARNING</span>' % red val = json.dumps(val) + ', click on show link to get more info<br/>' if lookup: if key.find('Member') != -1 and val: link = '/das/request?input=user%3D' if isinstance(val, list): val = ['<a href="%s%s">%s</a>' \ % (link, quote(v), quote(v)) for v in val] elif isinstance(val, basestring): val = '<a href="%s%s">%s</a>' \ % (link, quote(val), quote(val)) if isinstance(val, list): value = ', '.join([str(v) for v in val]) try: length = len(set(val)) except TypeError: # happens when val list contains a dict length = len(val) if length > 1 and \ (key.lower().find('number') != -1 or \ key.lower().find('size') != -1): if key not in ['Luminosity number', 'Run number']: value = '<span %s>%s</span>' % (red, value) elif key.lower().find('size') != -1 and val: value = size_format(val) elif key.find('Number of ') != -1 and val and int(val) != 0: value = int(val) elif key.find('Run number') != -1 and val: value = int(val) elif key.find('Lumi') != -1 and val: value = int(val) elif key.find('Tag') != -1 and val: if isinstance(val, basestring) and val.lower() == 'unknown': value = '<span %s>%s</span>' % (red, val) else: value = val elif key.find('Creation time') != -1 and val: try: value = time.strftime('%d/%b/%Y %H:%M:%S GMT', \ time.gmtime(val)) except: value = val else: value = val if isinstance(value, list) and isinstance(value[0], str): value = ', '.join(value) if key == 'Open' or key == 'Custodial': if value == 'n': value = '<span %s>%s</span>' % (green, value) else: value = '<span %s>%s</span>' % (red, value) if key.lower().find('presence') != -1 or \ key.lower().find('completion') != -1: if not value: continue else: if value == '100.00%': value = '<span %s>100%%</span>' % green else: value = '<span %s>%s</span>' % (red, value) key = tooltip_helper(key) to_show.append((key, value)) else: if key == 'result' and isinstance(val, dict) and \ 'value' in val: # result of aggregation function if 'key' in rdict and rdict['key'].find('.size') != -1: val = size_format(val['value']) elif isinstance(val['value'], float): val = '%.2f' % val['value'] else: val = val['value'] to_show.append((key, val)) if to_show: page += '<br />' tdict = {} for key, val in to_show: tdict[key] = val result_keys = ['function', 'result', 'key'] if set(tdict.keys()) & set(result_keys) == set(result_keys): page += '%s(%s)=%s' \ % (tdict['function'], tdict['key'], tdict['result']) else: rlist = ["%s: %s" \ % (k[0].capitalize()+k[1:], v) for k, v in to_show] rlist.sort() page += ', '.join(rlist) page = page.replace('<br/>,', '<br/>') if links: page += '<br />' + ', '.join(links) return page
def adjust_values(func, gen, links): """ Helper function to adjust values in UI. It groups values for identical key, make links for provided mapped function, represent "Number of" keys as integers and represents size values in GB format. The mapped function is the one from das_mapping_db which convert UI key into triplet of das key, das access key and link, see das_mapping_db:daskey_from_presentation """ rdict = {} for uikey, value in [k for k, _g in groupby(gen)]: val = quote(value) if rdict.has_key(uikey): existing_val = rdict[uikey] if not isinstance(existing_val, list): existing_val = [existing_val] if val not in existing_val: rdict[uikey] = existing_val + [val] else: rdict[uikey] = val page = "" to_show = [] error = 0 green = 'style="color:green"' red = 'style="color:red"' for key, val in rdict.iteritems(): lookup = func(key) if key.lower() == 'reason' or key.lower() == 'qhash': continue if key.lower() == 'error': key = '<span %s>WARNING</span>' % red error = 1 if val and isinstance(val, basestring): val += '<br/>' if lookup: if key.find('Member') != -1 and val: link = '/das/request?input=user%3D' if isinstance(val, list): val = ['<a href="%s%s">%s</a>' \ % (link, quote(v), quote(v)) for v in val] elif isinstance(val, basestring): val = '<a href="%s%s">%s</a>' \ % (link, quote(val), quote(val)) if isinstance(val, list): value = ', '.join([str(v) for v in val]) if len(set(val)) > 1 and \ (key.lower().find('number') != -1 or \ key.lower().find('size') != -1): value = '<span %s>%s</span>' % (red, value) elif key.lower().find('size') != -1 and val: value = size_format(val) elif key.find('Number of ') != -1 and val: value = int(val) elif key.find('Run number') != -1 and val: value = int(val) elif key.find('Lumi') != -1 and val: value = int(val) elif key.find('Creation time') != -1 and val: try: value = time.strftime('%d/%b/%Y %H:%M:%S GMT', \ time.gmtime(val)) except: value = val else: value = val if isinstance(value, list) and isinstance(value[0], str): value = ', '.join(value) if key == 'Open': if value == 'n': value = '<span %s>%s</span>' % (green, value) else: value = '<span %s>%s</span>' % (red, value) if key.find('Status') != -1: if value == 'VALID': value = '<span %s>%s</span>' % (green, value) else: value = '<span %s>%s</span>' % (red, value) if key.lower().find('presence') != -1 or \ key.lower().find('completion') != -1: if not value: continue else: if value == '100.00%': value = '<span %s>100%%</span>' % green else: value = '<span %s>%s</span>' % (red, value) key = tooltip_helper(key) to_show.append((key, value)) else: if key == 'result' and isinstance(val, dict) and \ val.has_key('value'): # result of aggregation function if rdict.has_key('key') and \ rdict['key'].find('.size') != -1: val = size_format(val['value']) elif isinstance(val['value'], float): val = '%.2f' % val['value'] else: val = val['value'] to_show.append((key, val)) if to_show: page += '<br />' tdict = {} for key, val in to_show: tdict[key] = val if set(tdict.keys()) == set(['function', 'result', 'key']): page += '%s(%s)=%s' \ % (tdict['function'], tdict['key'], tdict['result']) else: rlist = ["%s: %s" \ % (k[0].capitalize()+k[1:], v) for k, v in to_show] rlist.sort() page += ', '.join(rlist) page = page.replace('<br/>,', '<br/>') if links and not error: page += '<br />' + ', '.join(links) return page
def adjust_values(func, gen, links, pkey): """ Helper function to adjust values in UI. It groups values for identical key, make links for provided mapped function, represent "Number of" keys as integers and represents size values in GB format. The mapped function is the one from das_mapping_db which convert UI key into triplet of das key, das access key and link, see das_mapping_db:daskey_from_presentation """ rdict = {} uidict = {} for uikey, value, uilink, uidesc, uiexamples in [k for k, _g in groupby(gen)]: val = quote(value) if uikey in rdict: existing_val = rdict[uikey] if not isinstance(existing_val, list): existing_val = [existing_val] if val not in existing_val: rdict[uikey] = existing_val + [val] else: rdict[uikey] = val uidict[uikey] = (uilink, uidesc, uiexamples) page = "" to_show = [] green = 'style="color:green"' red = 'style="color:red"' for key, val in rdict.items(): uilink, _uidesc, _uiexamples = uidict[key] if uilink and val: if not isinstance(val, list): val = [val] values = [] for elem in val: for ilink in uilink: dasquery = ilink['query'] % elem val = '<a href="/das/request?input=%s">%s</a>' \ % (dasquery, elem) values.append(val) to_show.append((key, ', '.join(values))) continue lookup = func(key) if key.lower() == 'reason' or key.lower() == 'qhash' or key.lower() == 'hints': continue if key.lower() == 'error': key = '<span %s>WARNING</span>' % red val = json.dumps(val) + ', click on show link to get more info<br/>' if lookup: if key.find('Member') != -1 and val: link = '/das/request?input=user%3D' if isinstance(val, list): val = ['<a href="%s%s">%s</a>' \ % (link, quote(v), quote(v)) for v in val] elif isinstance(val, basestring): val = '<a href="%s%s">%s</a>' \ % (link, quote(val), quote(val)) if key.find('Config urls') != -1 and val: if isinstance(val, dict): urls = [] for rtype, rurls in val.items(): for vdx in range(len(rurls)): urls.append('<a href="%s">%s-config-%d</a>' % (rurls[vdx], rtype, vdx+1)) value = ', '.join(urls) else: value = '<a href="%s">config</a>' % val key = tooltip_helper(key) elif isinstance(val, list): value = ', '.join([str(v) for v in val]) try: length = len(set(val)) except TypeError: # happens when val list contains a dict length = len(val) if length > 1 and \ (key.lower().find('number') != -1 or \ key.lower().find('size') != -1): if key not in ['Luminosity number', 'Run number']: value = '<span %s>%s</span>' % (red, value) elif key.lower().find('size') != -1 and val: value = size_format(val) elif key.find('Number of ') != -1 and val and int(val) != 0: value = int(val) elif key.find('Run number') != -1 and val: value = int(val) elif key.find('Lumi') != -1 and val: value = int(val) elif key.find('Site type') != -1 and val: if isinstance(val, basestring) and val.lower() == 'disk': value = val else: value = '<span %s><b>TAPE</b></span> <b>no user access</b>' % red elif key.find('Tag') != -1 and val: if isinstance(val, basestring) and val.lower() == 'unknown': value = '<span %s>%s</span>' % (red, val) else: value = val elif key.find('Creation time') != -1 and val: try: value = time.strftime('%d/%b/%Y %H:%M:%S GMT', \ time.gmtime(val)) except: value = val else: value = val if isinstance(value, list) and isinstance(value[0], str): value = ', '.join(value) if key == 'Open' or key == 'Custodial': if value == 'n': value = '<span %s>%s</span>' % (green, value) else: value = '<span %s>%s</span>' % (red, value) if key.lower().find('presence') != -1 or \ key.lower().find('completion') != -1: if not value: continue else: if value == '100.00%': value = '<span %s>100%%</span>' % green else: value = '<span %s>%s</span>' % (red, value) key = tooltip_helper(key) to_show.append((key, value)) else: if key == 'result' and isinstance(val, dict) and \ 'value' in val: # result of aggregation function if 'key' in rdict and rdict['key'].find('.size') != -1: val = size_format(val['value']) elif isinstance(val['value'], float): val = '%.2f' % val['value'] else: val = val['value'] to_show.append((key, val)) if to_show: to_show = list(set(to_show)) page += '<br />' tdict = {} for key, val in to_show: tdict[key] = val result_keys = ['function', 'result', 'key'] if set(tdict.keys()) & set(result_keys) == set(result_keys): page += '%s(%s)=%s' \ % (tdict['function'], tdict['key'], tdict['result']) elif sorted(tdict.keys()) == sorted(['Luminosity number', 'Run number']): page += 'Run number: %s, Luminosity ranges: %s' \ % (tdict['Run number'], convert2ranges(rdict['Luminosity number'])) elif sorted(tdict.keys()) == sorted(['Events', 'Luminosity number', 'Run number']): page += 'Run number: %s, Luminosity ranges: %s' \ % (tdict['Run number'], convert2ranges(rdict['Luminosity number'])) page += lumi_evts(rdict) elif sorted(tdict.keys()) == sorted(['Luminosity number']): page += 'Luminosity ranges: %s' \ % (convert2ranges(rdict['Luminosity number'])) elif sorted(tdict.keys()) == sorted(['Events', 'Luminosity number']): page += 'Luminosity ranges: %s' \ % (convert2ranges(rdict['Luminosity number'])) page += lumi_evts(rdict) else: rlist = ["%s: %s" \ % (k[0].capitalize()+k[1:], v) for k, v in to_show] rlist.sort() page += ', '.join(rlist) page = page.replace('<br/>,', '<br/>') if links: page += '<br />' + ', '.join(links) return page