diff --git a/analytics.py b/analytics.py index 93da7af..2de43a3 100644 --- a/analytics.py +++ b/analytics.py @@ -36,6 +36,25 @@ class Database(metaclass=SingletonMeta): port=self.port) # Enable autocommit so we don't have to call commit() after every transaction. self.connection.autocommit = True + + self.init_log_db() + + # Function to initialize the database. + def init_log_db(self): + with self.connection.cursor() as cursor: + cursor.execute(''' + CREATE TABLE IF NOT EXISTS file_access_log ( + id SERIAL PRIMARY KEY, + timestamp TIMESTAMP, + rel_path TEXT, + filesize BIGINT, + mime TEXT, + ip_address TEXT, + user_agent TEXT, + device_id TEXT, + cached BOOLEAN + ) + ''') # Create a global database instance. log_db = Database() @@ -64,23 +83,6 @@ def get_device_type(user_agent): else: return 'Other' -# Function to initialize the database. -def init_log_db(): - with log_db.connection.cursor() as cursor: - cursor.execute(''' - CREATE TABLE IF NOT EXISTS file_access_log ( - id SERIAL PRIMARY KEY, - timestamp TIMESTAMP, - rel_path TEXT, - filesize BIGINT, - mime TEXT, - ip_address TEXT, - user_agent TEXT, - device_id TEXT, - cached BOOLEAN - ) - ''') - # Logging function that uses the singleton connection. def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, cached): timestamp = datetime.now() # Use datetime object directly @@ -261,5 +263,3 @@ def dashboard(): unique_user=unique_user, timeframe_data=timeframe_data) -if __name__ == '__main__': - init_log_db() diff --git a/docker-compose.yml b/docker-compose.yml index 828364d..4becdc4 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ services: flask-app: image: python:3.11-slim - container_name: "${CONTAINER_NAME}.web" + container_name: ${CONTAINER_NAME} restart: always working_dir: /app volumes: @@ -19,7 +19,7 @@ services: - FLASK_ENV=production - TITLE_SHORT=${TITLE_SHORT} - TITLE_LONG=${TITLE_LONG} - - DB_HOST=postgres + - DB_HOST=${CONTAINER_NAME}.sql - DB_PORT=5432 - DB_USER=${POSTGRES_USER} - DB_PASSWORD=${POSTGRES_PASSWORD} @@ -54,7 +54,7 @@ services: postgres: image: postgres:15 - container_name: "${CONTAINER_NAME}.postgres" + container_name: "${CONTAINER_NAME}.sql" restart: always environment: POSTGRES_USER: ${POSTGRES_USER}