Skip to content

Commit 077b6c5

Browse files
authored
Story: Gather Badge Statistics (#2070)
1 parent f67b084 commit 077b6c5

1 file changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import djclick as click
2+
import numpy as np
3+
4+
from openpyxl import Workbook
5+
from openpyxl import worksheet
6+
from openpyxl.worksheet.table import Table
7+
from typing import Any
8+
from typing import Iterable
9+
10+
from django.contrib.auth import get_user_model
11+
from django.db.models import Count
12+
from django.db.models import Q
13+
from django.db.models import QuerySet
14+
15+
from libraries.models import CommitAuthor
16+
17+
# Set a the number of percentiles to calculate.
18+
# e.g. 20 means calculate every 5 percent
19+
NUMBER_OF_PERCENTILES = 20
20+
STEP = int(100 / NUMBER_OF_PERCENTILES)
21+
PERCENTILE_RANKS = list(range(STEP, (NUMBER_OF_PERCENTILES + 1) * STEP, STEP))
22+
23+
24+
def calculate_percentile(
25+
data_list: Iterable[int],
26+
):
27+
a = np.array(data_list)
28+
res = np.percentile(a, PERCENTILE_RANKS)
29+
return res
30+
31+
32+
def insert_data_into_column(
33+
column_label: str,
34+
data_list: Iterable[Any],
35+
start_index: int,
36+
worksheet: worksheet,
37+
):
38+
index = start_index
39+
for i in data_list:
40+
worksheet[f"{column_label}{index}"] = i
41+
index += 1
42+
43+
44+
def insert_percentile_data_into_column(
45+
column_label: str,
46+
data_list: Iterable[int],
47+
start_index: int,
48+
worksheet: worksheet,
49+
):
50+
percentiles = calculate_percentile(data_list)
51+
insert_data_into_column(column_label, percentiles, start_index, worksheet)
52+
53+
54+
def insert_qs_data_into_percentile_column(
55+
column_label: str,
56+
qs: QuerySet,
57+
start_index: int,
58+
worksheet: worksheet,
59+
field_name: str,
60+
):
61+
data_list = qs.order_by(field_name).values_list(field_name, flat=True)
62+
title = " ".join(field_name.split("_")).capitalize()
63+
worksheet[f"{column_label}{start_index}"] = title
64+
insert_percentile_data_into_column(
65+
column_label, data_list, start_index + 1, worksheet
66+
)
67+
68+
69+
@click.command()
70+
def command():
71+
User = get_user_model()
72+
73+
user_qs = (
74+
User.objects.all()
75+
.annotate(
76+
library_versions_authored=Count("author_libraryversions", distinct=True),
77+
libraries_authored=Count("authors", distinct=True),
78+
)
79+
.order_by("-email")
80+
.exclude(Q(library_versions_authored=0) & Q(libraries_authored=0))
81+
)
82+
commit_author_qs = (
83+
CommitAuthor.objects.all()
84+
.annotate(
85+
commits_authored=Count("commit", distinct=True),
86+
reviews_submitted=Count("submitted_reviews", distinct=True),
87+
mailing_list_count=Count("emaildata", distinct=True),
88+
)
89+
.exclude(
90+
Q(commits_authored=0) & Q(reviews_submitted=0) & Q(mailing_list_count=0)
91+
)
92+
)
93+
94+
wb = Workbook()
95+
ws = wb.active
96+
97+
# Using the default sheet, Present Library Version Authors and Library Authors
98+
ws.title = "Library Authors and Versions"
99+
ws.append(
100+
[
101+
"Email",
102+
"Libraries Authored",
103+
"Library Versions Authored",
104+
]
105+
)
106+
for user in user_qs:
107+
ws.append(
108+
[
109+
user.email,
110+
user.libraries_authored,
111+
user.library_versions_authored,
112+
]
113+
)
114+
tab = Table(
115+
displayName="LibraryAuthorsandVersions", ref=f"A1:C{user_qs.count() + 1}"
116+
)
117+
ws.add_table(tab)
118+
119+
wb.create_sheet("Commit Author Data")
120+
ws = wb["Commit Author Data"]
121+
ws.append(
122+
[
123+
"Name",
124+
"Commits Authored",
125+
"Reviews Submitted",
126+
"Mailing List Contributions",
127+
]
128+
)
129+
for user in commit_author_qs:
130+
ws.append(
131+
[
132+
user.display_name,
133+
user.commits_authored,
134+
user.reviews_submitted,
135+
user.mailing_list_count,
136+
]
137+
)
138+
tab = Table(
139+
displayName="CommitAuthorData", ref=f"A1:D{commit_author_qs.count() + 1}"
140+
)
141+
ws.add_table(tab)
142+
143+
# Add percentile data
144+
wb.create_sheet("Percentile Analysis")
145+
ws = wb["Percentile Analysis"]
146+
insert_data_into_column("A", ["Percentile"] + PERCENTILE_RANKS, 1, ws)
147+
insert_qs_data_into_percentile_column("B", user_qs, 1, ws, "libraries_authored")
148+
insert_qs_data_into_percentile_column(
149+
"C", user_qs, 1, ws, "library_versions_authored"
150+
)
151+
insert_qs_data_into_percentile_column(
152+
"F", commit_author_qs.exclude(commits_authored=0), 1, ws, "commits_authored"
153+
)
154+
insert_qs_data_into_percentile_column(
155+
"G", commit_author_qs.exclude(reviews_submitted=0), 1, ws, "reviews_submitted"
156+
)
157+
insert_qs_data_into_percentile_column(
158+
"H",
159+
commit_author_qs.exclude(mailing_list_count=0),
160+
1,
161+
ws,
162+
"mailing_list_count",
163+
)
164+
165+
tab = Table(displayName="PercentilData", ref=f"A1:H{NUMBER_OF_PERCENTILES+1}")
166+
ws.add_table(tab)
167+
168+
wb.save("badge_info.xlsx")

0 commit comments

Comments
 (0)