diff --git a/ricochet/monitoring/ricochetdb.py b/ricochet/monitoring/ricochetdb.py index f475b208e3e2a5cdbec3cffe173f3b0c1e013757..71a36f44120c57b7f19219f0aba839577e7ab11f 100644 --- a/ricochet/monitoring/ricochetdb.py +++ b/ricochet/monitoring/ricochetdb.py @@ -159,7 +159,16 @@ class Database: return self.db.command("serverStatus",0,cursors=0,repl=0,metrics=0,locks=0,asserts=0,\ connections=0,opReadConcernCounters=0,transactions=0,wiredTiger=0,network=0,extra_info=0,\ twoPhaseCommitCoordinator=0,tcmalloc=0,storageEngine=0)["localTime"] - + + def is_configured(self): + """ + Check if the Database connexion is configured + + :return: + True if the connexion is configured, Flase otherwise + """ + return (self.db is not None) + @classmethod def get_database(cls): """ @@ -453,6 +462,10 @@ class DataFetcher: val=val[:-1] return None + def __json_convert__(self,o): + if isinstance(o, datetime): + return o.__str__() + def add_datasource(self, device_type, device_ip_address, file_location="/www/device.htm"): """ Adds a datasource to be queried by the DataFetcher. @@ -490,7 +503,11 @@ class DataFetcher: self.data_buffer = [] results_logical = {} - timestamp=Database.get_database().get_db_timestamp() + timestamp=None + if(Database.get_database().is_configured()): + timestamp = Database.get_database().get_db_timestamp() + else: + timestamp = datetime.utcnow() buff = [] if(data_type=="imacrt" and self.udp_status): @@ -556,6 +573,18 @@ class DataFetcher: Database.get_database().upload(self.data_buffer,collection) self.data_buffer=[] + def move_to_file(self, file_name): + """ + Move the data stored in the DataFetcher object in a local JSON file. + + :param file_name: Name of the output file + :type file_name: str + + """ + with open(file_name, 'a') as f: + json.dump(self.data_buffer, f, default = self.__json_convert__) + self.data_buffer=[] + def __get_data_from_json_http__(self,device_ip_address, data, file_location): r = requests.get('http://'+str(device_ip_address)+file_location) if(r.ok): diff --git a/tests/unit_test.py b/tests/unit_test.py index 019709db4838cd7244398152b51f0ec5a6419246..9cf93d6ec362abcffc8b2f0cb9824abbe2331166 100644 --- a/tests/unit_test.py +++ b/tests/unit_test.py @@ -178,6 +178,9 @@ class TestRicochetDB(unittest.TestCase): self.assertEqual(d.data_buffer[0]['heater_still'],0.0) d.upload('mmr3-http') self.assertEqual(len(d.data_buffer),0) + d.fetch("imacrt") + d.move_to_file("./output_file.json") + self.assertTrue(os.path.exists("./output_file.json")) col = Database.get_database().get_collection('mmr3-http') for r in col.find({}): self.assertEqual(r['ruo2_mc'],19.5673)