Conditions | 27 |
Total Lines | 155 |
Code Lines | 119 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like glances.plugins.containers.PluginModel.msg_curse() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
237 | def msg_curse(self, args=None, max_width=None): |
||
238 | """Return the dict to display in the curse interface.""" |
||
239 | # Init the return message |
||
240 | ret = [] |
||
241 | |||
242 | # Only process if stats exist (and non null) and display plugin enable... |
||
243 | if not self.stats or 'containers' not in self.stats or len(self.stats['containers']) == 0 or self.is_disabled(): |
||
244 | return ret |
||
245 | |||
246 | show_pod_name = False |
||
247 | if any(ct.get("pod_name") for ct in self.stats["containers"]): |
||
248 | show_pod_name = True |
||
249 | |||
250 | show_engine_name = False |
||
251 | if len(set(ct["engine"] for ct in self.stats["containers"])) > 1: |
||
252 | show_engine_name = True |
||
253 | |||
254 | # Build the string message |
||
255 | # Title |
||
256 | msg = '{}'.format('CONTAINERS') |
||
257 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
258 | msg = ' {}'.format(len(self.stats['containers'])) |
||
259 | ret.append(self.curse_add_line(msg)) |
||
260 | msg = ' sorted by {}'.format(sort_for_human[self.sort_key]) |
||
261 | ret.append(self.curse_add_line(msg)) |
||
262 | # msg = ' (served by Docker {})'.format(self.stats['version']["Version"]) |
||
263 | # ret.append(self.curse_add_line(msg)) |
||
264 | ret.append(self.curse_new_line()) |
||
265 | # Header |
||
266 | ret.append(self.curse_new_line()) |
||
267 | # Get the maximum containers name |
||
268 | # Max size is configurable. See feature request #1723. |
||
269 | name_max_width = min( |
||
270 | self.config.get_int_value('containers', 'max_name_size', default=20) if self.config is not None else 20, |
||
271 | len(max(self.stats['containers'], key=lambda x: len(x['name']))['name']), |
||
272 | ) |
||
273 | |||
274 | if show_engine_name: |
||
275 | msg = ' {:{width}}'.format('Engine', width=6) |
||
276 | ret.append(self.curse_add_line(msg)) |
||
277 | if show_pod_name: |
||
278 | msg = ' {:{width}}'.format('Pod', width=12) |
||
279 | ret.append(self.curse_add_line(msg)) |
||
280 | msg = ' {:{width}}'.format('Name', width=name_max_width) |
||
281 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT')) |
||
282 | msg = '{:>10}'.format('Status') |
||
283 | ret.append(self.curse_add_line(msg)) |
||
284 | msg = '{:>10}'.format('Uptime') |
||
285 | ret.append(self.curse_add_line(msg)) |
||
286 | msg = '{:>6}'.format('CPU%') |
||
287 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT')) |
||
288 | msg = '{:>7}'.format('MEM') |
||
289 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT')) |
||
290 | msg = '/{:<7}'.format('MAX') |
||
291 | ret.append(self.curse_add_line(msg)) |
||
292 | msg = '{:>7}'.format('IOR/s') |
||
293 | ret.append(self.curse_add_line(msg)) |
||
294 | msg = ' {:<7}'.format('IOW/s') |
||
295 | ret.append(self.curse_add_line(msg)) |
||
296 | msg = '{:>7}'.format('Rx/s') |
||
297 | ret.append(self.curse_add_line(msg)) |
||
298 | msg = ' {:<7}'.format('Tx/s') |
||
299 | ret.append(self.curse_add_line(msg)) |
||
300 | msg = ' {:8}'.format('Command') |
||
301 | ret.append(self.curse_add_line(msg)) |
||
302 | |||
303 | # Data |
||
304 | for container in self.stats['containers']: |
||
305 | ret.append(self.curse_new_line()) |
||
306 | if show_engine_name: |
||
307 | ret.append(self.curse_add_line(' {:{width}}'.format(container["engine"], width=6))) |
||
308 | if show_pod_name: |
||
309 | ret.append(self.curse_add_line(' {:{width}}'.format(container.get("pod_id", "-"), width=12))) |
||
310 | # Name |
||
311 | ret.append(self.curse_add_line(self._msg_name(container=container, max_width=name_max_width))) |
||
312 | # Status |
||
313 | status = self.container_alert(container['Status']) |
||
314 | msg = '{:>10}'.format(container['Status'][0:10]) |
||
315 | ret.append(self.curse_add_line(msg, status)) |
||
316 | # Uptime |
||
317 | if container['Uptime']: |
||
318 | msg = '{:>10}'.format(container['Uptime']) |
||
319 | else: |
||
320 | msg = '{:>10}'.format('_') |
||
321 | ret.append(self.curse_add_line(msg)) |
||
322 | # CPU |
||
323 | try: |
||
324 | msg = '{:>6.1f}'.format(container['cpu']['total']) |
||
325 | except KeyError: |
||
326 | msg = '{:>6}'.format('_') |
||
327 | ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='cpu', option='decoration'))) |
||
328 | # MEM |
||
329 | try: |
||
330 | msg = '{:>7}'.format(self.auto_unit(container['memory']['usage'])) |
||
331 | except KeyError: |
||
332 | msg = '{:>7}'.format('_') |
||
333 | ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='mem', option='decoration'))) |
||
334 | try: |
||
335 | msg = '/{:<7}'.format(self.auto_unit(container['memory']['limit'])) |
||
336 | except KeyError: |
||
337 | msg = '/{:<7}'.format('_') |
||
338 | ret.append(self.curse_add_line(msg)) |
||
339 | # IO R/W |
||
340 | unit = 'B' |
||
341 | try: |
||
342 | value = self.auto_unit(int(container['io']['ior'] // container['io']['time_since_update'])) + unit |
||
343 | msg = '{:>7}'.format(value) |
||
344 | except KeyError: |
||
345 | msg = '{:>7}'.format('_') |
||
346 | ret.append(self.curse_add_line(msg)) |
||
347 | try: |
||
348 | value = self.auto_unit(int(container['io']['iow'] // container['io']['time_since_update'])) + unit |
||
349 | msg = ' {:<7}'.format(value) |
||
350 | except KeyError: |
||
351 | msg = ' {:<7}'.format('_') |
||
352 | ret.append(self.curse_add_line(msg)) |
||
353 | # NET RX/TX |
||
354 | if args.byte: |
||
355 | # Bytes per second (for dummy) |
||
356 | to_bit = 1 |
||
357 | unit = '' |
||
358 | else: |
||
359 | # Bits per second (for real network administrator | Default) |
||
360 | to_bit = 8 |
||
361 | unit = 'b' |
||
362 | try: |
||
363 | value = ( |
||
364 | self.auto_unit( |
||
365 | int(container['network']['rx'] // container['network']['time_since_update'] * to_bit) |
||
366 | ) |
||
367 | + unit |
||
368 | ) |
||
369 | msg = '{:>7}'.format(value) |
||
370 | except KeyError: |
||
371 | msg = '{:>7}'.format('_') |
||
372 | ret.append(self.curse_add_line(msg)) |
||
373 | try: |
||
374 | value = ( |
||
375 | self.auto_unit( |
||
376 | int(container['network']['tx'] // container['network']['time_since_update'] * to_bit) |
||
377 | ) |
||
378 | + unit |
||
379 | ) |
||
380 | msg = ' {:<7}'.format(value) |
||
381 | except KeyError: |
||
382 | msg = ' {:<7}'.format('_') |
||
383 | ret.append(self.curse_add_line(msg)) |
||
384 | # Command |
||
385 | if container['Command'] is not None: |
||
386 | msg = ' {}'.format(' '.join(container['Command'])) |
||
387 | else: |
||
388 | msg = ' {}'.format('_') |
||
389 | ret.append(self.curse_add_line(msg, splittable=True)) |
||
390 | |||
391 | return ret |
||
392 | |||
431 |