| Conditions | 13 |
| Total Lines | 63 |
| Code Lines | 44 |
| Lines | 11 |
| Ratio | 17.46 % |
| 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_duckdb.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 | # |
||
| 84 | def update(self, stats): |
||
| 85 | """Update the DuckDB export module.""" |
||
| 86 | if not self.export_enable: |
||
| 87 | return False |
||
| 88 | |||
| 89 | # Get all the stats & limits |
||
| 90 | # Current limitation with sensors and fs plugins because fields list is not the same |
||
| 91 | self._last_exported_list = [p for p in self.plugins_to_export(stats) if p not in ['sensors', 'fs']] |
||
| 92 | all_stats = stats.getAllExportsAsDict(plugin_list=self.last_exported_list()) |
||
| 93 | all_limits = stats.getAllLimitsAsDict(plugin_list=self.last_exported_list()) |
||
| 94 | |||
| 95 | # Loop over plugins to export |
||
| 96 | for plugin in self.last_exported_list(): |
||
| 97 | # Remove some fields |
||
| 98 | View Code Duplication | if isinstance(all_stats[plugin], dict): |
|
| 99 | all_stats[plugin].update(all_limits[plugin]) |
||
| 100 | # Remove the <plugin>_disable field |
||
| 101 | all_stats[plugin].pop(f"{plugin}_disable", None) |
||
| 102 | elif isinstance(all_stats[plugin], list): |
||
| 103 | for i in all_stats[plugin]: |
||
| 104 | i.update(all_limits[plugin]) |
||
| 105 | # Remove the <plugin>_disable field |
||
| 106 | i.pop(f"{plugin}_disable", None) |
||
| 107 | else: |
||
| 108 | continue |
||
| 109 | |||
| 110 | plugin_stats = all_stats[plugin] |
||
| 111 | creation_list = [] # List used to create the DuckDB table |
||
| 112 | values_list = [] # List of values to insert (list of lists, one list per row) |
||
| 113 | if isinstance(plugin_stats, dict): |
||
| 114 | # Create the list to create the table |
||
| 115 | creation_list.append('time TIMETZ') |
||
| 116 | creation_list.append('hostname_id VARCHAR') |
||
| 117 | for key, value in plugin_stats.items(): |
||
| 118 | creation_list.append(f"{key} {convert_types[type(self.normalize(value)).__name__]}") |
||
| 119 | # Create the list of values to insert |
||
| 120 | item_list = [] |
||
| 121 | item_list.append(self.normalize(datetime.now().replace(microsecond=0))) |
||
| 122 | item_list.append(self.normalize(f"{self.hostname}")) |
||
| 123 | item_list.extend([self.normalize(value) for value in plugin_stats.values()]) |
||
| 124 | values_list = [item_list] |
||
| 125 | elif isinstance(plugin_stats, list) and len(plugin_stats) > 0 and 'key' in plugin_stats[0]: |
||
| 126 | # Create the list to create the table |
||
| 127 | creation_list.append('time TIMETZ') |
||
| 128 | creation_list.append('hostname_id VARCHAR') |
||
| 129 | creation_list.append('key_id VARCHAR') |
||
| 130 | for key, value in plugin_stats[0].items(): |
||
| 131 | creation_list.append(f"{key} {convert_types[type(self.normalize(value)).__name__]}") |
||
| 132 | # Create the list of values to insert |
||
| 133 | for plugin_item in plugin_stats: |
||
| 134 | item_list = [] |
||
| 135 | item_list.append(self.normalize(datetime.now().replace(microsecond=0))) |
||
| 136 | item_list.append(self.normalize(f"{self.hostname}")) |
||
| 137 | item_list.append(self.normalize(f"{plugin_item.get('key')}")) |
||
| 138 | item_list.extend([self.normalize(value) for value in plugin_item.values()]) |
||
| 139 | values_list.append(item_list) |
||
| 140 | else: |
||
| 141 | continue |
||
| 142 | |||
| 143 | # Export stats to DuckDB |
||
| 144 | self.export(plugin, creation_list, values_list) |
||
| 145 | |||
| 146 | return True |
||
| 147 | |||
| 196 |