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 = []
# 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))
connection = psycopg2.connect(dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
# Enable autocommit
connection.autocommit = True
log_db = connection
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
# 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
)
''')
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):
@ -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():

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'),
'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]

View File

@ -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}

View File

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