Conditions | 25 |
Total Lines | 126 |
Code Lines | 78 |
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.fs.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 -*- |
||
115 | @GlancesPluginModel._check_decorator |
||
116 | @GlancesPluginModel._log_result_decorator |
||
117 | def update(self): |
||
118 | """Update the FS stats using the input method.""" |
||
119 | # Init new stats |
||
120 | stats = self.get_init_value() |
||
121 | |||
122 | if self.input_method == 'local': |
||
123 | # Update stats using the standard system lib |
||
124 | |||
125 | # Grab the stats using the psutil disk_partitions |
||
126 | # If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys) |
||
127 | # and ignore all others (e.g. memory partitions such as /dev/shm) |
||
128 | try: |
||
129 | fs_stat = psutil.disk_partitions(all=False) |
||
130 | except (UnicodeDecodeError, PermissionError): |
||
131 | logger.debug("Plugin - fs: PsUtil fetch failed") |
||
132 | return self.stats |
||
133 | |||
134 | # Optional hack to allow logical mounts points (issue #448) |
||
135 | allowed_fs_types = self.get_conf_value('allow') |
||
136 | if allowed_fs_types: |
||
137 | # Avoid Psutil call unless mounts need to be allowed |
||
138 | try: |
||
139 | all_mounted_fs = psutil.disk_partitions(all=True) |
||
140 | except (UnicodeDecodeError, PermissionError): |
||
141 | logger.debug("Plugin - fs: PsUtil extended fetch failed") |
||
142 | else: |
||
143 | # Discard duplicates (#2299) and add entries matching allowed fs types |
||
144 | tracked_mnt_points = set(f.mountpoint for f in fs_stat) |
||
145 | for f in all_mounted_fs: |
||
146 | if ( |
||
147 | any(f.fstype.find(fs_type) >= 0 for fs_type in allowed_fs_types) |
||
148 | and f.mountpoint not in tracked_mnt_points |
||
149 | ): |
||
150 | fs_stat.append(f) |
||
151 | |||
152 | # Loop over fs |
||
153 | for fs in fs_stat: |
||
154 | # Hide the stats if the mount point is in the exclude list |
||
155 | if not self.is_display(fs.mountpoint): |
||
156 | continue |
||
157 | |||
158 | # Grab the disk usage |
||
159 | try: |
||
160 | fs_usage = psutil.disk_usage(fs.mountpoint) |
||
161 | except OSError: |
||
162 | # Correct issue #346 |
||
163 | # Disk is ejected during the command |
||
164 | continue |
||
165 | fs_current = { |
||
166 | 'device_name': fs.device, |
||
167 | 'fs_type': fs.fstype, |
||
168 | # Manage non breaking space (see issue #1065) |
||
169 | 'mnt_point': u(fs.mountpoint).replace(u'\u00A0', ' '), |
||
170 | 'size': fs_usage.total, |
||
171 | 'used': fs_usage.used, |
||
172 | 'free': fs_usage.free, |
||
173 | 'percent': fs_usage.percent, |
||
174 | 'key': self.get_key(), |
||
175 | } |
||
176 | |||
177 | # Hide the stats if the device name is in the exclude list |
||
178 | # Correct issue: glances.conf FS hide not applying #1666 |
||
179 | if not self.is_display(fs_current['device_name']): |
||
180 | continue |
||
181 | |||
182 | # Add alias if exist (define in the configuration file) |
||
183 | if self.has_alias(fs_current['mnt_point']) is not None: |
||
184 | fs_current['alias'] = self.has_alias(fs_current['mnt_point']) |
||
185 | |||
186 | stats.append(fs_current) |
||
187 | |||
188 | elif self.input_method == 'snmp': |
||
189 | # Update stats using SNMP |
||
190 | |||
191 | # SNMP bulk command to get all file system in one shot |
||
192 | try: |
||
193 | fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
||
194 | except KeyError: |
||
195 | fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'], bulk=True) |
||
196 | |||
197 | # Loop over fs |
||
198 | if self.short_system_name in ('windows', 'esxi'): |
||
199 | # Windows or ESXi tips |
||
200 | for fs in fs_stat: |
||
201 | # Memory stats are grabbed in the same OID table (ignore it) |
||
202 | if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory': |
||
203 | continue |
||
204 | size = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) |
||
205 | used = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) |
||
206 | percent = float(used * 100 / size) |
||
207 | fs_current = { |
||
208 | 'device_name': '', |
||
209 | 'mnt_point': fs.partition(' ')[0], |
||
210 | 'size': size, |
||
211 | 'used': used, |
||
212 | 'percent': percent, |
||
213 | 'key': self.get_key(), |
||
214 | } |
||
215 | # Do not take hidden file system into account |
||
216 | if self.is_hide(fs_current['mnt_point']): |
||
217 | continue |
||
218 | else: |
||
219 | stats.append(fs_current) |
||
220 | else: |
||
221 | # Default behavior |
||
222 | for fs in fs_stat: |
||
223 | fs_current = { |
||
224 | 'device_name': fs_stat[fs]['device_name'], |
||
225 | 'mnt_point': fs, |
||
226 | 'size': int(fs_stat[fs]['size']) * 1024, |
||
227 | 'used': int(fs_stat[fs]['used']) * 1024, |
||
228 | 'percent': float(fs_stat[fs]['percent']), |
||
229 | 'key': self.get_key(), |
||
230 | } |
||
231 | # Do not take hidden file system into account |
||
232 | if self.is_hide(fs_current['mnt_point']) or self.is_hide(fs_current['device_name']): |
||
233 | continue |
||
234 | else: |
||
235 | stats.append(fs_current) |
||
236 | |||
237 | # Update the stats |
||
238 | self.stats = stats |
||
239 | |||
240 | return self.stats |
||
241 | |||
306 |