@cryptotaxi247 / netdata-1 / commits / bfb51f930

comments

paulfantom committed Jun 23, 2016 at 15:30 UTC bfb51f93063ea8c5394a83c433eeb74455b9bc5e
1 file changed +66 -16
python.d/python_modules/base.py
+66 -16
@@ -138,6 +138,10 @@ class BaseService(threading.Thread):
138 time.sleep(self.timetable['freq'])
139
140 def _line(self, *params):
141 + """
142 + Converts *params to string and joins them with one space between every one.
143 + :param params: str/int/float
144 + """
145 for p in params:
146 if len(p) == 0:
147 p = "''"
@@ -146,70 +150,116 @@ class BaseService(threading.Thread):
150 self.data_stream += "\n"
151
152 def chart(self, type_id, name="''", title="''", units="''", family="''",
149 - category="''", charttype="''", priority="''", update_every="''"):
153 + category="''", charttype="line", priority="''", update_every="''"):
154 + """
155 + Defines a new chart.
156 + :param type_id: str
157 + :param name: str
158 + :param title: str
159 + :param units: str
160 + :param family: str
161 + :param category: str
162 + :param charttype: str
163 + :param priority: int/str
164 + :param update_every: int/str
165 + """
166 self.charts.append(type_id)
167
168 self._line("CHART", type_id, name, title, units, family, category, charttype, priority, update_every)
153 - pass
169
155 - def dimension(self, id, name="''", algorithm="''", multiplier=1, divisor=1, hidden=False):
170 + def dimension(self, id, name=None, algorithm="absolute", multiplier=1, divisor=1, hidden=False):
171 + """
172 + Defines a new dimension for the chart
173 + :param id: str
174 + :param name: str
175 + :param algorithm: str
176 + :param multiplier: int/str
177 + :param divisor: int/str
178 + :param hidden: boolean
179 + :return:
180 + """
181 try:
182 int(multiplier)
183 except TypeError:
159 - self.error("malformed dimension: multiplier is not a number")
184 + self.error("malformed dimension: multiplier is not a number:", multiplier)
185 multiplier = 1
186 try:
187 int(divisor)
188 except TypeError:
164 - self.error("malformed dimension: divisor is not a number")
189 + self.error("malformed dimension: divisor is not a number:", divisor)
190 divisor = 1
191 + if name is None:
192 + name = id
193
194 self.dimensions.append(id)
195 if hidden:
196 self._line("DIMENSION", id, name, algorithm, multiplier, divisor, "hidden")
197 else:
198 self._line("DIMENSION", id, name, algorithm, multiplier, divisor)
172 - pass
199
174 - def begin(self, type_id, microseconds="''"):
200 + def begin(self, type_id, microseconds=0):
201 + """
202 + Begin data set
203 + :param type_id: str
204 + :param microseconds: int
205 + :return: boolean
206 + """
207 if type_id not in self.charts:
176 - self.error("wrong chart type_id")
208 + self.error("wrong chart type_id:", type_id)
209 + return False
210 try:
211 int(microseconds)
212 except TypeError:
180 - self.error("malformed begin statement: microseconds are not a number")
213 + self.error("malformed begin statement: microseconds are not a number:", microseconds)
214 microseconds = "''"
215
216 self._line("BEGIN", type_id, microseconds)
184 - pass
217 + return True
218
219 def set(self, id, value):
220 + """
221 + Set value to dimension
222 + :param id: str
223 + :param value: int/float
224 + :return: boolean
225 + """
226 if id not in self.dimensions:
188 - self.error("wrong dimension id")
189 - return
227 + self.error("wrong dimension id:", id)
228 + return False
229 try:
230 value = str(int(value))
231 except TypeError:
193 - self.error("cannot set non-numeric value")
194 - return
232 + self.error("cannot set non-numeric value:", value)
233 + return False
234 self._line("SET", id, "=", value)
196 - pass
235 + return True
236
237 def end(self):
238 self._line("END")
200 - pass
239
240 def send(self):
241 + """
242 + Upload new data to netdata
243 + """
244 print(self.data_stream)
245 self.data_stream = ""
246
247 def error(self, *params):
248 + """
249 + Show error message on stderr
250 + """
251 msg.error(self.chart_name, *params)
252
253 def debug(self, *params):
254 + """
255 + Show debug message on stderr
256 + """
257 msg.debug(self.chart_name, *params)
258
259 def info(self, *params):
260 + """
261 + Show information message on stderr
262 + """
263 msg.info(self.chart_name, *params)
264
265 def check(self):