move log module to herolib
This commit is contained in:
25
lib/core/log/backend_db.v
Normal file
25
lib/core/log/backend_db.v
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
10
lib/core/log/events.v
Normal file
10
lib/core/log/events.v
Normal file
@@ -0,0 +1,10 @@
|
||||
module log
|
||||
|
||||
import time
|
||||
|
||||
@[params]
|
||||
pub struct ViewEvent {
|
||||
pub mut:
|
||||
page string
|
||||
duration time.Duration
|
||||
}
|
||||
18
lib/core/log/factory.v
Normal file
18
lib/core/log/factory.v
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
55
lib/core/log/logger.v
Normal file
55
lib/core/log/logger.v
Normal file
@@ -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
|
||||
}
|
||||
32
lib/core/log/model.v
Normal file
32
lib/core/log/model.v
Normal file
@@ -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{}
|
||||
// }
|
||||
Reference in New Issue
Block a user