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