Skip to content
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

276 create summary stats #300

Merged
merged 8 commits into from
Sep 25, 2023
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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: GitStats
Title: Get Statistics from Git Hosting Services
Version: 0.1.0.9005
Version: 0.1.0.901
Authors@R: c(
person(given = "Maciej", family = "Banaś", email = "[email protected]", role = c("aut", "cre")),
person(given = "Kamil", family = "Koziej", email = "[email protected]", role = "aut"),
Expand Down
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Generated by roxygen2: do not edit by hand

S3method(gitstats_plot,commits_stats)
S3method(gitstats_plot,repos_stats)
export("%>%")
export(create_gitstats)
export(get_commits)
export(get_commits_stats)
export(get_orgs)
export(get_repos)
export(get_repos_stats)
export(get_users)
export(plot_commits)
export(plot_repos)
export(gitstats_plot)
export(pull_commits)
export(pull_repos)
export(pull_repos_contributors)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
GitStats (development version)

- added `get_*_stats` functions to prepare summary stats from pulled data: repositories and commits (I: #276),
- rename and refactor plot functions to one generic `gitstats_plot` which takes as an input `repos_stats` or `commits_stats` (I: #276),
- changed names of `get_*` to `pull_*` functions; `get_*` functions are now to retrieve already pulled data from GitStats object (I: #294),
- changed name of `setup` to `set_params` function (I: #294),
- set new name for `set_connection` function: `set_host` as it is more informative (and shorter) (I: #271),
Expand Down
9 changes: 3 additions & 6 deletions R/EngineGraphQLGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
self$gql_query <- GQLQueryGitHub$new()
},

#' @description Get all groups from GitLab.
#' @description Get all orgs from GitHub.
pull_orgs = function() {
end_cursor <- NULL
has_next_page <- TRUE
Expand Down Expand Up @@ -134,7 +134,6 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
commits_table <- repos_list_with_commits %>%
purrr::discard(~ length(.) == 0) %>%
private$prepare_commits_table(org)

return(commits_table)
},

Expand Down Expand Up @@ -251,9 +250,7 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
repo$created_at <- gts_to_posixt(repo$created_at)
repo$issues_open <- repo$issues_open$totalCount
repo$issues_closed <- repo$issues_closed$totalCount
repo$last_activity_at <- difftime(Sys.time(), as.POSIXct(repo$last_activity_at),
units = "days"
) %>% round(2)
repo$last_activity_at <- as.POSIXct(repo$last_activity_at)
repo$organization <- repo$organization$login
data.frame(repo)
})
Expand Down Expand Up @@ -392,7 +389,7 @@ EngineGraphQLGitHub <- R6::R6Class("EngineGraphQLGitHub",
commits_row
}) %>%
purrr::discard(~ length(.) == 1) %>%
rbindlist()
rbindlist(use.names = TRUE)

if (nrow(commits_table) > 0) {
commits_table <- commits_table %>%
Expand Down
4 changes: 1 addition & 3 deletions R/EngineGraphQLGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ EngineGraphQLGitLab <- R6::R6Class("EngineGraphQLGitLab",
repo$issues_open <- repo$issues$opened
repo$issues_closed <- repo$issues$closed
repo$issues <- NULL
repo$last_activity_at <- difftime(Sys.time(), as.POSIXct(repo$last_activity_at),
units = "days"
) %>% round(2)
repo$last_activity_at <- as.POSIXct(repo$last_activity_at)
repo$organization <- repo$group$name
repo$group <- NULL
data.frame(repo)
Expand Down
4 changes: 1 addition & 3 deletions R/EngineRest.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ EngineRest <- R6::R6Class("EngineRest",
repos_dt <- dplyr::mutate(repos_dt,
id = as.character(id),
created_at = as.POSIXct(created_at),
last_activity_at = difftime(Sys.time(), as.POSIXct(last_activity_at),
units = "days"
) %>% round(2),
last_activity_at = as.POSIXct(last_activity_at),
forks = as.integer(forks),
issues_open = as.integer(issues_open),
issues_closed = as.integer(issues_closed)
Expand Down
2 changes: 1 addition & 1 deletion R/EngineRestGitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ EngineRestGitHub <- R6::R6Class("EngineRestGitHub",
pull_repos_contributors = function(repos_table) {
if (nrow(repos_table) > 0) {
if (!private$scan_all) {
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}] Pulling contributors...")
cli::cli_alert_info("[GitHub][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors...")
}
repo_iterator <- paste0(repos_table$organization, "/", repos_table$name)
user_name <- rlang::expr(.$login)
Expand Down
2 changes: 1 addition & 1 deletion R/EngineRestGitLab.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ EngineRestGitLab <- R6::R6Class("EngineRestGitLab",
pull_repos_contributors = function(repos_table) {
if (nrow(repos_table) > 0) {
if (!private$scan_all) {
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}] Pulling contributors...")
cli::cli_alert_info("[GitLab][Engine:{cli::col_green('REST')}][org:{unique(repos_table$organization)}] Pulling contributors...")
}
repo_iterator <- repos_table$id
user_name <- rlang::expr(.$name)
Expand Down
5 changes: 3 additions & 2 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ GitHost <- R6::R6Class("GitHost",
cli::cli_alert_info("[Host:{private$host}] {cli::col_yellow('Pulling commits from all organizations...')}")
}
commits_table <- purrr::map(private$orgs, function(org) {
commits_table_org <- NULL
tryCatch({
commits_table_org <- purrr::map(private$engines, ~ .$pull_commits(
org = org,
Expand All @@ -138,12 +139,12 @@ GitHost <- R6::R6Class("GitHost",
date_until = date_until,
settings = settings
)
})
}) %>%
purrr::list_rbind()
} else {
e
}
})

return(commits_table_org)
}, .progress = private$scan_all) %>%
purrr::list_rbind()
Expand Down
86 changes: 86 additions & 0 deletions R/get_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#' @title Get Statistics On Repositories
#' @name get_repos_stats
#' @description Prepare statistics from the pulled repositories data.
#' @param gitstats_obj A GitStats class object.
#' @return A table of `repos_stats` class.
#' @export
get_repos_stats <- function(gitstats_obj){
repos_data <- data.table::copy(get_repos(gitstats_obj))
repos_stats <- repos_data %>%
dplyr::mutate(
fullname = paste0(organization, "/", name)
) %>%
dplyr::mutate(
last_activity = difftime(
Sys.time(),
last_activity_at,
units = "days"
) %>% round(2),
repository = fullname,
platform = retrieve_platform(api_url)
)
if ("contributors" %in% colnames(repos_data)) {
repos_stats <- dplyr::mutate(
repos_stats,
contributors_n = purrr::map_vec(contributors, function(contributors_string) {
length(stringr::str_split(contributors_string[1], ", ")[[1]])
})
)
} else {
repos_stats <- dplyr::mutate(
repos_stats,
contributors_n = NA
)
}
repos_stats <- dplyr::select(
repos_stats,
repository, platform, created_at, last_activity, stars, forks,
languages, issues_open, issues_closed, contributors_n
)
class(repos_stats) <- append(class(repos_stats), "repos_stats")
return(repos_stats)
}

#' @title Get Statistics On Commits
#' @name get_commits_stats
#' @description Prepare statistics from the pulled commits data.
#' @param gitstats_obj A GitStats class object.
#' @param time_interval A character, specifying time interval to show statistics.
#' @return A table of `commits_stats` class.
#' @export
get_commits_stats <- function(gitstats_obj,
time_interval = c("month", "day", "week")){
commits_data <- data.table::copy(get_commits(gitstats_obj))
time_interval <- match.arg(time_interval)

commits_stats <- commits_data %>%
dplyr::mutate(
stats_date = lubridate::floor_date(
committed_date,
unit = time_interval
),
platform = retrieve_platform(api_url)
) %>%
dplyr::group_by(stats_date, platform, organization) %>%
dplyr::summarise(
commits_n = dplyr::n()
) %>%
dplyr::arrange(
stats_date
)
commits_stats <- commits_stats(
object = commits_stats,
time_interval = time_interval
)
return(commits_stats)
}

#' @noRd
#' @description A constructor for `commits_stats` class
commits_stats <- function(object, time_interval) {
stopifnot(inherits(object, "grouped_df"))
object <- dplyr::ungroup(object)
class(object) = append(class(object), "commits_stats")
attr(object, "time_interval") <- time_interval
object
}
6 changes: 4 additions & 2 deletions R/global.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# This is a solution for warnings in r-cmd checks of global variables
globalVariables(c(
".", "fullname", "platform", "organization", "repo_url",
"name", "last_activity_at", "stats_date", "committed_date",
"api_url", "row_no", ".N"
"name", "created_at", "last_activity_at", "last_activity", "stats_date",
"committed_date", "commits_n", "api_url", "row_no", ".N", ".data",
"repository", "stars", "forks", "languages", "issues_open", "issues_closed",
"contributors_n"
))
Loading
Loading