| Conditions | 23 |
| Total Lines | 108 |
| Code Lines | 80 |
| 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.vms.VmsPlugin.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 | # |
||
| 205 | def msg_curse(self, args=None, max_width: Optional[int] = None) -> list[str]: |
||
| 206 | """Return the dict to display in the curse interface.""" |
||
| 207 | # Init the return message |
||
| 208 | ret = [] |
||
| 209 | |||
| 210 | # Only process if stats exist (and non null) and display plugin enable... |
||
| 211 | if not self.stats or len(self.stats) == 0 or self.is_disabled(): |
||
| 212 | return ret |
||
| 213 | |||
| 214 | # Build the string message |
||
| 215 | # Title |
||
| 216 | msg = '{}'.format('VMs') |
||
| 217 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 218 | if len(self.stats) > 1: |
||
| 219 | msg = f' {len(self.stats)}' |
||
| 220 | ret.append(self.curse_add_line(msg)) |
||
| 221 | msg = f' sorted by {sort_for_human[self.sort_key]}' |
||
| 222 | ret.append(self.curse_add_line(msg)) |
||
| 223 | if not self.views['show_engine_name']: |
||
| 224 | msg = f' (served by {self.stats[0].get("engine", "")})' |
||
| 225 | ret.append(self.curse_add_line(msg)) |
||
| 226 | ret.append(self.curse_new_line()) |
||
| 227 | |||
| 228 | # Header |
||
| 229 | ret.append(self.curse_new_line()) |
||
| 230 | # Get the maximum VMs name length |
||
| 231 | # Max size is configurable. See feature request #1723. |
||
| 232 | name_max_width = min( |
||
| 233 | self.config.get_int_value('vms', 'max_name_size', default=20) if self.config is not None else 20, |
||
| 234 | len(max(self.stats, key=lambda x: len(x['name']))['name']), |
||
| 235 | ) |
||
| 236 | |||
| 237 | if self.views['show_engine_name']: |
||
| 238 | # Get the maximum engine length |
||
| 239 | engine_max_width = max(len(max(self.stats, key=lambda x: len(x['engine']))['engine']), 8) |
||
| 240 | msg = ' {:{width}}'.format('Engine', width=engine_max_width) |
||
| 241 | ret.append(self.curse_add_line(msg)) |
||
| 242 | msg = ' {:{width}}'.format('Name', width=name_max_width) |
||
| 243 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT')) |
||
| 244 | msg = '{:>10}'.format('Status') |
||
| 245 | ret.append(self.curse_add_line(msg)) |
||
| 246 | msg = '{:>6}'.format('Core') |
||
| 247 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_count' else 'DEFAULT')) |
||
| 248 | msg = '{:>6}'.format('CPU%') |
||
| 249 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_time' else 'DEFAULT')) |
||
| 250 | msg = '{:>7}'.format('MEM') |
||
| 251 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT')) |
||
| 252 | msg = '/{:<7}'.format('MAX') |
||
| 253 | ret.append(self.curse_add_line(msg)) |
||
| 254 | msg = '{:>17}'.format('LOAD 1/5/15min') |
||
| 255 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'load_1min' else 'DEFAULT')) |
||
| 256 | msg = '{:>10}'.format('Release') |
||
| 257 | ret.append(self.curse_add_line(msg)) |
||
| 258 | |||
| 259 | # Data |
||
| 260 | for vm in self.stats: |
||
| 261 | ret.append(self.curse_new_line()) |
||
| 262 | if self.views['show_engine_name']: |
||
| 263 | ret.append(self.curse_add_line(' {:{width}}'.format(vm["engine"], width=engine_max_width))) |
||
|
|
|||
| 264 | # Name |
||
| 265 | ret.append(self.curse_add_line(' {:{width}}'.format(vm['name'][:name_max_width], width=name_max_width))) |
||
| 266 | # Status |
||
| 267 | status = self.vm_alert(vm['status']) |
||
| 268 | msg = '{:>10}'.format(vm['status'][0:10]) |
||
| 269 | ret.append(self.curse_add_line(msg, status)) |
||
| 270 | # CPU (count) |
||
| 271 | try: |
||
| 272 | msg = '{:>6}'.format(vm['cpu_count']) |
||
| 273 | except (KeyError, TypeError): |
||
| 274 | msg = '{:>6}'.format('-') |
||
| 275 | ret.append(self.curse_add_line(msg, self.get_views(item=vm['name'], key='cpu_count', option='decoration'))) |
||
| 276 | # CPU (time) |
||
| 277 | try: |
||
| 278 | msg = '{:>6}'.format(vm['cpu_time_rate_per_sec']) |
||
| 279 | except (KeyError, TypeError): |
||
| 280 | msg = '{:>6}'.format('-') |
||
| 281 | ret.append( |
||
| 282 | self.curse_add_line( |
||
| 283 | msg, self.get_views(item=vm['name'], key='cpu_time_rate_per_sec', option='decoration') |
||
| 284 | ) |
||
| 285 | ) |
||
| 286 | # MEM |
||
| 287 | try: |
||
| 288 | msg = '{:>7}'.format(self.auto_unit(vm['memory_usage'])) |
||
| 289 | except KeyError: |
||
| 290 | msg = '{:>7}'.format('-') |
||
| 291 | ret.append( |
||
| 292 | self.curse_add_line(msg, self.get_views(item=vm['name'], key='memory_usage', option='decoration')) |
||
| 293 | ) |
||
| 294 | try: |
||
| 295 | msg = '/{:<7}'.format(self.auto_unit(vm['memory_total'])) |
||
| 296 | except (KeyError, TypeError): |
||
| 297 | msg = '/{:<7}'.format('-') |
||
| 298 | ret.append(self.curse_add_line(msg)) |
||
| 299 | # LOAD |
||
| 300 | try: |
||
| 301 | msg = '{:>5.1f}/{:>5.1f}/{:>5.1f}'.format(vm['load_1min'], vm['load_5min'], vm['load_15min']) |
||
| 302 | except (KeyError, TypeError): |
||
| 303 | msg = '{:>5}/{:>5}/{:>5}'.format('-', '-', '-') |
||
| 304 | ret.append(self.curse_add_line(msg, self.get_views(item=vm['name'], key='load_1min', option='decoration'))) |
||
| 305 | # Release |
||
| 306 | if vm['release'] is not None: |
||
| 307 | msg = ' {}'.format(vm['release']) |
||
| 308 | else: |
||
| 309 | msg = ' {}'.format('-') |
||
| 310 | ret.append(self.curse_add_line(msg, splittable=True)) |
||
| 311 | |||
| 312 | return ret |
||
| 313 | |||
| 349 |