Conditions | 15 |
Total Lines | 99 |
Code Lines | 52 |
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_mem.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 -*- |
||
124 | @GlancesPlugin._check_decorator |
||
125 | @GlancesPlugin._log_result_decorator |
||
126 | def update(self): |
||
127 | """Update RAM memory stats using the input method.""" |
||
128 | # Init new stats |
||
129 | stats = self.get_init_value() |
||
130 | |||
131 | if self.input_method == 'local': |
||
132 | # Update stats using the standard system lib |
||
133 | # Grab MEM using the psutil virtual_memory method |
||
134 | vm_stats = psutil.virtual_memory() |
||
135 | |||
136 | # Get all the memory stats (copy/paste of the psutil documentation) |
||
137 | # total: total physical memory available. |
||
138 | # available: the actual amount of available memory that can be given instantly |
||
139 | # to processes that request more memory in bytes; this is calculated by summing |
||
140 | # different memory values depending on the platform (e.g. free + buffers + cached on Linux) |
||
141 | # and it is supposed to be used to monitor actual memory usage in a cross platform fashion. |
||
142 | # percent: the percentage usage calculated as (total - available) / total * 100. |
||
143 | # used: memory used, calculated differently depending on the platform and designed for informational |
||
144 | # purposes only. |
||
145 | # free: memory not being used at all (zeroed) that is readily available; note that this doesn't |
||
146 | # reflect the actual memory available (use ‘available’ instead). |
||
147 | # Platform-specific fields: |
||
148 | # active: (UNIX): memory currently in use or very recently used, and so it is in RAM. |
||
149 | # inactive: (UNIX): memory that is marked as not used. |
||
150 | # buffers: (Linux, BSD): cache for things like file system metadata. |
||
151 | # cached: (Linux, BSD): cache for various things. |
||
152 | # wired: (BSD, macOS): memory that is marked to always stay in RAM. It is never moved to disk. |
||
153 | # shared: (BSD): memory that may be simultaneously accessed by multiple processes. |
||
154 | self.reset() |
||
155 | for mem in [ |
||
156 | 'total', |
||
157 | 'available', |
||
158 | 'percent', |
||
159 | 'used', |
||
160 | 'free', |
||
161 | 'active', |
||
162 | 'inactive', |
||
163 | 'buffers', |
||
164 | 'cached', |
||
165 | 'wired', |
||
166 | 'shared', |
||
167 | ]: |
||
168 | if hasattr(vm_stats, mem): |
||
169 | stats[mem] = getattr(vm_stats, mem) |
||
170 | |||
171 | # Use the 'free'/htop calculation |
||
172 | # free=available+buffer+cached |
||
173 | stats['free'] = stats['available'] |
||
174 | if hasattr(stats, 'buffers'): |
||
175 | stats['free'] += stats['buffers'] |
||
176 | if hasattr(stats, 'cached'): |
||
177 | stats['free'] += stats['cached'] |
||
178 | # used=total-free |
||
179 | stats['used'] = stats['total'] - stats['free'] |
||
180 | elif self.input_method == 'snmp': |
||
181 | # Update stats using SNMP |
||
182 | if self.short_system_name in ('windows', 'esxi'): |
||
183 | # Mem stats for Windows|Vmware Esxi are stored in the FS table |
||
184 | try: |
||
185 | fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
||
186 | except KeyError: |
||
187 | self.reset() |
||
188 | else: |
||
189 | for fs in fs_stat: |
||
190 | # The Physical Memory (Windows) or Real Memory (VMware) |
||
191 | # gives statistics on RAM usage and availability. |
||
192 | if fs in ('Physical Memory', 'Real Memory'): |
||
193 | stats['total'] = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) |
||
194 | stats['used'] = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) |
||
195 | stats['percent'] = float(stats['used'] * 100 / stats['total']) |
||
196 | stats['free'] = stats['total'] - stats['used'] |
||
197 | break |
||
198 | else: |
||
199 | # Default behavior for others OS |
||
200 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
201 | |||
202 | if stats['total'] == '': |
||
203 | self.reset() |
||
204 | return self.stats |
||
205 | |||
206 | for key in iterkeys(stats): |
||
207 | if stats[key] != '': |
||
208 | stats[key] = float(stats[key]) * 1024 |
||
209 | |||
210 | # Use the 'free'/htop calculation |
||
211 | stats['free'] = stats['free'] - stats['total'] + (stats['buffers'] + stats['cached']) |
||
212 | |||
213 | # used=total-free |
||
214 | stats['used'] = stats['total'] - stats['free'] |
||
215 | |||
216 | # percent: the percentage usage calculated as (total - available) / total * 100. |
||
217 | stats['percent'] = float((stats['total'] - stats['free']) / stats['total'] * 100) |
||
218 | |||
219 | # Update the stats |
||
220 | self.stats = stats |
||
221 | |||
222 | return self.stats |
||
223 | |||
283 |