Your Name commited on
Commit
19f7ecc
·
1 Parent(s): 561710b

Swap connect_db() for pool — persistent connection management

Browse files
Files changed (1) hide show
  1. app.R +23 -23
app.R CHANGED
@@ -5,6 +5,7 @@ library(lubridate)
5
  library(plotly)
6
  library(DBI)
7
  library(RPostgres)
 
8
  library(scales)
9
  library(DT)
10
  library(shinyWidgets)
@@ -14,14 +15,15 @@ library(strucchange) # ITS breakpoint detection
14
  library(lmtest) # ITS coefficient testing (coeftest, waldtest)
15
 
16
  # --- DATABASE CONNECTION ---
17
- connect_db <- function() {
18
- dbConnect(RPostgres::Postgres(),
19
- dbname = Sys.getenv("DB_NAME", "sales_db"),
20
- host = Sys.getenv("DB_HOST", "localhost"),
21
- port = as.numeric(Sys.getenv("DB_PORT", "5433")),
22
- user = Sys.getenv("DB_USER", Sys.info()[["user"]]),
23
- password = Sys.getenv("DB_PASS", ""))
24
- }
 
25
 
26
  # ---------------------------------------------------------------------------
27
  # UI
@@ -370,15 +372,12 @@ server <- function(input, output, session) {
370
  get_data <- reactive({
371
  input$refresh
372
  data_trigger()
373
- con <- tryCatch(connect_db(), error = function(e) NULL)
374
- if (is.null(con)) return(data.frame())
375
- if (!dbExistsTable(con, "monthly_sales")) { dbDisconnect(con); return(data.frame()) }
376
- df <- dbGetQuery(con, "
377
  SELECT e.primary_name, s.*, EXTRACT(YEAR FROM s.report_date) AS year_num
378
  FROM monthly_sales s
379
  JOIN estimators e ON s.estimator_id = e.id
380
  ")
381
- dbDisconnect(con)
382
  df
383
  })
384
 
@@ -397,37 +396,38 @@ server <- function(input, output, session) {
397
  Estimator %in% c("SW", "SH") ~ "Scott Hutchings",
398
  TRUE ~ Estimator))
399
 
400
- con <- connect_db()
401
- dbExecute(con, "CREATE TABLE IF NOT EXISTS estimators (
402
  id SERIAL PRIMARY KEY, primary_name VARCHAR(100) UNIQUE NOT NULL,
403
  active BOOLEAN DEFAULT TRUE);")
404
- dbExecute(con, "CREATE TABLE IF NOT EXISTS monthly_sales (
405
  id SERIAL PRIMARY KEY, estimator_id INTEGER REFERENCES estimators(id),
406
  report_date DATE NOT NULL, amt_bid NUMERIC(15,2), qty_bid INTEGER,
407
  amt_booked NUMERIC(15,2), qty_booked INTEGER,
408
  UNIQUE(estimator_id, report_date));")
409
 
410
  for (nm in unique(clean$clean_name))
411
- tryCatch(dbExecute(con,
412
  "INSERT INTO estimators (primary_name) VALUES ($1) ON CONFLICT DO NOTHING",
413
  params = list(nm)), error = function(e) NULL)
414
 
415
- est_map <- dbGetQuery(con, "SELECT id, primary_name FROM estimators")
416
  upload <- clean %>%
417
  left_join(est_map, by = c("clean_name" = "primary_name")) %>%
418
  select(estimator_id = id, report_date,
419
  amt_bid = AmtBid, qty_bid = QtyBid,
420
  amt_booked = AmtBooked, qty_booked = QtyBooked)
421
 
422
- dbWriteTable(con, "staging_sales", upload, overwrite = TRUE, temporary = TRUE)
423
- n <- dbExecute(con, "
 
 
 
424
  INSERT INTO monthly_sales
425
  (estimator_id,report_date,amt_bid,qty_bid,amt_booked,qty_booked)
426
  SELECT estimator_id,report_date,amt_bid,qty_bid,amt_booked,qty_booked
427
  FROM staging_sales
428
  ON CONFLICT (estimator_id, report_date) DO NOTHING;")
429
- dbExecute(con, "DROP TABLE IF EXISTS staging_sales")
430
- dbDisconnect(con)
431
 
432
  showNotification(paste("Success! Added/Merged", n, "records."), type = "message")
433
  data_trigger(data_trigger() + 1)
@@ -1722,4 +1722,4 @@ server <- function(input, output, session) {
1722
 
1723
  }
1724
 
1725
- shinyApp(ui, server)
 
5
  library(plotly)
6
  library(DBI)
7
  library(RPostgres)
8
+ library(pool)
9
  library(scales)
10
  library(DT)
11
  library(shinyWidgets)
 
15
  library(lmtest) # ITS coefficient testing (coeftest, waldtest)
16
 
17
  # --- DATABASE CONNECTION ---
18
+ pool <- dbPool(
19
+ drv = RPostgres::Postgres(),
20
+ dbname = Sys.getenv("DB_NAME", "sales_db"),
21
+ host = Sys.getenv("DB_HOST", "localhost"),
22
+ port = as.numeric(Sys.getenv("DB_PORT", "5433")),
23
+ user = Sys.getenv("DB_USER", Sys.info()[["user"]]),
24
+ password = Sys.getenv("DB_PASS", "")
25
+ )
26
+ onStop(function() poolClose(pool))
27
 
28
  # ---------------------------------------------------------------------------
29
  # UI
 
372
  get_data <- reactive({
373
  input$refresh
374
  data_trigger()
375
+ if (!dbExistsTable(pool, "monthly_sales")) return(data.frame())
376
+ df <- dbGetQuery(pool, "
 
 
377
  SELECT e.primary_name, s.*, EXTRACT(YEAR FROM s.report_date) AS year_num
378
  FROM monthly_sales s
379
  JOIN estimators e ON s.estimator_id = e.id
380
  ")
 
381
  df
382
  })
383
 
 
396
  Estimator %in% c("SW", "SH") ~ "Scott Hutchings",
397
  TRUE ~ Estimator))
398
 
399
+ dbExecute(pool, "CREATE TABLE IF NOT EXISTS estimators (
 
400
  id SERIAL PRIMARY KEY, primary_name VARCHAR(100) UNIQUE NOT NULL,
401
  active BOOLEAN DEFAULT TRUE);")
402
+ dbExecute(pool, "CREATE TABLE IF NOT EXISTS monthly_sales (
403
  id SERIAL PRIMARY KEY, estimator_id INTEGER REFERENCES estimators(id),
404
  report_date DATE NOT NULL, amt_bid NUMERIC(15,2), qty_bid INTEGER,
405
  amt_booked NUMERIC(15,2), qty_booked INTEGER,
406
  UNIQUE(estimator_id, report_date));")
407
 
408
  for (nm in unique(clean$clean_name))
409
+ tryCatch(dbExecute(pool,
410
  "INSERT INTO estimators (primary_name) VALUES ($1) ON CONFLICT DO NOTHING",
411
  params = list(nm)), error = function(e) NULL)
412
 
413
+ est_map <- dbGetQuery(pool, "SELECT id, primary_name FROM estimators")
414
  upload <- clean %>%
415
  left_join(est_map, by = c("clean_name" = "primary_name")) %>%
416
  select(estimator_id = id, report_date,
417
  amt_bid = AmtBid, qty_bid = QtyBid,
418
  amt_booked = AmtBooked, qty_booked = QtyBooked)
419
 
420
+ # Use a temporary connection from the pool for dbWriteTable
421
+ conn <- poolCheckout(pool)
422
+ on.exit(poolReturn(conn), add = TRUE)
423
+ dbWriteTable(conn, "staging_sales", upload, overwrite = TRUE, temporary = TRUE)
424
+ n <- dbExecute(conn, "
425
  INSERT INTO monthly_sales
426
  (estimator_id,report_date,amt_bid,qty_bid,amt_booked,qty_booked)
427
  SELECT estimator_id,report_date,amt_bid,qty_bid,amt_booked,qty_booked
428
  FROM staging_sales
429
  ON CONFLICT (estimator_id, report_date) DO NOTHING;")
430
+ dbExecute(conn, "DROP TABLE IF EXISTS staging_sales")
 
431
 
432
  showNotification(paste("Success! Added/Merged", n, "records."), type = "message")
433
  data_trigger(data_trigger() + 1)
 
1722
 
1723
  }
1724
 
1725
+ shinyApp(ui, server)