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