From cab7a47050138fab06e7cec8557d01aa8d515100 Mon Sep 17 00:00:00 2001 From: timurgordon Date: Wed, 22 Jan 2025 23:58:58 +0000 Subject: [PATCH] move log module to herolib --- lib/core/log/backend_db.v | 25 ++++++++++++++++++ lib/core/log/events.v | 10 +++++++ lib/core/log/factory.v | 18 +++++++++++++ lib/core/log/logger.v | 55 +++++++++++++++++++++++++++++++++++++++ lib/core/log/model.v | 32 +++++++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 lib/core/log/backend_db.v create mode 100644 lib/core/log/events.v create mode 100644 lib/core/log/factory.v create mode 100644 lib/core/log/logger.v create mode 100644 lib/core/log/model.v diff --git a/lib/core/log/backend_db.v b/lib/core/log/backend_db.v new file mode 100644 index 00000000..2417ae8d --- /dev/null +++ b/lib/core/log/backend_db.v @@ -0,0 +1,25 @@ +module log + +import db.sqlite + +pub struct DBBackend { +pub: + db sqlite.DB +} + +@[params] +pub struct DBBackendConfig { +pub: + db sqlite.DB +} + +// factory for +pub fn new_backend(config DBBackendConfig) !DBBackend { + sql config.db { + create table Log + } or { panic(err) } + + return DBBackend{ + db: config.db + } +} \ No newline at end of file diff --git a/lib/core/log/events.v b/lib/core/log/events.v new file mode 100644 index 00000000..555f35f0 --- /dev/null +++ b/lib/core/log/events.v @@ -0,0 +1,10 @@ +module log + +import time + +@[params] +pub struct ViewEvent { +pub mut: + page string + duration time.Duration +} \ No newline at end of file diff --git a/lib/core/log/factory.v b/lib/core/log/factory.v new file mode 100644 index 00000000..3547d0f2 --- /dev/null +++ b/lib/core/log/factory.v @@ -0,0 +1,18 @@ +module log + +import db.sqlite + +pub struct Logger { + db_path string + // DBBackend +} + +pub fn new(db_path string) !Logger { + db := sqlite.connect(db_path)! + sql db { + create table Log + } or { panic(err) } + return Logger{ + db_path: db_path + } +} diff --git a/lib/core/log/logger.v b/lib/core/log/logger.v new file mode 100644 index 00000000..a91e82c4 --- /dev/null +++ b/lib/core/log/logger.v @@ -0,0 +1,55 @@ +module log + +import db.sqlite + +pub fn (logger Logger) new_log(log Log) ! { + db := sqlite.connect(logger.db_path)! + + sql db { + insert log into Log + }! +} + +pub struct LogFilter { + Log + matches_all bool + limit int +} + +pub fn (logger Logger) filter_logs(filter LogFilter) ![]Log { + db := sqlite.connect(logger.db_path)! + mut select_stmt := 'select * from Log' + + mut matchers := []string{} + if filter.event != '' { + matchers << "event == '${filter.event}'" + } + + if filter.subject != '' { + matchers << "subject == '${filter.subject}'" + } + + if filter.object != '' { + matchers << "object == '${filter.object}'" + } + + if matchers.len > 0 { + matchers_str := if filter.matches_all { + matchers.join(' AND ') + } else { + matchers.join(' OR ') + } + select_stmt += ' where ${matchers_str}' + } + + responses := db.exec(select_stmt)! + + mut logs := []Log{} + for response in responses { + logs << sql db { + select from Log where id == response.vals[0].int() + }! + } + + return logs +} \ No newline at end of file diff --git a/lib/core/log/model.v b/lib/core/log/model.v new file mode 100644 index 00000000..852b2021 --- /dev/null +++ b/lib/core/log/model.v @@ -0,0 +1,32 @@ +module log + +import time + +pub struct Log { + id int @[primary; sql: serial] +pub: + timestamp time.Time +pub mut: + event string + subject string + object string + message string // a custom message that can be attached to a log +} + +// pub struct Event { +// name string +// description string +// } + +// // log_request logs http requests +// pub fn create_log(log Log) Log { +// return Log{ +// ...log +// timestamp: time.now() +// }) +// } + +// // log_request logs http requests +// pub fn (mut a Analyzer) get_logs(subject string) []Log { +// return []Log{} +// }