def save(self, namespace, value, timestamp=None): """Insert the data on influxdb. In this case (InfluxDB), the last namespace will be the table. timestamp must be on ISO-8601 format. """ if re.match(r'[+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?', value): value = float(value) elif not isinstance(value, bool) and isinstance(value, int): value = float(value) timestamp = timestamp or now() timestamp = iso_format_validation(timestamp) if timestamp == 400: return timestamp namespace, field = _verify_namespace(namespace) if isinstance(namespace, tuple): namespace = namespace[0] if namespace in (400, 404): return namespace data = [{ 'measurement': namespace, 'time': timestamp, 'fields': {field: value} }] return self._write_endpoints(data)
def save(self, namespace, data_to_save, timestamp=None): """Insert data on influxdb.""" try: for key, stat in data_to_save.items(): data_to_save[key] = float(stat) except ValueError: error = (f"Could not convert {type(stat)} to float: '{stat}' " f"for key '{key}'.") raise ValueError(error) _validate_namespace(namespace) timestamp = timestamp or now() if isinstance(timestamp, (int, float)) or \ iso_format_validation(timestamp) is False: timestamp = convert_to_iso(timestamp) data = [{ 'measurement': namespace, 'time': timestamp, 'fields': data_to_save }] return self._write_endpoints(data)
def _make_search(start, end, dataframe): '''Return part of the dataframe''' end = end or now() start = start or 0 validate_timestamp(start, end) start = iso_format_validation(start) end = iso_format_validation(end) iso = '%Y-%m-%dT%H:%M:%SZ' search = dataframe.Timestamp.dt.strftime(iso) search = search.between(start, end) return search
def _make_search(start, end, fname): """Return the result of the search in csv file.""" end = end or now() start = start or 0 validate_timestamp(start, end) start = iso_format_validation(start) end = iso_format_validation(end) search = [] with open(fname, 'r', newline='') as csvfile: csvreader = csv.reader(csvfile, delimiter=',') for row in csvreader: if start <= row[1] <= end: search.append(row) return search
def save(self, namespace, value, timestamp=None): """Insert data on influxdb.""" if _validate_namespace(namespace): namespace, field = _extract_field(namespace) try: value = float(value) except ValueError: error = f'Is not possible convert value \'{value}\' to float.' raise ValueConvertError(error) timestamp = timestamp or now() if iso_format_validation(timestamp) is False: timestamp = convert_to_iso(timestamp) data = [{ 'measurement': namespace, 'time': timestamp, 'fields': { field: value } }] return self._write_endpoints(data)
def _put_value(value, store_value, timestamp=None): """Save a value in a dataframe.""" store_value.loc[0, 'Value'] = value store_value.loc[0, 'Timestamp'] = timestamp or now()