| Conditions | 12 |
| Total Lines | 58 |
| Code Lines | 32 |
| Lines | 58 |
| Ratio | 100 % |
| 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_influxdb2.Export._normalize() 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 | # |
||
| 96 | exponential_base=2, |
||
| 97 | ) |
||
| 98 | ) |
||
| 99 | |||
| 100 | def export(self, name, columns, points): |
||
| 101 | """Write the points to the InfluxDB server.""" |
||
| 102 | # Manage prefix |
||
| 103 | if self.prefix is not None: |
||
| 104 | name = self.prefix + "." + name |
||
| 105 | # Write input to the InfluxDB database |
||
| 106 | if not points: |
||
| 107 | logger.debug(f"Cannot export empty {name} stats to InfluxDB") |
||
| 108 | else: |
||
| 109 | try: |
||
| 110 | self.client.write( |
||
| 111 | self.bucket, |
||
| 112 | self.org, |
||
| 113 | self.normalize_for_influxdb(name, columns, points), |
||
| 114 | time_precision="s", |
||
| 115 | ) |
||
| 116 | except Exception as e: |
||
| 117 | # Log level set to warning instead of error (see: issue #1561) |
||
| 118 | logger.warning(f"Cannot export {name} stats to InfluxDB ({e})") |
||
| 119 | else: |
||
| 120 | logger.debug(f"Export {name} stats to InfluxDB") |
||
| 121 |