-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_database.R
131 lines (113 loc) · 3.26 KB
/
populate_database.R
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
##########################################
#### this script pulls all the csvs ####
#### from the RMIS website, appends ####
#### to database. Ty Garber 4/13/2023 ####
#### if a sqlite database doesn't ####
#### exist it will create one. ####
##########################################
# libraries
library(RCurl)
library(purrr)
library(stringr)
library(DBI)
library(readr)
library(rvest)
library(dplyr)
# link to rmis ftp site
rmis_url <- "https://www.rmpc.org/pub/data/"
# fetch names, last updated times from website
filenames <- getURL(rmis_url)
# read html table into R
files <- filenames %>% read_html() %>% html_table()
files_df <- files[[1]] %>%
janitor::clean_names() %>%
select(name, last_modified) %>%
filter(
str_detect(tolower(name), '.csv'),
substr(tolower(name), 1,2 ) == tolower('RC') | # recoveries
tolower(name) == tolower('RL042_ALL_FULLSET.csv') | # releases
tolower(name) == tolower('LC042_ALL_FULLSET.csv') | # locations
substr(tolower(name), 1,2 ) == tolower('CS')
) %>%
rowwise() %>%
mutate(
file_id = uuid::UUIDgenerate(),
last_modified = as.character(as.Date(last_modified))
)
con <- dbConnect(RSQLite::SQLite(), "rmis.db")
# don't want to commit this to memory it's multiple gb
# populate the database
files_df %>%
pull(name) %>%
imap(~ {
if (tolower(.x) == tolower('LC042_ALL_FULLSET.csv')) {
dbWriteTable(
con,
'locations',
read_csv(paste0(rmis_url, .x), col_types = cols(.default = "c"))
,
overwrite = TRUE
)
} else if (str_detect(tolower(.x), tolower('RC'))) {
dbWriteTable(
con,
'recoveries',
read_csv(paste0(rmis_url, .x), col_types = cols(.default = "c")) %>%
mutate(file_id = files_df$file_id[.y])
,
append = TRUE
)
} else if (tolower(.x) == tolower('RL042_ALL_FULLSET.csv')){
dbWriteTable(
con,
'releases',
read_csv(paste0(rmis_url, .x), col_types = cols(.default = "c"))
,
overwrite = TRUE
)
} else if (str_detect(tolower(.x), tolower('CS'))) {
dbWriteTable(
con,
'catch_sample',
read_csv(paste0(rmis_url, .x), col_types = cols(.default = "c")) %>%
mutate(file_id = files_df$file_id[.y])
,
append = TRUE
)
}
}
)
# save a file log of updates to rmis csvs
dbWriteTable(
con,
'file_log',
files_df,
overwrite = TRUE
)
# set up indices on some tables - this will make
# queries much, much faster
rel_index <- DBI::dbSendStatement(con,
'
create index rel_tag
on releases(tag_code_or_release_id);
')
DBI::dbClearResult(rel_index)
rec_index <- DBI::dbSendStatement(con,
'
create index rec_tag
on recoveries(tag_code);
')
DBI::dbClearResult(rec_index)
loc_id_index <- DBI::dbSendStatement(con,
'
create index loc_location_id
on locations(location_code);
')
DBI::dbClearResult(loc_id_index)
loc_type_index <- DBI::dbSendStatement(con,
'
create index loc_location_type
on locations(location_type);
')
DBI::dbClearResult(loc_type_index)
DBI::dbDisconnect(con)