-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b0e621c
added telemetry functions
bendhouseart 952f2ac
add send telemetry to import
bf530b9
sends a request on library load
66982f8
add makefile and update DESCRIPTION
b3ddd09
gave up on fancy tricks, writing things to a json inside the library
614bafe
Apply suggestions from code review
bendhouseart c441aaa
Apply suggestions from code review
bendhouseart 4259612
commit before pull
8f3c504
Merge branch '32-adding-telemetry-kinfitr' of https://github.com/math…
270716f
'cleaned' things up
114f3ca
fixed url
1bb7770
Stylistic updates to telemetry PR
mathesong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,3 +5,5 @@ | |
| ^\.github$ | ||
| ^README.Rmd | ||
| ^data-raw | ||
| Makefile | ||
| installation_settings.json | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,7 +39,8 @@ Imports: | |
| stringr, | ||
| segmented, | ||
| LambertW, | ||
| rlang | ||
| rlang, | ||
| httr2 | ||
| Suggests: | ||
| knitr, | ||
| devtools, | ||
|
|
||
This file contains hidden or 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,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 |
This file contains hidden or 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,66 @@ | ||
| # set url_base to http://openneuropet.org for deployment | ||
| # url_base <- "http://54.144.240.214" | ||
| # get_url <- paste0(url_base, "/check/kinfitr/") | ||
| # post_url <- paste0(url_base, "/kinfitr/") | ||
|
|
||
|
|
||
|
|
||
| #' Get Telemetry Data | ||
| #' | ||
| #' Checks to see what has been posted to the URL endpoint. Should return | ||
| #' location and any any other data that gets put there with send_telemetry | ||
| #' | ||
| #' @param url The url endpoint | ||
| #' @param number_of_records How many records. 0 for all. | ||
| #' | ||
| #' @returns A list of the recorded telemetry data | ||
| get_telemetry <- function(url = "http://54.144.240.214/check/kinfitr/", | ||
| number_of_records = 0) { | ||
|
|
||
|
|
||
| req <- httr2::request(paste0(url, as.character(number_of_records))) %>% | ||
| httr2::req_headers("Accept" = "application/json") %>% | ||
| httr2::req_retry(max_tries = 3) %>% | ||
| httr2::req_timeout(3) | ||
|
|
||
| response <- httr2::req_perform(req) | ||
|
|
||
| httr2::resp_body_json(response) | ||
|
|
||
| } | ||
|
|
||
| #' Sending telemetry data | ||
| #' | ||
| #' Sending telemetry data to the URL endpoint | ||
| #' | ||
| #' @param telemetry_json_data The JSON data content to be sent | ||
| #' @param url The url endpoint | ||
| #' | ||
| #' @returns The status code | ||
| send_telemetry <- function(telemetry_json_data, | ||
| url = "http://54.144.240.214/kinfitr/") { | ||
|
|
||
| no_track <- Sys.getenv("KINFITR_NO_TRACK") | ||
|
|
||
| if (tolower(no_track) == "true") { | ||
| return(NULL) | ||
| } else { | ||
|
|
||
| try( | ||
| { | ||
| req <- httr2::request(url) %>% | ||
| httr2::req_retry(max_tries = 3) %>% | ||
| httr2::req_timeout(3) | ||
|
|
||
| response <- req %>% | ||
| httr2::req_body_json(data = telemetry_json_data) %>% | ||
| httr2::req_perform() | ||
|
|
||
| return_values <- list(status_code = response$status_code) | ||
|
|
||
| return(return_values) | ||
| }, | ||
| silent = FALSE | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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,144 @@ | ||
| 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) | ||
| }, | ||
| 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" | ||
| ) | ||
| install_info <- read_installation_info( | ||
| path_to_installation_info_json | ||
| ) | ||
| # Skip telemetry during tests | ||
| if (is_loading_for_tests()) { | ||
| return(NULL) | ||
| } | ||
|
|
||
| if (isTRUE(getOption("kinfitr.no_track") |> as.logical()) || | ||
| isTRUE(Sys.getenv("KINFITR_NO_TRACK") |> as.logical()) || | ||
| isTRUE(install_info$no_track |> as.logical())) { | ||
|
|
||
| install_info$no_track <- TRUE | ||
| write_installation_info(install_info, path_to_installation_info_json) | ||
|
|
||
| return(NULL) | ||
| } | ||
|
|
||
| if (!isTRUE(getOption("kinfitr.no_track") |> as.logical()) && | ||
| !isTRUE(Sys.getenv("KINFITR_NO_TRACK") |> as.logical()) && | ||
| !isTRUE(install_info$no_track |> as.logical()) && | ||
| !isTRUE(is_loading_for_tests())) { | ||
|
|
||
| send_telemetry(list("kinfitr_usage" = "package_installed")) | ||
| write_installation_info(install_info, path_to_installation_info_json) | ||
| } | ||
|
|
||
| # display this message at install | ||
| if (!isTRUE(install_info$displayed_message %>% as.logical())) { | ||
| message( | ||
| paste( | ||
| "Please note that kinfitr will send telemetry information to the", | ||
| "kinfitr developers in order to track real world usage, which is", | ||
| "critical for obtaining funding to continue future development.", | ||
| "This consists of information about approximate location and operating", | ||
| "system.\n\n", | ||
| "To opt-out of sending this information and disable usage 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 .", | ||
| sep = " " | ||
| ) | ||
| ) | ||
| install_info$displayed_message <- TRUE | ||
| 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" | ||
| ) | ||
|
|
||
| install_info <- read_installation_info( | ||
| path_to_installation_info_json | ||
| ) | ||
| # check to see if the user has opted out of tracking at a | ||
| # system environment, R environment, or via config file | ||
| # or if this library is being tested | ||
| if (isTRUE(getOption("kinfitr.no_track") |> as.logical()) || | ||
| isTRUE(Sys.getenv("KINFITR_NO_TRACK") |> as.logical()) || | ||
| isTRUE(install_info$no_track |> as.logical())) { | ||
|
|
||
| install_info$no_track <- TRUE | ||
| write_installation_info(install_info, path_to_installation_info_json) | ||
|
|
||
| return(NULL) | ||
| } | ||
|
|
||
| if (isTRUE(is_loading_for_tests())) { | ||
| # don't do anything if running tests | ||
| return(NULL) | ||
| } | ||
|
|
||
| if (!isTRUE(getOption("kinfitr.no_track") |> as.logical()) && | ||
| !isTRUE(Sys.getenv("KINFITR_NO_TRACK") |> as.logical()) && | ||
| !isTRUE(install_info$no_track |> as.logical()) && | ||
| !isTRUE(is_loading_for_tests())) { | ||
|
|
||
| # Send telemetry when package is loaded | ||
| send_telemetry(list("kinfitr_usage" = "package_loaded")) | ||
|
|
||
| # if none of the conditionals are met in the first statement, then | ||
| # set tracking to true (no_track=FALSE) | ||
| install_info$no_track <- FALSE | ||
| write_installation_info(install_info, path_to_installation_info_json) | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| Version: 1.0 | ||
| ProjectId: ee24a3d1-bafb-47d9-9f37-5906c440df0d | ||
|
|
||
| RestoreWorkspace: No | ||
| SaveWorkspace: No | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.