use only 1 database connection :0

This commit is contained in:
mcbloch 2023-12-07 21:53:44 +01:00
parent 645342ba52
commit 9e1807830d

View file

@ -64,24 +64,16 @@ pub fn create_db_connection() !sqlite.DB {
return sqlite.connect('pancakes.db')! return sqlite.connect('pancakes.db')!
} }
fn get_people() ![]Person { fn (mut app App) get_people() ![]Person {
mut db := create_db_connection()!
defer {
db.close() or { panic(err) }
}
status_filter := int(Status.afgegeven) status_filter := int(Status.afgegeven)
people := sql db { people := sql app.db {
select from Person where status < status_filter select from Person where status < status_filter
}! }!
return people return people
} }
fn get_finished_count() !int { fn (mut app App) get_finished_count() !int {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where status == 3 select from Person where status == 3
}! }!
return people.len return people.len
@ -95,32 +87,20 @@ struct PerHour {
color string color string
} }
fn get_all() ![]Person { fn (mut app App) get_all() ![]Person {
mut db := create_db_connection()! return sql app.db {
defer {
db.close() or { panic(err) }
}
return sql db {
select from Person order by id desc select from Person order by id desc
}! }!
} }
fn get_last_delivered() ![]Person { fn (mut app App) get_last_delivered() ![]Person {
mut db := create_db_connection()! return sql app.db {
defer {
db.close() or { panic(err) }
}
return sql db {
select from Person order by delivery_time desc limit 1 select from Person order by delivery_time desc limit 1
}! }!
} }
fn get_finished_per_hour() ![]PerHour { fn (mut app App) get_finished_per_hour() ![]PerHour {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where status == 3 select from Person where status == 3
}! }!
@ -143,12 +123,8 @@ fn get_finished_per_hour() ![]PerHour {
return grouped_arr return grouped_arr
} }
fn get_ppu() !f64 { fn (mut app App) get_ppu() !f64 {
mut db := create_db_connection()! mut people := sql app.db {
defer {
db.close() or { panic(err) }
}
mut people := sql db {
select from Person where status == 3 select from Person where status == 3
}! }!
if people.len == 0 { if people.len == 0 {
@ -159,68 +135,48 @@ fn get_ppu() !f64 {
return people.len / time_range.hours() return people.len / time_range.hours()
} }
fn get_mean_time_between_pannenkoeken() !time.Duration { fn (mut app App) get_mean_time_between_pannenkoeken() !time.Duration {
mut db := create_db_connection()!
defer {
db.close() or { panic(err) }
}
time_zero := time.Time{ time_zero := time.Time{
unix: 0 unix: 0
} }
mut people := sql db { mut people := sql app.db {
select from Person where (status == 3 && delivery_time > time_zero) order by delivery_time desc limit 10 select from Person where (status == 3 && delivery_time > time_zero) order by delivery_time desc limit 10
}! }!
return stats.mean(arrays.window(people, size: 2).map(it[0].delivery_time - it[1].delivery_time)) return stats.mean(arrays.window(people, size: 2).map(it[0].delivery_time - it[1].delivery_time))
} }
fn get_last_done_person() ![]Person { fn (mut app App) get_last_done_person() ![]Person {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where status == 3 order by delivery_time desc limit 1 select from Person where status == 3 order by delivery_time desc limit 1
}! }!
return people return people
} }
fn get_next_person() ![]Person { fn (mut app App) get_next_person() ![]Person {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where status < 3 order by id limit 1 select from Person where status < 3 order by id limit 1
}! }!
return people return people
} }
fn status_update(user_id int) !Person { fn (mut app App) do_status_update(user_id int) !Person {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where id == user_id select from Person where id == user_id
}! }!
person := people.first() person := people.first()
sql db { sql app.db {
update Person set status = person.status + 1 where id == person.id update Person set status = person.status + 1 where id == person.id
}! }!
if person.status == 2 { if person.status == 2 {
sql db { sql app.db {
update Person set delivery_time = time.now() where id == person.id update Person set delivery_time = time.now() where id == person.id
}! }!
} }
return person return person
} }
fn add_person(name string, remark string) ! { fn (mut app App) do_add_person(name string, remark string) ! {
mut db := create_db_connection()! people := sql app.db {
defer {
db.close() or { panic(err) }
}
people := sql db {
select from Person where name == name && status < 3 select from Person where name == name && status < 3
}! }!
@ -231,7 +187,7 @@ fn add_person(name string, remark string) ! {
name: name name: name
remark: remark remark: remark
} }
sql db { sql app.db {
insert p into Person insert p into Person
}! }!
} }
@ -241,9 +197,11 @@ fn add_person(name string, remark string) ! {
struct App { struct App {
vweb.Context vweb.Context
mut:
db sqlite.DB
} }
pub fn (app App) before_request() { pub fn (mut app App) before_request() {
println('[Vweb] ${app.Context.req.method} ${app.Context.req.url}') println('[Vweb] ${app.Context.req.method} ${app.Context.req.url}')
} }
@ -255,39 +213,41 @@ fn main() {
create table Person create table Person
} or { panic('error on create table: ${err}') } } or { panic('error on create table: ${err}') }
db.close() or { panic(err) } // db.close() or { panic(err) }
vweb.run(&App{}, http_port) vweb.run(&App{
db: db
}, http_port)
} }
@['/'; get] @['/'; get]
pub fn (mut app App) home() vweb.Result { pub fn (mut app App) home() vweb.Result {
people := get_people() or { people := app.get_people() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
person_finished_count := get_finished_count() or { person_finished_count := app.get_finished_count() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
finished_per_hour := get_finished_per_hour() or { finished_per_hour := app.get_finished_per_hour() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
// pannenkoek per uur // pannenkoek per uur
ppu := get_ppu() or { ppu := app.get_ppu() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
all_people := get_all() or { all_people := app.get_all() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
mean_time := get_mean_time_between_pannenkoeken() or { mean_time := app.get_mean_time_between_pannenkoeken() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
mut last_delivered := get_last_delivered() or { mut last_delivered := app.get_last_delivered() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
@ -305,11 +265,11 @@ pub fn (mut app App) home() vweb.Result {
@['/banner'; get] @['/banner'; get]
pub fn (mut app App) banner() vweb.Result { pub fn (mut app App) banner() vweb.Result {
last_done := get_last_done_person() or { last_done := app.get_last_done_person() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
next_person := get_next_person() or { next_person := app.get_next_person() or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }
@ -320,7 +280,7 @@ pub fn (mut app App) banner() vweb.Result {
@['/status_update'; post] @['/status_update'; post]
pub fn (mut app App) status_update() vweb.Result { pub fn (mut app App) status_update() vweb.Result {
if person := status_update(app.form['id'].int()) { if person := app.do_status_update(app.form['id'].int()) {
if person.status == 1 { if person.status == 1 {
spawn fn () { spawn fn () {
http.post('http://10.1.0.224:8080/blink', '') or {} http.post('http://10.1.0.224:8080/blink', '') or {}
@ -342,7 +302,7 @@ pub fn (mut app App) add_person() vweb.Result {
return app.redirect('/') return app.redirect('/')
} }
add_person(app.form['name'], app.form['remark']) or { app.do_add_person(app.form['name'], app.form['remark']) or {
app.set_status(400, '') app.set_status(400, '')
return app.text('${err}') return app.text('${err}')
} }