| Conditions | 13 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 61 |
| 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 | # -*- coding: utf-8 -*- |
||
| 100 | View Code Duplication | def _normalize(self, name, columns, points): |
|
|
|
|||
| 101 | """Normalize data for the InfluxDB's data model. |
||
| 102 | |||
| 103 | :return: a list of measurements. |
||
| 104 | """ |
||
| 105 | ret = [] |
||
| 106 | |||
| 107 | # Build initial dict by crossing columns and point |
||
| 108 | data_dict = dict(zip(columns, points)) |
||
| 109 | |||
| 110 | # issue1871 - Check if a key exist. If a key exist, the value of |
||
| 111 | # the key should be used as a tag to identify the measurement. |
||
| 112 | keys_list = [k.split('.')[0] for k in columns if k.endswith('.key')] |
||
| 113 | if len(keys_list) == 0: |
||
| 114 | keys_list = [None] |
||
| 115 | |||
| 116 | for measurement in keys_list: |
||
| 117 | # Manage field |
||
| 118 | if measurement is not None: |
||
| 119 | fields = { |
||
| 120 | k.replace('{}.'.format(measurement), ''): data_dict[k] |
||
| 121 | for k in data_dict |
||
| 122 | if k.startswith('{}.'.format(measurement)) |
||
| 123 | } |
||
| 124 | else: |
||
| 125 | fields = data_dict |
||
| 126 | # Transform to InfluxDB datamodel |
||
| 127 | # https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/ |
||
| 128 | for k in fields: |
||
| 129 | # Do not export empty (None) value |
||
| 130 | if fields[k] is None: |
||
| 131 | continue |
||
| 132 | # Convert numerical to float |
||
| 133 | try: |
||
| 134 | fields[k] = float(fields[k]) |
||
| 135 | except (TypeError, ValueError): |
||
| 136 | # Convert others to string |
||
| 137 | try: |
||
| 138 | fields[k] = str(fields[k]) |
||
| 139 | except (TypeError, ValueError): |
||
| 140 | pass |
||
| 141 | # Manage tags |
||
| 142 | tags = self.parse_tags(self.tags) |
||
| 143 | if 'key' in fields and fields['key'] in fields: |
||
| 144 | # Create a tag from the key |
||
| 145 | # Tag should be an string (see InfluxDB data model) |
||
| 146 | tags[fields['key']] = str(fields[fields['key']]) |
||
| 147 | # Remove it from the field list (can not be a field and a tag) |
||
| 148 | fields.pop(fields['key']) |
||
| 149 | # Add the hostname as a tag |
||
| 150 | tags['hostname'] = self.hostname |
||
| 151 | # Add name as a tag (example for the process list) |
||
| 152 | for k in FIELD_TO_TAG: |
||
| 153 | if k in fields: |
||
| 154 | tags[k] = str(fields[k]) |
||
| 155 | # Remove it from the field list (can not be a field and a tag) |
||
| 156 | if k in fields: |
||
| 157 | fields.pop(fields[k]) |
||
| 158 | # Add the measurement to the list |
||
| 159 | ret.append({'measurement': name, 'tags': tags, 'fields': fields}) |
||
| 160 | return ret |
||
| 161 | |||
| 178 |