| Conditions | 18 |
| Total Lines | 105 |
| Code Lines | 64 |
| 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.network.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 | # -*- coding: utf-8 -*- |
||
| 215 | def msg_curse(self, args=None, max_width=None): |
||
| 216 | """Return the dict to display in the curse interface.""" |
||
| 217 | # Init the return message |
||
| 218 | ret = [] |
||
| 219 | |||
| 220 | # Only process if stats exist and display plugin enable... |
||
| 221 | if not self.stats or self.is_disabled(): |
||
| 222 | return ret |
||
| 223 | |||
| 224 | # Max size for the interface name |
||
| 225 | if max_width: |
||
| 226 | name_max_width = max_width - 12 |
||
| 227 | else: |
||
| 228 | # No max_width defined, return an emptu curse message |
||
| 229 | logger.debug("No max_width defined for the {} plugin, it will not be displayed.".format(self.plugin_name)) |
||
| 230 | return ret |
||
| 231 | |||
| 232 | # Header |
||
| 233 | msg = '{:{width}}'.format('NETWORK', width=name_max_width) |
||
| 234 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 235 | if args.network_cumul: |
||
| 236 | # Cumulative stats |
||
| 237 | if args.network_sum: |
||
| 238 | # Sum stats |
||
| 239 | msg = '{:>14}'.format('Rx+Tx') |
||
| 240 | ret.append(self.curse_add_line(msg)) |
||
| 241 | else: |
||
| 242 | # Rx/Tx stats |
||
| 243 | msg = '{:>7}'.format('Rx') |
||
| 244 | ret.append(self.curse_add_line(msg)) |
||
| 245 | msg = '{:>7}'.format('Tx') |
||
| 246 | ret.append(self.curse_add_line(msg)) |
||
| 247 | else: |
||
| 248 | # Bitrate stats |
||
| 249 | if args.network_sum: |
||
| 250 | # Sum stats |
||
| 251 | msg = '{:>14}'.format('Rx+Tx/s') |
||
| 252 | ret.append(self.curse_add_line(msg)) |
||
| 253 | else: |
||
| 254 | msg = '{:>7}'.format('Rx/s') |
||
| 255 | ret.append(self.curse_add_line(msg)) |
||
| 256 | msg = '{:>7}'.format('Tx/s') |
||
| 257 | ret.append(self.curse_add_line(msg)) |
||
| 258 | # Interface list (sorted by name) |
||
| 259 | for i in self.sorted_stats(): |
||
| 260 | # Do not display interface in down state (issue #765) |
||
| 261 | if ('is_up' in i) and (i['is_up'] is False): |
||
| 262 | continue |
||
| 263 | # Hide stats if never be different from 0 (issue #1787) |
||
| 264 | if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]): |
||
| 265 | continue |
||
| 266 | # Format stats |
||
| 267 | # Is there an alias for the interface name ? |
||
| 268 | if i['alias'] is None: |
||
| 269 | if_name = i['interface_name'].split(':')[0] |
||
| 270 | else: |
||
| 271 | if_name = i['alias'] |
||
| 272 | if len(if_name) > name_max_width: |
||
| 273 | # Cut interface name if it is too long |
||
| 274 | if_name = '_' + if_name[-name_max_width + 1 :] |
||
| 275 | |||
| 276 | if args.byte: |
||
| 277 | # Bytes per second (for dummy) |
||
| 278 | to_bit = 1 |
||
| 279 | unit = '' |
||
| 280 | else: |
||
| 281 | # Bits per second (for real network administrator | Default) |
||
| 282 | to_bit = 8 |
||
| 283 | unit = 'b' |
||
| 284 | |||
| 285 | if args.network_cumul and 'bytes_recv' in i: |
||
| 286 | rx = self.auto_unit(int(i['bytes_recv'] * to_bit)) + unit |
||
| 287 | tx = self.auto_unit(int(i['bytes_sent'] * to_bit)) + unit |
||
| 288 | ax = self.auto_unit(int(i['bytes_all'] * to_bit)) + unit |
||
| 289 | elif 'bytes_recv_rate_per_sec' in i: |
||
| 290 | rx = self.auto_unit(int(i['bytes_recv_rate_per_sec'] * to_bit)) + unit |
||
| 291 | tx = self.auto_unit(int(i['bytes_sent_rate_per_sec'] * to_bit)) + unit |
||
| 292 | ax = self.auto_unit(int(i['bytes_all_rate_per_sec'] * to_bit)) + unit |
||
| 293 | else: |
||
| 294 | # Avoid issue when a new interface is created on the fly |
||
| 295 | # Example: start Glances, then start a new container |
||
| 296 | continue |
||
| 297 | |||
| 298 | # New line |
||
| 299 | ret.append(self.curse_new_line()) |
||
| 300 | msg = '{:{width}}'.format(if_name, width=name_max_width) |
||
| 301 | ret.append(self.curse_add_line(msg)) |
||
| 302 | if args.network_sum: |
||
| 303 | msg = '{:>14}'.format(ax) |
||
| 304 | ret.append(self.curse_add_line(msg)) |
||
| 305 | else: |
||
| 306 | msg = '{:>7}'.format(rx) |
||
| 307 | ret.append( |
||
| 308 | self.curse_add_line( |
||
| 309 | msg, self.get_views(item=i[self.get_key()], key='bytes_recv', option='decoration') |
||
| 310 | ) |
||
| 311 | ) |
||
| 312 | msg = '{:>7}'.format(tx) |
||
| 313 | ret.append( |
||
| 314 | self.curse_add_line( |
||
| 315 | msg, self.get_views(item=i[self.get_key()], key='bytes_sent', option='decoration') |
||
| 316 | ) |
||
| 317 | ) |
||
| 318 | |||
| 319 | return ret |
||
| 320 |