| Conditions | 14 |
| Total Lines | 95 |
| Code Lines | 50 |
| Lines | 32 |
| Ratio | 33.68 % |
| 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.mem.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 | # |
||
| 122 | """Init the plugin.""" |
||
| 123 | super().__init__( |
||
| 124 | args=args, config=config, items_history_list=items_history_list, fields_description=fields_description |
||
| 125 | ) |
||
| 126 | |||
| 127 | # We want to display the stat in the curse interface |
||
| 128 | self.display_curse = True |
||
| 129 | |||
| 130 | def _update_for_local(self, stats): |
||
| 131 | # Update stats using the standard system lib |
||
| 132 | # Grab MEM using the psutil virtual_memory method |
||
| 133 | vm_stats = psutil.virtual_memory() |
||
| 134 | |||
| 135 | # Get all the memory stats (copy/paste of the psutil documentation) |
||
| 136 | # total: total physical memory available. |
||
| 137 | # available: the actual amount of available memory that can be given instantly |
||
| 138 | # to processes that request more memory in bytes; this is calculated by summing |
||
| 139 | # different memory values depending on the platform (e.g. free + buffers + cached on Linux) |
||
| 140 | # and it is supposed to be used to monitor actual memory usage in a cross platform fashion. |
||
| 141 | # percent: the percentage usage calculated as (total - available) / total * 100. |
||
| 142 | # used: memory used, calculated differently depending on the platform and designed for informational |
||
| 143 | # purposes only. |
||
| 144 | # free: memory not being used at all (zeroed) that is readily available; note that this doesn't |
||
| 145 | # reflect the actual memory available (use ‘available’ instead). |
||
| 146 | # Platform-specific fields: |
||
| 147 | # active: (UNIX): memory currently in use or very recently used, and so it is in RAM. |
||
| 148 | # inactive: (UNIX): memory that is marked as not used. |
||
| 149 | # buffers: (Linux, BSD): cache for things like file system metadata. |
||
| 150 | # cached: (Linux, BSD): cache for various things. |
||
| 151 | # wired: (BSD, macOS): memory that is marked to always stay in RAM. It is never moved to disk. |
||
| 152 | # shared: (BSD): memory that may be simultaneously accessed by multiple processes. |
||
| 153 | self.reset() |
||
| 154 | for mem in [ |
||
| 155 | 'total', |
||
| 156 | 'available', |
||
| 157 | 'percent', |
||
| 158 | 'used', |
||
| 159 | 'free', |
||
| 160 | 'active', |
||
| 161 | 'inactive', |
||
| 162 | 'buffers', |
||
| 163 | 'cached', |
||
| 164 | 'wired', |
||
| 165 | 'shared', |
||
| 166 | ]: |
||
| 167 | if hasattr(vm_stats, mem): |
||
| 168 | stats[mem] = getattr(vm_stats, mem) |
||
| 169 | |||
| 170 | # Use the 'free'/htop calculation |
||
| 171 | # free=available+buffer+cached |
||
| 172 | stats['free'] = stats['available'] |
||
| 173 | if hasattr(stats, 'buffers'): |
||
| 174 | stats['free'] += stats['buffers'] |
||
| 175 | if hasattr(stats, 'cached'): |
||
| 176 | stats['free'] += stats['cached'] |
||
| 177 | # used=total-free |
||
| 178 | stats['used'] = stats['total'] - stats['free'] |
||
| 179 | |||
| 180 | return stats |
||
| 181 | |||
| 182 | def _update_for_win_os_esxi(self, stats): |
||
| 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 | |||
| 199 | return stats |
||
| 200 | |||
| 201 | def _update_for_other_oses(self, stats): |
||
| 202 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
| 203 | |||
| 204 | if stats['total'] == '': |
||
| 205 | self.reset() |
||
| 206 | return 'reset' |
||
| 207 | |||
| 208 | for k in stats: |
||
| 209 | stats[k] = int(stats[k]) * 1024 |
||
| 210 | |||
| 211 | # used=total-free |
||
| 212 | stats['used'] = stats['total'] - stats['free'] |
||
| 213 | |||
| 214 | # percent: the percentage usage calculated as (total - available) / total * 100. |
||
| 215 | stats['percent'] = float((stats['total'] - stats['free']) / stats['total'] * 100) |
||
| 216 | |||
| 217 | return stats |
||
| 295 |