Skip to content

Commit

Permalink
R code for pulling user info from controller and record info from ind…
Browse files Browse the repository at this point in the history
…icia. Expanded config.
  • Loading branch information
simonrolph committed Sep 23, 2024
1 parent 373f0c5 commit c8777db
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
46 changes: 46 additions & 0 deletions R/get_records_from_indicia.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
get_data_helper <- function(base_url,auth_header,query){
h <- new_handle()

#add the authentication header
handle_setheaders(h,
"Content-Type" = "application/json",
"Authorization" = auth_header
)

# add the query
handle_setopt(h,postfields = query)

req <- curl_fetch_memory(url = base_url,handle = h)
fromJSON(rawToChar(req$content))
}



get_user_records_from_indicia <- function(base_url,client_id,shared_secret,user_warehouse_id,n_records){
#create the authentication header
auth_header <- paste('USER', client_id, 'SECRET', shared_secret, sep = ':')

#generate the elasticsearchquery
query <- paste0('{"size": "',n_records,'","query":{"bool":{"must":[{"term":{"metadata.created_by_id":"',user_warehouse_id,'"}}]}},"sort":[{"metadata.created_on" : {"order" : "desc"}}]}')

#make the request
records <- get_data_helper(base_url = base_url, auth_header = auth_header,query = query)
records
}



#test example
if(F){
library(curl)
config <- config::get()

# Get subscribers and print the data frame
data_df <- get_user_records_from_indicia(base_url = config$indicia_warehouse_base_url,
client_id = config$indicia_warehouse_client_id,
shared_secret = config$indicia_warehouse_secret,
user_warehouse_id = 1,
n_records = 1)
print(data_df)
}

2 changes: 2 additions & 0 deletions R/get_subscribers_from_controller.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ get_subscribers_from_controller <- function(api_url, email_list_id, api_token) {

# Convert the list of subscribers to a data frame
subscribers_df <- as.data.frame(data$subscribers)
subscribers_df <- subscribers_df[, c(2,4,1)]
names(subscribers_df)[1] <- "user_id"

return(subscribers_df)
}
Expand Down
54 changes: 54 additions & 0 deletions R/get_users_and_records.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(httr)
library(jsonlite)
library(config)
library(curl)

config <- config::get()

source("R/get_records_from_indicia.R")
source("R/get_subscribers_from_controller.R")

email_list_id <- "1" # Replace with your email list ID

# Get subscribers and print the data frame
subscribers_df <- get_subscribers_from_controller(api_url = config$controller_app_base_url,
email_list_id,
api_token = config$controller_app_api_key)
subscribers_df

#save the data
write.csv(subscribers_df,config$participant_data_file,row.names = F)



#loop through users

records_data <- data.frame()#create an empty data frame
for (i in 1:nrow(subscribers_df)){
data_out <- get_user_records_from_indicia(base_url = config$indicia_warehouse_base_url,
client_id = config$indicia_warehouse_client_id,
shared_secret = config$indicia_warehouse_secret,
user_warehouse_id = subscribers_df$user_id[i],
n_records = 20)

if (data_out$hits$total$value >0){
#any intermediate data processing
latlong <- data_out$hits$hits$`_source`$location$point

#extract all the columns you need
user_records <- data.frame(latitude = strsplit(latlong,",")[[1]][1],
longitude = strsplit(latlong,",")[[1]][2],
species = data_out$hits$hits$`_source`$taxon$taxon_name,
species_vernacular = data_out$hits$hits$`_source`$taxon$vernacular_name,
date = data_out$hits$hits$`_source`$event$date_start,
user_id = data_out$hits$hits$`_source`$metadata$created_by_id
)

records_data <- rbind(records_data,user_records)
}
}

records_data

#save the data
write.csv(subscribers_df,config$data_file,row.names = F)
5 changes: 5 additions & 0 deletions R/send_email.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
send_email <- function(){



}
6 changes: 5 additions & 1 deletion config_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ default:
template_html_file: "templates_html/basic_template.html"
pandoc_path: "C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools"
controller_app_base_url: ""
controller_app_api_key: ""
controller_app_api_key: ""
indicia_warehouse_base_url: ""
indicia_warehouse_client_id: ""
indicia_warehouse_secret: ""

0 comments on commit c8777db

Please sign in to comment.