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