diff --git a/analytics.py b/analytics.py index f580b6a..a876cb9 100644 --- a/analytics.py +++ b/analytics.py @@ -7,52 +7,41 @@ import psycopg2 file_access_temp = [] -# singleton metaclass. -class SingletonMeta(type): - _instances = {} - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - instance = super().__call__(*args, **kwargs) - cls._instances[cls] = instance - return cls._instances[cls] +dbname = os.environ.get('DB_NAME') +user = os.environ.get('DB_USER') +password = os.environ.get('DB_PASSWORD') +host = os.environ.get('DB_HOST') +port = int(os.environ.get('DB_PORT', 5432)) -# Database class that only handles the connection. -class Database(metaclass=SingletonMeta): - def __init__(self): - self.dbname = os.environ.get('DB_NAME') - self.user = os.environ.get('DB_USER') - self.password = os.environ.get('DB_PASSWORD') - self.host = os.environ.get('DB_HOST') - self.port = int(os.environ.get('DB_PORT', 5432)) - - self.connection = psycopg2.connect(dbname=self.dbname, - user=self.user, - password=self.password, - host=self.host, - port=self.port) - # Enable autocommit - self.connection.autocommit = True - - self.init_log_db() +connection = psycopg2.connect(dbname=dbname, + user=user, + password=password, + host=host, + port=port + ) +# Enable autocommit +connection.autocommit = True +log_db = connection + +# Function to initialize the database. +def init_log_db(): + with log_db.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 + ) + ''') + +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 - ) - ''') - -log_db = Database() def lookup_location(ip, reader): @@ -88,7 +77,7 @@ def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, INSERT INTO file_access_log (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ''', (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)) - file_access_temp.insert(0, [timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached]) + file_access_temp.insert(0, [timestamp.isoformat(), rel_path, filesize, mime, ip_address, user_agent, device_id, cached]) return timestamp.isoformat() def return_file_access(): diff --git a/app.py b/app.py index 4c2969a..113ba50 100755 --- a/app.py +++ b/app.py @@ -365,7 +365,7 @@ def query_recent_connections(): 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'), 'full_path': row[1], 'filesize' : row[2], - 'mime-typ' : row[3], + 'mime_typ' : row[3], 'ip_address': row[4], 'user_agent': row[5], 'cached': row[7] @@ -406,7 +406,7 @@ def handle_request_initial_data(): 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'), 'full_path': row[1], 'filesize' : row[2], - 'mime-typ' : row[3], + 'mime_typ' : row[3], 'ip_address': row[4], 'user_agent': row[5], 'cached': row[7] diff --git a/docker-compose.yml b/docker-compose.yml index d90563f..63a065c 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,7 +20,6 @@ services: - TITLE_SHORT=${TITLE_SHORT} - TITLE_LONG=${TITLE_LONG} - DB_HOST=postgres-db - - DB_PORT=5432 - DB_USER=${DB_USER} - DB_PASSWORD=${DB_PASSWORD} - DB_NAME=${DB_NAME} diff --git a/templates/connections.html b/templates/connections.html index 373eacb..a41dc15 100644 --- a/templates/connections.html +++ b/templates/connections.html @@ -75,7 +75,7 @@