| Conditions | 15 |
| Total Lines | 74 |
| Code Lines | 37 |
| Lines | 35 |
| Ratio | 47.3 % |
| 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.memswap.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 | # |
||
| 69 | @GlancesPluginModel._check_decorator |
||
| 70 | @GlancesPluginModel._log_result_decorator |
||
| 71 | def update(self): |
||
| 72 | """Update swap memory stats using the input method.""" |
||
| 73 | # Init new stats |
||
| 74 | stats = self.get_init_value() |
||
| 75 | |||
| 76 | if self.input_method == 'local': |
||
| 77 | # Update stats using the standard system lib |
||
| 78 | # Grab SWAP using the psutil swap_memory method |
||
| 79 | try: |
||
| 80 | sm_stats = psutil.swap_memory() |
||
| 81 | except (OSError, RuntimeError): |
||
| 82 | # Crash on startup on Illumos when no swap is configured #1767 |
||
| 83 | # OpenBSD crash on start without a swap file/partition #2719 |
||
| 84 | pass |
||
| 85 | else: |
||
| 86 | # Get all the swap stats (copy/paste of the psutil documentation) |
||
| 87 | # total: total swap memory in bytes |
||
| 88 | # used: used swap memory in bytes |
||
| 89 | # free: free swap memory in bytes |
||
| 90 | # percent: the percentage usage |
||
| 91 | # sin: the number of bytes the system has swapped in from disk (cumulative) |
||
| 92 | # sout: the number of bytes the system has swapped out from disk (cumulative) |
||
| 93 | for swap in ['total', 'used', 'free', 'percent', 'sin', 'sout']: |
||
| 94 | if hasattr(sm_stats, swap): |
||
| 95 | stats[swap] = getattr(sm_stats, swap) |
||
| 96 | |||
| 97 | # By storing time data we enable sin/s and sout/s calculations in the |
||
| 98 | # XML/RPC API, which would otherwise be overly difficult work |
||
| 99 | # for users of the API |
||
| 100 | stats['time_since_update'] = getTimeSinceLastUpdate('memswap') |
||
| 101 | elif self.input_method == 'snmp': |
||
| 102 | # Update stats using SNMP |
||
| 103 | if self.short_system_name == 'windows': |
||
| 104 | # Mem stats for Windows OS are stored in the FS table |
||
| 105 | try: |
||
| 106 | fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], bulk=True) |
||
| 107 | except KeyError: |
||
| 108 | self.reset() |
||
| 109 | else: |
||
| 110 | for fs in fs_stat: |
||
| 111 | # The virtual memory concept is used by the operating |
||
| 112 | # system to extend (virtually) the physical memory and |
||
| 113 | # thus to run more programs by swapping unused memory |
||
| 114 | # zone (page) to a disk file. |
||
| 115 | if fs == 'Virtual Memory': |
||
| 116 | stats['total'] = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) |
||
| 117 | stats['used'] = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) |
||
| 118 | stats['percent'] = float(stats['used'] * 100 / stats['total']) |
||
| 119 | stats['free'] = stats['total'] - stats['used'] |
||
| 120 | break |
||
| 121 | else: |
||
| 122 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
| 123 | |||
| 124 | if stats['total'] == '': |
||
| 125 | self.reset() |
||
| 126 | return stats |
||
| 127 | |||
| 128 | for key in iterkeys(stats): |
||
| 129 | if stats[key] != '': |
||
| 130 | stats[key] = float(stats[key]) * 1024 |
||
| 131 | |||
| 132 | # used=total-free |
||
| 133 | stats['used'] = stats['total'] - stats['free'] |
||
| 134 | |||
| 135 | # percent: the percentage usage calculated as (total - |
||
| 136 | # available) / total * 100. |
||
| 137 | stats['percent'] = float((stats['total'] - stats['free']) / stats['total'] * 100) |
||
| 138 | |||
| 139 | # Update the stats |
||
| 140 | self.stats = stats |
||
| 141 | |||
| 142 | return self.stats |
||
| 143 | |||
| 192 |