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