示例#1
0
    def get(self,
            namespace,
            start=None,
            end=None,
            method=None,
            fill=None,
            group=None):
        """Make a query to retrieve something in the database."""
        if _validate_namespace(namespace):
            namespace, field = _extract_field(namespace)

        if not self._namespace_exists(namespace):
            error = (f'Error to get values because namespace \'{namespace}\''
                     'does not exist.')
            raise NamespaceError(error)

        if start is None and end is None:
            error = 'Start and end value should not be \'None\'.'
            raise ValueError(error)

        if iso_format_validation(start) is False and start is not None:
            start = convert_to_iso(start)
        if iso_format_validation(end) is False and end is not None:
            end = convert_to_iso(end)

        if validate_timestamp(start, end) is False:
            error = 'Error to get values due end value is smaller than start.'
            raise ValueError(error)

        points = self._get_points(namespace, start, end, field, method, fill,
                                  group)
        return points
示例#2
0
    def test_convert_to_iso_success(self):
        """Test success in method convert_to_iso."""
        timestamp = 0

        return_value = convert_to_iso(timestamp)

        self.assertEqual('1970-01-01T00:00:00Z', return_value)
示例#3
0
    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)
示例#4
0
    def delete(self, namespace, start=None, end=None):
        """Delete data in influxdb. Start and end must be a timestamp."""
        if iso_format_validation(start) is False and start is not None:
            start = convert_to_iso(start)
        if iso_format_validation(end) is False and end is not None:
            end = convert_to_iso(end)

        if _validate_namespace(namespace):
            namespace, _ = _extract_field(namespace)

        if not self._namespace_exists(namespace):
            error = (f'Error deleting because namespace \'{namespace}\' does'
                     'not exist.')
            raise NamespaceError(error)

        if validate_timestamp(start, end) is False:
            error = 'Error to get values due end value is smaller than start.'
            raise ValueError(error)

        return self._delete_points(namespace, start, end)
示例#5
0
    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)
示例#6
0
    def test_convert_to_iso_failed_with_large_value(self):
        """Test fail case in method convert_to_iso with a value too large."""
        timestamp = 10**100

        with self.assertRaises(ValueError):
            convert_to_iso(timestamp)
示例#7
0
    def test_convert_to_iso_failed_with_invalid_value(self):
        """Test fail case in method convert_to_iso with an invalid value."""
        timestamp = 'abc'

        with self.assertRaises(ValueError):
            convert_to_iso(timestamp)