Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: use in memory caching to improve endpoint performance #133

Merged
merged 3 commits into from
Mar 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/serverless-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ async function getInstallations(app) {
)
}

const accountsCache = {}

async function popularInstallations({ app, installations }) {
let popular = await Promise.all(
installations.map(async installation => {
if (accountsCache[installation.id]) {
return accountsCache[installation.id]
}

const { account } = installation

const github = await app.auth(installation.id)
Expand All @@ -42,6 +48,8 @@ async function popularInstallations({ app, installations }) {
return stars + repository.stargazers_count
}, 0)

accountsCache[installation.id] = account

return account
}),
)
Expand All @@ -50,6 +58,8 @@ async function popularInstallations({ app, installations }) {
return popular.sort((a, b) => b.stars - a.stars).slice(0, 10)
}

let installationCache

async function getStats(probot) {
const app = probot.apps[0]

Expand All @@ -59,10 +69,17 @@ async function getStats(probot) {
)
}

const installations = await getInstallations(app)
const popular = await popularInstallations({ app, installations })
if (!installationCache) {
const installationsNew = await getInstallations(app)
installationCache = installationsNew
}

const popular = await popularInstallations({
app,
installations: installationCache,
})
return {
installations: installations.length,
installations: installationCache.length,
popular,
}
}
Expand Down