forked from Nuri22/csDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommitAnalysis.py
299 lines (250 loc) · 9.07 KB
/
commitAnalysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import git
import csv
import os
from dateutil.relativedelta import relativedelta
from typing import List
from progress.bar import Bar
from datetime import datetime
from utils import authorIdExtractor
from statsAnalysis import outputStatistics
from sentistrength import PySentiStr
from git.objects.commit import Commit
from configuration import Configuration
import pytz
def commitAnalysis(
senti: PySentiStr,
commits: List[git.Commit],
delta: relativedelta,
config: Configuration,
):
# sort commits
commits.sort(key=lambda o: o.committed_datetime)
# split commits into batches
batches = []
batch = []
startDate = None
if config.startDate is not None:
startDate = datetime.strptime(config.startDate, "%Y-%m-%d")
startDate = startDate.replace(tzinfo=pytz.UTC)
batchStartDate = None
batchEndDate = None
batchDates = []
for commit in Bar("Batching commits").iter(commits):
if startDate is not None and startDate > commit.committed_datetime:
continue
# prepare first batch
if batchStartDate == None:
batchStartDate = commit.committed_datetime
batchEndDate = batchStartDate + delta
batchDates.append(batchStartDate)
# prepare next batch
elif commit.committed_datetime > batchEndDate:
batches.append(batch)
batch = []
batchStartDate = commit.committed_datetime
batchEndDate = batchStartDate + delta
batchDates.append(batchStartDate)
# populate current batch
batch.append(commit)
# complete batch list and perform clean up
batches.append(batch)
del batch, commits
# run analysis per batch
authorInfoDict = {}
daysActive = list()
for idx, batch in enumerate(batches):
# get batch authors
batchAuthorInfoDict, batchDaysActive = commitBatchAnalysis(
idx, senti, batch, config
)
# combine with main lists
authorInfoDict.update(batchAuthorInfoDict)
daysActive.append(batchDaysActive)
return batchDates, authorInfoDict, daysActive
def commitBatchAnalysis(
idx: int, senti: PySentiStr, commits: List[git.Commit], config: Configuration
):
authorInfoDict = {}
timezoneInfoDict = {}
experienceDays = 150
# traverse all commits
print("Analyzing commits")
startDate = None
if config.startDate is not None:
startDate = datetime.strptime(config.startDate, "%Y-%m-%d")
startDate = startDate.replace(tzinfo=pytz.UTC)
# sort commits
commits.sort(key=lambda o: o.committed_datetime, reverse=True)
commitMessages = []
commit: Commit
lastDate = None
firstDate = None
realCommitCount = 0
for commit in Bar("Processing").iter(commits):
if startDate is not None and startDate > commit.committed_datetime:
continue
if lastDate is None:
lastDate = commit.committed_date
firstDate = commit.committed_date
realCommitCount = realCommitCount + 1
# extract info
author = authorIdExtractor(commit.author)
timezone = commit.author_tz_offset
time = commit.authored_datetime
# get timezone
timezoneInfo = timezoneInfoDict.setdefault(
timezone, dict(commitCount=0, authors=set())
)
# save info
timezoneInfo["authors"].add(author)
if commit.message and commit.message.strip():
commitMessages.append(commit.message)
# increase commit count
timezoneInfo["commitCount"] += 1
# get author
authorInfo = authorInfoDict.setdefault(
author,
dict(
commitCount=0,
sponsoredCommitCount=0,
earliestCommitDate=time,
latestCommitDate=time,
sponsored=False,
activeDays=0,
experienced=False,
),
)
# increase commit count
authorInfo["commitCount"] += 1
# validate earliest commit
# by default GitPython orders commits from latest to earliest
if time < authorInfo["earliestCommitDate"]:
authorInfo["earliestCommitDate"] = time
# check if commit was between 9 and 5
if not commit.author_tz_offset == 0 and time.hour >= 9 and time.hour <= 17:
authorInfo["sponsoredCommitCount"] += 1
print("Analyzing commit message sentiment")
sentimentScores = []
commitMessageSentimentsPositive = []
commitMessageSentimentsNegative = []
if len(commitMessages) > 0:
sentimentScores = senti.getSentiment(commitMessages)
commitMessageSentimentsPositive = list(
result for result in filter(lambda value: value >= 1, sentimentScores)
)
commitMessageSentimentsNegative = list(
result for result in filter(lambda value: value <= -1, sentimentScores)
)
print("Analyzing authors")
sponsoredAuthorCount = 0
for login, author in authorInfoDict.items():
# check if sponsored
commitCount = int(author["commitCount"])
sponsoredCommitCount = int(author["sponsoredCommitCount"])
diff = sponsoredCommitCount / commitCount
if diff >= 0.95:
author["sponsored"] = True
sponsoredAuthorCount += 1
# calculate active days
earliestDate = author["earliestCommitDate"]
latestDate = author["latestCommitDate"]
activeDays = (latestDate - earliestDate).days + 1
author["activeDays"] = activeDays
# check if experienced
if activeDays >= experienceDays:
author["experienced"] = True
# calculate percentage sponsored authors
percentageSponsoredAuthors = sponsoredAuthorCount / len([*authorInfoDict])
# calculate active project days
firstCommitDate = None
lastCommitDate = None
if firstDate is not None:
firstCommitDate = datetime.fromtimestamp(firstDate)
if lastDate is not None:
lastCommitDate = datetime.fromtimestamp(lastDate)
daysActive = 0
if lastCommitDate is not None:
daysActive = (lastCommitDate - firstCommitDate).days
print("Outputting CSVs")
# output author days on project
with open(
os.path.join(config.metricsPath, f"authorDaysOnProject_{idx}.csv"),
"a",
newline="",
) as f:
w = csv.writer(f, delimiter=",")
w.writerow(["Author", "# of Days"])
for login, author in authorInfoDict.items():
w.writerow([login, author["activeDays"]])
# output commits per author
with open(
os.path.join(config.metricsPath, f"commitsPerAuthor_{idx}.csv"), "a", newline=""
) as f:
w = csv.writer(f, delimiter=",")
w.writerow(["Author", "Commit Count"])
for login, author in authorInfoDict.items():
w.writerow([login, author["commitCount"]])
# output timezones
with open(
os.path.join(config.metricsPath, f"timezones_{idx}.csv"), "a", newline=""
) as f:
w = csv.writer(f, delimiter=",")
w.writerow(["Timezone Offset", "Author Count", "Commit Count"])
for key, timezone in timezoneInfoDict.items():
w.writerow([key, len(timezone["authors"]), timezone["commitCount"]])
# output results
with open(
os.path.join(config.resultsPath, f"results_{idx}.csv"), "a", newline=""
) as f:
w = csv.writer(f, delimiter=",")
w.writerow(["CommitCount", realCommitCount])
w.writerow(["DaysActive", daysActive])
w.writerow(["FirstCommitDate", "{:%Y-%m-%d}".format(firstCommitDate)])
w.writerow(["LastCommitDate", "{:%Y-%m-%d}".format(lastCommitDate)])
w.writerow(["AuthorCount", len([*authorInfoDict])])
w.writerow(["SponsoredAuthorCount", sponsoredAuthorCount])
w.writerow(["PercentageSponsoredAuthors", percentageSponsoredAuthors])
w.writerow(["TimezoneCount", len([*timezoneInfoDict])])
outputStatistics(
idx,
[author["activeDays"] for login, author in authorInfoDict.items()],
"AuthorActiveDays",
config.resultsPath,
)
outputStatistics(
idx,
[author["commitCount"] for login, author in authorInfoDict.items()],
"AuthorCommitCount",
config.resultsPath,
)
outputStatistics(
idx,
[len(timezone["authors"]) for key, timezone in timezoneInfoDict.items()],
"TimezoneAuthorCount",
config.resultsPath,
)
outputStatistics(
idx,
[timezone["commitCount"] for key, timezone in timezoneInfoDict.items()],
"TimezoneCommitCount",
config.resultsPath,
)
outputStatistics(
idx,
sentimentScores,
"CommitMessageSentiment",
config.resultsPath,
)
outputStatistics(
idx,
commitMessageSentimentsPositive,
"CommitMessageSentimentsPositive",
config.resultsPath,
)
outputStatistics(
idx,
commitMessageSentimentsNegative,
"CommitMessageSentimentsNegative",
config.resultsPath,
)
return authorInfoDict, daysActive