diff --git a/aerosense_tools/queries.py b/aerosense_tools/queries.py index ef1d5a4..5436aab 100644 --- a/aerosense_tools/queries.py +++ b/aerosense_tools/queries.py @@ -423,12 +423,11 @@ def extract_and_add_new_measurement_sessions(self, sensors=None): :param list(str)|None sensors: the sensors to search for new measurement sessions for :return None: """ - table_name = DATASET_NAME + ".sessions" + sessions_table_name = DATASET_NAME + ".sessions" sensors = sensors or ( "connection_statistics", "magnetometer", - "connection-statistics", "barometer", "barometer_thermometer", "accelerometer", @@ -454,7 +453,7 @@ def extract_and_add_new_measurement_sessions(self, sensors=None): result = ( self.client.query( f""" - SELECT finish_datetime FROM {table_name} + SELECT finish_datetime FROM {sessions_table_name} WHERE installation_reference = @installation_reference AND node_id = @node_id AND sensor_type_reference = sensor_type_reference @@ -480,29 +479,28 @@ def extract_and_add_new_measurement_sessions(self, sensors=None): try: latest_session_finish_datetime = result.iloc[0]["finish_datetime"].to_pydatetime() except IndexError: - logger.info( - "No new sessions available for installation %r, node %r, sensor type %r.", - installation_reference, - node_id, - sensor_type_reference, - ) + self._log_no_sessions(installation_reference, node_id, sensor_type_reference, "existing") continue - sensor_data_df, _ = self.get_sensor_data( - installation_reference=installation_reference, - node_id=node_id, - sensor_type_reference=sensor_type_reference, - start=latest_session_finish_datetime, - finish=datetime.datetime.now(), - ) + if sensor_type_reference == "connection_statistics": + sensor_data_df = self.get_aggregated_connection_statistics( + installation_reference=installation_reference, + node_id=node_id, + start=latest_session_finish_datetime, + finish=datetime.datetime.now(), + ) - if sensor_data_df.empty: - logger.info( - "No new sessions available for installation %r, node %r, sensor type %r.", - installation_reference, - node_id, - sensor_type_reference, + else: + sensor_data_df, _ = self.get_sensor_data( + installation_reference=installation_reference, + node_id=node_id, + sensor_type_reference=sensor_type_reference, + start=latest_session_finish_datetime, + finish=datetime.datetime.now(), ) + + if sensor_data_df.empty: + self._log_no_sessions(installation_reference, node_id, sensor_type_reference) continue sensor_data_df = remove_metadata_columns_and_set_datetime_index(sensor_data_df) @@ -512,6 +510,10 @@ def extract_and_add_new_measurement_sessions(self, sensors=None): sensor_type=sensor_type_reference, ).extract_measurement_sessions() + if measurement_sessions.empty: + self._log_no_sessions(installation_reference, node_id, sensor_type_reference) + continue + # Add columns needed for sessions table. measurement_sessions["installation_reference"] = installation_reference measurement_sessions["node_id"] = node_id @@ -531,9 +533,16 @@ def extract_and_add_new_measurement_sessions(self, sensors=None): # Add new sessions to sessions table. self.client.load_table_from_dataframe( dataframe=measurement_sessions, - destination=table_name, + destination=sessions_table_name, ).result() + logger.info( + "New sessions added for installation %r, node %r, sensor type %r.", + installation_reference, + node_id, + sensor_type_reference, + ) + def query(self, query_string): """Query the dataset with an arbitrary query. @@ -556,3 +565,20 @@ def _get_time_period(self, start=None, finish=None): finish = finish or dt.datetime.now() start = start or finish - dt.timedelta(days=1) return start, finish + + def _log_no_sessions(self, installation_reference, node_id, sensor_type_reference, session_type="new"): + """Log that none of the given session type are available. + + :param str installation_reference: + :param str node_id: + :param str sensor_type_reference: + :param str session_type: + :return None: + """ + logger.info( + "No %s sessions available for installation %r, node %r, sensor type %r.", + session_type, + installation_reference, + node_id, + sensor_type_reference, + ) diff --git a/pyproject.toml b/pyproject.toml index f758f29..a9a78d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [tool.poetry] name = "aerosense-tools" -version = "0.10.0" +version = "0.10.1" description = "Functions for working with aerosense data, useful in building dashboards, analysis notebooks and digital twin services" authors = ["Tom Clark", "Marcus Lugg", "Yuriy Marykovsky"] license = "BSD-3"