-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,27 @@ | ||
#' Extract total CRAN downloads for nominated package over period defined by | ||
#' `options("githist_period")`. | ||
#' | ||
#' @param pkg_name Name of package. For packages not on CRAN, the 'cranlogs' | ||
#' API returns download counts of 0. | ||
#' @param end_date The date up to which download counts are to be aggregated. | ||
#' @return A single integer counting the number of downloads. | ||
#' @noRd | ||
cran_downloads <- function (pkg_name, end_date = Sys.Date ()) { | ||
|
||
checkmate::assert_character (pkg_name, len = 1L) | ||
checkmate::assert_date (end_date) | ||
period <- get_githist_period () | ||
start_date <- as.Date (end_date - period) | ||
interval <- paste (start_date, sep = ":", end_date) | ||
|
||
base_url <- "http://cranlogs.r-pkg.org/" | ||
daily_url <- paste0 (base_url, "downloads/total/") | ||
req_url <- paste0 (daily_url, interval, "/", pkg_name) | ||
|
||
req <- httr2::request (req_url) | ||
resp <- httr2::req_perform (req) | ||
httr2::resp_check_status (resp) | ||
|
||
body <- httr2::resp_body_json (resp) | ||
return (body [[1]]$downloads) | ||
} |
This file contains 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
This file contains 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
This file contains 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,20 @@ | ||
function (resp) { | ||
|
||
resp <- httptest2::gsub_response ( | ||
resp, | ||
"http://cranlogs.r-pkg.org/downloads/total/", | ||
"cranlogs/", | ||
fixed = TRUE | ||
) | ||
|
||
# Timestamp pattern, where replacing with "" removes sub-dir: | ||
ptn <- "[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}" | ||
resp <- httptest2::gsub_response ( | ||
resp, | ||
paste0 (ptn, "\\:", ptn), | ||
"", | ||
fixed = FALSE | ||
) | ||
|
||
return (resp) | ||
} |
This file contains 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
This file contains 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,8 @@ | ||
[ | ||
{ | ||
"start": "2023-10-03", | ||
"end": "2024-01-01", | ||
"downloads": 2308, | ||
"package": "goodpractice" | ||
} | ||
] |
This file contains 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,11 @@ | ||
test_that ("chaoss external cran_downloads", { | ||
|
||
pkg_name <- "goodpractice" | ||
end_date <- as.Date ("2024-01-01") | ||
dl <- with_mock_dir ("cran_dl", { | ||
cran_downloads (pkg_name = pkg_name, end_date = end_date) | ||
}) | ||
expect_type (dl, "integer") | ||
expect_length (dl, 1L) | ||
expect_equal (dl, 2308) | ||
}) |