-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_archival_data_uuid.R
More file actions
101 lines (92 loc) · 3.01 KB
/
Copy pathget_archival_data_uuid.R
File metadata and controls
101 lines (92 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#' Fetch a table with UUID references to archival data files
#'
#' Archival data is stored in csv files hosted on lifewatch.com. To fetch these
#' we must first query ETN for uuids that are needed to build the paths to read
#' these csv files.
#'
#' @inheritParams get_animals
#'
#' @returns A tibble with a column `converted_archival_file_uuid` that contains
#' the UUID pointing to the archival data csv file. Other columns are:
#' `animal_id`, `tag_serial_number`, and `animal_project_code`.
#' @export
#'
#' @examples
#' get_archival_data_uuid(tag_serial_number = "22035610")
get_archival_data_uuid <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
),
tag_serial_number = NULL,
animal_project_code = NULL,
animal_id = NULL) {
# 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
check_connection(connection)
# Check tag_serial_number
if (is.null(tag_serial_number)) {
tag_serial_number_query <- "True"
} else {
tag_serial_number <- check_value(
as.character(tag_serial_number), # Cast to character
list_tag_serial_numbers(credentials),
name = "tag_serial_number"
)
tag_serial_number_query <- glue::glue_sql(
"af.tag_serial_number IN ({tag_serial_number*})",
.con = connection
)
}
# Check animal_project_code
if (is.null(animal_project_code)) {
animal_project_code_query <- "True"
} else {
animal_project_code <- check_value(
as.character(animal_project_code), # Cast to character
list_animal_project_codes(credentials),
name = "animal_project_code",
lowercase = TRUE
)
animal_project_code_query <- glue::glue_sql(
"LOWER(af.animal_project_code) IN ({animal_project_code*})",
.con = connection
)
}
# Check animal_id
if (is.null(animal_id)) {
animal_id_query <- "True"
} else {
animal_id <- check_value(
as.character(animal_id), # Cast to character
list_animal_ids(credentials),
name = "animal_id"
)
animal_id_query <- glue::glue_sql(
"af.animal_id IN ({animal_id*})",
.con = connection
)
}
# Build Query
query <- glue::glue_sql("
SELECT *
FROM digital_twin.archival_files AS af
WHERE
{tag_serial_number_query}
AND {animal_project_code_query}
AND {animal_id_query}
", .con = connection)
uuid_tbl <- DBI::dbGetQuery(connection, query)
# Close connection
DBI::dbDisconnect(connection)
# Return uuids
dplyr::as_tibble(uuid_tbl)
}