-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdatautils.js
More file actions
179 lines (155 loc) · 4.5 KB
/
datautils.js
File metadata and controls
179 lines (155 loc) · 4.5 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
/**
* 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 getConflictingClaimByPr(genericUrl) {
return db.Database.query(
`SELECT * FROM claims WHERE "pullUrl" LIKE '%${genericUrl}%';`
)
}
function getConflictingClaimByIssue(genericUrl) {
return db.Database.query(
`SELECT * FROM claims WHERE "issueUrl" LIKE '%${genericUrl}%';`
)
}
function delClaim(claimId) {
if (isNaN(+claimId)) {
return res.send('ClaimId must be a number')
}
return db.Claim.destroy({
where: {
id: claimId
}
})
}
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
})
}
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"
FROM "claims" AS "claim"
where "createdAt" between '${period.start_date}' and '${period.end_date}'
GROUP BY "user"
ORDER BY SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) DESC, COUNT("bounty") 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,
getClaimById,
updateClaim,
getCounts,
getConflictingClaimByIssue,
getConflictingClaimByPr
}