Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
^\.github$
^README.Rmd
^data-raw
Makefile
installation_settings.json
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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
66 changes: 66 additions & 0 deletions R/kinfitr_telemetry.R
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
)
}
}
144 changes: 144 additions & 0 deletions R/zzz.R
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)
}
}
1 change: 1 addition & 0 deletions kinfitr.Rproj
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
Expand Down
23 changes: 23 additions & 0 deletions man/get_telemetry.Rd

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

19 changes: 19 additions & 0 deletions man/send_telemetry.Rd

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