def test_TimeSpanFormatting(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(TimeSpan.FromDays(1), WritePrecision.NS) self.assertEqual("h2o,location=europe level=2i 86400000000000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(TimeSpan.FromHours(356), WritePrecision.US) self.assertEqual("h2o,location=europe level=2i 1281600000000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(TimeSpan.FromSeconds(156), WritePrecision.MS) self.assertEqual("h2o,location=europe level=2i 156000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(TimeSpan.FromSeconds(123), WritePrecision.S) self.assertEqual("h2o,location=europe level=2i 123", point.to_line_protocol())
def test_MeasurementEscape(self): point = Point.measurement("h2 o").tag("location", "europe").tag("", "warn").field("level", 2) self.assertEqual(point.to_line_protocol(), "h2\\ o,location=europe level=2i") point = Point.measurement("h2,o").tag("location", "europe").tag("", "warn").field("level", 2) self.assertEqual(point.to_line_protocol(), "h2\\,o,location=europe level=2i") pass
def test_point_protocol(self): dt = datetime(year=2009, month=11, day=10, hour=23, minute=0, second=0, microsecond=123456) point = Point.measurement('weather').time(dt, WritePrecision.MS) \ .tag("location", "Přerov") \ .tag("sid", "12345") \ .field("temperature", 30.1) \ .field("int_field", 2) \ .field("float_field", 0) self.assertEqual( point.to_line_protocol(), "weather,location=Přerov,sid=12345 float_field=0i,int_field=2i,temperature=30.1 1257894000123" ) point = Point.measurement('weather').time(dt, WritePrecision.MS) \ .field("temperature", 30.1) \ .field("float_field", 0) print(point.to_line_protocol()) self.assertEqual( point.to_line_protocol(), "weather float_field=0i,temperature=30.1 1257894000123")
def test_DateTimeFormatting(self): dateTime = datetime(2015, 10, 15, 8, 20, 15) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(dateTime, WritePrecision.MS) self.assertEqual("h2o,location=europe level=2i 1444897215000", point.to_line_protocol()) dateTime = datetime(2015, 10, 15, 8, 20, 15, 750, UTC) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", False) \ .time(dateTime, WritePrecision.S) self.assertEqual("h2o,location=europe level=false 1444897215", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", True) \ .time(datetime.now(UTC), WritePrecision.S) lineProtocol = point.to_line_protocol() self.assertTrue("." not in lineProtocol) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", True) \ .time(datetime.now(UTC), WritePrecision.NS) lineProtocol = point.to_line_protocol() self.assertTrue("." not in lineProtocol)
def save_device_package(self, device_token: str, country: str, region_state: str, city: str, street: str, building: str, index: str, temperature: float, air_humidity: float) -> bool: p_temperature = Point.measurement("temperature") p_temperature.tag("device_token", device_token) p_temperature.tag("country", country) p_temperature.tag("region_state", region_state) p_temperature.tag("city", city) p_temperature.tag("street", street) p_temperature.tag("building", building) p_temperature.tag("index", index) p_temperature.field("c", temperature) self.__write_api.write(bucket=self.__bucket, record=p_temperature) p_air_humidity = Point.measurement("air_humidity") p_air_humidity.tag("device_token", device_token) p_air_humidity.tag("country", country) p_air_humidity.tag("region_state", region_state) p_air_humidity.tag("city", city) p_air_humidity.tag("street", street) p_air_humidity.tag("building", building) p_air_humidity.tag("index", index) p_air_humidity.field("percentage", air_humidity) self.__write_api.write(bucket=self.__bucket, record=p_air_humidity) return True
def test_points_from_different_timezones(self): time_in_utc = UTC.localize(datetime(2020, 7, 4, 0, 0, 0, 123456)) time_in_hk = timezone('Asia/Hong_Kong').localize( datetime(2020, 7, 4, 8, 0, 0, 123456)) # +08:00 point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc) point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk) self.assertEqual(point_utc.to_line_protocol(), point_hk.to_line_protocol())
def test_points_from_different_timezones(self): time_in_utc = datetime(2020, 7, 4, 0, 0, 0, 123456).replace(tzinfo=timezone.utc) time_in_hk = datetime(2020, 7, 4, 8, 0, 0, 123456).replace( tzinfo=tz.gettz('Asia/Hong_Kong')) # +08:00 point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc) point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk) self.assertEqual(point_utc.to_line_protocol(), point_hk.to_line_protocol())
def test_unsupported_field_type(self): with self.assertRaises(ValueError) as ve: Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", timezone.utc) \ .to_line_protocol() exception = ve.exception self.assertEqual( 'Type: "<class \'datetime.timezone\'>" of field: "level" is not supported.', f'{exception}')
def test_FieldEscape(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", "string esc\\ape value") self.assertEqual("h2o,location=europe level=\"string esc\\\\ape value\"", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", "string esc\"ape value") self.assertEqual("h2o,location=europe level=\"string esc\\\"ape value\"", point.to_line_protocol())
def parse_mitemp_msg(topic, msg): sensor_name = topic.split('/')[-1] return Point.measurement('mitemperature').time(datetime.fromtimestamp(msg['timestamp'])).tag('room', sensor_name) \ .field('temperature', msg['temperature']) \ .field('humidity', msg['humidity']) \ .field('batt_voltage', msg['batt_voltage']) \ .field('batt_level', msg['batt_level'])
def test_FieldNullValue(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .field("warning", None) self.assertEqual("h2o,location=europe level=2i", point.to_line_protocol())
def test_TagEmptyValue(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .tag("log", "") \ .field("level", 2) self.assertEqual("h2o,location=europe level=2i", point.to_line_protocol())
def test_Time(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(123, WritePrecision.S) self.assertEqual("h2o,location=europe level=2i 123", point.to_line_protocol())
def fetch_inverter_data(url: str, serial_number: str, username: str, password: str): """Fetch data from inverter livedata :return: livedata json """ global _last_timestamp try: response = requests.get(url, auth=(username, password), timeout=3) if response.status_code == 200: livedata = json.loads(response.text) timestamp = datetime.strptime(livedata[serial_number]["timestamp"], "%Y-%m-%dT%H:%M:%S%z") if timestamp != _last_timestamp: points = livedata[serial_number]["points"] for point in points: yield Point.measurement("FV_measurement").field( point["name"], point["value"]).time(timestamp, write_precision=WritePrecision.S) _last_timestamp = timestamp else: logging.warning("Same timestamp, skipping...") return None else: logging.error("Inverter response not OK") except Exception: logging.exception("No inverter connection?") return None
def test_EqualSignEscaping(self): point = Point.measurement("h=2o") \ .tag("l=ocation", "e=urope") \ .field("l=evel", 2) self.assertEqual("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", point.to_line_protocol())
def test_OverrideTagField(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .tag("location", "europe2") \ .field("level", 2) \ .field("level", 3) self.assertEqual("h2o,location=europe2 level=3i", point.to_line_protocol())
def test_name_start_with_hash(self): point = Point.measurement("#hash_start").tag("location", "europe").field( "level", 2.2) with pytest.warns(SyntaxWarning) as warnings: self.assertEqual('#hash_start,location=europe level=2.2', point.to_line_protocol()) self.assertEqual(1, len(warnings))
def test_InstantFormatting(self): instant = "1970-01-01T00:00:45.999999999Z" point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(instant, WritePrecision.S) self.assertEqual("h2o,location=europe level=2i 45", point.to_line_protocol())
def test_DateTimeUtc(self): date_time = datetime(2015, 10, 15, 8, 20, 15) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(date_time) self.assertEqual("h2o,location=europe level=2i 1444897215000000000", point.to_line_protocol())
def test_TagEscapingKeyAndValue(self): point = Point.measurement("h\n2\ro\t_data") \ .tag("new\nline", "new\nline") \ .tag("carriage\rreturn", "carriage\nreturn") \ .tag("t\tab", "t\tab") \ .field("level", 2) self.assertEqual( "h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.to_line_protocol())
def test_only_infinity_values(self): _point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("decimal-infinity-positive", Decimal('Infinity')) \ .field("decimal-infinity-negative", Decimal('-Infinity')) \ .field("decimal-nan", Decimal('NaN')) \ .field("flout-infinity-positive", float('inf')) \ .field("flout-infinity-negative", float('-inf')) \ .field("flout-nan", float('nan')) self.assertEqual("", _point.to_line_protocol())
def parse_state_msg(topic, msg): return Point.measurement('smartplugstate').time(msg['Time']).tag('spid', int(topic.split('/')[2])) \ .field('uptime_sec', msg['UptimeSec']) \ .field('heap', msg['Heap']) \ .field('sleep_mode', msg['SleepMode']) \ .field('sleep', msg['Sleep']) \ .field('loadavg', msg['LoadAvg']) \ .field('mqtt_count', msg['MqttCount']) \ .field('power', msg['POWER']) \ .field('is_on', msg['POWER'] == 'ON') \ .field('wifi_channel', msg['Wifi']['Channel']) \ .field('wifi_rssi', msg['Wifi']['RSSI']) \ .field('wifi_signal', msg['Wifi']['Signal']) \ .field('wifi_link_count', msg['Wifi']['LinkCount'])
def parse_sensor_msg(topic, msg): time = msg['Time'] msg = msg['ENERGY'] return Point.measurement('smartplugsensor').time(time).tag('spid', int(topic.split('/')[2])) \ .field('total_start_time', msg['TotalStartTime']) \ .field('total', msg['Total']) \ .field('yesterday', msg['Yesterday']) \ .field('today', msg['Today']) \ .field('period', msg['Period']) \ .field('power', msg['Power']) \ .field('apparent_power', msg['ApparentPower']) \ .field('reactive_power', msg['ReactivePower']) \ .field('factor', msg['Factor']) \ .field('voltage', msg['Voltage']) \ .field('current', msg['Current'])
def test_timestamp(self): """Test timezone in TestLineProtocol object.""" dt = datetime(2009, 11, 10, 23, 0, 0, 123456) utc = UTC.localize(dt) berlin = timezone('Europe/Berlin').localize(dt) eastern = berlin.astimezone(timezone('US/Eastern')) exp_utc = 'A val=1i 1257894000123456000' exp_est = 'A val=1i 1257890400123456000' point = Point.measurement("A").field("val", 1).time(dt) self.assertEqual(point.to_line_protocol(), exp_utc) self.assertEqual(point.time(utc).to_line_protocol(), exp_utc) self.assertEqual(point.time(berlin).to_line_protocol(), exp_est) self.assertEqual(point.time(eastern).to_line_protocol(), exp_est)
def _generate_data(data: List[dict]) -> List[Point]: points = [] for tags, fields, timestamp in map( itemgetter('tags', 'fields', 'timestamp'), data): collector = tags['collector'] # Every measurement goes against what collected it p = Point.measurement(collector) # which makes the collector tag not needed, remove to reduce dimensionality [ p.tag(key, value) for key, value in tags.items() if key != 'collector' and not isinstance(value, type(None)) ] [ p.field(key, value) for key, value in fields.items() if not isinstance(value, type(None)) ] p.time(datetime.fromtimestamp(timestamp, tz=timezone.utc), write_precision=WritePrecision.S) points.append(p) return points
def test_lineprotocol_encode(self): point = Point.measurement('test') point._tags = { "empty_tag": "", "none_tag": None, "backslash_tag": "C:\\", "integer_tag": 2, "string_tag": "hello" } point._fields = { "string_val": "hello!", "int_val": 1, "float_val": 1.1, "none_field": None, "bool_val": True, } self.assertEqual( point.to_line_protocol(), 'test,backslash_tag=C:\\\\ ,integer_tag=2,string_tag=hello ' 'bool_val=true,float_val=1.1,int_val=1i,string_val="hello!"')
def test_numpy_types(self): from influxdb_client.extras import np point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("np.float1", np.float(1.123)) \ .field("np.float2", np.float16(2.123)) \ .field("np.float3", np.float32(3.123)) \ .field("np.float4", np.float64(4.123)) \ .field("np.int1", np.int8(1)) \ .field("np.int2", np.int16(2)) \ .field("np.int3", np.int32(3)) \ .field("np.int4", np.int64(4)) \ .field("np.uint1", np.uint8(5)) \ .field("np.uint2", np.uint16(6)) \ .field("np.uint3", np.uint32(7)) \ .field("np.uint4", np.uint64(8)) self.assertEqual( "h2o,location=europe np.float1=1.123,np.float2=2.123,np.float3=3.123,np.float4=4.123,np.int1=1i,np.int2=2i,np.int3=3i,np.int4=4i,np.uint1=5i,np.uint2=6i,np.uint3=7i,np.uint4=8i", point.to_line_protocol())
def test_TimeSpanFormatting(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(days=1), WritePrecision.NS) self.assertEqual("h2o,location=europe level=2i 86400000000000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(hours=356), WritePrecision.US) self.assertEqual("h2o,location=europe level=2i 1281600000000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(seconds=156), WritePrecision.MS) self.assertEqual("h2o,location=europe level=2i 156000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(seconds=123), WritePrecision.S) self.assertEqual("h2o,location=europe level=2i 123", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(microseconds=876), WritePrecision.NS) self.assertEqual("h2o,location=europe level=2i 876000", point.to_line_protocol()) point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", 2) \ .time(timedelta(milliseconds=954), WritePrecision.NS) self.assertEqual("h2o,location=europe level=2i 954000000", point.to_line_protocol())
def test_FieldTypes(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("long", 1) \ .field("double", 250.69) \ .field("float", 35.0) \ .field("integer", 7) \ .field("short", 8) \ .field("byte", 9) \ .field("ulong", 10) \ .field("uint", 11) \ .field("sbyte", 12) \ .field("ushort", 13) \ .field("point", 13.3) \ .field("decimal", 25.6) \ .field("decimal-object", Decimal('0.142857')) \ .field("boolean", False) \ .field("string", "string value") expected = "h2o,location=europe boolean=false,byte=9i,decimal=25.6,decimal-object=0.142857,double=250.69," \ "float=35.0,integer=7i,long=1i,point=13.3,sbyte=12i,short=8i,string=\"string value\"," \ "uint=11i,ulong=10i,ushort=13i" self.assertEqual(expected, point.to_line_protocol())
def test_timezone(self): """Test timezone in TestLineProtocol object.""" dt = datetime(2009, 11, 10, 23, 0, 0, 123456) utc = dt.replace(tzinfo=timezone.utc) berlin = dt.replace(tzinfo=tz.gettz('Europe/Berlin')) eastern = berlin.astimezone(tz.gettz('US/Eastern')) self.assertEqual( "h2o val=1i 0", Point.measurement("h2o").field("val", 1).time(0).to_line_protocol()) self.assertEqual( "h2o val=1i 1257894000123456000", Point.measurement("h2o").field( "val", 1).time("2009-11-10T23:00:00.123456Z").to_line_protocol()) self.assertEqual( "h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(utc).to_line_protocol()) self.assertEqual( "h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(dt).to_line_protocol()) self.assertEqual( "h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time( 1257894000123456000, write_precision=WritePrecision.NS).to_line_protocol()) self.assertEqual( "h2o val=1i 1257890400123456000", Point.measurement("h2o").field("val", 1).time(eastern).to_line_protocol()) self.assertEqual( "h2o val=1i 1257890400123456000", Point.measurement("h2o").field("val", 1).time(berlin).to_line_protocol())