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