-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongoloide.py
174 lines (145 loc) · 6.75 KB
/
mongoloide.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
from pymongo import MongoClient
from collections import Counter
import random
import json
import time
import datetime
import dateutil
class Mongoloide:
def __init__(self):
client = MongoClient('mongo', 27017)
db = client.test_database
self.db = client.test_database
self.comparisons = db.comparisons
self.users = db.users
self.repos = db.repos
self.scores = db.scores
def add_comparison(self, name1, name2):
compA = self.comparisons.find_one({"repo_name":name1})
compB = self.comparisons.find_one({"repo_name":name2})
if compA == None:
compA = {"repo_name":name1,
"compared_to":[name2,],
"history":[]}
comp = self.comparisons.insert_one(compA)
else:
new_values = compA["compared_to"] [name2]
compA = self.comparisons.update_one({"repo_name":name1}, {'$set': {'compared_to': new_values}})
if compB == None:
compB = {"repo_name":name2, "compared_to":[name1,], "history":[]}
comp = self.comparisons.insert_one(compB)
else:
new_values = compB["compared_to"] [name1]
compB = self.comparisons.update_one({"repo_name":name2}, {'$set': {'compared_to': new_values}})
def get_related(self, name1, name2):
try:
compA = self.comparisons.find_one({"repo_name":name1})
compB = self.comparisons.find_one({"repo_name":name2})
related = [x for x in compA['compared_to'] if x != name2] [x for x in compB['compared_to'] if x != name1]
ret = [x for (x,y) in Counter(related).most_common(3)]
return ret
except Exception:
return []
def set_score(self, name, score):
comp = self.comparisons.find_one({"repo_name":name })
new_scores = comp["history"] [(score,datetime.datetime.now())]
comp = self.comparisons.update_one({"repo_name":name }, {'$set': {'history': new_scores}})
def get_history(self, name):
comp = self.comparisons.find_one({"repo_name":name })
return [{ 'date': key.strftime("%Y-%m-%d %H:%M:%S"), name: value } for (value, key) in comp["history"]]
def get_user(self, name):
return self.users.find_one({"user_name":name})
def store_user(self, name, data):
user = self.users.find_one({"user_name":name})
for key, value in data.items():
if key != "createdAt" and key != "pushedAt":
user = self.users.update_one({"user_name":name}, {'$set': {key: value}}, upsert=True)
else:
value = int(time.mktime(dateutil.parser.parse(value).timetuple()))
user = self.users.update_one({"user_name":name}, {'$set': {key: value}}, upsert=True)
return user
def users_average(self):
avg = ([ x for x in self.users.aggregate(
[
{
"$group":
{
"_id": "null",
"avg_followers": { "$avg": "$followers" },
"avg_following": { "$avg": "$following" },
"avg_issuesOpen": { "$avg": "$issuesOpen" },
"avg_issuesClosed": { "$avg": "$issuesClosed" },
"avg_organizations": { "$avg": "$organizations" },
"avg_pinnedRepositories": { "$avg": "$pinnedRepositories" },
"avg_pullOpen": { "$avg": "$pullOpen" },
"avg_pullClosed": { "$avg": "$pullClosed" },
"avg_pullMerged": { "$avg": "$pullMerged" },
"avg_repositories": { "$avg": "$repositories" },
"avg_repositoriesContributedTo": { "$avg": "$repositoriesContributedTo" },
"avg_starredRepositories": { "$avg": "$starredRepositories" },
"avg_watching": { "$avg": "$watching" },
"avg_createdAt": { "$avg": "$createdAt" }
}
}
]
) ])[0]
return avg
def get_repo(self, name):
return self.repos.find_one({"repo_name":name})
def store_repo(self, name, data):
repo = self.repos.find_one({"repo_name":name})
for key, value in data.items():
if key != "createdAt" and key != "pushedAt":
repo = self.repos.update_one({"repo_name":name}, {'$set': {key: value}}, upsert=True)
else:
value = int(time.mktime(dateutil.parser.parse(value).timetuple()))
repo = self.repos.update_one({"repo_name":name}, {'$set': {key: value}}, upsert=True)
return repo
def repos_average(self):
avg = ([ x for x in self.repos.aggregate(
[
{
"$group":
{
"_id": "null",
"avg_stargazers": { "$avg": "$stargazers" },
"avg_watchers": { "$avg": "$watchers" },
"avg_forkCount": { "$avg": "$forkCount" },
"avg_branches": { "$avg": "$branches" },
"avg_tags": { "$avg": "$tags" },
"avg_totalCommits": { "$avg": "$totalCommits" },
"avg_deployments": { "$avg": "$deployments" },
"avg_releases": { "$avg": "$releases" },
"avg_issuesOpen": { "$avg": "$issuesOpen" },
"avg_issuesClosed": { "$avg": "$issuesClosed" },
"avg_pullOpen": { "$avg": "$pullOpen" },
"avg_pullClosed": { "$avg": "$pullClosed" },
"avg_pullMerged": { "$avg": "$pullMerged" },
"avg_mileOpen": { "$avg": "$mileOpen" },
"avg_mileClosed": { "$avg": "$mileClosed" },
"avg_createdAt": { "$avg": "$createdAt" },
"avg_pushedAt": { "$avg": "$pushedAt" }
}
}
]
)])[0]
return avg
def get_score(self, name):
return self.scores.find_one({"user_name":name})
def store_score(self, name, score):
self.set_score(name, score)
score = self.scores.update_one({"user_name":name}, {'$set': {"score": score}}, upsert=True)
return score
def scores_average(self):
avg = ([ x for x in self.scores.aggregate(
[
{
"$group":
{
"_id": "null",
"avg_score": { "$avg": "$score" },
}
}
]
)])[0]
return avg