| Conditions | 12 |
| Total Lines | 84 |
| Code Lines | 48 |
| 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_diskio.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 -*- |
||
| 72 | @GlancesPlugin._check_decorator |
||
| 73 | @GlancesPlugin._log_result_decorator |
||
| 74 | def update(self): |
||
| 75 | """Update disk I/O stats using the input method.""" |
||
| 76 | # Init new stats |
||
| 77 | stats = self.get_init_value() |
||
| 78 | |||
| 79 | if self.input_method == 'local': |
||
| 80 | # Update stats using the standard system lib |
||
| 81 | # Grab the stat using the psutil disk_io_counters method |
||
| 82 | # read_count: number of reads |
||
| 83 | # write_count: number of writes |
||
| 84 | # read_bytes: number of bytes read |
||
| 85 | # write_bytes: number of bytes written |
||
| 86 | # read_time: time spent reading from disk (in milliseconds) |
||
| 87 | # write_time: time spent writing to disk (in milliseconds) |
||
| 88 | try: |
||
| 89 | diskio = psutil.disk_io_counters(perdisk=True) |
||
| 90 | except Exception: |
||
| 91 | return stats |
||
| 92 | |||
| 93 | # Previous disk IO stats are stored in the diskio_old variable |
||
| 94 | # By storing time data we enable Rx/s and Tx/s calculations in the |
||
| 95 | # XML/RPC API, which would otherwise be overly difficult work |
||
| 96 | # for users of the API |
||
| 97 | time_since_update = getTimeSinceLastUpdate('disk') |
||
| 98 | |||
| 99 | diskio = diskio |
||
| 100 | for disk in diskio: |
||
| 101 | # By default, RamFS is not displayed (issue #714) |
||
| 102 | if self.args is not None and not self.args.diskio_show_ramfs and disk.startswith('ram'): |
||
| 103 | continue |
||
| 104 | |||
| 105 | # Do not take hide disk into account |
||
| 106 | if self.is_hide(disk): |
||
| 107 | continue |
||
| 108 | |||
| 109 | # Compute count and bit rate |
||
| 110 | try: |
||
| 111 | diskstat = { |
||
| 112 | 'time_since_update': time_since_update, |
||
| 113 | 'disk_name': n(disk), |
||
| 114 | 'read_count': diskio[disk].read_count - \ |
||
| 115 | self.diskio_old[disk].read_count, |
||
| 116 | 'write_count': diskio[disk].write_count - \ |
||
| 117 | self.diskio_old[disk].write_count, |
||
| 118 | 'read_bytes': diskio[disk].read_bytes - \ |
||
| 119 | self.diskio_old[disk].read_bytes, |
||
| 120 | 'write_bytes': diskio[disk].write_bytes - \ |
||
| 121 | self.diskio_old[disk].write_bytes |
||
| 122 | } |
||
| 123 | except (KeyError, AttributeError): |
||
| 124 | diskstat = { |
||
| 125 | 'time_since_update': time_since_update, |
||
| 126 | 'disk_name': n(disk), |
||
| 127 | 'read_count': 0, |
||
| 128 | 'write_count': 0, |
||
| 129 | 'read_bytes': 0, |
||
| 130 | 'write_bytes': 0} |
||
| 131 | |||
| 132 | # Add alias if exist (define in the configuration file) |
||
| 133 | if self.has_alias(disk) is not None: |
||
| 134 | diskstat['alias'] = self.has_alias(disk) |
||
| 135 | |||
| 136 | # Add the dict key |
||
| 137 | diskstat['key'] = self.get_key() |
||
| 138 | |||
| 139 | # Ad dthe current disk stat to the list |
||
| 140 | stats.append(diskstat) |
||
| 141 | |||
| 142 | # Save stats to compute next bitrate |
||
| 143 | try: |
||
| 144 | self.diskio_old = diskio |
||
| 145 | except (IOError, UnboundLocalError): |
||
| 146 | pass |
||
| 147 | elif self.input_method == 'snmp': |
||
| 148 | # Update stats using SNMP |
||
| 149 | # No standard way for the moment... |
||
| 150 | pass |
||
| 151 | |||
| 152 | # Update the stats |
||
| 153 | self.stats = stats |
||
| 154 | |||
| 155 | return self.stats |
||
| 156 | |||
| 251 |