forked from coding-blocks/boss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatautils.js
More file actions
241 lines (213 loc) · 6.41 KB
/
Copy pathdatautils.js
File metadata and controls
241 lines (213 loc) · 6.41 KB
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
/**
* Created by championswimmer on 16/05/17.
*/
const db = require('./db')
const fs = require('fs')
const consts = require('./consts')
function getContestPeriod(year) {
if (year)
return {
start_date: consts[`BOSS_${year}_START_DATE`].toISOString(),
end_date: consts[`BOSS_${year}_END_DATE`].toISOString()
}
return {
start_date: consts.BOSS_START_DATE.toISOString(),
end_date: consts.BOSS_END_DATE.toISOString()
}
}
function getClaims(options) {
const offset = (options.page - 1) * options.size
const period = getContestPeriod()
const baseClause = { status: options.status, createdAt: { $between: [period.start_date, period.end_date] } }
const whereClause = { ...baseClause }
if (options.username) {
whereClause.user = options.username
} else if (options.projectname) {
whereClause.repo = options.projectname
} else if (options.minbounty && options.maxbounty) {
whereClause.bounty = { $between: [options.minbounty, options.maxbounty] }
}
const distinctUsers = db.Claim.aggregate('user', 'DISTINCT', { plain: false, where: baseClause })
const distinctProjects = db.Claim.aggregate('repo', 'DISTINCT', { plain: false, where: baseClause })
const allClaims = db.Claim.findAndCountAll({
limit: options.size,
offset: offset,
where: whereClause,
order: [['updatedAt', 'DESC']]
})
return Promise.all([distinctUsers, allClaims, distinctProjects])
}
function getClaimById(claimId) {
return db.Claim.findById(claimId)
}
function delClaim(claimId) {
if (isNaN(+claimId)) {
return res.send('ClaimId must be a number')
}
return db.Claim.destroy({
where: {
id: claimId
}
})
}
async function getConflictsReport(claim){
try{
const issues = await db.Database.query(`
SELECT *
FROM "claims"
WHERE ( "issueUrl"='${claim.issueUrl}' OR
"pullUrl"='${claim.issueUrl}' )
AND "id" != ${claim.id}
`)
const pulls = await db.Database.query(`
SELECT *
FROM "claims"
WHERE ("issueUrl"='${claim.pullUrl}')
AND "id" != ${claim.id}
`)
const both = await db.Database.query(`
SELECT *
FROM "claims"
WHERE (( "issueUrl"='${claim.issueUrl}' AND
"pullUrl"='${claim.pullUrl}' )
OR ( "issueUrl"='${claim.pullUrl}' AND
"pullUrl"='${claim.issueUrl}' ))
AND "id" != ${claim.id}
`)
// if both urls same
const object = {
both: both[0]
}
if(claim.issueUrl === claim.pullUrl){
// if url is of an issue
if(claim.issueUrl.includes("/issues/")){
object.issue = issues[0]
object.pulls = []
}else{
object.issue = []
object.pulls = pulls[0]
}
}
else{
object.issue = issues[0]
object.pulls = pulls[0]
}
return object
}catch(e){
console.log(e.message);
return {
issue: [],
pulls: [],
both: []
}
}
}
function updateClaim(claimId, { status, reason, bounty }) {
const claim = {
action: 'update',
claimId,
status,
bounty
}
fs.writeFile(__dirname + '/../audit/' + new Date().toISOString() + '.json', JSON.stringify(claim), () => {})
return db.Claim.update(
{
status: status,
reason: reason,
bounty: bounty
},
{
where: {
id: claimId
},
returning: true
}
)
}
function createClaim(user, issueUrl, pullUrl, bounty, status) {
const claim = {
action: 'create',
user,
issueUrl,
pullUrl,
bounty,
status
}
fs.writeFile(__dirname + '/../audit/' + new Date().toISOString() + '.json', JSON.stringify(claim), () => {})
return db.Claim.create({
user,
issueUrl,
pullUrl,
repo: pullUrl.split('github.com/')[1].split('/')[1],
bounty: bounty,
status: status
})
}
async function getLoggedInUserStats(options = {}, username) {
const period = getContestPeriod(options.year)
const result = await db.Database.query(`with RankTable as (
SELECT "user",
SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) as "bounty",
COUNT("bounty") as "pulls",
ROW_NUMBER() OVER(ORDER BY SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) DESC, COUNT("bounty") DESC) as rank
FROM "claims" AS "claim"
where "createdAt" between '${period.start_date}' and '${period.end_date}'
GROUP BY "user"
ORDER BY "bounty" DESC, "pulls" DESC
)
SELECT RankTable.* from RankTable where RankTable.user = '${username}'`)
return result
}
function getLeaderboard(options = {}) {
options.size = parseInt(options.size || 0)
const offset = (options.page - 1) * options.size
const period = getContestPeriod(options.year)
const userCount = db.Claim.aggregate('user', 'count', {
distinct: true,
where: {
createdAt: {
$between: [period.start_date, period.end_date]
}
}
})
const results = db.Database.query(`SELECT "user",
SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) as "bounty",
COUNT("bounty") as "pulls",
ROW_NUMBER() OVER(ORDER BY SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) DESC, COUNT("bounty") DESC) as rank
FROM "claims" AS "claim"
where "createdAt" between '${period.start_date}' and '${period.end_date}'
GROUP BY "user"
ORDER BY "bounty" DESC, "pulls" DESC
LIMIT ${options.size} OFFSET ${offset}`)
return Promise.all([userCount, results])
}
function getCounts() {
const where = {
createdAt: {
$between: [getContestPeriod().start_date, getContestPeriod().end_date]
}
}
const participants = db.Claim.aggregate('user', 'count', { distinct: true, where })
const claims = db.Claim.aggregate('*', 'count', { where })
var accepted = db.Claim.aggregate('bounty', 'sum', {
where: {
status: 'accepted',
...where
}
})
var totalclaimed = db.Claim.aggregate('bounty', 'sum', { where })
var filterNaN = data => data || 0
var counts = Promise.all([participants, claims, accepted, totalclaimed]).then(values => values.map(filterNaN))
return counts
}
module.exports = {
getClaims,
delClaim,
createClaim,
getLeaderboard,
getLoggedInUserStats,
getClaimById,
updateClaim,
getCounts,
getConflictsReport
}