Conditions | 15 |
Total Lines | 73 |
Code Lines | 50 |
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.exports.glances_timescaledb.Export.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 | # |
||
92 | def update(self, stats): |
||
93 | """Update the TimescaleDB export module.""" |
||
94 | if not self.export_enable: |
||
95 | return False |
||
96 | |||
97 | # Get all the stats & limits |
||
98 | # Current limitation with sensors and fs plugins because fields list is not the same |
||
99 | self._last_exported_list = [p for p in self.plugins_to_export(stats) if p not in ['sensors', 'fs']] |
||
100 | all_stats = stats.getAllExportsAsDict(plugin_list=self.last_exported_list()) |
||
101 | all_limits = stats.getAllLimitsAsDict(plugin_list=self.last_exported_list()) |
||
102 | |||
103 | # Loop over plugins to export |
||
104 | for plugin in self.last_exported_list(): |
||
105 | if isinstance(all_stats[plugin], dict): |
||
106 | all_stats[plugin].update(all_limits[plugin]) |
||
107 | # Remove the <plugin>_disable field |
||
108 | all_stats[plugin].pop(f"{plugin}_disable", None) |
||
109 | # user is a special field that should not be exported |
||
110 | # rename it to user_<plugin> |
||
111 | if 'user' in all_stats[plugin]: |
||
112 | all_stats[plugin][f'user_{plugin}'] = all_stats[plugin].pop('user') |
||
113 | elif isinstance(all_stats[plugin], list): |
||
114 | for i in all_stats[plugin]: |
||
115 | i.update(all_limits[plugin]) |
||
116 | # Remove the <plugin>_disable field |
||
117 | i.pop(f"{plugin}_disable", None) |
||
118 | # user is a special field that should not be exported |
||
119 | # rename it to user_<plugin> |
||
120 | if 'user' in i: |
||
121 | i[f'user_{plugin}'] = i.pop('user') |
||
122 | else: |
||
123 | continue |
||
124 | |||
125 | plugin_stats = all_stats[plugin] |
||
126 | creation_list = [] # List used to create the TimescaleDB table |
||
127 | segmented_by = [] # List of columns used to segment the data |
||
128 | values_list = [] # List of values to insert (list of lists, one list per row) |
||
129 | if isinstance(plugin_stats, dict): |
||
130 | # Stats is a dict |
||
131 | # Create the list used to create the TimescaleDB table |
||
132 | creation_list.append('time TIMESTAMPTZ NOT NULL') |
||
133 | creation_list.append('hostname_id TEXT NOT NULL') |
||
134 | segmented_by.extend(['hostname_id']) # Segment by hostname |
||
135 | for key, value in plugin_stats.items(): |
||
136 | creation_list.append(f"{key} {convert_types[type(value).__name__]} NULL") |
||
137 | values_list.append('NOW()') # Add the current time (insertion time) |
||
138 | values_list.append(f"'{self.hostname}'") # Add the hostname |
||
139 | values_list.extend([self.normalize(value) for value in plugin_stats.values()]) |
||
140 | values_list = [values_list] |
||
141 | elif isinstance(plugin_stats, list) and len(plugin_stats) > 0 and 'key' in plugin_stats[0]: |
||
142 | # Stats is a list |
||
143 | # Create the list used to create the TimescaleDB table |
||
144 | creation_list.append('time TIMESTAMPTZ NOT NULL') |
||
145 | creation_list.append('hostname_id TEXT NOT NULL') |
||
146 | creation_list.append('key_id TEXT NOT NULL') |
||
147 | segmented_by.extend(['hostname_id', 'key_id']) # Segment by hostname and key |
||
148 | for key, value in plugin_stats[0].items(): |
||
149 | creation_list.append(f"{key} {convert_types[type(value).__name__]} NULL") |
||
150 | # Create the values list (it is a list of list to have a single datamodel for all the plugins) |
||
151 | for plugin_item in plugin_stats: |
||
152 | item_list = [] |
||
153 | item_list.append('NOW()') # Add the current time (insertion time) |
||
154 | item_list.append(f"'{self.hostname}'") # Add the hostname |
||
155 | item_list.append(f"'{plugin_item.get('key')}'") |
||
156 | item_list.extend([self.normalize(value) for value in plugin_item.values()]) |
||
157 | values_list.append(item_list[:-1]) |
||
158 | else: |
||
159 | continue |
||
160 | |||
161 | # Export stats to TimescaleDB |
||
162 | self.export(plugin, creation_list, segmented_by, values_list) |
||
163 | |||
164 | return True |
||
165 | |||
218 |