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