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