From e1ff87bf2bdeec3864fb0deeb20cd35463e1ed21 Mon Sep 17 00:00:00 2001 From: Chris Veilleux Date: Mon, 10 Jun 2019 16:19:26 -0500 Subject: [PATCH] add logic to update the device last_contact_ts whenever it makes and API call. --- shared/selene/api/blueprint.py | 18 ++++++++++++++++++ shared/selene/data/device/repository/device.py | 7 +++++++ .../repository/sql/update_last_contact_ts.sql | 6 ++++++ 3 files changed, 31 insertions(+) create mode 100644 shared/selene/data/device/repository/sql/update_last_contact_ts.sql diff --git a/shared/selene/api/blueprint.py b/shared/selene/api/blueprint.py index 1aa25bfb..c6616e30 100644 --- a/shared/selene/api/blueprint.py +++ b/shared/selene/api/blueprint.py @@ -5,6 +5,7 @@ from http import HTTPStatus from flask import current_app, Blueprint, g as global_context from schematics.exceptions import DataError +from selene.data.device import DeviceRepository from selene.data.metrics import ApiMetric, ApiMetricsRepository from selene.util.auth import AuthenticationError from selene.util.db import connect_to_db @@ -36,11 +37,13 @@ def setup_request(): @selene_api.after_app_request def teardown_request(response): add_api_metric(response.status_code) + update_device_last_contact() return response def add_api_metric(http_status): + """Add a row to the table tracking metrics for API calls""" api = None # We are not logging metrics for the public API until after the socket # implementation to avoid putting millions of rows a day on the table @@ -74,3 +77,18 @@ def add_api_metric(http_status): ) metric_repository = ApiMetricsRepository(global_context.db) metric_repository.add(api_metric) + + +def update_device_last_contact(): + """Update the timestamp on the device table indicating last contact. + + This should only be done on public API calls because we are tracking + device activity only. + """ + if 'public' in current_app.name and 'device_id' in global_context: + if 'db' not in global_context: + global_context.db = connect_to_db( + current_app.config['DB_CONNECTION_CONFIG'] + ) + device_repository = DeviceRepository(global_context.db) + device_repository.update_last_contact_ts(global_context.device_id) diff --git a/shared/selene/data/device/repository/device.py b/shared/selene/data/device/repository/device.py index de5bcff7..0bc20aad 100644 --- a/shared/selene/data/device/repository/device.py +++ b/shared/selene/data/device/repository/device.py @@ -170,3 +170,10 @@ class DeviceRepository(RepositoryBase): ) self.cursor.update(db_request) + + def update_last_contact_ts(self, device_id): + db_request = self._build_db_request( + sql_file_name='update_last_contact_ts.sql', + args=dict(device_id=device_id) + ) + self.cursor.update(db_request) diff --git a/shared/selene/data/device/repository/sql/update_last_contact_ts.sql b/shared/selene/data/device/repository/sql/update_last_contact_ts.sql new file mode 100644 index 00000000..aa9e4f8a --- /dev/null +++ b/shared/selene/data/device/repository/sql/update_last_contact_ts.sql @@ -0,0 +1,6 @@ +UPDATE + device.device +SET + last_contact_ts = now() +WHERE + id = %(device_id)s