Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Imports:
readr,
rlang,
stringr,
utils
utils,
withr
Suggests:
jsonlite,
purrr,
Expand Down
7 changes: 7 additions & 0 deletions R/get_acoustic_deployment_logs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ get_acoustic_deployment_logs <- function(credentials = list(
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down
7 changes: 7 additions & 0 deletions R/get_acoustic_deployments.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ get_acoustic_deployments <- function(
connection <-
connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check if we can make a connection
check_connection(connection)

Expand Down
12 changes: 10 additions & 2 deletions R/get_acoustic_detections.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ get_acoustic_detections <- function(credentials = list(
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check if we can make a connection
check_connection(connection)

Expand Down Expand Up @@ -268,15 +275,16 @@ get_acoustic_detections <- function(credentials = list(
", .con = connection)
detections <- DBI::dbGetQuery(connection, query)

# Close connection
DBI::dbDisconnect(connection)

# Sort data (faster than in SQL)
detections <-
detections |>
dplyr::arrange(
factor(.data$acoustic_tag_id, levels = list_acoustic_tag_ids(credentials)),
.data$date_time
)
# Close connection
DBI::dbDisconnect(connection)

# Return detections
dplyr::as_tibble(detections)
Expand Down
7 changes: 7 additions & 0 deletions R/get_acoustic_detections_page.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ get_acoustic_detections_page <- function(credentials = list(
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check if we can make a connection
check_connection(connection)

Expand Down
7 changes: 7 additions & 0 deletions R/get_acoustic_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ get_acoustic_projects <- function(credentials = list(
connection <-
connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down
14 changes: 10 additions & 4 deletions R/get_acoustic_receivers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ get_acoustic_receivers <- function(credentials = list(
status = NULL) {
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)
# Check connection
check_connection(connection)

Expand Down Expand Up @@ -122,14 +128,14 @@ get_acoustic_receivers <- function(credentials = list(
", .con = connection)
receivers <- DBI::dbGetQuery(connection, query)

# Close connection
DBI::dbDisconnect(connection)

# Sort data
receivers <-
receivers |>
dplyr::arrange(.data$receiver_id)

# Close connection
DBI::dbDisconnect(connection)

# Return receivers
dplyr::as_tibble(receivers)
}
9 changes: 8 additions & 1 deletion R/get_animal_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ get_animal_projects <- function(credentials = list(

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down
15 changes: 11 additions & 4 deletions R/get_animals.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ get_animals <- function(credentials = list(

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down Expand Up @@ -221,6 +228,9 @@ get_animals <- function(credentials = list(
", .con = connection)
animals <- DBI::dbGetQuery(connection, query)

# Close connection
DBI::dbDisconnect(connection)

# Collapse tag information, to obtain one row = one animal
tag_cols <-
animals |>
Expand All @@ -247,9 +257,6 @@ get_animals <- function(credentials = list(
factor(.data$tag_serial_number, levels = list_tag_serial_numbers(credentials))
)

# Close connection
DBI::dbDisconnect(connection)

# Return animals
dplyr::as_tibble(animals) # Is already a tibble, but added if code above changes
}
7 changes: 7 additions & 0 deletions R/get_archival_data_uuid.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ get_archival_data_uuid <- function(credentials = list(
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down
14 changes: 11 additions & 3 deletions R/get_cpod_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ get_cpod_projects <- function(credentials = list(

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down Expand Up @@ -72,12 +79,13 @@ get_cpod_projects <- function(credentials = list(
", .con = connection)
projects <- DBI::dbGetQuery(connection, query)

# Close connection
DBI::dbDisconnect(connection)

# Sort data
projects <-
projects |>
dplyr::arrange(.data$project_code)
# Close connection
DBI::dbDisconnect(connection)

# Return data
dplyr::as_tibble(projects)
Expand Down
7 changes: 7 additions & 0 deletions R/get_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ get_tags <- function(credentials = list(
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check connection
check_connection(connection)

Expand Down
13 changes: 13 additions & 0 deletions R/list_acoustic_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ list_acoustic_project_codes <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

check_connection(connection)

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
Expand Down
14 changes: 14 additions & 0 deletions R/list_acoustic_tag_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ list_acoustic_tag_ids <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

check_connection(connection)

acoustic_tag_id_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etnservice")),
.con = connection
Expand Down
15 changes: 12 additions & 3 deletions R/list_animal_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ list_animal_ids <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
stopifnot(is.list(credentials))
stopifnot(any(names(credentials) == c("username", "password")))

# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

check_connection(connection)
query <- glue::glue_sql(
"SELECT DISTINCT id_pk FROM common.animal_release",
.con = connection
Expand Down
13 changes: 12 additions & 1 deletion R/list_animal_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ list_animal_project_codes <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
connection <- connect_to_etn(credentials$username, credentials$password)
# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

project_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "project.sql", package = "etnservice")),
.con = connection
Expand Down
9 changes: 8 additions & 1 deletion R/list_cpod_project_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ list_cpod_project_codes <- function(credentials = list(

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

# Check if we can make a connection
check_connection(connection)

Expand Down
12 changes: 11 additions & 1 deletion R/list_deployment_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ list_deployment_ids <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)


# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

query <- glue::glue_sql(
"SELECT DISTINCT id_pk FROM acoustic.deployments",
.con = connection
Expand Down
12 changes: 12 additions & 0 deletions R/list_receiver_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ list_receiver_ids <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
)) {
# Check if credentials object has right shape
check_credentials(credentials)

# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)

# Ensure the connection is closed when the function exits, even when it fails.
withr::defer(
if (DBI::dbIsValid(connection)) {
DBI::dbDisconnect(connection)
}
)

query <- glue::glue_sql(
"SELECT DISTINCT receiver FROM acoustic.receivers",
.con = connection
Expand Down
Loading