Conditions | 20 |
Total Lines | 93 |
Code Lines | 70 |
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.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 | # |
||
190 | def msg_curse(self, args=None, max_width: Optional[int] = None) -> List[str]: |
||
191 | """Return the dict to display in the curse interface.""" |
||
192 | # Init the return message |
||
193 | ret = [] |
||
194 | |||
195 | # Only process if stats exist (and non null) and display plugin enable... |
||
196 | if not self.stats or len(self.stats) == 0 or self.is_disabled(): |
||
197 | return ret |
||
198 | |||
199 | # Build the string message |
||
200 | # Title |
||
201 | msg = '{}'.format('VMs') |
||
202 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
203 | if len(self.stats) > 1: |
||
204 | msg = f' {len(self.stats)}' |
||
205 | ret.append(self.curse_add_line(msg)) |
||
206 | msg = f' sorted by {sort_for_human[self.sort_key]}' |
||
207 | ret.append(self.curse_add_line(msg)) |
||
208 | if not self.views['show_engine_name']: |
||
209 | msg = f' (served by {self.stats[0].get("engine", "")})' |
||
210 | ret.append(self.curse_add_line(msg)) |
||
211 | ret.append(self.curse_new_line()) |
||
212 | # Header |
||
213 | ret.append(self.curse_new_line()) |
||
214 | # Get the maximum VMs name |
||
215 | # Max size is configurable. See feature request #1723. |
||
216 | name_max_width = min( |
||
217 | self.config.get_int_value('vms', 'max_name_size', default=20) if self.config is not None else 20, |
||
218 | len(max(self.stats, key=lambda x: len(x['name']))['name']), |
||
219 | ) |
||
220 | |||
221 | if self.views['show_engine_name']: |
||
222 | msg = ' {:{width}}'.format('Engine', width=8) |
||
223 | ret.append(self.curse_add_line(msg)) |
||
224 | msg = ' {:{width}}'.format('Name', width=name_max_width) |
||
225 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'name' else 'DEFAULT')) |
||
226 | msg = '{:>10}'.format('Status') |
||
227 | ret.append(self.curse_add_line(msg)) |
||
228 | msg = '{:>6}'.format('Core') |
||
229 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'cpu_count' else 'DEFAULT')) |
||
230 | msg = '{:>7}'.format('MEM') |
||
231 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'memory_usage' else 'DEFAULT')) |
||
232 | msg = '/{:<7}'.format('MAX') |
||
233 | ret.append(self.curse_add_line(msg)) |
||
234 | msg = '{:>17}'.format('LOAD 1/5/15min') |
||
235 | ret.append(self.curse_add_line(msg, 'SORT' if self.sort_key == 'load_1min' else 'DEFAULT')) |
||
236 | msg = '{:>10}'.format('Release') |
||
237 | ret.append(self.curse_add_line(msg)) |
||
238 | |||
239 | # Data |
||
240 | for vm in self.stats: |
||
241 | ret.append(self.curse_new_line()) |
||
242 | if self.views['show_engine_name']: |
||
243 | ret.append(self.curse_add_line(' {:{width}}'.format(vm["engine"], width=8))) |
||
244 | # Name |
||
245 | ret.append(self.curse_add_line(' {:{width}}'.format(vm['name'][:name_max_width], width=name_max_width))) |
||
246 | # Status |
||
247 | status = self.vm_alert(vm['status']) |
||
248 | msg = '{:>10}'.format(vm['status'][0:10]) |
||
249 | ret.append(self.curse_add_line(msg, status)) |
||
250 | # CPU (count) |
||
251 | try: |
||
252 | msg = '{:>6}'.format(vm['cpu_count']) |
||
253 | except (KeyError, TypeError): |
||
254 | msg = '{:>6}'.format('-') |
||
255 | ret.append(self.curse_add_line(msg, self.get_views(item=vm['name'], key='cpu_count', option='decoration'))) |
||
256 | # MEM |
||
257 | try: |
||
258 | msg = '{:>7}'.format(self.auto_unit(vm['memory_usage'])) |
||
259 | except KeyError: |
||
260 | msg = '{:>7}'.format('-') |
||
261 | ret.append( |
||
262 | self.curse_add_line(msg, self.get_views(item=vm['name'], key='memory_usage', option='decoration')) |
||
263 | ) |
||
264 | try: |
||
265 | msg = '/{:<7}'.format(self.auto_unit(vm['memory_total'])) |
||
266 | except (KeyError, TypeError): |
||
267 | msg = '/{:<7}'.format('-') |
||
268 | ret.append(self.curse_add_line(msg)) |
||
269 | # LOAD |
||
270 | try: |
||
271 | msg = '{:>5.1f}/{:>5.1f}/{:>5.1f}'.format(vm['load_1min'], vm['load_5min'], vm['load_15min']) |
||
272 | except (KeyError, TypeError): |
||
273 | msg = '{:>5}/{:>5}/{:>5}'.format('-', '-', '-') |
||
274 | ret.append(self.curse_add_line(msg, self.get_views(item=vm['name'], key='load_1min', option='decoration'))) |
||
275 | # Release |
||
276 | if vm['release'] is not None: |
||
277 | msg = ' {}'.format(vm['release']) |
||
278 | else: |
||
279 | msg = ' {}'.format('-') |
||
280 | ret.append(self.curse_add_line(msg, splittable=True)) |
||
281 | |||
282 | return ret |
||
283 | |||
319 |