Ver código fonte

added tag columns to the write method

tanja 3 anos atrás
pai
commit
07c1673eed
1 arquivos alterados com 23 adições e 5 exclusões
  1. 23 5
      cdplib/db_handlers/InfluxdbHandler.py

+ 23 - 5
cdplib/db_handlers/InfluxdbHandler.py

@@ -125,20 +125,38 @@ class InfluxdbHandler:
         return self.query_to_dataframe(query)
 
     def insert_dataframe(self, dataframe: pd.DataFrame,
+                         tag_columns: list[str] = None,
                          batch_size: int = 10000,
                          time_precision: str = 'u'):
         """
-        :param dataframe: DESCRIPTION
+        Writes each column of the dataframe which is not 
+        in tag_columns as a separate measurement to the database.
+        
+        Tag columns are put as tags to each measurement.
+        
+        The dataframe has to have a datatime index!
+        
+        :param dataframe: dataframe to write to the database
         :type dataframe: pd.DataFrame
-        :return: DESCRIPTION
-        :rtype: TYPE
-
+        :param tag_columns: column names to be used as tags
+        :type tag_columns: list
+        :param betch_size:
+        :type batch_size: int
+        :param time_precision:
+        :type tiime_precision: str
         """
-        for column in dataframe.columns:
+        
+        tags = {col: dataframe[col] for col in tag_columns}
+        
+        measurement_columns = [c for c in dataframe.columns
+                               if c not in tag_columns]
+        
+        for column in measurement_columns:
             try:
                 self.client.write_points(
                     dataframe=dataframe[column].to_frame(),
                     measurement=column,
+                    tags=tags,
                     protocol='line',
                     batch_size=batch_size,
                     time_precision=time_precision)