import json
import os
import csv
import pandas as pd


def calc_file_denom(project_name):
    with open('/data/users/mgaughan/kkex/contrib_uni_rosters_013124/' + 'contrib_roster_' + project_name + '.json') as file:
        data = json.load(file)
        print(len(data['api_contributors']) + len(data['issue_pr_contributors']) + len(data['file_contributors']) + len(data['wiki_contributors']))
        running_roster = data['api_contributors']
        for individual in data['issue_pr_contributors']:
            if individual not in running_roster:
                running_roster.append(individual)
        for individual in data['file_contributors']:
            if individual not in running_roster:
                running_roster.append(individual)
        for individual in data['wiki_contributors']:
            if individual not in running_roster:
                running_roster.append(individual)
        return len(running_roster)

def for_all_projects():
    with open('final_data/deb_octo_data.csv', newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        with open('new_denom_032624.csv', 'w', newline='') as writefile:
            keys = ["project_name","underproduction_mean","underproduction_low","underproduction_high","debian_vcs_link","upstream_vcs_link","age_of_project","contributors","collaborators","milestone_count", "api_contrib_count", "issue_contrib_count", "file_contrib_count", "wiki_contrib_count", "contrib_denom"]
            writer = csv.DictWriter(writefile, fieldnames=keys)
            for row in reader:
                row['contrib_denom'] = calc_file_denom(row['project_name'])
                #print(row)
                writer.writerow(row)


if __name__ == "__main__":
    for_all_projects()
    #print(calc_file_denom("zzz-to-char"))