Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE: Experimental Okta Branch #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: datim-narratives-app
Type: Shiny App
Title: Self-service app for analyzing narrative data
Version: 0.0.7
Date: 2024-02-09
Version: 0.0.8
Date: 2024-03-15
Authors@R: c( person("Jason", "Pickering", email =
"[email protected]", role = c("aut", "cre"))
Depends: R (>= 4.2.1)
4 changes: 2 additions & 2 deletions partner_narratives_template.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ cat('\n\n')

country_data<-partner_data %>%
dplyr::filter(country == unique_countries[x]) %>%
dplyr::arrange(agency_name,mech_code,`Data`)
dplyr::arrange(agency_name,mech_code,technical_area,support_type)

unique_mechs<-unique(sort(country_data$mech_code))

Expand All @@ -104,7 +104,7 @@ for ( y in seq_along(unique_mechs)) {

mech_data<-country_data %>%
dplyr::filter(mech_code == unique_mechs[y]) %>%
dplyr::arrange(partner_name,mech_code,`Data`)
dplyr::arrange(partner_name,mech_code,technical_area, support_type)

cat(paste0("### Mechanism: ",mech_data$mech_name[1]))
cat('\n\n')
Expand Down
103 changes: 48 additions & 55 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ oauth_app <- httr::oauth_app(Sys.getenv("OAUTH_APPNAME"),
redirect_uri = APP_URL)


oauth_api <- httr::oauth_endpoint(base_url = paste0(getBaseURL(), "uaa/oauth"),
oauth_api <- httr::oauth_endpoint(base_url = paste0(Sys.getenv("OAUTH_URL"), "oauth2/v1"),
request = NULL, # Documentation says to leave this NULL for OAuth2
authorize = "authorize",
access = "token")

oauth_scope <- "ALL"
oauth_scope <- "openid"


has_auth_code <- function(params) {
Expand Down Expand Up @@ -136,42 +136,31 @@ shinyServer(function(input, output, session) {
#Wait until the auth code actually exists
req(has_auth_code(params))

#Manually create a token
token <- httr::oauth2.0_token(
app = oauth_app,
endpoint = oauth_api,
scope = oauth_scope,
use_basic_auth = TRUE,
oob_value = APP_URL,
cache = FALSE,
credentials = httr::oauth2.0_access_token(endpoint = oauth_api,
app = oauth_app,
code = params$code,
use_basic_auth = TRUE)
)
req <- httr::POST(oauth_api$access, encode = "form",
body = list(
redirect_uri = APP_URL,
grant_type = "authorization_code",
code = params$code),
httr::authenticate(oauth_app$key, oauth_app$secret))
token <- httr::content(req)
url <- utils::URLencode(URL = paste0(Sys.getenv("BASE_URL"), "api", "/me"))
handle <- httr::handle(Sys.getenv("BASE_URL"))
#Get Request
r <- httr::GET(
url,
httr::add_headers(Authorization = paste("Bearer", token$access_token, sep = " ")),
httr::timeout(60),
handle = handle
)

me <- jsonlite::fromJSON(httr::content(r, as = "text"))
d2_default_session <- d2Session$new(
base_url = Sys.getenv("BASE_URL"),
me = me,
handle = handle,
token = token
)

loginAttempt <- tryCatch({

datimutils::loginToDATIMOAuth(base_url = getBaseURL(),
token = token,
app = oauth_app,
api = oauth_api,
redirect_uri = APP_URL,
scope = oauth_scope,
d2_session_envir = parent.env(environment()))

},
# This function throws an error if the login is not successful
error = function(e) {
shinyWidgets::sendSweetAlert(
session,
title = "Login failed",
text = "Please check your username/password!",
type = "error")
flog.info(paste0("User ", input$user_name, " login failed. ", e$message), name = "datapack")
}
)

waiter::waiter_show(html = waiting_screen, color = "black")
if (exists("d2_default_session")) {
shinyWidgets::sendSweetAlert(
Expand All @@ -192,7 +181,7 @@ shinyServer(function(input, output, session) {
user_input$is_usg_user <- isUSGUser(d2_session = user_input$d2_session)
user_input$user_mechs<-getUserMechanisms(d2_session = user_input$d2_session)
user_input$mech_dropdown <- getMechDropDown(user_input$user_mechs,NULL)
user_input$partner_data_elements<-getNarrativeDataElements(user_input$fiscal_year, d2_session = user_input$d2_session)
user_input$partner_data_elements<-getNarrativeDataElements(d2_session = user_input$d2_session)
user_input$data_elements_dropdown <- user_input$partner_data_elements %>%
dplyr::select(technical_area) %>%
dplyr::distinct() %>%
Expand Down Expand Up @@ -466,7 +455,6 @@ shinyServer(function(input, output, session) {
file.copy(img, 'pepfar.png', overwrite = TRUE)

library(rmarkdown)

out <- rmarkdown::render('report.Rmd', word_document())
file.rename(out, file)
}
Expand All @@ -485,6 +473,7 @@ shinyServer(function(input, output, session) {

partner_data<-filtered_narratives() %>%
purrr::pluck("partner")

if (!is.null(partner_data) & NROW(partner_data) > 0) {
partner_data %<>% dplyr::select("Operating unit" = ou,
"Country" = country,
Expand Down Expand Up @@ -540,29 +529,29 @@ shinyServer(function(input, output, session) {
if ( is.null(input$des )) {return(user_input$partner_data_elements$de_uid)} else {
user_input$partner_data_elements %>%
dplyr::filter(technical_area %in% input$des) %>%
dplyr::filter(year = user_input$fiscal_year) %>%
dplyr::pull(de_uid) %>%
unique(.)
} }


all_des <- get_des()
d<-list()

url <- assemblePartnerNarrativeURL(ou = countries,
fiscal_year = user_input$fiscal_year,
fiscal_quarter = user_input$fiscal_quarter,
all_des = get_des(),
all_des = all_des,
d2_session = user_input$d2_session)


d$partner <- d2_analyticsResponse(url, d2_session = user_input$d2_session)
d$partner <- d2_analyticsResponse(url, remapCols = FALSE,d2_session = user_input$d2_session)

if (user_input$is_usg_user & input$includeUSGNarratives ) {
url<- assembleUSGNarrativeURL(ou = countries,
fiscal_year = user_input$fiscal_year,
fiscal_quarter = user_input$fiscal_quarter,
d2_session = user_input$d2_session)

d$usg<-d2_analyticsResponse(url, d2_session = user_input$d2_session)
d$usg<-d2_analyticsResponse(url, remapCols = FALSE, d2_session = user_input$d2_session)

} else {
d$usg<-NULL
Expand All @@ -581,25 +570,29 @@ shinyServer(function(input, output, session) {
return(NULL)
ready$needs_refresh <- FALSE
}

de_map <- user_input$partner_data_elements %>%
dplyr::select(de_name,de_uid,technical_area, support_type) %>%
dplyr::distinct()
if(!is.null(d$partner)) {

d$partner %<>% dplyr::rename(country = `Organisation unit`) %>%
dplyr::inner_join(user_input$user_operating_units,by="country") %>%
dplyr::mutate(mech_code = ( stringr::str_split(`Funding Mechanism`," - ") %>%
purrr::map(.,purrr::pluck(2)) %>%
unlist() ) ) %>%
dplyr::left_join(user_input$user_mechs, by = "mech_code") %>%
dplyr::left_join(user_input$partner_data_elements, by=c(`Data` = "de_name"))
d$partner %<>%
dplyr::rename(country_id = `Organisation unit`) %>%
dplyr::rename(category_option_id = `Funding Mechanism`) %>%
dplyr::rename(de_uid = `Data`) %>%
dplyr::inner_join(user_input$user_operating_units,by = "country_id") %>%
dplyr::left_join(user_input$user_mechs, by = "category_option_id") %>%
dplyr::left_join(de_map, by="de_uid")

}


if (!is.null(d$usg)) {

d$usg %<>% dplyr::rename(country = `Organisation unit`) %>%
dplyr::inner_join(user_input$user_operating_units,by="country") %>%
dplyr::left_join(user_input$partner_data_elements, by=c(`Data` = "de_name"))
d$usg %<>%
dplyr::rename(country_id = `Organisation unit`) %>%
dplyr::rename(de_uid = `Data`) %>%
dplyr::inner_join(user_input$user_operating_units,by="country_id") %>%
dplyr::left_join(de_map, by="de_uid")
}


Expand Down
Loading