-
Notifications
You must be signed in to change notification settings - Fork 8
added telemetry to kinfitr #33
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
Changes from 5 commits
b0e621c
952f2ac
bf530b9
66982f8
b3ddd09
614bafe
c441aaa
4259612
8f3c504
270716f
114f3ca
1bb7770
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ | |
| ^\.github$ | ||
| ^README.Rmd | ||
| ^data-raw | ||
| 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 |
| 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 | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| get_url <- "http://54.144.240.214/check/kinfitr/" | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 = "")) | ||
|
||
| req |> req_headers("Accept" = "application/json") | ||
| req |> req_retry(max_tries = 3) | ||
| req |> req_timeout(3) | ||
| response <- req_perform(req) | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Fix the request chain - these need to be assigned | ||
| req <- req_retry(req, max_tries = 3) | ||
| req <- req_timeout(req, 3) | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| message("Debug: Sending to URL: ", url) | ||
| response <- req |> | ||
| req_body_json(data = telemetry_json_data) |> | ||
| req_perform() | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| library(jsonlite) | ||
bendhouseart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| 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")) | ||
bendhouseart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.