-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
R code for pulling user info from controller and record info from ind…
…icia. Expanded config.
- Loading branch information
1 parent
373f0c5
commit c8777db
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
send_email <- function(){ | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters