Kaynağa Gözat

Updates to query_between_dates

ogert 4 yıl önce
ebeveyn
işleme
da05d386b0
1 değiştirilmiş dosya ile 22 ekleme ve 10 silme
  1. 22 10
      cdplib/db_handlers/MongodbHandler.py

+ 22 - 10
cdplib/db_handlers/MongodbHandler.py

@@ -388,7 +388,7 @@ class MongodbHandler:
         '''
         return self._database[collection_name].find({query_label:query_value}).count() > 0
 
-    def query_data_between_dates_and_generate_dataframe(self, collection_name: str, date_label: str, from_date_value: str, to_date_value: str, index: str = None, return_id: bool = False, return_as_dataframe: bool = True):
+    def query_data_between_dates_and_generate_dataframe(self, collection_name: str, date_label: str = None, from_date_value: str = None, to_date_value: str = None, index: str = None, return_id: bool = False, return_as_dataframe: bool = True, find_query: dict = None):
         '''
             Queries data between two dates.
 
@@ -401,18 +401,30 @@ class MongodbHandler:
         '''
         assert(isinstance(collection_name, str)),\
             "Parameter 'collection_name' must be a string type"
-        try:
-            query = {date_label: {'$gt': from_date_value, '$lt': to_date_value}}
-            data = self._database[collection_name].find(query, {'_id': return_id})
 
-        except Exception as error:
-            self._log.log_and_raise_error(('An error occured trying to query data from {}, with query {}. \nError:{}').format(collection_name, query, error))
+        if date_label and from_date_value and to_date_value or find_query:
 
-        if data.collection.count_documents(query) != 0:
-            if return_as_dataframe:
-                return self.convert_mongo_data_into_dataframe(data, index, collection_name)
+            try:
+
+                if find_query:
+                    query = find_query
+                else:
+                    query = {date_label: {'$gt': from_date_value, '$lt': to_date_value}}
+                
+                data = self._database[collection_name].find(query, {'_id': return_id})
+
+            except Exception as error:
+                self._log.log_and_raise_error(('An error occured trying to query data from {}, with query {}. \nError:{}').format(collection_name, query, error))
+
+            if data.collection.count_documents(query) != 0:
+                if return_as_dataframe:
+                    return self.convert_mongo_data_into_dataframe(data, index, collection_name)
+                else:
+                    return data
             else:
-                return data
+                self._log.warning(('No data was found for the query: {}, in collection: {}').format(query, collection_name))
+                return None
+        
 
     def query_oldest_or_newest_date_in_collection(self, collection_name: str, date_label: str, oldest: bool = False):