Skip to content
Merged
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^\.github$
^README.Rmd
^data-raw
Makefile
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Depends:
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
LazyLoad: no
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% on what LazyLoad is, but I'm really hesitant for it to be altered...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what it does either, but I'm planning on reverting this before we merge.

RoxygenNote: 7.3.2
Imports:
pracma,
Expand Down Expand Up @@ -39,7 +40,8 @@ Imports:
stringr,
segmented,
LambertW,
rlang
rlang,
httr2
Suggests:
knitr,
devtools,
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Makefile
.PHONY: reload clean install

reload:
Rscript -e "if('kinfitr' %in% (.packages())) { detach('package:kinfitr', unload=TRUE); try(unloadNamespace('kinfitr'), silent=TRUE) }; devtools::install('.', force=TRUE); library(kinfitr)"

clean:
Rscript -e "remove.packages('kinfitr'); devtools::clean_dll()"

install:
Rscript -e "devtools::install('.', force=TRUE)"

interactive:
R

debug: reload interactive
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,11 @@ export(weights_create)
export(weights_create_bids)
import(ggplot2)
importFrom(dplyr,"%>%")
importFrom(httr2,req_body_json)
importFrom(httr2,req_headers)
importFrom(httr2,req_perform)
importFrom(httr2,req_retry)
importFrom(httr2,req_timeout)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(magrittr,"%>%")
52 changes: 52 additions & 0 deletions R/kinfitr_telemetry.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' @importFrom httr2 request req_headers req_retry req_timeout req_perform
#' @importFrom httr2 resp_body_json req_body_json

get_url <- "http://54.144.240.214/check/kinfitr/"
post_url <- "http://54.144.240.214/kinfitr/"

get_telemetry <- function(url = get_url, number_of_records = 0) {
# checks to see what's been posted to the url endpoint, should return location,
# and any other data that gets put there with send_telemetry
req <- request(paste(url, as.character(number_of_records), sep = ""))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you didn't chain these together? I've not used httr2 before, but usually these kinds of things would be chained together.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because I'm writing things like C or Python functions and not R functions. I'm also wrapping them into line by line b/c my R linter is screaming at me if I go over 80 characters.

req |> req_headers("Accept" = "application/json")
req |> req_retry(max_tries = 3)
req |> req_timeout(3)
response <- req_perform(req)
return(resp_body_json(response))
}

#' Send telemetry data
#' @keywords internal
send_telemetry <- function(telemetry_json_data, url = post_url) {
# Add debug messages
message("Debug: Starting send_telemetry")
message("Debug: Telemetry data: ", toString(telemetry_json_data))

no_track <- Sys.getenv("KINFITR_NO_TRACK")
message("Debug: KINFITR_NO_TRACK value: ", no_track)

if (tolower(no_track) == "true") {
message("Debug: Tracking disabled")
return(NULL)
} else {
message("Debug: Attempting to send telemetry")
try(
{
req <- request(url)
# Fix the request chain - these need to be assigned
req <- req_retry(req, max_tries = 3)
req <- req_timeout(req, 3)

message("Debug: Sending to URL: ", url)
response <- req |>
req_body_json(data = telemetry_json_data) |>
req_perform()

message("Debug: Response status: ", response$status_code)
return_values <- list(status_code = response$status_code)
return(return_values)
},
silent = FALSE # Changed to FALSE to see any errors
)
}
}
103 changes: 103 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
library(jsonlite)


read_installation_info <- function(path_to_installation_info_json) {
# Initialize a list to store the key-value pairs
installation_info <- list(
package = NULL,
installed_on = NULL,
no_track = NULL,
displayed_message = NULL
)

# Read the JSON file
tryCatch(
{
info <- jsonlite::fromJSON(path_to_installation_info_json)
# update installation info with what's in the json file
installation_info <- as.list(info)
},
error = function(e) {
# Fail silently, keep default NULL values
}
)

return(installation_info)
}

write_installation_info <- function(
install_info, path_to_installation_info_json) {
# Create a list to write to JSON
install_info$installed_on <- Sys.Date()

# Write the list to the JSON file
tryCatch(
{
jsonlite::write_json(install_info, path_to_installation_info_json)
message("Installation settings written to: ", path_to_installation_info_json)
},
error = function(e) {
# Fail silently
message("Failed to write installation info to: ", path_to_installation_info_json)
message("Error: ", e)
}
)
}

is_loading_for_tests <- function() {
!interactive() && (
identical(Sys.getenv("DEVTOOLS_LOAD"), "kinfitr") ||
identical(Sys.getenv("TESTTHAT"), "true")
)
}

.onAttach <- function(libname, pkgname) {
path_to_installation_info_json <- file.path(libname, "kinfitr", "installation_settings.json")
# Check if tracking is disabled
if (!isTRUE(getOption("kinfitr_no_track"))) {
message("Opt-out of sending tracking information to
the KinFitR developers.")
}

# Skip telemetry during tests
if (is_loading_for_tests()) {
return(NULL)
}

install_info <- read_installation_info(path_to_installation_info_json)
if (!isTRUE(getOption("kinfitr.no_track")) &&
Sys.getenv("KINFITR_NO_TRACK") != "TRUE" &&
!isTRUE(install_info$displayed_message)) {
message(
"Opt-out of sending tracking information to the KinFitR developers. ",
"This information provides an indicator of real world usage crucial for ",
"obtaining funding. To disable tracking set ",
"options(kinfitr.no_track = TRUE) or the environment variable ",
"KINFITR_NO_TRACK to TRUE), to hide this message set ",
"options(kinfitr.no_track = FALSE) or the environment variable ",
"KINFITR_NO_TRACK to FALSE"
)
send_telemetry(list("kinfitr_usage" = "package_installed"))
install_info$displayed_message <- TRUE
message("Writing installation info to: ", path_to_installation_info_json)
write_installation_info(install_info, path_to_installation_info_json)
}
}

.onLoad <- function(libname, pkgname) {
path_to_installation_info_json <- file.path(libname, "kinfitr", "installation_settings.json")
if (isTRUE(getOption("kinfitr.no_track")) ||
Sys.getenv("KINFITR_NO_TRACK") == "TRUE" ||
isTRUE(is_loading_for_tests())) {
# do nothing
install_info <- read_installation_info(path_to_installation_info_json)
install_info$no_track <- TRUE
write_installation_info(install_info, path_to_installation_info_json)
message("Installation info written to: ", path_to_installation_info_json)
return(NULL)
} else {
# Send telemetry when package is loaded
send_telemetry(list("kinfitr_usage" = "package_loaded"))
}

}