Fixed issues with db

This commit is contained in:
Tristan Ancelet 2023-05-09 10:08:02 -05:00
parent 78d49a9209
commit 51fa1944f5

View File

@ -1,8 +1,13 @@
CREATE TABLE IF NOT EXISTS log_levels ( CREATE TABLE IF NOT EXISTS log_levels (
log_level_id INTEGER PRIMARY KEY AUTOINCREMENT, log_level_id INTEGER PRIMARY KEY,
log_level_name VARCHAR(10) NOT NULL log_level_name VARCHAR(10) NOT NULL
); );
INSERT INTO log_levels (log_level_id, log_level_name) VALUES
(0, 'INFO'),
(1, 'CRITICAL'),
(2, 'WARNING');
CREATE TABLE IF NOT EXISTS logs ( CREATE TABLE IF NOT EXISTS logs (
log_id INTEGER PRIMARY KEY AUTOINCREMENT, log_id INTEGER PRIMARY KEY AUTOINCREMENT,
log_level_id INTEGER NOT NULL DEFAULT 0, log_level_id INTEGER NOT NULL DEFAULT 0,
@ -10,18 +15,17 @@ CREATE TABLE IF NOT EXISTS logs (
log_message TEXT NOT NULl, log_message TEXT NOT NULl,
log_entry_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, log_entry_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (log_level_id) REFERENCES log FOREIGN KEY (log_level_id) REFERENCES log_levels(log_level_id)
); );
CREATE VIEW log_statistics AS CREATE VIEW IF NOT EXISTS log_statistics AS
SELECT levels.log_level_name, COUNT(logs.log_id) FROM logs SELECT levels.log_level_name, COUNT(logs.log_id) FROM logs
JOIN log_levels levels ON log_levels.log_level_id = logs.log_level_id JOIN log_levels levels ON log_levels.log_level_id = logs.log_level_id
GROUP BY log_level_id;
CREATE VIEW log_statistics_last_7_days AS
SELECT log_levels.log_level_name, COUNT(logs.log_id) FROM logs
JOIN log_levels ON log_levels.log_level_id = (logs.log_level_id + 1)
WHERE logs.log_entry_timestamp > DATETIME(CURRENT_TIMESTAMP, '-7 day')
GROUP BY logs.log_level_id; GROUP BY logs.log_level_id;
CREATE VIEW IF NOT EXISTS log_statistics_last_7_days AS
SELECT log_levels.log_level_name, COUNT(logs.log_id) FROM logs
JOIN log_levels ON log_levels.log_level_id = logs.log_level_id
WHERE logs.log_entry_timestamp > DATETIME(CURRENT_TIMESTAMP, '-7 day')
GROUP BY logs.log_level_id;