Skip to content

Commit

Permalink
Merge pull request #44 from laijasmine/main
Browse files Browse the repository at this point in the history
Save as we iterate through awards
  • Loading branch information
Jasmine authored May 19, 2021
2 parents 1172a9e + 0dd713c commit 96fdf31
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
.Rhistory
.RData
.Ruserdata

*.csv
*.DS_Store
60 changes: 39 additions & 21 deletions R/RT_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
#' 'send_one_month_remaining_correspondence'
#'
#' @param awards_db (data.frame) awards database
#' @param database_path (character) the path to save the database to
#'
#' @importFrom magrittr '%>%'
send_correspondences <- function(awards_db) {
send_correspondences <- function(awards_db, database_path) {
indices <- which(awards_db$active_award_flag == 'yes')
db <- awards_db[indices,]

db <- create_ticket_and_send_initial_correspondence(db) %>%
send_annual_report_correspondence() %>%
send_aon_correspondence() %>%
send_one_month_remaining_correspondence()
db <- create_ticket_and_send_initial_correspondence(db, database_path) %>%
send_annual_report_correspondence(., database_path) %>%
send_aon_correspondence(., database_path) %>%
send_one_month_remaining_correspondence(., database_path)

awards_db[indices,] <- db

#send summary of tickets sent
if(nrow(awards_db[indices,]) > 0) {
out <- sprintf('I sent %s tickets today', nrow(awards_db[indices,]))
slackr::slackr_bot(out)
}

return(awards_db)
}

Expand Down Expand Up @@ -55,16 +62,18 @@ create_ticket <- function(award, requestor) {
#' pi_email, pi_first_name, id (NSF award #), title (NSF award title).
#'
#' @param awards_db (data.frame) database of NSF awards pulled from NSF-API
#' @param database_path (character) the path to save the database to
#'
#' @return awards_db (data.frame) The initial database with updated RT ticket numbers
create_ticket_and_send_initial_correspondence <- function(awards_db) {
create_ticket_and_send_initial_correspondence <- function(awards_db, database_path) {
# Get awards without an initial correspondence
indices <- which(is.na(awards_db$contact_initial)) # save indices to re-merge
db <- awards_db[indices,]

for (i in seq_len(nrow(db))) {
# Create RT ticket
db$rt_ticket[i] <- create_ticket(db$id[i], db$pi_email[i])

if (db$rt_ticket[i] == 'rt_ticket_create_error') {
next
}
Expand All @@ -78,16 +87,18 @@ create_ticket_and_send_initial_correspondence <- function(awards_db) {
reply <- check_rt_reply(db$rt_ticket[i], email_text)

db$contact_initial[i] <- as.character(Sys.Date())

# re-merge temporary database into permanent
awards_db[i,] <- db[i,]
#save the result inbetween
utils::write.csv(db, file = database_path, row.names = FALSE)
}

# re-merge temporary database into permanent
awards_db[indices,] <- db

return(awards_db)
}


send_annual_report_correspondence <- function(awards_db) {
send_annual_report_correspondence <- function(awards_db, database_path) {
# Get awards to send annual report correspondence
current_date <- as.character(Sys.Date())
indices <- which(awards_db$contact_annual_report_next == current_date) # save indices to re-merge
Expand All @@ -103,19 +114,22 @@ send_annual_report_correspondence <- function(awards_db) {

# Update last contact date
db$contact_annual_report_previous[i] <- db$contact_annual_report_next[i]

# re-merge temporary database into permanent
awards_db[i,] <- db[i, ]
#save the result inbetween
utils::write.csv(db, file = database_path, row.names = FALSE)
}

# re-merge temporary database into permanent
awards_db[indices,] <- db

## TODO
# add function that updates annual report correspondence times


return(awards_db)
}


send_aon_correspondence <- function(awards_db){
send_aon_correspondence <- function(awards_db, database_path){
current_date <- as.character(Sys.Date())
indices <- which(awards_db$contact_aon_next == current_date)
db <- awards_db[indices,]
Expand All @@ -130,16 +144,18 @@ send_aon_correspondence <- function(awards_db){

# Update last contact date
db$contact_aon_previous[i] <- db$contact_aon_next[i]

# re-merge temporary database into permanent
awards_db[i,] <- db[i, ]
#save the result inbetween
utils::write.csv(db, file = database_path, row.names = FALSE)
}

# re-merge temporary database into permanent
awards_db[indices,] <- db

return(awards_db)
}


send_one_month_remaining_correspondence <- function(awards_db) {
send_one_month_remaining_correspondence <- function(awards_db, database_path) {
indices <- which(awards_db$contact_1mo == as.character(Sys.Date()))
db <- awards_db[indices,]

Expand All @@ -155,10 +171,12 @@ send_one_month_remaining_correspondence <- function(awards_db) {

# Update last contact date
db$contact_1mo[i] <- as.character(Sys.Date())

# re-merge temporary database into permanent
awards_db[i,] <- db[i, ]
#save the result inbetween
utils::write.csv(db, file = database_path, row.names = FALSE)
}

# re-merge temporary database into permanent
awards_db[indices,] <- db

return(awards_db)
}
Expand Down
4 changes: 1 addition & 3 deletions R/main.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
## TODO add a slack message that details every email sent per day?
#' main function
#'
#' @param database_path (character) Path to the database of awards and correspondences
Expand Down Expand Up @@ -33,11 +32,10 @@ main <- function(database_path = Sys.getenv('DATABASE_PATH'),
db <- update_contact_dates(db, annual_report_time, initial_aon_offset, aon_recurring_interval)

## Send correspondences
db <- send_correspondences(db)
send_correspondences(db, database_path)

## Save lastrun and database
save_lastrun(current_date, lastrun_path)
utils::write.csv(db, file = database_path, row.names = FALSE)

return(invisible())
}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Every 24 hours the bot queries NSF's award [API](https://www.research.gov/common
DATABASE_PATH # Path to the database of awards and correspondences
LASTRUN_PATH=LASTRUN # Determines where the bot stores its state
SLACK_WEBHOOK_URL="{URL}" # Your Slack webhook URL
SLACK_OAUTH_TOKEN="TOKEN" # Slack OAUTH token (see your bot admin for access)
RT_URL="https://example.com/rt" # The URL of your RT install
RT_USER="your_rt_user" # Your RT username
RT_PASS="your_rt_password" # Your RT password
Expand Down Expand Up @@ -58,12 +59,13 @@ Please follow this closely so no emails are sent to researchers by mistake while
1. Add the following lines to the `.Renviron` (can be accessed by `usethis::edit_r_environ()`)
```
SLACK_WEBHOOK_URL="{URL}" # Your Slack webhook URL
SLACK_OAUTH_TOKEN="TOKEN"
RT_URL="https://example.com/rt" # The URL of your RT install
RT_USER="your_rt_user" # Your RT username
RT_PASS="your_rt_password" # Your RT password
```

2. Set up slackbot: `slackr::slackr_setup(channel = "#awardbot",incoming_webhook_url = Sys.getenv("SLACK_WEBHOOK_URL"), cacheChannels = F)`
2. Set up slackbot: `slackr::slackr_setup(channel = "#awardbot", username = 'awardbot', incoming_webhook_url = Sys.getenv("SLACK_WEBHOOK_URL"), bot_user_oauth_token = Sys.getenv("SLACK_OAUTH_TOKEN"))`

3. Login to RT: `rt::rt_login()`

Expand Down
20 changes: 11 additions & 9 deletions inst/main_cron_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ library(slackr)
library(stringr)
library(XML)

## Set up environment variables
readRenviron(file.path(getwd(), '.Renviron'))
## Set up environment variables
readRenviron(file.path(getwd(), ".Renviron"))

## Source the bot
remotes::install_github('NCEAS/awards-bot')
remotes::install_github("NCEAS/awards-bot")
library(awardsBot)

# Log in to RT/SLACK
rt::rt_login(Sys.getenv('RT_USER'), Sys.getenv('RT_PASS'), Sys.getenv('RT_URL'))
# Log in to RT/SLACK
rt::rt_login(Sys.getenv("RT_USER"), Sys.getenv("RT_PASS"), Sys.getenv("RT_URL"))

slackr::slackr_setup(channel = "#awardbot", username = 'awardbot',
incoming_webhook_url = Sys.getenv("SLACK_WEBHOOK_URL"),
cacheChannels = F)
slackr::slackr_setup(
channel = "#awardbot", username = "awardbot",
incoming_webhook_url = Sys.getenv("SLACK_WEBHOOK_URL"),
bot_user_oauth_token = Sys.getenv("SLACK_OAUTH_TOKEN")
)

awardsBot::main()
awardsBot::main()
4 changes: 3 additions & 1 deletion man/create_ticket_and_send_initial_correspondence.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/send_correspondences.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions tests/testthat/test_RT_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ test_that('we can send an initial correspondence', {
}

db <- create_dummy_database()
db <- create_ticket_and_send_initial_correspondence(db)

db <- create_ticket_and_send_initial_correspondence(db, database_path = "inst/test_db.csv")

ticket <- rt::rt_ticket_properties(db$rt_ticket)
expect_equal(ticket$Requestors, '[email protected]')
Expand All @@ -36,9 +37,9 @@ test_that('we can send an annual report correspondence', {
}

db <- create_dummy_database()
db <- create_ticket_and_send_initial_correspondence(db)
db <- create_ticket_and_send_initial_correspondence(db, database_path = "inst/test_db.csv")
db$contact_annual_report_next <- as.character(Sys.Date())
db <- send_annual_report_correspondence(db)
db <- send_annual_report_correspondence(db, database_path = "inst/test_db.csv")

expect_equal(db$contact_annual_report_next, db$contact_annual_report_previous)
})
Expand All @@ -49,10 +50,10 @@ test_that('we can send a one month remaining correspondence',{
}

db <- create_dummy_database()
db <- create_ticket_and_send_initial_correspondence(db)
db <- create_ticket_and_send_initial_correspondence(db, database_path = "inst/test_db.csv")
# Set expiration date to one month from now
db$contact_1mo <- as.character(Sys.Date())
db <- send_one_month_remaining_correspondence(db)
db <- send_one_month_remaining_correspondence(db, database_path = "inst/test_db.csv")

expect_equal(db$contact_1mo, as.character(Sys.Date()))
})
Expand All @@ -63,9 +64,9 @@ test_that('we can send an aon correspondence', {
}

db <- create_dummy_database()
db <- create_ticket_and_send_initial_correspondence(db)
db <- create_ticket_and_send_initial_correspondence(db, database_path = "inst/test_db.csv")
db$contact_aon_next <- as.character(Sys.Date())
db <- send_aon_correspondence(db)
db <- send_aon_correspondence(db, database_path = "inst/test_db.csv")

expect_equal(db$contact_aon_previous, db$contact_aon_next)
})
Expand All @@ -80,7 +81,7 @@ test_that('check_rt_reply catches both potential errors', {
}

db <- create_dummy_database()
db <- create_ticket_and_send_initial_correspondence(db)
db <- create_ticket_and_send_initial_correspondence(db, database_path = "inst/test_db.csv")

template <- read_initial_template(db$fund_program_name[1])
email_text <- sprintf(template,
Expand Down

0 comments on commit 96fdf31

Please sign in to comment.