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