-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathspaces.ts
228 lines (201 loc) · 6.21 KB
/
spaces.ts
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
import snapshot from '@snapshot-labs/snapshot.js';
import networks from '@snapshot-labs/snapshot.js/src/networks.json';
import { uniq } from 'lodash';
import db from './mysql';
import log from './log';
import { capture } from '@snapshot-labs/snapshot-sentry';
export let spaces = {};
export const spacesMetadata = {};
export let rankedSpaces: any = [];
const spaceProposals = {};
const spaceVotes = {};
const spaceFollowers = {};
const testnets = Object.values(networks)
.filter((network: any) => network.testnet)
.map((network: any) => network.key);
const testStrategies = [
'ticket',
'api',
'api-v2',
'api-post',
'api-v2-override'
];
function getPopularity(
id: string,
params: {
verified: boolean;
turbo: boolean;
networks: string[];
strategies: any[];
}
): number {
let popularity =
(spaceVotes[id]?.count || 0) / 100 +
(spaceVotes[id]?.count_7d || 0) +
(spaceProposals[id]?.count || 0) / 100 +
(spaceProposals[id]?.count_7d || 0) +
(spaceFollowers[id]?.count || 0) / 50 +
(spaceFollowers[id]?.count_7d || 0);
if (params.networks.some(network => testnets.includes(network)))
popularity = 1;
if (params.strategies.some(strategy => testStrategies.includes(strategy)))
popularity = 1;
if (params.verified) popularity *= 100000;
if (params.turbo) popularity *= 100000;
return popularity;
}
function mapSpaces() {
Object.entries(spaces).forEach(([id, space]: any) => {
const verified = space.verified || false;
const flagged = space.flagged || false;
const turbo = space.turbo || false;
const hibernated = space.hibernated || false;
const networks = uniq(
(space.strategies || [])
.map(strategy => strategy?.network || space.network)
.concat(space.network)
);
const strategies = uniq(
space.strategies?.map(strategy => strategy.name) || []
);
const popularity = getPopularity(id, {
verified,
turbo,
networks,
strategies
});
spacesMetadata[id] = {
id,
name: space.name,
verified,
flagged,
turbo,
hibernated,
popularity,
private: space.private ?? false,
categories: space.categories ?? [],
networks,
counts: {
activeProposals: spaceProposals[id]?.active || 0,
proposalsCount: spaceProposals[id]?.count || 0,
proposalsCount7d: spaceProposals[id]?.count_7d || 0,
followersCount: spaceFollowers[id]?.count || 0,
followersCount7d: spaceFollowers[id]?.count_7d || 0,
votesCount: spaceVotes[id]?.count || 0,
votesCount7d: spaceVotes[id]?.count_7d || 0
}
};
});
rankedSpaces = Object.values(spacesMetadata)
.filter((space: any) => !space.private && !space.flagged)
.sort((a: any, b: any) => b.popularity - a.popularity);
rankedSpaces.forEach((space: any, i: number) => {
spacesMetadata[space.id].rank = i + 1;
});
}
async function loadSpaces() {
const query =
'SELECT id, settings, flagged, verified, turbo, hibernated FROM spaces WHERE deleted = 0 ORDER BY id ASC';
const s = await db.queryAsync(query);
spaces = Object.fromEntries(
s.map(ensSpace => [
ensSpace.id,
{
...JSON.parse(ensSpace.settings),
flagged: ensSpace.flagged === 1,
verified: ensSpace.verified === 1,
turbo: ensSpace.turbo === 1,
hibernated: ensSpace.hibernated === 1
}
])
);
const totalSpaces = Object.keys(spaces).length;
log.info(`[spaces] total spaces ${totalSpaces}`);
mapSpaces();
}
async function getProposals() {
const ts = parseInt((Date.now() / 1e3).toFixed());
const query = `
SELECT space, COUNT(id) AS count,
COUNT(IF(start < ? AND end > ? AND flagged = 0, 1, NULL)) AS active,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM proposals GROUP BY space
`;
return await db.queryAsync(query, [ts, ts]);
}
async function getVotes() {
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM votes GROUP BY space
`;
return await db.queryAsync(query);
}
export async function getUniqueVotersForSpace(spaceId) {
const query = `
SELECT space, GROUP_CONCAT(DISTINCT voter) AS unique_voters
FROM votes
WHERE space = ?
GROUP BY space
`;
const [result] = await db.queryAsync(query, [spaceId]);
if (!result) return Promise.reject(new Error('NOT_FOUND'));
const votersArray = result.unique_voters ? result.unique_voters.split(',') : [];
return votersArray;
}
async function getFollowers() {
const query = `
SELECT space, COUNT(id) as count,
count(IF(created > (UNIX_TIMESTAMP() - 604800), 1, NULL)) as count_7d
FROM follows GROUP BY space
`;
return await db.queryAsync(query);
}
async function loadSpacesMetrics() {
const followersMetrics = await getFollowers();
followersMetrics.forEach(followers => {
if (spaces[followers.space]) spaceFollowers[followers.space] = followers;
});
log.info('[spaces] Followers metrics loaded');
mapSpaces();
const proposalsMetrics = await getProposals();
proposalsMetrics.forEach(proposals => {
if (spaces[proposals.space]) spaceProposals[proposals.space] = proposals;
});
log.info('[spaces] Proposals metrics loaded');
mapSpaces();
const votesMetrics = await getVotes();
votesMetrics.forEach(votes => {
if (spaces[votes.space]) spaceVotes[votes.space] = votes;
});
log.info('[spaces] Votes metrics loaded');
mapSpaces();
}
export async function getSpace(id: string) {
const query = `
SELECT settings, flagged, verified, turbo, hibernated, deleted
FROM spaces
WHERE id = ?
LIMIT 1`;
const [space] = await db.queryAsync(query, [id]);
if (!space) return Promise.reject(new Error('NOT_FOUND'));
return {
...JSON.parse(space.settings),
flagged: space.flagged === 1,
verified: space.verified === 1,
turbo: space.turbo === 1,
hibernated: space.hibernated === 1,
deleted: space.deleted === 1
};
}
export default async function run() {
try {
await loadSpaces();
await loadSpacesMetrics();
} catch (e: any) {
capture(e);
log.error(`[spaces] failed to load spaces, ${JSON.stringify(e)}`);
}
await snapshot.utils.sleep(360e3);
run();
}