Conditions | 21 |
Total Lines | 151 |
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 -*- |
||
73 | @GlancesPlugin._check_decorator |
||
74 | @GlancesPlugin._log_result_decorator |
||
75 | def update(self): |
||
76 | """Update network stats using the input method. |
||
77 | |||
78 | Stats is a list of dict (one dict per interface) |
||
79 | """ |
||
80 | # Init new stats |
||
81 | stats = self.get_init_value() |
||
82 | |||
83 | if self.input_method == 'local': |
||
84 | # Update stats using the standard system lib |
||
85 | |||
86 | # Grab network interface stat using the psutil net_io_counter method |
||
87 | try: |
||
88 | netiocounters = psutil.net_io_counters(pernic=True) |
||
89 | except UnicodeDecodeError as e: |
||
90 | logger.debug('Can not get network interface counters ({})'.format(e)) |
||
91 | return self.stats |
||
92 | |||
93 | # Grab interface's status (issue #765) |
||
94 | # Grab interface's speed (issue #718) |
||
95 | netstatus = {} |
||
96 | try: |
||
97 | netstatus = psutil.net_if_stats() |
||
98 | except OSError as e: |
||
99 | # see psutil #797/glances #1106 |
||
100 | logger.debug('Can not get network interface status ({})'.format(e)) |
||
101 | |||
102 | # Previous network interface stats are stored in the network_old variable |
||
103 | if not hasattr(self, 'network_old'): |
||
104 | # First call, we init the network_old var |
||
105 | try: |
||
106 | self.network_old = netiocounters |
||
107 | except (IOError, UnboundLocalError): |
||
108 | pass |
||
109 | return self.stats |
||
110 | |||
111 | # By storing time data we enable Rx/s and Tx/s calculations in the |
||
112 | # XML/RPC API, which would otherwise be overly difficult work |
||
113 | # for users of the API |
||
114 | time_since_update = getTimeSinceLastUpdate('net') |
||
115 | |||
116 | # Loop over interfaces |
||
117 | network_new = netiocounters |
||
118 | for net in network_new: |
||
119 | # Do not take hidden interface into account |
||
120 | # or KeyError: 'eth0' when interface is not connected #1348 |
||
121 | if self.is_hide(net) or net not in netstatus: |
||
122 | continue |
||
123 | try: |
||
124 | cumulative_rx = network_new[net].bytes_recv |
||
125 | cumulative_tx = network_new[net].bytes_sent |
||
126 | cumulative_cx = cumulative_rx + cumulative_tx |
||
127 | rx = cumulative_rx - self.network_old[net].bytes_recv |
||
128 | tx = cumulative_tx - self.network_old[net].bytes_sent |
||
129 | cx = rx + tx |
||
130 | netstat = {'interface_name': n(net), |
||
131 | 'time_since_update': time_since_update, |
||
132 | 'cumulative_rx': cumulative_rx, |
||
133 | 'rx': rx, |
||
134 | 'cumulative_tx': cumulative_tx, |
||
135 | 'tx': tx, |
||
136 | 'cumulative_cx': cumulative_cx, |
||
137 | 'cx': cx, |
||
138 | # Interface status |
||
139 | 'is_up': netstatus[net].isup, |
||
140 | # Interface speed in Mbps, convert it to bps |
||
141 | # Can be always 0 on some OSes |
||
142 | 'speed': netstatus[net].speed * 1048576, |
||
143 | # Set the key for the dict |
||
144 | 'key': self.get_key() |
||
145 | } |
||
146 | except KeyError: |
||
147 | continue |
||
148 | else: |
||
149 | # Append the interface stats to the list |
||
150 | stats.append(netstat) |
||
151 | |||
152 | # Save stats to compute next bitrate |
||
153 | self.network_old = network_new |
||
154 | |||
155 | elif self.input_method == 'snmp': |
||
156 | # Update stats using SNMP |
||
157 | |||
158 | # SNMP bulk command to get all network interface in one shot |
||
159 | try: |
||
160 | netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], |
||
161 | bulk=True) |
||
162 | except KeyError: |
||
163 | netiocounters = self.get_stats_snmp(snmp_oid=snmp_oid['default'], |
||
164 | bulk=True) |
||
165 | |||
166 | # Previous network interface stats are stored in the network_old variable |
||
167 | if not hasattr(self, 'network_old'): |
||
168 | # First call, we init the network_old var |
||
169 | try: |
||
170 | self.network_old = netiocounters |
||
171 | except (IOError, UnboundLocalError): |
||
172 | pass |
||
173 | else: |
||
174 | # See description in the 'local' block |
||
175 | time_since_update = getTimeSinceLastUpdate('net') |
||
176 | |||
177 | # Loop over interfaces |
||
178 | network_new = netiocounters |
||
179 | |||
180 | for net in network_new: |
||
181 | # Do not take hidden interface into account |
||
182 | if self.is_hide(net): |
||
183 | continue |
||
184 | |||
185 | try: |
||
186 | # Windows: a tips is needed to convert HEX to TXT |
||
187 | # 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 |
||
188 | if self.short_system_name == 'windows': |
||
189 | try: |
||
190 | interface_name = str(base64.b16decode(net[2:-2].upper())) |
||
191 | except TypeError: |
||
192 | interface_name = net |
||
193 | else: |
||
194 | interface_name = net |
||
195 | |||
196 | cumulative_rx = float(network_new[net]['cumulative_rx']) |
||
197 | cumulative_tx = float(network_new[net]['cumulative_tx']) |
||
198 | cumulative_cx = cumulative_rx + cumulative_tx |
||
199 | rx = cumulative_rx - float(self.network_old[net]['cumulative_rx']) |
||
200 | tx = cumulative_tx - float(self.network_old[net]['cumulative_tx']) |
||
201 | cx = rx + tx |
||
202 | netstat = { |
||
203 | 'interface_name': interface_name, |
||
204 | 'time_since_update': time_since_update, |
||
205 | 'cumulative_rx': cumulative_rx, |
||
206 | 'rx': rx, |
||
207 | 'cumulative_tx': cumulative_tx, |
||
208 | 'tx': tx, |
||
209 | 'cumulative_cx': cumulative_cx, |
||
210 | 'cx': cx} |
||
211 | except KeyError: |
||
212 | continue |
||
213 | else: |
||
214 | netstat['key'] = self.get_key() |
||
215 | stats.append(netstat) |
||
216 | |||
217 | # Save stats to compute next bitrate |
||
218 | self.network_old = network_new |
||
219 | |||
220 | # Update the stats |
||
221 | self.stats = stats |
||
222 | |||
223 | return self.stats |
||
224 | |||
350 |