| Conditions | 18 |
| Total Lines | 87 |
| Code Lines | 63 |
| 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.raid.RaidPlugin.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 | # |
||
| 68 | def msg_curse(self, args=None, max_width=None): |
||
| 69 | """Return the dict to display in the curse interface.""" |
||
| 70 | # Init the return message |
||
| 71 | ret = [] |
||
| 72 | |||
| 73 | # Only process if stats exist... |
||
| 74 | if not self.stats or self.is_disabled(): |
||
| 75 | return ret |
||
| 76 | |||
| 77 | # Max size for the interface name |
||
| 78 | if max_width: |
||
| 79 | name_max_width = max_width - 12 |
||
| 80 | else: |
||
| 81 | # No max_width defined, return an empty curse message |
||
| 82 | logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") |
||
| 83 | return ret |
||
| 84 | |||
| 85 | # Header |
||
| 86 | msg = '{:{width}}'.format('RAID disks', width=name_max_width) |
||
| 87 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 88 | msg = '{:>7}'.format('Used') |
||
| 89 | ret.append(self.curse_add_line(msg)) |
||
| 90 | msg = '{:>7}'.format('Avail') |
||
| 91 | ret.append(self.curse_add_line(msg)) |
||
| 92 | # Data |
||
| 93 | arrays = sorted(self.stats.keys()) |
||
| 94 | for array in arrays: |
||
| 95 | array_stats = self.stats[array] |
||
| 96 | |||
| 97 | if not isinstance(array_stats, dict): |
||
| 98 | continue |
||
| 99 | |||
| 100 | # Display the current status |
||
| 101 | status = self.raid_alert( |
||
| 102 | array_stats['status'], |
||
| 103 | array_stats['used'], |
||
| 104 | array_stats['available'], |
||
| 105 | array_stats['type'], |
||
| 106 | ) |
||
| 107 | |||
| 108 | # New line |
||
| 109 | ret.append(self.curse_new_line()) |
||
| 110 | # Data: RAID type name | disk used | disk available |
||
| 111 | array_type = array_stats['type'].upper() if array_stats['type'] is not None else 'UNKNOWN' |
||
| 112 | # Build the full name = array type + array name |
||
| 113 | full_name = f'{array_type} {array}' |
||
| 114 | msg = '{:{width}}'.format(full_name, width=name_max_width) |
||
| 115 | ret.append(self.curse_add_line(msg)) |
||
| 116 | if array_stats['type'] == 'raid0' and array_stats['status'] == 'active': |
||
| 117 | msg = '{:>7}'.format(len(array_stats['components'])) |
||
| 118 | ret.append(self.curse_add_line(msg, status)) |
||
| 119 | msg = '{:>7}'.format('-') |
||
| 120 | ret.append(self.curse_add_line(msg, status)) |
||
| 121 | elif array_stats['status'] == 'active': |
||
| 122 | msg = '{:>7}'.format(array_stats['used']) |
||
| 123 | ret.append(self.curse_add_line(msg, status)) |
||
| 124 | msg = '{:>7}'.format(array_stats['available']) |
||
| 125 | ret.append(self.curse_add_line(msg, status)) |
||
| 126 | elif array_stats['status'] == 'inactive': |
||
| 127 | ret.append(self.curse_new_line()) |
||
| 128 | msg = '└─ Status {}'.format(array_stats['status']) |
||
| 129 | ret.append(self.curse_add_line(msg, status)) |
||
| 130 | components = sorted(array_stats['components'].keys()) |
||
| 131 | for i, component in enumerate(components): |
||
| 132 | if i == len(components) - 1: |
||
| 133 | tree_char = '└─' |
||
| 134 | else: |
||
| 135 | tree_char = '├─' |
||
| 136 | ret.append(self.curse_new_line()) |
||
| 137 | msg = ' {} disk {}: '.format(tree_char, array_stats['components'][component]) |
||
| 138 | ret.append(self.curse_add_line(msg)) |
||
| 139 | msg = f'{component}' |
||
| 140 | ret.append(self.curse_add_line(msg)) |
||
| 141 | |||
| 142 | if array_stats['type'] != 'raid0' and ( |
||
| 143 | array_stats['used'] and array_stats['available'] and array_stats['used'] < array_stats['available'] |
||
| 144 | ): |
||
| 145 | # Display current array configuration |
||
| 146 | ret.append(self.curse_new_line()) |
||
| 147 | msg = '└─ Degraded mode' |
||
| 148 | ret.append(self.curse_add_line(msg, status)) |
||
| 149 | if len(array_stats['config']) < 17: |
||
| 150 | ret.append(self.curse_new_line()) |
||
| 151 | msg = ' └─ {}'.format(array_stats['config'].replace('_', 'A')) |
||
| 152 | ret.append(self.curse_add_line(msg)) |
||
| 153 | |||
| 154 | return ret |
||
| 155 | |||
| 173 |