@@ -98,7 +98,7 @@ def __str__(self) -> str:
98
98
99
99
100
100
def _body_reduce (batch_items ):
101
- return '\n ' .join (map (lambda batch_item : batch_item .data , batch_items ))
101
+ return b '\n ' .join (map (lambda batch_item : batch_item .data , batch_items ))
102
102
103
103
104
104
def _create_batch (group : GroupedObservable ):
@@ -144,7 +144,8 @@ def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions()
144
144
self ._disposable = None
145
145
146
146
def write (self , bucket : str , org : str ,
147
- record : Union [str , List ['str' ], Point , List ['Point' ], dict , List ['dict' ], Observable ],
147
+ record : Union [
148
+ str , List ['str' ], Point , List ['Point' ], dict , List ['dict' ], bytes , List ['bytes' ], Observable ],
148
149
write_precision : WritePrecision = DEFAULT_WRITE_PRECISION ) -> None :
149
150
"""
150
151
Writes time-series data into influxdb.
@@ -159,27 +160,7 @@ def write(self, bucket: str, org: str,
159
160
if self ._write_options .write_type is WriteType .batching :
160
161
return self ._write_batching (bucket , org , record , write_precision )
161
162
162
- final_string = ''
163
-
164
- if isinstance (record , str ):
165
- final_string = record
166
-
167
- if isinstance (record , Point ):
168
- final_string = record .to_line_protocol ()
169
-
170
- if isinstance (record , dict ):
171
- final_string = Point .from_dict (record , write_precision = write_precision ).to_line_protocol ()
172
-
173
- if isinstance (record , list ):
174
- lines = []
175
- for item in record :
176
- if isinstance (item , str ):
177
- lines .append (item )
178
- if isinstance (item , Point ):
179
- lines .append (item .to_line_protocol ())
180
- if isinstance (item , dict ):
181
- lines .append (Point .from_dict (item , write_precision = write_precision ).to_line_protocol ())
182
- final_string = '\n ' .join (lines )
163
+ final_string = self ._serialize (record , write_precision )
183
164
184
165
_async_req = True if self ._write_options .write_type == WriteType .asynchronous else False
185
166
@@ -203,13 +184,35 @@ def __del__(self):
203
184
self ._disposable = None
204
185
pass
205
186
187
+ def _serialize (self , record , write_precision ) -> bytes :
188
+ _result = b''
189
+ if isinstance (record , bytes ):
190
+ _result = record
191
+
192
+ elif isinstance (record , str ):
193
+ _result = record .encode ("utf-8" )
194
+
195
+ elif isinstance (record , Point ):
196
+ _result = self ._serialize (record .to_line_protocol (), write_precision = write_precision )
197
+
198
+ elif isinstance (record , dict ):
199
+ _result = self ._serialize (Point .from_dict (record , write_precision = write_precision ),
200
+ write_precision = write_precision )
201
+ elif isinstance (record , list ):
202
+ _result = b'\n ' .join ([self ._serialize (item , write_precision = write_precision ) for item in record ])
203
+
204
+ return _result
205
+
206
206
def _write_batching (self , bucket , org , data , precision = DEFAULT_WRITE_PRECISION ):
207
207
_key = _BatchItemKey (bucket , org , precision )
208
- if isinstance (data , str ):
208
+ if isinstance (data , bytes ):
209
209
self ._subject .on_next (_BatchItem (key = _key , data = data ))
210
210
211
+ elif isinstance (data , str ):
212
+ self ._write_batching (bucket , org , data .encode ("utf-8" ), precision )
213
+
211
214
elif isinstance (data , Point ):
212
- self ._subject . on_next ( _BatchItem ( key = _key , data = data .to_line_protocol ()) )
215
+ self ._write_batching ( bucket , org , data .to_line_protocol (), precision )
213
216
214
217
elif isinstance (data , dict ):
215
218
self ._write_batching (bucket , org , Point .from_dict (data , write_precision = precision ), precision )
@@ -234,7 +237,7 @@ def _http(self, batch_item: _BatchItem):
234
237
return _BatchResponse (data = batch_item )
235
238
236
239
def _post_write (self , _async_req , bucket , org , body , precision ):
237
- return self ._write_service .post_write (org = org , bucket = bucket , body = body . encode ( "utf-8" ) , precision = precision ,
240
+ return self ._write_service .post_write (org = org , bucket = bucket , body = body , precision = precision ,
238
241
async_req = _async_req , content_encoding = "identity" ,
239
242
content_type = "text/plain; charset=utf-8" )
240
243
@@ -272,4 +275,4 @@ def _on_error(ex):
272
275
273
276
def _on_complete (self ):
274
277
self ._disposable .dispose ()
275
- logger .info ("the batching processor was dispose " )
278
+ logger .info ("the batching processor was disposed " )
0 commit comments