Conditions | 21 |
Total Lines | 152 |
Code Lines | 94 |
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.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 -*- |
||
106 | @GlancesPluginModel._log_result_decorator |
||
107 | def update(self): |
||
108 | """Update network stats using the input method. |
||
109 | |||
110 | :return: list of stats dict (one dict per interface) |
||
111 | """ |
||
112 | # Init new stats |
||
113 | stats = self.get_init_value() |
||
114 | |||
115 | if self.input_method == 'local': |
||
116 | # Update stats using the standard system lib |
||
117 | |||
118 | # Grab network interface stat using the psutil net_io_counter method |
||
119 | try: |
||
120 | net_io_counters = psutil.net_io_counters(pernic=True) |
||
121 | except UnicodeDecodeError as e: |
||
122 | logger.debug('Can not get network interface counters ({})'.format(e)) |
||
123 | return self.stats |
||
124 | |||
125 | # Grab interface's status (issue #765) |
||
126 | # Grab interface's speed (issue #718) |
||
127 | net_status = {} |
||
128 | try: |
||
129 | net_status = psutil.net_if_stats() |
||
130 | except OSError as e: |
||
131 | # see psutil #797/glances #1106 |
||
132 | logger.debug('Can not get network interface status ({})'.format(e)) |
||
133 | |||
134 | # Previous network interface stats are stored in the network_old variable |
||
135 | if not hasattr(self, 'network_old'): |
||
136 | # First call, we init the network_old var |
||
137 | try: |
||
138 | self.network_old = net_io_counters |
||
139 | except (IOError, UnboundLocalError): |
||
140 | pass |
||
141 | return self.stats |
||
142 | |||
143 | # By storing time data we enable Rx/s and Tx/s calculations in the |
||
144 | # XML/RPC API, which would otherwise be overly difficult work |
||
145 | # for users of the API |
||
146 | time_since_update = getTimeSinceLastUpdate('net') |
||
147 | |||
148 | # Loop over interfaces |
||
149 | network_new = net_io_counters |
||
150 | for net in network_new: |
||
151 | # Do not take hidden interface into account |
||
152 | # or KeyError: 'eth0' when interface is not connected #1348 |
||
153 | if not self.is_display(net) or net not in net_status: |
||
154 | continue |
||
155 | try: |
||
156 | cumulative_rx = network_new[net].bytes_recv |
||
157 | cumulative_tx = network_new[net].bytes_sent |
||
158 | cumulative_cx = cumulative_rx + cumulative_tx |
||
159 | rx = cumulative_rx - self.network_old[net].bytes_recv |
||
160 | tx = cumulative_tx - self.network_old[net].bytes_sent |
||
161 | cx = rx + tx |
||
162 | netstat = { |
||
163 | 'interface_name': net, |
||
164 | 'alias': self.has_alias(net), |
||
165 | 'time_since_update': time_since_update, |
||
166 | 'cumulative_rx': cumulative_rx, |
||
167 | 'rx': rx, |
||
168 | 'cumulative_tx': cumulative_tx, |
||
169 | 'tx': tx, |
||
170 | 'cumulative_cx': cumulative_cx, |
||
171 | 'cx': cx, |
||
172 | # Interface status |
||
173 | 'is_up': net_status[net].isup, |
||
174 | # Interface speed in Mbps, convert it to bps |
||
175 | # Can be always 0 on some OSes |
||
176 | 'speed': net_status[net].speed * 1048576, |
||
177 | # Set the key for the dict |
||
178 | 'key': self.get_key(), |
||
179 | } |
||
180 | except KeyError: |
||
181 | continue |
||
182 | else: |
||
183 | # Append the interface stats to the list |
||
184 | stats.append(netstat) |
||
185 | |||
186 | # Save stats to compute next bitrate |
||
187 | self.network_old = network_new |
||
188 | |||
189 | elif self.input_method == 'snmp': |
||
190 | # Update stats using SNMP |
||
191 | |||
192 | # SNMP bulk command to get all network interface in one shot |
||
193 | try: |
||
194 | net_io_counters = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
||
195 | except KeyError: |
||
196 | net_io_counters = self.get_stats_snmp(snmp_oid=snmp_oid['default'], bulk=True) |
||
197 | |||
198 | # Previous network interface stats are stored in the network_old variable |
||
199 | if not hasattr(self, 'network_old'): |
||
200 | # First call, we init the network_old var |
||
201 | try: |
||
202 | self.network_old = net_io_counters |
||
203 | except (IOError, UnboundLocalError): |
||
204 | pass |
||
205 | else: |
||
206 | # See description in the 'local' block |
||
207 | time_since_update = getTimeSinceLastUpdate('net') |
||
208 | |||
209 | # Loop over interfaces |
||
210 | network_new = net_io_counters |
||
211 | |||
212 | for net in network_new: |
||
213 | # Do not take hidden interface into account |
||
214 | if not self.is_display(net): |
||
215 | continue |
||
216 | |||
217 | try: |
||
218 | # Windows: a tips is needed to convert HEX to TXT |
||
219 | # http://blogs.technet.com/b/networking/archive/2009/12/18/how-to-query-the-list-of-network-interfaces-using-snmp-via-the-ifdescr-counter.aspx |
||
220 | if self.short_system_name == 'windows': |
||
221 | try: |
||
222 | interface_name = str(base64.b16decode(net[2:-2].upper())) |
||
223 | except TypeError: |
||
224 | interface_name = net |
||
225 | else: |
||
226 | interface_name = net |
||
227 | |||
228 | cumulative_rx = float(network_new[net]['cumulative_rx']) |
||
229 | cumulative_tx = float(network_new[net]['cumulative_tx']) |
||
230 | cumulative_cx = cumulative_rx + cumulative_tx |
||
231 | rx = cumulative_rx - float(self.network_old[net]['cumulative_rx']) |
||
232 | tx = cumulative_tx - float(self.network_old[net]['cumulative_tx']) |
||
233 | cx = rx + tx |
||
234 | netstat = { |
||
235 | 'interface_name': interface_name, |
||
236 | 'alias': self.has_alias(interface_name), |
||
237 | 'time_since_update': time_since_update, |
||
238 | 'cumulative_rx': cumulative_rx, |
||
239 | 'rx': rx, |
||
240 | 'cumulative_tx': cumulative_tx, |
||
241 | 'tx': tx, |
||
242 | 'cumulative_cx': cumulative_cx, |
||
243 | 'cx': cx, |
||
244 | } |
||
245 | except KeyError: |
||
246 | continue |
||
247 | else: |
||
248 | netstat['key'] = self.get_key() |
||
249 | stats.append(netstat) |
||
250 | |||
251 | # Save stats to compute next bitrate |
||
252 | self.network_old = network_new |
||
253 | |||
254 | # Update the stats |
||
255 | self.stats = stats |
||
256 | |||
257 | return self.stats |
||
258 | |||
390 |