This repository was archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.js
54 lines (42 loc) · 1.69 KB
/
stats.js
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
// Built-in plugin to expose stats about the deployment
module.exports = async robot => {
const REFRESH_INTERVAL = 60 * 60 * 1000
// Cache of stats that get reported
const stats = {installations: 0, popular: []}
// Refresh the stats when the plugin is loaded
const initializing = refresh()
// Refresh the stats on an interval
setInterval(refresh, REFRESH_INTERVAL)
// Setup /probot/stats endpoint to return cached stats
robot.router.get('/probot/stats', async (req, res) => {
// ensure stats are loaded
await initializing
res.json(stats)
})
async function refresh () {
const installations = await getInstallations()
stats.installations = installations.length
stats.popular = await popularInstallations(installations)
}
async function getInstallations () {
const github = await robot.auth()
const req = github.apps.getInstallations({per_page: 100})
return github.paginate(req, res => res.data)
}
async function popularInstallations (installations) {
let popular = await Promise.all(installations.map(async installation => {
const github = await robot.auth(installation.id)
const req = github.apps.getInstallationRepositories({per_page: 100})
const repositories = await github.paginate(req, res => {
return res.data.repositories.filter(repository => !repository.private)
})
const account = installation.account
account.stars = repositories.reduce((stars, repository) => {
return stars + repository.stargazers_count
}, 0)
return account
}))
popular = popular.filter(installation => installation.stars > 0)
return popular.sort((a, b) => b.stars - a.stars).slice(0, 10)
}
}