-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdatautils.js
More file actions
277 lines (245 loc) · 7.51 KB
/
datautils.js
File metadata and controls
277 lines (245 loc) · 7.51 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
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
/**
* Created by championswimmer on 16/05/17.
*/
const db = require('./db')
const fs = require('fs')
const consts = require('./consts')
const {Op} = require('sequelize')
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']],
include: [
{
model: db.GithubResource,
as: 'pr',
...(options.merged
? {
where: { type: 'PULL_REQUEST', status: 'MERGED' },
required: true
}
: {})
},
{ model: db.GithubResource, as: 'issue' }
]
})
return Promise.all([distinctUsers, allClaims, distinctProjects])
}
function getClaimById(claimId) {
return db.Claim.findById(claimId, {
include: [
{ model: db.GithubResource, as: 'pr' },
{ model: db.GithubResource, as: 'issue' }
]
})
}
function delClaim(claimId) {
if (isNaN(+claimId)) {
return res.send('ClaimId must be a number')
}
return db.Claim.destroy({
where: {
id: claimId
}
})
}
function getConflictedClaims(claim,issueUrlDetail,pullUrlType) {
projectName = '/' + issueUrlDetail.project + '/'
issueId = '/' + issueUrlDetail.id
pullUrlType = projectName + pullUrlType + '/'
return db.Claim.findAll({
where : {
[Op.and] : [
{
[Op.or] : [
{ issueUrl: { [Op.like]: '%' + projectName + '%' + issueId } },
{ issueUrl: { [Op.like]: '%' + projectName + '%' + issueId + '/' } }
]
},
{ pullUrl: { [Op.like]: '%' + pullUrlType + '%' } },
{ id : { [Op.ne] : claim.id } }
]
}
})
}
function getConflictsCount(claim,issueUrlDetail,pullUrlType) {
projectName = '/' + issueUrlDetail.project + '/'
issueId = '/' + issueUrlDetail.id
pullUrlType = projectName + pullUrlType + '/'
return db.Claim.count({
where : {
[Op.and] : [
{
[Op.or] : [
{ issueUrl: { [Op.like]: '%' + projectName + '%' + issueId } },
{ issueUrl: { [Op.like]: '%' + projectName + '%' + issueId + '/' } }
]
},
{ pullUrl: { [Op.like]: '%' + pullUrlType + '%' } },
{ id : { [Op.ne] : claim.id } }
]
}
})
}
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 getGithubResource(url) {
const meta = getResourceFromUrl(url)
return db.GithubResource.findOne({
where: {
owner: meta.owner,
project: meta.repo,
type: meta.type,
resource_id: meta.id
}
})
}
async 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), () => {})
const [pr, issue] = await Promise.all([getGithubResource(pullUrl), getGithubResource(issueUrl)])
return db.Claim.create({
user,
issueUrl,
pullUrl,
repo: pullUrl.split('github.com/')[1].split('/')[1],
bounty: bounty,
pr_resource_id: pr && pr.id,
issue_resource_id: issue && issue.id,
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
}
const getResourceFromUrl = url => {
if (!url.match(/^https:\/\/github.com\/[^\/]*\/[^\/]*\/(issues|pull)\/[0-9]*\/?$/))
throw new Error('Unsupported URL provided')
const [owner, repo, type, id] = url.replace('https://github.com/', '').split('/')
return {
owner: owner.toLowerCase(),
repo: repo.toLowerCase(),
type: type === 'issues' ? 'ISSUE' : 'PULL_REQUEST',
id: Number(id)
}
}
module.exports = {
getClaims,
delClaim,
createClaim,
getLeaderboard,
getLoggedInUserStats,
getClaimById,
updateClaim,
getCounts,
getConflictedClaims,
getResourceFromUrl,
getConflictsCount
}