Пример #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 get(self, namespace, start=None, end=None,
         method=None, fill=None, group=None):
     """Make a query to retrieve something in the database."""
     start = iso_format_validation(start)
     end = iso_format_validation(end)
     namespace, field = _verify_namespace(namespace)
     if not self._namespace_exists(namespace):
         return 400
     if validate_timestamp(start, end) == 400:
         return 400
     points = self._get_points(namespace, start, end,
                               field, method, fill, group)
     return _parse_result_set(points, field)
Пример #3
0
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
Пример #4
0
    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)
Пример #5
0
    def test_iso_format_validation_success(self):
        """Test success in method iso_format_validation."""
        timestamp = "1970-01-01T00:00:00Z"

        return_value = iso_format_validation(timestamp)

        self.assertEqual(return_value, True)
Пример #6
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)
Пример #7
0
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
Пример #8
0
    def test_iso_format_validation_fail(self):
        """Test fail in method iso_format_validation."""
        # invalid timestamp
        timestamp = "abc"

        return_value = iso_format_validation(timestamp)

        self.assertEqual(return_value, False)
Пример #9
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)
Пример #10
0
    def delete(self, namespace, start=None, end=None):
        """Delete the entire database.

        start and end most be a timestamp
        """
        start = iso_format_validation(start)
        end = iso_format_validation(end)
        namespace = _verify_namespace(namespace)
        if isinstance(namespace, tuple):
            namespace = namespace[0]

        if namespace in (400, 404):
            return namespace

        if not self._namespace_exists(namespace):
            log.error("Namespace {} does not exist".format(namespace))
            return 400

        if validate_timestamp(start, end) == 400:
            return 400

        self._delete_points(namespace, start, end)
        return 200
Пример #11
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)