Skip to content

Commit f3a9491

Browse files
authored
Merge pull request #156 from inbo/live-test
[WIP] Prepare for minor release; `get_archival_data_uuid()`, `get_acoustic_deployment_logs()`, increased db connection sanitation.
2 parents 79b3b73 + 4f7ef0d commit f3a9491

34 files changed

Lines changed: 649 additions & 359 deletions

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: etnservice
22
Title: Serve Data from the European Tracking Network
3-
Version: 0.6.0.9000
3+
Version: 0.6.0.9005
44
Authors@R: c(
55
person("Pieter", "Huybrechts", , "pieter.huybrechts@inbo.be", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-6658-6062")),
@@ -31,12 +31,13 @@ Imports:
3131
readr,
3232
rlang,
3333
stringr,
34-
utils
34+
utils,
35+
withr
3536
Suggests:
3637
jsonlite,
3738
purrr,
3839
testthat (>= 3.0.0)
3940
Config/testthat/edition: 3
4041
Encoding: UTF-8
4142
Roxygen: list(markdown = TRUE)
42-
RoxygenNote: 7.3.3
43+
Config/roxygen2/version: 8.0.0

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(connect_to_etn)
4+
export(get_acoustic_deployment_logs)
45
export(get_acoustic_deployments)
56
export(get_acoustic_detections)
67
export(get_acoustic_detections_page)
78
export(get_acoustic_projects)
89
export(get_acoustic_receivers)
910
export(get_animal_projects)
1011
export(get_animals)
12+
export(get_archival_data_uuid)
1113
export(get_cpod_projects)
12-
export(get_receiver_logs)
1314
export(get_tags)
1415
export(get_version)
1516
export(list_acoustic_project_codes)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# etnservice (development version)
2+
- Added `get_archival_data_uuid()` which fetches a table with UUID references to archival data files. These UUIDs can be used to construct paths to download the archival data CSV files hosted on lifewatch.com (#126).
23
- The function `write_dwc()` has been updated in the `etn` package and is no longer needed in `etnservice`. (#141)
34
- The `jsonlite` package is no longer a direct dependency, it is only used by a maintenance script. (#144)
45
- etnservice now relies on R >= 4.1.0 and uses base pipes (`|>` rather than `%>%`) (#151).
6+
- The function `get_receiver_logs()` has been renamed to `get_acoustic_deployment_logs()` during the review process of the respective etn client function. `get_acoustic_deployment_logs()` also supports less arguments but has a greatly simplified user interface. (#160)
57
# etnservice 0.6.0
68
- Added `get_receiver_logs()` to fetch receiver logs from the database. The data is returned as a tibble with a column with logs in JSON format. (#117)
79

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
#' Retrieve log files/diagnostic information for acoustic receivers.
1+
#' Retrieve log files/diagnostic information for deployments of acoustic
2+
#' receivers.
3+
#'
4+
#' This function retrieves log files and diagnostic information for deployments
5+
#' of acoustic receivers from the ETN database. The returned data includes
6+
#' deployment ID, receiver ID, station name, datetime, record type, and log data
7+
#' in JSON format.
28
#'
39
#' @inheritParams get_acoustic_detections
410
#' @inheritParams get_acoustic_deployments
@@ -9,24 +15,27 @@
915
#' @export
1016
#'
1117
#' @examples
12-
#' get_receiver_logs(deployment_id = 6028,
13-
#' start_date = "2020",
14-
#' end_date = "2020-02-01")
15-
get_receiver_logs <- function(credentials = list(
18+
#' get_acoustic_deployment_logs(deployment_id = 6028)
19+
#'
20+
#' get_acoustic_deployment_logs(deployment_id = c(53790, 1758), limit = TRUE)
21+
get_acoustic_deployment_logs <- function(credentials = list(
1622
username = Sys.getenv("ETN_USER"),
1723
password = Sys.getenv("ETN_PWD")),
1824
deployment_id,
19-
receiver_id = NULL,
20-
station_name = NULL,
21-
start_date = NULL,
22-
end_date = NULL,
2325
limit = FALSE) {
2426
# Check if credentials object has right shape
2527
check_credentials(credentials)
2628

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

32+
# Ensure the connection is closed when the function exits, even when it fails.
33+
withr::defer(
34+
if (DBI::dbIsValid(connection)) {
35+
DBI::dbDisconnect(connection)
36+
}
37+
)
38+
3039
# Check connection
3140
check_connection(connection)
3241

@@ -49,52 +58,6 @@ get_receiver_logs <- function(credentials = list(
4958
)
5059
}
5160

52-
# Check start_date
53-
if (is.null(start_date)) {
54-
start_date_query <- "True"
55-
} else {
56-
start_date <- check_date_time(start_date, "start_date")
57-
start_date_query <- glue::glue_sql("log.datetime >= {start_date}", .con = connection)
58-
}
59-
60-
# Check end_date
61-
if (is.null(end_date)) {
62-
end_date_query <- "True"
63-
} else {
64-
end_date <- check_date_time(end_date, "end_date")
65-
end_date_query <- glue::glue_sql("log.datetime < {end_date}", .con = connection)
66-
}
67-
68-
# Check receiver_id
69-
if (is.null(receiver_id)) {
70-
receiver_id_query <- "True"
71-
} else {
72-
receiver_id <- check_value(
73-
receiver_id,
74-
list_receiver_ids(credentials),
75-
name = "receiver_id"
76-
)
77-
receiver_id_query <- glue::glue_sql(
78-
"receiver.receiver IN ({receiver_id*})",
79-
.con = connection
80-
)
81-
}
82-
83-
# Check station_name
84-
if (is.null(station_name)) {
85-
station_name_query <- "True"
86-
} else {
87-
station_name <- check_value(
88-
station_name,
89-
list_station_names(credentials),
90-
"station_name"
91-
)
92-
station_name_query <- glue::glue_sql(
93-
"dep.station_name IN ({station_name*})",
94-
.con = connection
95-
)
96-
}
97-
9861
# Check limit
9962
assertthat::assert_that(is.logical(limit),
10063
msg = "limit must be a logical: TRUE/FALSE.")
@@ -121,27 +84,23 @@ get_receiver_logs <- function(credentials = list(
12184
LEFT JOIN acoustic.receivers AS receiver
12285
ON dep.receiver_fk = receiver.id_pk
12386
WHERE
124-
{start_date_query}
125-
AND {end_date_query}
126-
AND {deployment_id_query}
127-
AND {receiver_id_query}
128-
AND {station_name_query}
87+
{deployment_id_query}
12988
{limit_query}",
13089
.con = connection,
13190
.null = "NULL"
13291
)
13392

13493
## Query database
135-
receiver_logs <- DBI::dbGetQuery(connection, query)
94+
deployment_logs <- DBI::dbGetQuery(connection, query)
13695
# Close connection
13796
DBI::dbDisconnect(connection)
13897

13998
# Sort data
140-
receiver_logs <-
141-
receiver_logs |>
99+
deployment_logs <-
100+
deployment_logs |>
142101
dplyr::arrange(factor(
143102
.data$deployment_id, levels = list_deployment_ids(credentials)
144103
))
145104

146-
dplyr::as_tibble(receiver_logs)
105+
dplyr::as_tibble(deployment_logs)
147106
}

R/get_acoustic_deployments.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ get_acoustic_deployments <- function(
5959
connection <-
6060
connect_to_etn(credentials$username, credentials$password)
6161

62+
# Ensure the connection is closed when the function exits, even when it fails.
63+
withr::defer(
64+
if (DBI::dbIsValid(connection)) {
65+
DBI::dbDisconnect(connection)
66+
}
67+
)
68+
6269
# Check if we can make a connection
6370
check_connection(connection)
6471

R/get_acoustic_detections.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ get_acoustic_detections <- function(credentials = list(
9696
# Create connection object
9797
connection <- connect_to_etn(credentials$username, credentials$password)
9898

99+
# Ensure the connection is closed when the function exits, even when it fails.
100+
withr::defer(
101+
if (DBI::dbIsValid(connection)) {
102+
DBI::dbDisconnect(connection)
103+
}
104+
)
105+
99106
# Check if we can make a connection
100107
check_connection(connection)
101108

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

278+
# Close connection
279+
DBI::dbDisconnect(connection)
280+
271281
# Sort data (faster than in SQL)
272282
detections <-
273283
detections |>
274284
dplyr::arrange(
275285
factor(.data$acoustic_tag_id, levels = list_acoustic_tag_ids(credentials)),
276286
.data$date_time
277287
)
278-
# Close connection
279-
DBI::dbDisconnect(connection)
280288

281289
# Return detections
282290
dplyr::as_tibble(detections)

R/get_acoustic_detections_page.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ get_acoustic_detections_page <- function(credentials = list(
6060
# Create connection object
6161
connection <- connect_to_etn(credentials$username, credentials$password)
6262

63+
# Ensure the connection is closed when the function exits, even when it fails.
64+
withr::defer(
65+
if (DBI::dbIsValid(connection)) {
66+
DBI::dbDisconnect(connection)
67+
}
68+
)
69+
6370
# Check if we can make a connection
6471
check_connection(connection)
6572

@@ -251,8 +258,7 @@ get_acoustic_detections_page <- function(credentials = list(
251258
AND det.detection_id_pk > {next_id_pk}
252259
{limit_query}
253260
",
254-
.con = connection,
255-
page_size_query = ifelse(count, "ALL", page_size)
261+
.con = connection
256262
)
257263

258264
# Execute query -----

R/get_acoustic_projects.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ get_acoustic_projects <- function(credentials = list(
3737
connection <-
3838
connect_to_etn(credentials$username, credentials$password)
3939

40+
# Ensure the connection is closed when the function exits, even when it fails.
41+
withr::defer(
42+
if (DBI::dbIsValid(connection)) {
43+
DBI::dbDisconnect(connection)
44+
}
45+
)
46+
4047
# Check connection
4148
check_connection(connection)
4249

R/get_acoustic_receivers.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ get_acoustic_receivers <- function(credentials = list(
3737
status = NULL) {
3838
# Create connection object
3939
connection <- connect_to_etn(credentials$username, credentials$password)
40-
40+
41+
# Ensure the connection is closed when the function exits, even when it fails.
42+
withr::defer(
43+
if (DBI::dbIsValid(connection)) {
44+
DBI::dbDisconnect(connection)
45+
}
46+
)
4147
# Check connection
4248
check_connection(connection)
4349

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

131+
# Close connection
132+
DBI::dbDisconnect(connection)
133+
125134
# Sort data
126135
receivers <-
127136
receivers |>
128137
dplyr::arrange(.data$receiver_id)
129138

130-
# Close connection
131-
DBI::dbDisconnect(connection)
132-
133139
# Return receivers
134140
dplyr::as_tibble(receivers)
135141
}

R/get_animal_projects.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ get_animal_projects <- function(credentials = list(
3535

3636
# Create connection object
3737
connection <- connect_to_etn(credentials$username, credentials$password)
38-
38+
39+
# Ensure the connection is closed when the function exits, even when it fails.
40+
withr::defer(
41+
if (DBI::dbIsValid(connection)) {
42+
DBI::dbDisconnect(connection)
43+
}
44+
)
45+
3946
# Check connection
4047
check_connection(connection)
4148

0 commit comments

Comments
 (0)