| Conditions | 14 |
| Total Lines | 63 |
| Code Lines | 46 |
| 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.glances_connections.Plugin.update() 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 -*- |
||
| 61 | @GlancesPlugin._check_decorator |
||
| 62 | @GlancesPlugin._log_result_decorator |
||
| 63 | def update(self): |
||
| 64 | """Update connections stats using the input method. |
||
| 65 | |||
| 66 | Stats is a dict |
||
| 67 | """ |
||
| 68 | # Init new stats |
||
| 69 | stats = self.get_init_value() |
||
| 70 | |||
| 71 | if self.input_method == 'local': |
||
| 72 | # Update stats using the PSUtils lib |
||
| 73 | |||
| 74 | # Grab network interface stat using the psutil net_connections method |
||
| 75 | if stats['net_connections_enabled']: |
||
| 76 | try: |
||
| 77 | net_connections = psutil.net_connections(kind="tcp") |
||
| 78 | except Exception as e: |
||
| 79 | logger.warning('Can not get network connections stats ({})'.format(e)) |
||
| 80 | logger.info('Disable connections stats') |
||
| 81 | stats['net_connections_enabled'] = False |
||
| 82 | self.stats = stats |
||
| 83 | return self.stats |
||
| 84 | |||
| 85 | for s in self.status_list: |
||
| 86 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 87 | initiated = 0 |
||
| 88 | for s in self.initiated_states: |
||
| 89 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 90 | initiated += stats[s] |
||
| 91 | stats['initiated'] = initiated |
||
| 92 | terminated = 0 |
||
| 93 | for s in self.initiated_states: |
||
| 94 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 95 | terminated += stats[s] |
||
| 96 | stats['terminated'] = terminated |
||
| 97 | |||
| 98 | if stats['nf_conntrack_enabled']: |
||
| 99 | # Grab connections track directly from the /proc file |
||
| 100 | for i in self.conntrack: |
||
| 101 | try: |
||
| 102 | with open(self.conntrack[i], 'r') as f: |
||
| 103 | stats[i] = float(f.readline().rstrip("\n")) |
||
| 104 | except (IOError, FileNotFoundError) as e: |
||
| 105 | logger.warning('Can not get network connections track ({})'.format(e)) |
||
| 106 | logger.info('Disable connections track') |
||
| 107 | stats['nf_conntrack_enabled'] = False |
||
| 108 | self.stats = stats |
||
| 109 | return self.stats |
||
| 110 | if 'nf_conntrack_max' in stats and 'nf_conntrack_count' in stats: |
||
| 111 | stats['nf_conntrack_percent'] = stats['nf_conntrack_count'] * 100 / stats['nf_conntrack_max'] |
||
| 112 | else: |
||
| 113 | stats['nf_conntrack_enabled'] = False |
||
| 114 | self.stats = stats |
||
| 115 | return self.stats |
||
| 116 | |||
| 117 | elif self.input_method == 'snmp': |
||
| 118 | # Update stats using SNMP |
||
| 119 | pass |
||
| 120 | |||
| 121 | # Update the stats |
||
| 122 | self.stats = stats |
||
| 123 | return self.stats |
||
| 124 | |||
| 173 |