Exemple #1
0
    def test_write_point_different_precision(self):
        httpretty.register_uri(httpretty.POST,
                               uri="http://localhost/api/v2/write",
                               status=204)

        _point1 = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 5.0).time(5, WritePrecision.S)
        _point2 = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 6.0).time(6, WritePrecision.NS)

        self._write_client.write("my-bucket", "my-org", [_point1, _point2])

        time.sleep(1)

        _requests = httpretty.httpretty.latest_requests

        self.assertEqual(2, len(_requests))

        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=5.0 5",
            _requests[0].parsed_body)
        self.assertEqual("s", _requests[0].querystring["precision"][0])
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=6.0 6",
            _requests[1].parsed_body)
        self.assertEqual("ns", _requests[1].querystring["precision"][0])
Exemple #2
0
    def _write_batching(self,
                        bucket,
                        org,
                        data,
                        precision=DEFAULT_WRITE_PRECISION):
        _key = _BatchItemKey(bucket, org, precision)
        if isinstance(data, bytes):
            self._subject.on_next(_BatchItem(key=_key, data=data))

        elif isinstance(data, str):
            self._write_batching(bucket, org, data.encode("utf-8"), precision)

        elif isinstance(data, Point):
            self._write_batching(bucket, org, data.to_line_protocol(),
                                 precision)

        elif isinstance(data, dict):
            self._write_batching(
                bucket, org, Point.from_dict(data, write_precision=precision),
                precision)

        elif isinstance(data, list):
            for item in data:
                self._write_batching(bucket, org, item, precision)

        elif isinstance(data, Observable):
            data.subscribe(
                lambda it: self._write_batching(bucket, org, it, precision))
            pass

        return None
Exemple #3
0
    def test_to_low_flush_interval(self):

        self._write_client.__del__()
        self._write_client = WriteApi(influxdb_client=self.influxdb_client,
                                      write_options=WriteOptions(
                                          batch_size=8,
                                          flush_interval=1,
                                          jitter_interval=1000))

        httpretty.register_uri(httpretty.POST,
                               uri="http://localhost/api/v2/write",
                               status=204)

        for i in range(50):
            val_one = float(i)
            val_two = float(i) + 0.5
            point_one = Point("OneMillis").tag("sensor", "sensor1").field(
                "PSI", val_one).time(time=i)
            point_two = Point("OneMillis").tag("sensor", "sensor2").field(
                "PSI", val_two).time(time=i)

            self._write_client.write("my-bucket", "my-org",
                                     [point_one, point_two])
            time.sleep(0.1)

        self._write_client.__del__()

        _requests = httpretty.httpretty.latest_requests

        for _request in _requests:
            body = _request.parsed_body
            self.assertTrue(body,
                            msg="Parsed body should be not empty " +
                            str(_request))

        httpretty.reset()
Exemple #4
0
    def write(self,
              bucket_name,
              point_name,
              tag_name,
              tag_value,
              field_name,
              field_value,
              synchronous=False):
        """
        Writes a Point to the bucket specified.
        Example: Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
        """
        # By defaults its Batching
        if synchronous:
            write_api = self.client.write_api(write_options=SYNCHRONOUS)
        else:
            write_api = self.client.write_api()

        p = Point(point_name).tag(tag_name,
                                  tag_value).field(field_name, field_value)

        write_api.write(bucket=bucket_name, record=p)
Exemple #5
0
    def _serialize(self, record, write_precision) -> bytes:
        _result = b''
        if isinstance(record, bytes):
            _result = record

        elif isinstance(record, str):
            _result = record.encode("utf-8")

        elif isinstance(record, Point):
            _result = self._serialize(record.to_line_protocol(),
                                      write_precision=write_precision)

        elif isinstance(record, dict):
            _result = self._serialize(Point.from_dict(
                record, write_precision=write_precision),
                                      write_precision=write_precision)
        elif isinstance(record, list):
            _result = b'\n'.join([
                self._serialize(item, write_precision=write_precision)
                for item in record
            ])

        return _result
Exemple #6
0
def send_to_influx(options, units, uptime, nvidia):
    if influx is None:
        return

    metric_data = get_metric_data(options, units, uptime, nvidia)
    data = [
        Point.from_dict({
            'measurement': pt['metric'].split('.')[0],
            'tags': pt['tags'],
            'fields': {pt['metric'].split('.', 1)[1]: pt['value']},
            'time': datetime.datetime.utcnow()
        })
        for pt in metric_data
    ]
    
    try:
        influx_writer.write(
            bucket="fah",
            record=data
        )
        print('wrote to influxdb')
    except Exception as ex:
        print(str(ex))
