use only 1 database connection :0
This commit is contained in:
parent
645342ba52
commit
9e1807830d
1 changed files with 43 additions and 83 deletions
126
src/main.v
126
src/main.v
|
@ -64,24 +64,16 @@ pub fn create_db_connection() !sqlite.DB {
|
|||
return sqlite.connect('pancakes.db')!
|
||||
}
|
||||
|
||||
fn get_people() ![]Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
fn (mut app App) get_people() ![]Person {
|
||||
status_filter := int(Status.afgegeven)
|
||||
people := sql db {
|
||||
people := sql app.db {
|
||||
select from Person where status < status_filter
|
||||
}!
|
||||
return people
|
||||
}
|
||||
|
||||
fn get_finished_count() !int {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) get_finished_count() !int {
|
||||
people := sql app.db {
|
||||
select from Person where status == 3
|
||||
}!
|
||||
return people.len
|
||||
|
@ -95,32 +87,20 @@ struct PerHour {
|
|||
color string
|
||||
}
|
||||
|
||||
fn get_all() ![]Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
return sql db {
|
||||
fn (mut app App) get_all() ![]Person {
|
||||
return sql app.db {
|
||||
select from Person order by id desc
|
||||
}!
|
||||
}
|
||||
|
||||
fn get_last_delivered() ![]Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
return sql db {
|
||||
fn (mut app App) get_last_delivered() ![]Person {
|
||||
return sql app.db {
|
||||
select from Person order by delivery_time desc limit 1
|
||||
}!
|
||||
}
|
||||
|
||||
fn get_finished_per_hour() ![]PerHour {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) get_finished_per_hour() ![]PerHour {
|
||||
people := sql app.db {
|
||||
select from Person where status == 3
|
||||
}!
|
||||
|
||||
|
@ -143,12 +123,8 @@ fn get_finished_per_hour() ![]PerHour {
|
|||
return grouped_arr
|
||||
}
|
||||
|
||||
fn get_ppu() !f64 {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
mut people := sql db {
|
||||
fn (mut app App) get_ppu() !f64 {
|
||||
mut people := sql app.db {
|
||||
select from Person where status == 3
|
||||
}!
|
||||
if people.len == 0 {
|
||||
|
@ -159,68 +135,48 @@ fn get_ppu() !f64 {
|
|||
return people.len / time_range.hours()
|
||||
}
|
||||
|
||||
fn get_mean_time_between_pannenkoeken() !time.Duration {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
fn (mut app App) get_mean_time_between_pannenkoeken() !time.Duration {
|
||||
time_zero := time.Time{
|
||||
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
|
||||
}!
|
||||
return stats.mean(arrays.window(people, size: 2).map(it[0].delivery_time - it[1].delivery_time))
|
||||
}
|
||||
|
||||
fn get_last_done_person() ![]Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) get_last_done_person() ![]Person {
|
||||
people := sql app.db {
|
||||
select from Person where status == 3 order by delivery_time desc limit 1
|
||||
}!
|
||||
return people
|
||||
}
|
||||
|
||||
fn get_next_person() ![]Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) get_next_person() ![]Person {
|
||||
people := sql app.db {
|
||||
select from Person where status < 3 order by id limit 1
|
||||
}!
|
||||
return people
|
||||
}
|
||||
|
||||
fn status_update(user_id int) !Person {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) do_status_update(user_id int) !Person {
|
||||
people := sql app.db {
|
||||
select from Person where id == user_id
|
||||
}!
|
||||
person := people.first()
|
||||
sql db {
|
||||
sql app.db {
|
||||
update Person set status = person.status + 1 where id == person.id
|
||||
}!
|
||||
if person.status == 2 {
|
||||
sql db {
|
||||
sql app.db {
|
||||
update Person set delivery_time = time.now() where id == person.id
|
||||
}!
|
||||
}
|
||||
return person
|
||||
}
|
||||
|
||||
fn add_person(name string, remark string) ! {
|
||||
mut db := create_db_connection()!
|
||||
defer {
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
people := sql db {
|
||||
fn (mut app App) do_add_person(name string, remark string) ! {
|
||||
people := sql app.db {
|
||||
select from Person where name == name && status < 3
|
||||
}!
|
||||
|
||||
|
@ -231,7 +187,7 @@ fn add_person(name string, remark string) ! {
|
|||
name: name
|
||||
remark: remark
|
||||
}
|
||||
sql db {
|
||||
sql app.db {
|
||||
insert p into Person
|
||||
}!
|
||||
}
|
||||
|
@ -241,9 +197,11 @@ fn add_person(name string, remark string) ! {
|
|||
|
||||
struct App {
|
||||
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}')
|
||||
}
|
||||
|
||||
|
@ -255,39 +213,41 @@ fn main() {
|
|||
create table Person
|
||||
} 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]
|
||||
pub fn (mut app App) home() vweb.Result {
|
||||
people := get_people() or {
|
||||
people := app.get_people() or {
|
||||
app.set_status(400, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
person_finished_count := get_finished_count() or {
|
||||
person_finished_count := app.get_finished_count() or {
|
||||
app.set_status(400, '')
|
||||
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, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
// pannenkoek per uur
|
||||
ppu := get_ppu() or {
|
||||
ppu := app.get_ppu() or {
|
||||
app.set_status(400, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
all_people := get_all() or {
|
||||
all_people := app.get_all() or {
|
||||
app.set_status(400, '')
|
||||
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, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
mut last_delivered := get_last_delivered() or {
|
||||
mut last_delivered := app.get_last_delivered() or {
|
||||
app.set_status(400, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
|
@ -305,11 +265,11 @@ pub fn (mut app App) home() vweb.Result {
|
|||
|
||||
@['/banner'; get]
|
||||
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, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
next_person := get_next_person() or {
|
||||
next_person := app.get_next_person() or {
|
||||
app.set_status(400, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
|
@ -320,7 +280,7 @@ pub fn (mut app App) banner() vweb.Result {
|
|||
|
||||
@['/status_update'; post]
|
||||
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 {
|
||||
spawn fn () {
|
||||
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('/')
|
||||
}
|
||||
|
||||
add_person(app.form['name'], app.form['remark']) or {
|
||||
app.do_add_person(app.form['name'], app.form['remark']) or {
|
||||
app.set_status(400, '')
|
||||
return app.text('${err}')
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue