Conditions | 12 |
Total Lines | 99 |
Code Lines | 66 |
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.diskio.DiskioPlugin.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 | # |
||
210 | def msg_curse(self, args=None, max_width=None): |
||
211 | """Return the dict to display in the curse interface.""" |
||
212 | # Init the return message |
||
213 | ret = [] |
||
214 | |||
215 | # Only process if stats exist and display plugin enable... |
||
216 | if not self.stats or self.is_disabled(): |
||
217 | return ret |
||
218 | |||
219 | # Max size for the interface name |
||
220 | if max_width: |
||
221 | name_max_width = max_width - 13 |
||
222 | else: |
||
223 | # No max_width defined, return an empty curse message |
||
224 | logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.") |
||
225 | return ret |
||
226 | |||
227 | # Header |
||
228 | msg = '{:{width}}'.format('DISK I/O', width=name_max_width) |
||
229 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
230 | if args.diskio_iops: |
||
231 | msg = '{:>8}'.format('IOR/s') |
||
232 | ret.append(self.curse_add_line(msg)) |
||
233 | msg = '{:>7}'.format('IOW/s') |
||
234 | ret.append(self.curse_add_line(msg)) |
||
235 | elif args.diskio_latency: |
||
236 | msg = '{:>8}'.format('ms/opR') |
||
237 | ret.append(self.curse_add_line(msg)) |
||
238 | msg = '{:>7}'.format('ms/opW') |
||
239 | ret.append(self.curse_add_line(msg)) |
||
240 | else: |
||
241 | msg = '{:>8}'.format('R/s') |
||
242 | ret.append(self.curse_add_line(msg)) |
||
243 | msg = '{:>7}'.format('W/s') |
||
244 | ret.append(self.curse_add_line(msg)) |
||
245 | # Disk list (sorted by name) |
||
246 | for i in self.sorted_stats(): |
||
247 | # Hide stats if never be different from 0 (issue #1787) |
||
248 | if all(self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields): |
||
249 | continue |
||
250 | # Is there an alias for the disk name ? |
||
251 | disk_name = i['alias'] if 'alias' in i else i['disk_name'] |
||
252 | # New line |
||
253 | ret.append(self.curse_new_line()) |
||
254 | if len(disk_name) > name_max_width: |
||
255 | # Cut disk name if it is too long |
||
256 | disk_name = disk_name[:name_max_width] + '_' |
||
257 | msg = '{:{width}}'.format(nativestr(disk_name), width=name_max_width + 1) |
||
258 | ret.append(self.curse_add_line(msg)) |
||
259 | if args.diskio_iops: |
||
260 | # count |
||
261 | txps = self.auto_unit(i.get('read_count_rate_per_sec', None)) |
||
262 | rxps = self.auto_unit(i.get('write_count_rate_per_sec', None)) |
||
263 | msg = f'{txps:>7}' |
||
264 | ret.append( |
||
265 | self.curse_add_line( |
||
266 | msg, self.get_views(item=i[self.get_key()], key='read_count', option='decoration') |
||
267 | ) |
||
268 | ) |
||
269 | msg = f'{rxps:>7}' |
||
270 | ret.append( |
||
271 | self.curse_add_line( |
||
272 | msg, self.get_views(item=i[self.get_key()], key='write_count', option='decoration') |
||
273 | ) |
||
274 | ) |
||
275 | elif args.diskio_latency: |
||
276 | # latency (mean time spent reading/writing per operation) |
||
277 | txps = self.auto_unit(i.get('read_latency', None), low_precision=True) |
||
278 | rxps = self.auto_unit(i.get('write_latency', None), low_precision=True) |
||
279 | msg = f'{txps:>7}' |
||
280 | ret.append( |
||
281 | self.curse_add_line( |
||
282 | msg, self.get_views(item=i[self.get_key()], key='read_latency', option='decoration') |
||
283 | ) |
||
284 | ) |
||
285 | msg = f'{rxps:>7}' |
||
286 | ret.append( |
||
287 | self.curse_add_line( |
||
288 | msg, self.get_views(item=i[self.get_key()], key='write_latency', option='decoration') |
||
289 | ) |
||
290 | ) |
||
291 | else: |
||
292 | # Bitrate |
||
293 | txps = self.auto_unit(i.get('read_bytes_rate_per_sec', None)) |
||
294 | rxps = self.auto_unit(i.get('write_bytes_rate_per_sec', None)) |
||
295 | msg = f'{txps:>7}' |
||
296 | ret.append( |
||
297 | self.curse_add_line( |
||
298 | msg, self.get_views(item=i[self.get_key()], key='read_bytes', option='decoration') |
||
299 | ) |
||
300 | ) |
||
301 | msg = f'{rxps:>7}' |
||
302 | ret.append( |
||
303 | self.curse_add_line( |
||
304 | msg, self.get_views(item=i[self.get_key()], key='write_bytes', option='decoration') |
||
305 | ) |
||
306 | ) |
||
307 | |||
308 | return ret |
||
309 |