Exemple #7
0
    def test_record_types(self):
        httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/write", status=204)

        # Record item
        _record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
        self._write_client.write("my-bucket", "my-org", _record)

        # Point item
        _point = Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 2.0).time(2)
        self._write_client.write("my-bucket", "my-org", _point)

        # Record list
        self._write_client.write("my-bucket", "my-org",
                                 ["h2o_feet,location=coyote_creek level\\ water_level=3.0 3",
                                  "h2o_feet,location=coyote_creek level\\ water_level=4.0 4"])

        # Point list
        _point1 = Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 5.0).time(5)
        _point2 = Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 6.0).time(6)
        self._write_client.write("my-bucket", "my-org", [_point1, _point2])

        # Observable
        _recordObs = "h2o_feet,location=coyote_creek level\\ water_level=7.0 7"
        _pointObs = Point("h2o_feet").tag("location", "coyote_creek").field("level water_level", 8.0).time(8)

        self._write_client.write("my-bucket", "my-org", rx.of(_recordObs, _pointObs))

        _data = rx \
            .range(9, 13) \
            .pipe(ops.map(lambda i: "h2o_feet,location=coyote_creek level\\ water_level={0}.0 {0}".format(i)))
        self._write_client.write("my-bucket", "my-org", _data)

        # Dictionary item
        _dict = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
                 "time": 13, "fields": {"level water_level": 13.0}}
        self._write_client.write("my-bucket", "my-org", _dict)

        # Dictionary list
        _dict1 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
                  "time": 14, "fields": {"level water_level": 14.0}}
        _dict2 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
                  "time": 15, "fields": {"level water_level": 15.0}}
        self._write_client.write("my-bucket", "my-org", [_dict1, _dict2])

        # Bytes item
        _bytes = "h2o_feet,location=coyote_creek level\\ water_level=16.0 16".encode("utf-8")
        self._write_client.write("my-bucket", "my-org", _bytes)

        # Bytes list
        _bytes1 = "h2o_feet,location=coyote_creek level\\ water_level=17.0 17".encode("utf-8")
        _bytes2 = "h2o_feet,location=coyote_creek level\\ water_level=18.0 18".encode("utf-8")
        self._write_client.write("my-bucket", "my-org", [_bytes1, _bytes2])

        time.sleep(1)

        _requests = httpretty.httpretty.latest_requests

        self.assertEqual(9, len(_requests))

        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=2.0 2", _requests[0].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=4.0 4", _requests[1].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=5.0 5\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=6.0 6", _requests[2].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=7.0 7\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=8.0 8", _requests[3].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=9.0 9\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=10.0 10", _requests[4].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=11.0 11\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=12.0 12", _requests[5].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=13.0 13\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=14.0 14", _requests[6].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=15.0 15\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=16.0 16", _requests[7].parsed_body)
        self.assertEqual("h2o_feet,location=coyote_creek level\\ water_level=17.0 17\n"
                         "h2o_feet,location=coyote_creek level\\ water_level=18.0 18", _requests[8].parsed_body)

        pass
    def test_record_types(self):
        httpretty.register_uri(httpretty.POST,
                               uri="http://localhost/api/v2/write",
                               status=204)

        # Record item
        _record = "h2o_feet,location=coyote_creek level\\ water_level=1.0 1"
        self._write_client.write("my-bucket", "my-org", _record)

        # Point item
        _point = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 2.0).time(2)
        self._write_client.write("my-bucket", "my-org", _point)

        # Record list
        self._write_client.write("my-bucket", "my-org", [
            "h2o_feet,location=coyote_creek level\\ water_level=3.0 3",
            "h2o_feet,location=coyote_creek level\\ water_level=4.0 4"
        ])

        # Point list
        _point1 = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 5.0).time(5)
        _point2 = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 6.0).time(6)
        self._write_client.write("my-bucket", "my-org", [_point1, _point2])

        # Observable
        _recordObs = "h2o_feet,location=coyote_creek level\\ water_level=7.0 7"
        _pointObs = Point("h2o_feet").tag("location", "coyote_creek").field(
            "level water_level", 8.0).time(8)

        self._write_client.write("my-bucket", "my-org",
                                 rx.of(_recordObs, _pointObs))

        _data = rx \
            .range(9, 13) \
            .pipe(ops.map(lambda i: "h2o_feet,location=coyote_creek level\\ water_level={0}.0 {0}".format(i)))
        self._write_client.write("my-bucket", "my-org", _data)

        time.sleep(1)

        _requests = httpretty.httpretty.latest_requests

        self.assertEqual(6, len(_requests))

        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=1.0 1\n"
            "h2o_feet,location=coyote_creek level\\ water_level=2.0 2",
            _requests[0].parsed_body)
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=3.0 3\n"
            "h2o_feet,location=coyote_creek level\\ water_level=4.0 4",
            _requests[1].parsed_body)
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=5.0 5\n"
            "h2o_feet,location=coyote_creek level\\ water_level=6.0 6",
            _requests[2].parsed_body)
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=7.0 7\n"
            "h2o_feet,location=coyote_creek level\\ water_level=8.0 8",
            _requests[3].parsed_body)
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=9.0 9\n"
            "h2o_feet,location=coyote_creek level\\ water_level=10.0 10",
            _requests[4].parsed_body)
        self.assertEqual(
            "h2o_feet,location=coyote_creek level\\ water_level=11.0 11\n"
            "h2o_feet,location=coyote_creek level\\ water_level=12.0 12",
            _requests[5].parsed_body)

        pass