Conditions | 24 |
Total Lines | 145 |
Code Lines | 113 |
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 -*- |
||
314 | def msg_curse(self, args=None, max_width=None): |
||
315 | """Return the dict to display in the curse interface.""" |
||
316 | # Init the return message |
||
317 | ret = [] |
||
318 | |||
319 | # Only process if stats exist (and non null) and display plugin enable... |
||
320 | if not self.stats or len(self.stats) == 0 or self.is_disabled(): |
||
321 | return ret |
||
322 | |||
323 | # Build the string message |
||
324 | # Title |
||
325 | msg = '{}'.format('CONTAINERS') |
||
326 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
327 | msg = ' {}'.format(len(self.stats)) |
||
328 | ret.append(self.curse_add_line(msg)) |
||
329 | msg = ' sorted by {}'.format(sort_for_human[self.sort_key]) |
||
330 | ret.append(self.curse_add_line(msg)) |
||
331 | ret.append(self.curse_new_line()) |
||
332 | # Header |
||
333 | ret.append(self.curse_new_line()) |
||
334 | # Get the maximum containers name |
||
335 | # Max size is configurable. See feature request #1723. |
||
336 | name_max_width = min( |
||
337 | self.config.get_int_value('containers', 'max_name_size', default=20) if self.config is not None else 20, |
||
338 | len(max(self.stats, key=lambda x: len(x['name']))['name']), |
||
339 | ) |
||
340 | |||
341 | if self.views['show_engine_name']: |
||
342 | msg = ' {:{width}}'.format('Engine', width=6) |
||
343 | ret.append(self.curse_add_line(msg)) |
||
344 | if self.views['show_pod_name']: |
||
345 | msg = ' {:{width}}'.format('Pod', width=12) |
||
346 | ret.append(self.curse_add_line(msg)) |
||
347 | msg = ' {:{width}}'.format('Name', width=name_max_width) |
||
348 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT')) |
||
349 | msg = '{:>10}'.format('Status') |
||
350 | ret.append(self.curse_add_line(msg)) |
||
351 | msg = '{:>10}'.format('Uptime') |
||
352 | ret.append(self.curse_add_line(msg)) |
||
353 | msg = '{:>6}'.format('CPU%') |
||
354 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_percent' else 'DEFAULT')) |
||
355 | msg = '{:>7}'.format('MEM') |
||
356 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT')) |
||
357 | msg = '/{:<7}'.format('MAX') |
||
358 | ret.append(self.curse_add_line(msg)) |
||
359 | msg = '{:>7}'.format('IOR/s') |
||
360 | ret.append(self.curse_add_line(msg)) |
||
361 | msg = ' {:<7}'.format('IOW/s') |
||
362 | ret.append(self.curse_add_line(msg)) |
||
363 | msg = '{:>7}'.format('Rx/s') |
||
364 | ret.append(self.curse_add_line(msg)) |
||
365 | msg = ' {:<7}'.format('Tx/s') |
||
366 | ret.append(self.curse_add_line(msg)) |
||
367 | msg = ' {:8}'.format('Command') |
||
368 | ret.append(self.curse_add_line(msg)) |
||
369 | |||
370 | # Data |
||
371 | for container in self.stats: |
||
372 | ret.append(self.curse_new_line()) |
||
373 | if self.views['show_engine_name']: |
||
374 | ret.append(self.curse_add_line(' {:{width}}'.format(container["engine"], width=6))) |
||
375 | if self.views['show_pod_name']: |
||
376 | ret.append(self.curse_add_line(' {:{width}}'.format(container.get("pod_id", "-"), width=12))) |
||
377 | # Name |
||
378 | ret.append(self.curse_add_line(self._msg_name(container=container, max_width=name_max_width))) |
||
379 | # Status |
||
380 | status = self.container_alert(container['status']) |
||
381 | msg = '{:>10}'.format(container['status'][0:10]) |
||
382 | ret.append(self.curse_add_line(msg, status)) |
||
383 | # Uptime |
||
384 | if container['uptime']: |
||
385 | msg = '{:>10}'.format(container['uptime']) |
||
386 | else: |
||
387 | msg = '{:>10}'.format('_') |
||
388 | ret.append(self.curse_add_line(msg)) |
||
389 | # CPU |
||
390 | try: |
||
391 | msg = '{:>6.1f}'.format(container['cpu']['total']) |
||
392 | except (KeyError, TypeError): |
||
393 | msg = '{:>6}'.format('_') |
||
394 | ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='cpu', option='decoration'))) |
||
395 | # MEM |
||
396 | try: |
||
397 | msg = '{:>7}'.format(self.auto_unit(self.memory_usage_no_cache(container['memory']))) |
||
398 | except KeyError: |
||
399 | msg = '{:>7}'.format('_') |
||
400 | ret.append(self.curse_add_line(msg, self.get_views(item=container['name'], key='mem', option='decoration'))) |
||
401 | try: |
||
402 | msg = '/{:<7}'.format(self.auto_unit(container['memory']['limit'])) |
||
403 | except (KeyError, TypeError): |
||
404 | msg = '/{:<7}'.format('_') |
||
405 | ret.append(self.curse_add_line(msg)) |
||
406 | # IO R/W |
||
407 | unit = 'B' |
||
408 | try: |
||
409 | value = self.auto_unit(int(container['io_rx'])) + unit |
||
410 | msg = '{:>7}'.format(value) |
||
411 | except (KeyError, TypeError): |
||
412 | msg = '{:>7}'.format('_') |
||
413 | ret.append(self.curse_add_line(msg)) |
||
414 | try: |
||
415 | value = self.auto_unit(int(container['io_wx'])) + unit |
||
416 | msg = ' {:<7}'.format(value) |
||
417 | except (KeyError, TypeError): |
||
418 | msg = ' {:<7}'.format('_') |
||
419 | ret.append(self.curse_add_line(msg)) |
||
420 | # NET RX/TX |
||
421 | if args.byte: |
||
422 | # Bytes per second (for dummy) |
||
423 | to_bit = 1 |
||
424 | unit = '' |
||
425 | else: |
||
426 | # Bits per second (for real network administrator | Default) |
||
427 | to_bit = 8 |
||
428 | unit = 'b' |
||
429 | try: |
||
430 | value = ( |
||
431 | self.auto_unit( |
||
432 | int(container['network_rx'] * to_bit) |
||
433 | ) |
||
434 | + unit |
||
435 | ) |
||
436 | msg = '{:>7}'.format(value) |
||
437 | except (KeyError, TypeError): |
||
438 | msg = '{:>7}'.format('_') |
||
439 | ret.append(self.curse_add_line(msg)) |
||
440 | try: |
||
441 | value = ( |
||
442 | self.auto_unit( |
||
443 | int(container['network_tx'] * to_bit) |
||
444 | ) |
||
445 | + unit |
||
446 | ) |
||
447 | msg = ' {:<7}'.format(value) |
||
448 | except (KeyError, TypeError): |
||
449 | msg = ' {:<7}'.format('_') |
||
450 | ret.append(self.curse_add_line(msg)) |
||
451 | # Command |
||
452 | if container['command'] is not None: |
||
453 | msg = ' {}'.format(container['command']) |
||
454 | else: |
||
455 | msg = ' {}'.format('_') |
||
456 | ret.append(self.curse_add_line(msg, splittable=True)) |
||
457 | |||
458 | return ret |
||
459 | |||
498 |