easy implementation

This commit is contained in:
lelo 2025-03-31 21:41:42 +00:00
parent 47e321134e
commit 4db37c49ff
4 changed files with 37 additions and 49 deletions

View File

@ -7,52 +7,41 @@ import psycopg2
file_access_temp = [] file_access_temp = []
# singleton metaclass. dbname = os.environ.get('DB_NAME')
class SingletonMeta(type): user = os.environ.get('DB_USER')
_instances = {} password = os.environ.get('DB_PASSWORD')
def __call__(cls, *args, **kwargs): host = os.environ.get('DB_HOST')
if cls not in cls._instances: port = int(os.environ.get('DB_PORT', 5432))
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]
# Database class that only handles the connection. connection = psycopg2.connect(dbname=dbname,
class Database(metaclass=SingletonMeta): user=user,
def __init__(self): password=password,
self.dbname = os.environ.get('DB_NAME') host=host,
self.user = os.environ.get('DB_USER') port=port
self.password = os.environ.get('DB_PASSWORD') )
self.host = os.environ.get('DB_HOST') # Enable autocommit
self.port = int(os.environ.get('DB_PORT', 5432)) connection.autocommit = True
log_db = connection
self.connection = psycopg2.connect(dbname=self.dbname, # Function to initialize the database.
user=self.user, def init_log_db():
password=self.password, with log_db.cursor() as cursor:
host=self.host, cursor.execute('''
port=self.port) CREATE TABLE IF NOT EXISTS file_access_log (
# Enable autocommit id SERIAL PRIMARY KEY,
self.connection.autocommit = True timestamp TIMESTAMP,
rel_path TEXT,
filesize BIGINT,
mime TEXT,
ip_address TEXT,
user_agent TEXT,
device_id TEXT,
cached BOOLEAN
)
''')
self.init_log_db() 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): 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) 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) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
''', (timestamp, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)) ''', (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() return timestamp.isoformat()
def return_file_access(): def return_file_access():

4
app.py
View File

@ -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'), 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'),
'full_path': row[1], 'full_path': row[1],
'filesize' : row[2], 'filesize' : row[2],
'mime-typ' : row[3], 'mime_typ' : row[3],
'ip_address': row[4], 'ip_address': row[4],
'user_agent': row[5], 'user_agent': row[5],
'cached': row[7] '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'), 'timestamp': datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime('%d.%m.%Y %H:%M:%S'),
'full_path': row[1], 'full_path': row[1],
'filesize' : row[2], 'filesize' : row[2],
'mime-typ' : row[3], 'mime_typ' : row[3],
'ip_address': row[4], 'ip_address': row[4],
'user_agent': row[5], 'user_agent': row[5],
'cached': row[7] 'cached': row[7]

View File

@ -20,7 +20,6 @@ services:
- TITLE_SHORT=${TITLE_SHORT} - TITLE_SHORT=${TITLE_SHORT}
- TITLE_LONG=${TITLE_LONG} - TITLE_LONG=${TITLE_LONG}
- DB_HOST=postgres-db - DB_HOST=postgres-db
- DB_PORT=5432
- DB_USER=${DB_USER} - DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD} - DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME} - DB_NAME=${DB_NAME}

View File

@ -75,7 +75,7 @@
<td>${record.user_agent}</td> <td>${record.user_agent}</td>
<td>${record.full_path}</td> <td>${record.full_path}</td>
<td>${record.filesize}</td> <td>${record.filesize}</td>
<td>${record.mime-typ}</td> <td>${record.mime_typ}</td>
<td>${record.cached}</td> <td>${record.cached}</td>
`; `;
tbody.appendChild(row); tbody.appendChild(row);