| 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.connections.PluginModel.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 | # |
||
| 105 | def update_for_net_connections_method(self, stats): |
||
| 106 | try: |
||
| 107 | net_connections = psutil.net_connections(kind="tcp") |
||
| 108 | except Exception as e: |
||
| 109 | logger.warning(f'Can not get network connections stats ({e})') |
||
| 110 | logger.info('Disable connections stats') |
||
| 111 | stats['net_connections_enabled'] = False |
||
| 112 | |||
| 113 | return stats |
||
| 114 | |||
| 115 | for s in self.status_list: |
||
| 116 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 117 | initiated = 0 |
||
| 118 | for s in self.initiated_states: |
||
| 119 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 120 | initiated += stats[s] |
||
| 121 | stats['initiated'] = initiated |
||
| 122 | terminated = 0 |
||
| 123 | for s in self.initiated_states: |
||
| 124 | stats[s] = len([c for c in net_connections if c.status == s]) |
||
| 125 | terminated += stats[s] |
||
| 126 | stats['terminated'] = terminated |
||
| 127 | |||
| 128 | return stats |
||
| 129 | |||
| 130 | def update_for_nf_conntrack_method(self, stats): |
||
| 131 | # Grab connections track directly from the /proc file |
||
| 132 | for i in self.conntrack: |
||
| 133 | try: |
||
| 134 | with open(self.conntrack[i]) as f: |
||
| 135 | stats[i] = float(f.readline().rstrip("\n")) |
||
| 136 | except (OSError, FileNotFoundError) as e: |
||
| 137 | logger.warning(f'Can not get network connections track ({e})') |
||
| 138 | logger.info('Disable connections track') |
||
| 139 | stats['nf_conntrack_enabled'] = False |
||
| 140 | |||
| 141 | return stats |
||
| 142 | if 'nf_conntrack_max' in stats and 'nf_conntrack_count' in stats: |
||
| 143 | stats['nf_conntrack_percent'] = stats['nf_conntrack_count'] * 100 / stats['nf_conntrack_max'] |
||
| 144 | else: |
||
| 145 | stats['nf_conntrack_enabled'] = False |
||
| 146 | |||
| 147 | return stats |
||
| 148 | |||
| 149 | @GlancesPluginModel._check_decorator |
||
| 150 | @GlancesPluginModel._log_result_decorator |
||
| 151 | def update(self): |
||
| 152 | """Update connections stats using the input method. |
||
| 153 | |||
| 154 | Stats is a dict |
||
| 155 | """ |
||
| 156 | # Init new stats |
||
| 157 | stats = self.get_init_value() |
||
| 158 | |||
| 159 | if self.input_method == 'local': |
||
| 160 | # Update stats using the PSUtils lib |
||
| 161 | |||
| 162 | # Grab network interface stat using the psutil net_connections method |
||
| 163 | if stats['net_connections_enabled']: |
||
| 164 | stats = self.update_for_net_connections_method(stats) |
||
| 165 | |||
| 166 | if stats['nf_conntrack_enabled']: |
||
| 167 | stats = self.update_for_nf_conntrack_method(stats) |
||
| 168 | elif self.input_method == 'snmp': |
||
| 230 |