@@ -5,29 +5,33 @@ import db from '../src/helpers/mysql';
5
5
// This script assumes there are no active proposals in the space
6
6
7
7
async function getFirstAndLastVote ( space : string ) {
8
- console . log ( 'Getting first and last vote for space' , space ) ;
9
- const firstVoted = await db . queryAsync (
10
- 'SELECT created FROM votes WHERE space = ? ORDER BY created ASC LIMIT 1' ,
11
- [ space ]
12
- ) ;
13
-
14
- if ( ! firstVoted . length ) throw new Error ( 'No votes found in the database' ) ;
15
- const firstVote = firstVoted [ 0 ] . created as number ;
16
-
17
- const lastVote = (
18
- await db . queryAsync ( 'SELECT created FROM votes WHERE space = ? ORDER BY created DESC LIMIT 1' , [
19
- space
20
- ] )
21
- ) [ 0 ] . created ;
8
+ const startArg = process . argv . indexOf ( '--start' ) ;
9
+ const endArg = process . argv . indexOf ( '--end' ) ;
10
+ let start = startArg !== - 1 ? parseInt ( process . argv [ startArg + 1 ] ) : null ;
11
+ let end = endArg !== - 1 ? parseInt ( process . argv [ endArg + 1 ] ) : null ;
22
12
23
- console . log (
24
- 'Will process votes from' ,
25
- new Date ( firstVote * 1000 ) ,
26
- 'to' ,
27
- new Date ( lastVote * 1000 )
28
- ) ;
13
+ console . log ( 'Getting first and last vote for space' , space ) ;
14
+ if ( ! start ) {
15
+ console . log ( 'Start date is missing' ) ;
16
+ const firstVoted = await db . queryAsync (
17
+ `SELECT created FROM votes ${ space ? 'WHERE space = ?' : '' } ORDER BY created ASC LIMIT 1` ,
18
+ space ? [ space ] : [ ]
19
+ ) ;
20
+ if ( ! firstVoted . length ) throw new Error ( 'No votes found in the database' ) ;
21
+ start = firstVoted [ 0 ] . created as number ;
22
+ }
23
+ if ( ! end ) {
24
+ end = (
25
+ await db . queryAsync (
26
+ `SELECT created FROM votes ${ space ? 'WHERE space = ?' : '' } ORDER BY created DESC LIMIT 1` ,
27
+ space ? [ space ] : [ ]
28
+ )
29
+ ) [ 0 ] . created ;
30
+ }
31
+ // @ts -ignore
32
+ console . log ( 'Will process votes from' , new Date ( start * 1000 ) , 'to' , new Date ( end * 1000 ) ) ;
29
33
30
- return { firstVote, lastVote } ;
34
+ return { firstVote : start , lastVote : end } ;
31
35
}
32
36
33
37
async function deleteLeaderboard ( space : string ) {
@@ -37,17 +41,17 @@ async function deleteLeaderboard(space: string) {
37
41
}
38
42
39
43
async function processVotes ( space : string , start : number , end : number ) {
40
- console . log ( 'Get votes from' , new Date ( start * 1000 ) , 'to' , new Date ( end * 1000 ) ) ;
44
+ console . log ( 'Get votes from' , new Date ( start * 1000 ) , start , 'to' , new Date ( end * 1000 ) , end ) ;
41
45
42
46
await db . queryAsync (
43
47
`INSERT INTO leaderboard (space, user, vote_count, last_vote)
44
48
(SELECT space, voter AS user, COUNT(*) AS vote_count, MAX(created) AS last_vote
45
49
FROM votes
46
- WHERE space = ? AND created >= ? AND created < ?
47
- GROUP BY voter)
50
+ WHERE ${ space ? 'WHERE space = ? AND' : '' } created >= ? AND created < ?
51
+ GROUP BY space, voter)
48
52
ON DUPLICATE KEY UPDATE vote_count = vote_count + VALUES(vote_count), last_vote = VALUES(last_vote)
49
53
` ,
50
- [ space , start , end ]
54
+ space ? [ space , start , end ] : [ start , end ]
51
55
) ;
52
56
}
53
57
@@ -58,17 +62,17 @@ async function processProposalsCount(space: string) {
58
62
INSERT INTO leaderboard (space, user, proposal_count)
59
63
(SELECT space, author AS user, COUNT(*) AS proposal_count
60
64
FROM proposals
61
- WHERE space = ?
62
- GROUP BY author)
65
+ ${ space ? ' WHERE space = ?' : '' }
66
+ GROUP BY space, author)
63
67
ON DUPLICATE KEY UPDATE proposal_count = proposal_count + VALUES(proposal_count)
64
68
` ,
65
- [ space ]
69
+ space ? [ space ] : [ ]
66
70
) ;
67
71
console . log ( 'Proposals count for space' , space , 'has been processed' ) ;
68
72
}
69
73
70
74
async function main ( space ) {
71
- await deleteLeaderboard ( space ) ;
75
+ if ( space ) await deleteLeaderboard ( space ) ;
72
76
const { firstVote, lastVote } = await getFirstAndLastVote ( space ) ;
73
77
74
78
// Process votes in chunks of 24 hours
@@ -77,6 +81,7 @@ async function main(space) {
77
81
const end = start + 86400 ; // 24 hours
78
82
await processVotes ( space , start , end ) ;
79
83
84
+ // @ts -ignore
80
85
if ( end > lastVote ) break ;
81
86
start = end ;
82
87
}
@@ -85,6 +90,7 @@ async function main(space) {
85
90
process . exit ( 0 ) ;
86
91
}
87
92
88
- const space = process . argv [ process . argv . indexOf ( '--space' ) + 1 ] ;
89
- if ( ! space ) throw new Error ( 'Space ID is missing' ) ;
93
+ const spaceArg = process . argv . indexOf ( '--space' ) ;
94
+ const space = spaceArg !== - 1 ? process . argv [ spaceArg + 1 ] : null ;
95
+ if ( spaceArg !== - 1 && ! space ) throw new Error ( 'Space ID is missing' ) ;
90
96
main ( space ) ;
0 commit comments