@cryptotaxi247 / netdata-1 / commits / 4e92014bf

simple is better. replace class Checks with some if checks.

lgz committed Oct 13, 2017 at 19:13 UTC 4e92014bf1e84b8377c5d3cb5fbdc206d59fb46e
1 file changed +20 -51
python.d/python_modules/bases/charts.py
+20 -51
@@ -43,46 +43,6 @@ class ReAddingError(Exception):
43 pass
44
45
46 -class Checks:
47 - @staticmethod
48 - def chart_params(params, chart_name):
49 - """
50 - :param params: <list>
51 - :param chart_name: <str>
52 - :return:
53 - """
54 - for param in CHART_PARAMS:
55 - try:
56 - params[param]
57 - except KeyError:
58 - raise KeyError('{param} is missing ({chart})'.format(param=param, chart=chart_name))
59 - value = params[param]
60 - if any([param == 'id', param == 'units', param == 'context', param == 'chart_type']):
61 - if not value:
62 - raise ValueError('Wrong chart value ({chart}). {param} must not be empty'.format(chart=chart_name,
63 - param=param))
64 - if param == 'chart_type':
65 - if value not in CHART_TYPES:
66 - raise ValueError('Wrong chart type ({chart}). Must be on of {types}'.format(chart=chart_name,
67 - types=CHART_TYPES))
68 -
69 - @staticmethod
70 - def dimension_params(params, chart_name):
71 - """
72 - :param params: <list>
73 - :param chart_name: <str>
74 - :return:
75 - """
76 - if not params.get('id'):
77 - raise ValueError('Wrong dimension id ({chart}). Must not be empty'.format(chart=chart_name))
78 - if params['algorithm'] not in DIMENSION_ALGORITHMS:
79 - raise ValueError('Wrong dimension algorithm ({chart}). '
80 - 'Must be one of {algorithms}'.format(chart=chart_name,
81 - algorithms=DIMENSION_ALGORITHMS))
82 - if not (str(params['multiplier']).isdigit() and str(params['divisor']).isdigit()):
83 - raise ValueError('Wrong multiplier or divisor ({chart}). Must be a digit'.format(chart=chart_name))
84 -
85 -
46 class Charts:
47 def __init__(self, job_name, priority, update_every):
48 """
@@ -146,9 +106,14 @@ class Chart:
106 """
107 :param params: <list>
108 """
109 + if not params or not isinstance(params, list):
110 + raise ValueError('Chart params must be a not empty list')
111 +
112 self.params = dict(zip(CHART_PARAMS, (p or str() for p in params)))
113 self.name = '.'.join([self.params['type'], self.params['id']])
151 - Checks.chart_params(self.params, self.name)
114 + if self.params.get('chart_type') not in CHART_TYPES:
115 + self.params['chart_type'] = 'absolute'
116 +
117 self.dimensions = list()
118 self.penalty = 0
119
@@ -169,20 +134,20 @@ class Chart:
134 :param dimension: <list>
135 :return:
136 """
172 - if dimension[0] in self.dimensions:
137 + if dimension[0] in [repr(d) for d in self.dimensions]:
138 raise ReAddingError('{dimension} already in {chart} dimensions'.format(dimension=dimension[0],
139 chart=self.name))
175 - self.dimensions.append(Dimension(dimension, self.name))
140 + self.dimensions.append(Dimension(dimension))
141
142 def add_dimension_and_push_chart(self, dimension):
143 """
144 :param dimension: <list>
145 :return:
146 """
182 - if dimension[0] in self.dimensions:
147 + if dimension[0] in [repr(d) for d in self.dimensions]:
148 raise ReAddingError('{dimension} already in {chart} dimensions'.format(dimension=dimension[0],
149 chart=self.name))
185 - dimension = Dimension(dimension, self.name)
150 + dimension = Dimension(dimension)
151 self.dimensions.append(dimension)
152 safe_print(self.create(dimension))
153
@@ -212,18 +177,22 @@ class Chart:
177
178
179 class Dimension:
215 - def __init__(self, params, chart_name):
180 + def __init__(self, params):
181 """
182 :param params: <list>
218 - :param chart_name: <str>
183 """
184 + if not params or not isinstance(params, list):
185 + raise ValueError('Dimension params must be a not empty list')
186 +
187 self.params = dict(zip(DIMENSION_PARAMS, (p or str() for p in params)))
188 self.params['name'] = self.params.get('name') or self.params.get('id')
222 - self.params.setdefault('algorithm', 'absolute')
223 - self.params.setdefault('multiplier', 1)
224 - self.params.setdefault('divisor', 1)
189 + if self.params.get('algorithm') not in DIMENSION_ALGORITHMS:
190 + self.params['algorithm'] = 'absolute'
191 + if not str(self.params.get('multiplier')).isdigit():
192 + self.params['multiplier'] = 1
193 + if not str(self.params.get('divisor')).isdigit():
194 + self.params['divisor'] = 1
195 self.params.setdefault('hidden', '')
226 - Checks.dimension_params(self.params, chart_name)
196
197 def __repr__(self):
198 return self.params['id']