Skip to content

Commit

Permalink
MRG: Merge pull request #31 from aerosense-ai/fix/fix-session-extraction
Browse files Browse the repository at this point in the history
Get connection statistics sessions correctly
  • Loading branch information
cortadocodes authored Jun 13, 2023
2 parents d354668 + eb35d59 commit 12f38a0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
72 changes: 49 additions & 23 deletions aerosense_tools/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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,
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 12f38a0

Please sign in to comment.