Commit dd32b479 authored by Julius Metz's avatar Julius Metz

add comments and validation_example_html

parent f8502ef8
...@@ -34,18 +34,25 @@ DEFAULT_FILTER = 'average' ...@@ -34,18 +34,25 @@ DEFAULT_FILTER = 'average'
### validation Config ### validation Config
# Merger that exist and posible kwargs/parameter # Merger that exist and possible kwargs/parameter
# key = merger_func name
# value = dict with kwarguments of merger_func
# kwargument dict:
# key = argument name
# value = 1. 'type' with a type obj or a tuple of type objs
# or
# 2. 'choices' list of possible choices for the kwargument
MERGER = { MERGER = {
'x2oneaddition_int': { 'x2oneaddition_int': {
'roundingpoint': {'type':int}, 'roundingpoint': {'type':int},
'operator': {'type': str, 'choices': ['+', '-', '*', '/', '%']}, 'operator': {'choices': ['+', '-', '*', '/', '%']},
'operator_value': {'type': (int, float)}, 'operator_value': {'type': (int, float)},
}, },
'x2oneaddition_float': { 'x2oneaddition_float': {
'roundingpoint': {'type':int}, 'roundingpoint': {'type':int},
'operator': {'type': str, 'choices': ['+', '-', '*', '/', '%']}, 'operator': {'type': str, 'choices': ['+', '-', '*', '/', '%']},
'operator_value': {'type': (int, float)}, 'operator_value': {'type': (int, float)},
} },
} }
# validation infos of config # validation infos of config
...@@ -180,8 +187,11 @@ def is_merger(to_check): ...@@ -180,8 +187,11 @@ def is_merger(to_check):
except ValueError: except ValueError:
return False, "'{0}' has no right merger structur expected: (name[str], kwargs[dict])".format(to_check) return False, "'{0}' has no right merger structur expected: (name[str], kwargs[dict])".format(to_check)
#check name and mergerkwargs has right types
if isinstance(name, str) and isinstance(mergerkwargs, dict): if isinstance(name, str) and isinstance(mergerkwargs, dict):
# check is the given merger known
if name in MERGER: if name in MERGER:
# check every kwarg of the given merger is it known and is right value for it given.
for kwarg_name, value in mergerkwargs.items(): for kwarg_name, value in mergerkwargs.items():
if kwarg_name in MERGER[name]: if kwarg_name in MERGER[name]:
if 'choices' in MERGER[name][kwarg_name]: if 'choices' in MERGER[name][kwarg_name]:
...@@ -209,20 +219,25 @@ def check_valid(to_check, validationinfos): ...@@ -209,20 +219,25 @@ def check_valid(to_check, validationinfos):
Arguments: Arguments:
to_check {unkown} -- object to check is valid to_check {unkown} -- object to check is valid
validationinfos {dict or type or tuple with types} -- infos what is needed that to_check is valid validationinfos {validationdict(see comment of VALIDATIONS_INFOS) or type or tuple with types} -- infos what is needed that to_check is valid
Returns: Returns:
(bool, str) -- return true if it is valid, false and error if not (bool, str) -- return true if it is valid, false and error if not
""" """
# check is validationinfos a validationdict or only a type/tuple of types
if isinstance(validationinfos, dict): if isinstance(validationinfos, dict):
if validationinfos['type'] == '__merger__': if validationinfos['type'] == '__merger__':
return is_merger(to_check) return is_merger(to_check)
else: else:
# check is given elem expected type
if isinstance(to_check, validationinfos['type']): if isinstance(to_check, validationinfos['type']):
# if elem dict check special cases of it
if type(to_check) == dict: if type(to_check) == dict:
# check is elem is empty and is this ok
if not validationinfos.get('emptyable', False) and not len(to_check): if not validationinfos.get('emptyable', False) and not len(to_check):
return False, '{0} can not be empty!'.format(to_check) return False, '{0} can not be empty!'.format(to_check)
# check special keys and there values if 'fixed_keys' is given
for fixed_name, infos in validationinfos.get('fixed_keys', {}).items(): for fixed_name, infos in validationinfos.get('fixed_keys', {}).items():
if fixed_name in to_check: if fixed_name in to_check:
if 'validator' in infos: if 'validator' in infos:
...@@ -233,6 +248,7 @@ def check_valid(to_check, validationinfos): ...@@ -233,6 +248,7 @@ def check_valid(to_check, validationinfos):
elif infos.get('required', True): elif infos.get('required', True):
return False, '{0} missing key: "{1}"'.format(to_check, fixed_name) return False, '{0} missing key: "{1}"'.format(to_check, fixed_name)
# check all keys and there values if 'changing_keys' is given
if 'changing_keys' in validationinfos: if 'changing_keys' in validationinfos:
for key, subvalue in to_check.items(): for key, subvalue in to_check.items():
if not isinstance(key, validationinfos['changing_keys']['key_type']): if not isinstance(key, validationinfos['changing_keys']['key_type']):
...@@ -243,11 +259,13 @@ def check_valid(to_check, validationinfos): ...@@ -243,11 +259,13 @@ def check_valid(to_check, validationinfos):
valid, error_msg = check_valid(subvalue, validationinfos['changing_keys']['validator']) valid, error_msg = check_valid(subvalue, validationinfos['changing_keys']['validator'])
if not valid: if not valid:
return valid, '[{0}] => {1}'.format(key, error_msg) return valid, '[{0}] => {1}'.format(key, error_msg)
# if elem list or tuple check special cases of it
if isinstance(to_check, (list, tuple)): elif isinstance(to_check, (list, tuple)):
# check is elem is empty and is this ok
if not validationinfos.get('emptyable', False) and not len(to_check): if not validationinfos.get('emptyable', False) and not len(to_check):
return False, '{0} can not be empty!'.format(to_check) return False, '{0} can not be empty!'.format(to_check)
# validate all values of tuple/list if 'subvalue_validator' is given
if 'subvalue_validator' in validationinfos: if 'subvalue_validator' in validationinfos:
for i, subvalue in enumerate(to_check): for i, subvalue in enumerate(to_check):
valid, error_msg = check_valid(subvalue, validationinfos['subvalue_validator']) valid, error_msg = check_valid(subvalue, validationinfos['subvalue_validator'])
...@@ -260,9 +278,11 @@ def check_valid(to_check, validationinfos): ...@@ -260,9 +278,11 @@ def check_valid(to_check, validationinfos):
) )
else: else:
# check it is the special case that is the type is '__merger__'
if validationinfos == '__merger__': if validationinfos == '__merger__':
return is_merger(to_check) return is_merger(to_check)
else: else:
# check is given elem expected type
if not isinstance(to_check, validationinfos): if not isinstance(to_check, validationinfos):
return False, '{0} is {1} expected: {2}'.format( return False, '{0} is {1} expected: {2}'.format(
to_check, type(to_check), validationinfos, to_check, type(to_check), validationinfos,
...@@ -598,6 +618,7 @@ def build_html(plots_dict): ...@@ -598,6 +618,7 @@ def build_html(plots_dict):
with tag('head'): with tag('head'):
with tag('script', src='plotly.js'): with tag('script', src='plotly.js'):
pass pass
# build plotly javascipt in html
with tag('script'): with tag('script'):
doc.asis("document.onreadystatechange = () => {if (document.readyState === 'complete') {") doc.asis("document.onreadystatechange = () => {if (document.readyState === 'complete') {")
for name, plots in plots_dict.items(): for name, plots in plots_dict.items():
...@@ -615,6 +636,7 @@ def build_html(plots_dict): ...@@ -615,6 +636,7 @@ def build_html(plots_dict):
with tag('li'): with tag('li'):
with tag('a', href='#'+name): with tag('a', href='#'+name):
text(name) text(name)
# add div for all plots in html
for name, plots in plots_dict.items(): for name, plots in plots_dict.items():
with tag('div', id=name): with tag('div', id=name):
for i in range(len(plots)): for i in range(len(plots)):
...@@ -663,10 +685,12 @@ def data_from_file(arguments): ...@@ -663,10 +685,12 @@ def data_from_file(arguments):
""" """
path, collectl, shorten_cmds, coarsest, filtercmds, filtervalue, filtertype, config = arguments path, collectl, shorten_cmds, coarsest, filtercmds, filtervalue, filtertype, config = arguments
host = '' host = ''
# get hostname
with gzip.open(path, 'r') as f: with gzip.open(path, 'r') as f:
for line in f: for line in f:
if line.startswith(b'# Host:'): if line.startswith(b'# Host:'):
host = re.search(r'# Host: *([^ ]+)', line.decode()).group(1) host = re.search(r'# Host: *([^ ]+)', line.decode()).group(1)
data, filter_data = parse_file(path, collectl, shorten_cmds, coarsest, config) data, filter_data = parse_file(path, collectl, shorten_cmds, coarsest, config)
filter_infos = None filter_infos = None
...@@ -722,6 +746,7 @@ def main(source, collectl, plotlypath, destination, configpath, ...@@ -722,6 +746,7 @@ def main(source, collectl, plotlypath, destination, configpath,
print('in destination "collectlplots" already exist') print('in destination "collectlplots" already exist')
exit(1) exit(1)
# load custome conf if given
config_module = default_plot_conf config_module = default_plot_conf
if configpath: if configpath:
if configpath.endswith('.py'): if configpath.endswith('.py'):
......
../../Collectl2plotly/Collectl2plotly/plotly-latest.min.js
\ No newline at end of file
This diff is collapsed.
...@@ -290,7 +290,7 @@ A merger is a function that merge a values from collectl to existing base value. ...@@ -290,7 +290,7 @@ A merger is a function that merge a values from collectl to existing base value.
### Add new Merger ### Add new Merger
In default it gives only two filter but in value_merger.py it is possible to add more. In default it gives only two filter but in value_merger.py it is possible to add more.
If one merger is added in value_merger.py he must be added in the global var MERGER in Collectl2plotly.py. If one merger is added in value_merger.py he must be added in the global variable MERGER in Collectl2plotly.py.
#### Arguments of a merger function #### Arguments of a merger function
1. base value is the initial value for merging on it. Normaly has the same type like the return value 1. base value is the initial value for merging on it. Normaly has the same type like the return value
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment