1
0
govdoc-cr-analysis/cleaning_scripts/get_weekly_commit_counts.R

159 lines
6.8 KiB
R
Raw Normal View History

2025-01-30 04:24:43 +00:00
library(tidyverse)
# test data directory: /gscratch/comdata/users/mjilg/program_testing/
# load in the paritioned directories
library(dplyr)
library(lubridate)
#for a given file we want to get the count data and produce a csv
2025-02-06 21:51:46 +00:00
readme_pub_info <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/metadata/0205_README_publication_commits.csv"
contributing_pub_info <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/metadata/0205_CONTRIBUTING_publication_commits.csv"
2025-02-02 20:16:42 +00:00
readme_dir <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/main_commit_data/readme/"
contributing_dir <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/main_commit_data/contributing/"
2025-01-30 04:24:43 +00:00
2025-02-06 21:51:46 +00:00
test_file <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/13125_hyak_test/main_commit_data/contributing/_linuxmint_cjs.git_commits.csv"
2025-02-01 05:09:18 +00:00
transform_commit_data <- function(filepath, ref_df){
#basic, loading in the file
2025-01-30 04:24:43 +00:00
df = read.csv(filepath, header = TRUE)
2025-02-01 05:09:18 +00:00
temp_df <- df
2025-01-30 04:24:43 +00:00
dir_path = dirname(filepath)
file_name = basename(filepath)
2025-02-01 05:09:18 +00:00
# isolate project id
project_id <- sub("_commits\\.csv$", "", file_name)
project_id <- sub("^_", "", project_id)
2025-01-30 04:24:43 +00:00
2025-02-01 05:09:18 +00:00
#make sure the dates are formatted correctly and state the project_id
2025-01-30 04:24:43 +00:00
df <- df |>
mutate(commit_date = ymd_hms(commit_date)) |>
2025-02-01 05:09:18 +00:00
mutate(project_id = project_id)
#find the publication entry, in the specified df
matched_entry <- ref_df |>
filter(repo_id == project_id)
2025-02-06 21:51:46 +00:00
pub_commit_date <- min(as.Date(matched_entry$commit_date))
2025-02-01 05:09:18 +00:00
#get information about project age either in the "present"
#or at the time of first commit
oldest_commit_date <- min(as.Date(df$commit_date))
project_age <- as.numeric(as.Date("2024-06-24") - oldest_commit_date)
2025-02-06 21:51:46 +00:00
age_at_commit <- as.numeric(pub_commit_date - oldest_commit_date)
2025-02-01 05:09:18 +00:00
#add that to the data
df <- df |>
mutate(age = project_age,
age_at_commit = age_at_commit)
#we are looking at weekly data, 6m before and 6m after
2025-02-06 21:51:46 +00:00
start_date <- pub_commit_date %m-% months(6)
#calculated_start_date <- pub_commit_date %m-% months(6)
#start_date <- max(calculated_start_date, oldest_commit_date)
end_date <- pub_commit_date %m+% months(6)
#getting the relative weeks to the publication date
relative_week <- function(date, ref_date) {
as.integer(as.numeric(difftime(date, ref_date, units = "days")) %/% 7)
}
df <- df |>
mutate(relative_week = relative_week(commit_date, pub_commit_date))
2025-02-01 05:09:18 +00:00
#filler for when there are weeks without commits
2025-02-06 21:51:46 +00:00
all_weeks <- seq(relative_week(start_date, pub_commit_date), relative_week(end_date, pub_commit_date))
complete_weeks_df <- expand.grid(relative_week = all_weeks,
2025-02-01 05:09:18 +00:00
project_id = project_id,
age = project_age,
age_at_commit = age_at_commit)
#for each week, get the list of unique authors that committed
cumulative_authors <- df %>%
2025-02-06 21:51:46 +00:00
arrange(relative_week) %>%
group_by(relative_week) %>%
2025-02-01 05:09:18 +00:00
summarize(cumulative_author_emails = list(unique(author_email)), .groups = 'drop')
#same for each committer
cumulative_committers <- df %>%
2025-02-06 21:51:46 +00:00
arrange(relative_week) %>%
group_by(relative_week) %>%
2025-02-01 05:09:18 +00:00
summarize(cumulative_committer_emails = list(unique(committer_email)), .groups = 'drop')
#now cut out the commit data that we don't care about
df <- df |>
2025-02-06 21:51:46 +00:00
filter(as.Date(pub_commit_date) >= start_date & as.Date(pub_commit_date) <= end_date)
2025-02-01 05:09:18 +00:00
#in order:
# - we group by project, week, ages
# - and we summarize commit and authorship details
# - we then fill in information for missingness
# - and add in vars for before/after
# - and weekly index
2025-01-30 04:24:43 +00:00
weekly_commits <- df |>
2025-02-06 21:51:46 +00:00
group_by(project_id, relative_week, age, age_at_commit) |>
2025-02-01 05:09:18 +00:00
summarise(commit_count = n(),
author_emails = list(unique(author_email)),
committer_emails = list(unique(committer_email)),
.groups = 'drop') |>
2025-02-06 21:51:46 +00:00
right_join(complete_weeks_df, by=c("relative_week", "project_id", "age", "age_at_commit")) |>
2025-02-01 05:09:18 +00:00
replace_na(list(commit_count = 0)) |>
2025-02-06 21:51:46 +00:00
mutate(before_after = if_else(relative_week < 0, 0, 1))
2025-02-01 05:09:18 +00:00
# then, to get the authorship details in
# we check if the email data is present, if not we fill in blank
# we bring in the information about authorship lists that we already had
# then comparing the current week's author list with the previous week's cumulative list, or empty
# ---- the length of that difference is the 'new' value
# then we delete out the author list information
weekly_with_authorship <- weekly_commits |>
mutate(
author_emails = ifelse(is.na(author_emails), list(character()), author_emails),
committer_emails = ifelse(is.na(committer_emails), list(character()), committer_emails)
) |>
2025-02-06 21:51:46 +00:00
left_join(cumulative_authors, by = "relative_week") |>
left_join(cumulative_committers, by = "relative_week") |>
2025-02-01 05:09:18 +00:00
mutate(new_author_emails = mapply(function(x, y) length(setdiff(x, y)), author_emails, lag(cumulative_author_emails, default = list(character(1)))),
new_committer_emails = mapply(function(x, y) length(setdiff(x, y)), committer_emails, lag(cumulative_committer_emails, default = list(character(1))))) |>
select(-author_emails, -committer_emails, -cumulative_author_emails, -cumulative_committer_emails)
#gracefully exit
return(weekly_with_authorship)
2025-01-30 04:24:43 +00:00
}
#then for all files in a directory
2025-02-01 05:09:18 +00:00
transform_directory_of_commit_data <- function(is_readme) {
ref_df <- read.csv(contributing_pub_info)
dir_path <- contributing_dir
if (is_readme){
ref_df <- read.csv(readme_pub_info)
dir_path <- readme_dir
}
counted_list <- list()
2025-01-30 04:24:43 +00:00
file_list <- list.files(path = dir_path, pattern = "*.csv", full.names = TRUE)
for (filepath in file_list) {
2025-02-01 05:09:18 +00:00
transformed_data <- transform_commit_data(filepath, ref_df)
counted_list <- append(counted_list, list(transformed_data))
2025-01-30 04:24:43 +00:00
}
2025-02-01 05:09:18 +00:00
counted_df <- bind_rows(counted_list)
return(counted_df)
2025-01-30 04:24:43 +00:00
}
2025-02-06 21:51:46 +00:00
#test_ref_df <- read.csv(contributing_pub_info)
#test_count_df <- transform_commit_data(test_file, test_ref_df)
2025-02-01 05:09:18 +00:00
#below is for contributing file
2025-02-06 21:51:46 +00:00
#big_df <- transform_directory_of_commit_data(is_readme=FALSE)
#output_filepath <-"/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/metadata/co_0205_CONTRIBUTING_weekly_count_data.csv"
2025-02-01 05:09:18 +00:00
#below is for readme
2025-02-02 20:16:42 +00:00
big_df <- transform_directory_of_commit_data(is_readme=TRUE)
2025-02-06 21:51:46 +00:00
output_filepath <-"/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/metadata/0205_README_weekly_count_data.csv"
2025-02-01 05:09:18 +00:00
#validation testing
2025-02-06 21:51:46 +00:00
#length(unique(big_df$project_id))
2025-02-01 05:09:18 +00:00
#filtered_df <- test_big_df %>%
# filter(commit_count != 0, new_author_emails == 0, new_committer_emails == 0)
2025-02-06 21:51:46 +00:00
test_df <- big_df |>
filter(age_at_commit >= 0)
2025-02-01 05:09:18 +00:00
#another graceful exit
2025-02-06 21:51:46 +00:00
write.csv(test_df, output_filepath, row.names = FALSE)