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