U vi4 @sTdZddlZddlZddlmZddlmZmZmZddlm Z ddl m Z ddl m Z ddlmZejdejd ZejZed d d d ddZed dd d d ddZedddZzddlZdZWnek rdZYnXGdddeZddZ ddZ!edddZ"d*edd d!Z#edd"d#Z$edd$d%Z%efd&d'Z&d(d)Z'dS)+z/Point data structure to represent LineProtocol.N)int)datetime timedeltatimezone)Decimal)Integral)get_date_helper)WritePrecision)tzz\,z\ z\nz\tz\r),    z\=)r =r r rrz\"z\\)"\TFc@seZdZdZeddZeefeedddZ ddZ efd d Z d d Z d dZ dddZeddZeddZddZddZdS)Pointz Point defines the values that will be written to the database. Ref: https://docs.influxdata.com/influxdb/latest/reference/key-concepts/data-elements/#point cCs t|}|S)z3Create a new Point with specified measurement name.)r) measurementprS/opt/alt/python38/lib/python3.8/site-packages/influxdb_client/client/write/point.pyr6szPoint.measurement) dictionarywrite_precisionc KsJ|dd}|dkr$||dd}t|}|dd}|dk rd|D]}||krD||||qDn*d|kr|dD]\}}|||qx|dd}|dk r|D]} | |kr|| || qn"|dD]\} } || | q|d d } | |kr|j|| |d |d i} d |kr.|d } ttd d| |_|S)a, Initialize point from 'dict' structure. The expected dict structure is: - measurement - tags - fields - time Example: .. code-block:: python # Use default dictionary structure dict_structure = { "measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, "fields": {"water_level": 1.0}, "time": 1 } point = Point.from_dict(dict_structure, WritePrecision.NS) Example: .. code-block:: python # Use custom dictionary structure dictionary = { "name": "sensor_pt859", "location": "warehouse_125", "version": "2021.06.05.5874", "pressure": 125, "temperature": 10, "created": 1632208639, } point = Point.from_dict(dictionary, write_precision=WritePrecision.S, record_measurement_key="name", record_time_key="created", record_tag_keys=["location", "version"], record_field_keys=["pressure", "temperature"]) Int Types: The following example shows how to configure the types of integers fields. It is useful when you want to serialize integers always as ``float`` to avoid ``field type conflict`` or use ``unsigned 64-bit integer`` as the type for serialization. .. code-block:: python # Use custom dictionary structure dict_structure = { "measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, "fields": { "water_level": 1.0, "some_counter": 108913123234 }, "time": 1 } point = Point.from_dict(dict_structure, field_types={"some_counter": "uint"}) :param dictionary: dictionary for serialize into data Point :param write_precision: sets the precision for the supplied time values :key record_measurement_key: key of dictionary with specified measurement :key record_measurement_name: static measurement name for data Point :key record_time_key: key of dictionary with specified timestamp :key record_tag_keys: list of dictionary keys to use as a tag :key record_field_keys: list of dictionary keys to use as a field :key field_types: optional dictionary to specify types of serialized fields. Currently, is supported customization for integer types. Possible integers types: - ``int`` - serialize integers as "**Signed 64-bit integers**" - ``9223372036854775807i`` (default behaviour) - ``uint`` - serialize integers as "**Unsigned 64-bit integers**" - ``9223372036854775807u`` - ``float`` - serialize integers as "**IEEE-754 64-bit floating-point numbers**". Useful for unify number types in your pipeline to avoid field type conflict - ``9223372036854775807`` The ``field_types`` can be also specified as part of incoming dictionary. For more info see an example above. :return: new data point Zrecord_measurement_nameNZrecord_measurement_keyrrecord_tag_keystagsrecord_field_keysfieldsrecord_time_keytime)r field_typescSs,|d|ddkrdn|ddkr&dndfS)NrriZuintur)itemrrrz!Point.from_dict..) getrtagitemsfieldrdictmap _field_types) rrkwargsZ measurement_pointrtag_key tag_valuerZ field_keyZ field_valuerr.rrr from_dict<s<M       zPoint.from_dictcCs(i|_i|_||_d|_t|_i|_dS)zInitialize defaults.N)_tags_fields_name_timeDEFAULT_WRITE_PRECISION_write_precisionr.)selfZmeasurement_namerrr__init__s zPoint.__init__cCs||_||_|S)a Specify timestamp for DataPoint with declared precision. If time doesn't have specified timezone we assume that timezone is UTC. Examples:: Point.measurement("h2o").field("val", 1).time("2009-11-10T23:00:00.123456Z") Point.measurement("h2o").field("val", 1).time(1257894000123456000) Point.measurement("h2o").field("val", 1).time(datetime(2009, 11, 10, 23, 0, 0, 123456)) Point.measurement("h2o").field("val", 1).time(1257894000123456000, write_precision=WritePrecision.NS) :param time: the timestamp for your data :param write_precision: sets the precision for the supplied time values :return: this point )r9r7)r:rrrrrrsz Point.timecCs||j|<|S)zAdd tag with key and value.)r4)r:keyvaluerrrr)s z Point.tagcCs||j|<|S)zAdd field with key and value.)r5)r:r+r=rrrr+s z Point.fieldNcCs|t|jt}|dr.d|d}t|tt|j}t |j |j }|sNdSt |j |dkrb|jn|}||||S)z Create LineProtocol. :param precision: required precision of LineProtocol. If it's not set then use the precision from ``Point``. #zThe measurement name 'z' start with '#'. The output Line protocol will be interpret as a comment by InfluxDB. For more info see: - https://docs.influxdata.com/influxdb/latest/reference/syntax/line-protocol/#comments r$N) _escape_keyr6_ESCAPE_MEASUREMENT startswithwarningswarn SyntaxWarning _append_tagsr4_append_fieldsr5r. _append_timer7r9)r: precisionZ _measurementmessager4r5r7rrrto_line_protocols     zPoint.to_line_protocolcCs|jS)zGet precision.)r9r:rrrrszPoint.write_precisioncCs ||_dS)z-Set the string representation for all Points.N)Z_Point__str___rep)clsZ rep_functionrrr set_str_repszPoint.set_str_repcCs|S)z+Create string representation of this Point.)rJrKrrr__str__sz Point.__str__cCsVt|tsdS|j|jkoT|j|jkoT|j|jkoT|j|jkoT|j|jkoT|j|jkS)z'Return true iff other is equal to self.F) isinstancerr4r5r6r7r9r.)r:otherrrr__eq__s       z Point.__eq__)N)__name__ __module__ __qualname____doc__ staticmethodrr8r,r r3r;rr)r+rJpropertyr classmethodrMrNrQrrrrr/s  u    rcCstg}t|D]F\}}|dkr"qt|}t|}|dkr|dkr||d|q|r`dndd|dS)Nr$rr r )sortedr*r?_escape_tag_valueappendjoin)r_returnr1r2r)r=rrrrE srEcCsRg}t|D]2\}}|dkr$qt|tsBt|tsBt|drt|sNqt|}| drl|dd}| t |d|qt|t st|drt|t s||d}| t |dt||qt|t r| t |dt|qt|tr*| t |dt|dqtd t|d |d qd |S) Nfloatz.0rrr"z="rzType: "z " of field: "z" is not supported.r )rYr*rOr^r_np_is_subtypemathisfinitestrendswithr[r?rboolr(lower_escape_string ValueErrortyper\)rr r]r+r=s_typerrrrFs(    " "  rF)returncCs |dkr dSdtt||S)Nr$r )r_convert_timestamp)rrrrrrG9srGcCs|dkr t}t||SN) _ESCAPE_KEYrc translate)r)Z escape_listrrrr??sr?cCst|}|dr|d7}|S)Nrr )r?rd)r=retrrrrZEs rZcCst|tSrn)rcrp_ESCAPE_STRING)r=rrrrgLsrgcCst}t|tr|St|tr(||}t|tst|tjSdS)NZdtypeFr^r) _HAS_NUMPYhasattrnpZ issubdtypeZfloatinginteger)r=Znp_typerrrr`ksr`)N)(rUrarBbuiltinsrrrrdecimalrnumbersrZ&influxdb_client.client.util.date_utilsrZ&influxdb_client.domain.write_precisionr fromtimestamputcrsrtr8rc maketransr@rorrnumpyrzrxModuleNotFoundErrorobjectrrErFrGr?rZrgrmr`rrrrsX       ]