Add support for lines and traffic plugins

This commit is contained in:
lowlighter
2020-09-17 21:17:04 +02:00
parent 6710a1be6e
commit 2493e29ebc
15 changed files with 322 additions and 26 deletions

View File

@@ -3,6 +3,7 @@
import fs from "fs"
import path from "path"
import octokit from "@octokit/graphql"
import OctokitRest from "@octokit/rest"
import cache from "memory-cache"
import ratelimit from "express-rate-limit"
import metrics from "./metrics.mjs"
@@ -19,10 +20,11 @@
const settings = JSON.parse((await fs.promises.readFile(path.join("settings.json"))).toString())
const {token, maxusers = 0, restricted = [], debug = false, cached = 30*60*1000, port = 3000, ratelimiter = null, plugins = null} = settings
if (debug)
console.log(settings)
console.debug(settings)
//Load svg template, style and query
let [template, style, query] = await load()
const graphql = octokit.graphql.defaults({headers:{authorization: `token ${token}`}})
const rest = new OctokitRest.Octokit({auth:token})
//Setup server
const app = express()
@@ -51,8 +53,10 @@
//Request params
const {login} = req.params
if ((restricted.length)&&(!restricted.includes(login)))
if ((restricted.length)&&(!restricted.includes(login))) {
console.debug(`metrics/app/${login} > 403 (not in whitelisted users)`)
return res.sendStatus(403)
}
//Read cached data if possible
if ((!debug)&&(cached)&&(cache.get(login))) {
@@ -61,15 +65,17 @@
return
}
//Maximum simultaneous users
if ((maxusers)&&(cache.size()+1 > maxusers))
if ((maxusers)&&(cache.size()+1 > maxusers)) {
console.debug(`metrics/app/${login} > 503 (maximum users reached)`)
return res.sendStatus(503)
}
//Compute rendering
try {
//Render
if (debug)
[template, style, query] = await load()
const rendered = await metrics({login, q:req.query}, {template, style, query, graphql, plugins})
const rendered = await metrics({login, q:req.query}, {template, style, query, graphql, rest, plugins})
//Cache
if ((!debug)&&(cached))
cache.put(login, rendered, cached)
@@ -80,8 +86,10 @@
//Internal error
catch (error) {
//Not found user
if ((error instanceof Error)&&(/^user not found$/.test(error.message)))
if ((error instanceof Error)&&(/^user not found$/.test(error.message))) {
console.debug(`metrics/app/${login} > 404 (user not found)`)
return res.sendStatus(404)
}
//General error
console.error(error)
res.sendStatus(500)
@@ -95,6 +103,7 @@
`Restricted to users | ${restricted.size ? [...restricted].join(", ") : "(unrestricted)"}`,
`Cached time | ${cached} seconds`,
`Rate limiter | ${ratelimiter ? JSON.stringify(ratelimiter) : "(enabled)"}`,
`Max simultaneous users | ${maxusers ? `${maxusers} users` : "(unrestricted)"}`
`Max simultaneous users | ${maxusers ? `${maxusers} users` : "(unrestricted)"}`,
`Plugins enabled | ${Object.entries(plugins).filter(([key, plugin]) => plugin.enabled).map(([key]) => key).join(", ")}`
].join("\n")))
}

View File

@@ -3,11 +3,12 @@
import Plugins from "./plugins/index.mjs"
//Setup
export default async function metrics({login, q}, {template, style, query, graphql, plugins}) {
export default async function metrics({login, q}, {template, style, query, graphql, rest, plugins}) {
//Compute rendering
try {
//Query data from GitHub API
console.debug(`metrics/metrics/${login} > query`)
const data = await graphql(query
.replace(/[$]login/, `"${login}"`)
.replace(/[$]calendar.to/, `"${(new Date()).toISOString()}"`)
@@ -23,6 +24,8 @@
//Plugins
if (data.user.websiteUrl)
Plugins.pagespeed({url:data.user.websiteUrl, computed, pending, q}, plugins.pagespeed)
Plugins.lines({login, repositories:data.user.repositories.nodes.map(({name}) => name), rest, computed, pending, q}, plugins.lines)
Plugins.traffic({login, repositories:data.user.repositories.nodes.map(({name}) => name), rest, computed, pending, q}, plugins.traffic)
//Iterate through user's repositories
for (const repository of data.user.repositories.nodes) {
@@ -69,6 +72,7 @@
await Promise.all(pending)
//Eval rendering and return
console.debug(`metrics/metrics/${login} > computed`)
return eval(`\`${template}\``)
}
//Internal error

View File

@@ -1,7 +1,11 @@
//Imports
import lines from "./lines/index.mjs"
import pagespeed from "./pagespeed/index.mjs"
import traffic from "./traffic/index.mjs"
//Exports
export default {
pagespeed
lines,
pagespeed,
traffic,
}

View File

@@ -1 +1,43 @@
//Placeholder for https://docs.github.com/en/rest/reference/repos#get-all-contributor-commit-activity
//Formatter
function format(n) {
for (const {u, v} of [{u:"b", v:10**9}, {u:"m", v:10**6}, {u:"k", v:10**3}])
if (n/v >= 1)
return `${(n/v).toFixed(2).substr(0, 4).replace(/[.]0*$/, "")}${u}`
return n
}
//Setup
export default function ({login, repositories = [], rest, computed, pending, q}, {enabled = false} = {}) {
//Check if plugin is enabled and requirements are met
if (!enabled)
return computed.plugins.lines = null
if (!q.lines)
return computed.plugins.lines = null
console.debug(`metrics/plugins/lines/${login} > started`)
//Plugin execution
pending.push(new Promise(async solve => {
//Get contributors stats from repositories
const lines = {added:0, deleted:0}
const response = await Promise.all(repositories.map(async repo => await rest.repos.getContributorsStats({owner:login, repo})))
//Compute changed lines
response.map(({data:repository}) => {
//Check if data are available
if (!repository)
return
//Extract author
const [contributor] = repository.filter(({author}) => author.login === login)
//Compute editions
if (contributor)
contributor.weeks.forEach(({a, d}) => (lines.added += a, lines.deleted += d))
})
//Format values
lines.added = format(lines.added)
lines.deleted = format(lines.deleted)
//Save results
computed.plugins = {lines}
console.debug(`metrics/plugins/lines/${login} > ${JSON.stringify(computed.plugins.lines)}`)
solve()
}))
}

View File

@@ -2,14 +2,17 @@
import axios from "axios"
//Setup
export default function ({url, computed, pending, q}, {enabled = false, token = null} = {}) {
export default function ({login, url, computed, pending, q}, {enabled = false, token = null} = {}) {
//Check if plugin is enabled and requirements are met
if (!enabled)
return computed.plugins.pagespeed = null
if (!token)
return computed.plugins.pagespeed = null
if (!url)
return computed.plugins.pagespeed = null
if (!q.pagespeed)
return computed.plugins.pagespeed = null
console.debug(`metrics/plugins/pagespeed/${login} > started`)
//Plugin execution
pending.push(new Promise(async solve => {
@@ -24,6 +27,7 @@
}))
//Save results
computed.plugins.pagespeed = {url, scores:[scores.get("performance"), scores.get("accessibility"), scores.get("best-practices"), scores.get("seo")]}
console.debug(`metrics/plugins/pagespeed/${login} > ${JSON.stringify(computed.plugins.pagespeed)}`)
solve()
}))
}

View File

@@ -1 +1,33 @@
//Placeholder for https://docs.github.com/en/rest/reference/repos#get-page-views
//Formatter
function format(n) {
for (const {u, v} of [{u:"b", v:10**9}, {u:"m", v:10**6}, {u:"k", v:10**3}])
if (n/v >= 1)
return `${(n/v).toFixed(2).substr(0, 4).replace(/[.]0*$/, "")}${u}`
return n
}
//Setup
export default function ({login, repositories = [], rest, computed, pending, q}, {enabled = false} = {}) {
//Check if plugin is enabled and requirements are met
if (!enabled)
return computed.plugins.traffic = null
if (!q.traffic)
return computed.plugins.traffic = null
console.debug(`metrics/plugins/traffic/${login} > started`)
//Plugin execution
pending.push(new Promise(async solve => {
//Get views stats from repositories
const views = {count:0, uniques:0}
const response = await Promise.all(repositories.map(async repo => await rest.repos.getViews({owner:login, repo})))
//Compute views
response.filter(({data}) => data).map(({data:{count, uniques}}) => (views.count += count, views.uniques += uniques))
//Format values
views.count = format(views.count)
views.uniques = format(views.uniques)
//Save results
computed.plugins.traffic = {views}
console.debug(`metrics/plugins/traffic/${login} > ${JSON.stringify(computed.plugins.traffic)}`)
solve()
}))
}

View File

@@ -8,6 +8,7 @@ query Metrics {
repositories(last: 100, isFork: false, ownerAffiliations: OWNER) {
totalCount
nodes {
name
watchers {
totalCount
}

View File

@@ -110,6 +110,12 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M8.878.392a1.75 1.75 0 00-1.756 0l-5.25 3.045A1.75 1.75 0 001 4.951v6.098c0 .624.332 1.2.872 1.514l5.25 3.045a1.75 1.75 0 001.756 0l5.25-3.045c.54-.313.872-.89.872-1.514V4.951c0-.624-.332-1.2-.872-1.514L8.878.392zM7.875 1.69a.25.25 0 01.25 0l4.63 2.685L8 7.133 3.245 4.375l4.63-2.685zM2.5 5.677v5.372c0 .09.047.171.125.216l4.625 2.683V8.432L2.5 5.677zm6.25 8.271l4.625-2.683a.25.25 0 00.125-.216V5.677L8.75 8.432v5.516z"></path></svg>
${data.user.packages.totalCount} Package${data.user.packages.totalCount > 1 ? "s" : ""}
</div>
${computed.plugins.lines ? `
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M4.72 3.22a.75.75 0 011.06 1.06L2.06 8l3.72 3.72a.75.75 0 11-1.06 1.06L.47 8.53a.75.75 0 010-1.06l4.25-4.25zm6.56 0a.75.75 0 10-1.06 1.06L13.94 8l-3.72 3.72a.75.75 0 101.06 1.06l4.25-4.25a.75.75 0 000-1.06l-4.25-4.25z"></path></svg>
${computed.plugins.lines.added} added, ${computed.plugins.lines.deleted} removed
</div>` : ""
}
</section>
<section>
@@ -121,6 +127,12 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.679 7.932c.412-.621 1.242-1.75 2.366-2.717C5.175 4.242 6.527 3.5 8 3.5c1.473 0 2.824.742 3.955 1.715 1.124.967 1.954 2.096 2.366 2.717a.119.119 0 010 .136c-.412.621-1.242 1.75-2.366 2.717C10.825 11.758 9.473 12.5 8 12.5c-1.473 0-2.824-.742-3.955-1.715C2.92 9.818 2.09 8.69 1.679 8.068a.119.119 0 010-.136zM8 2c-1.981 0-3.67.992-4.933 2.078C1.797 5.169.88 6.423.43 7.1a1.619 1.619 0 000 1.798c.45.678 1.367 1.932 2.637 3.024C4.329 13.008 6.019 14 8 14c1.981 0 3.67-.992 4.933-2.078 1.27-1.091 2.187-2.345 2.637-3.023a1.619 1.619 0 000-1.798c-.45-.678-1.367-1.932-2.637-3.023C11.671 2.992 9.981 2 8 2zm0 8a2 2 0 100-4 2 2 0 000 4z"></path></svg>
${data.computed.repositories.watchers} Watcher${data.computed.repositories.watchers > 1 ? "s" : ""}
</div>
${computed.plugins.traffic ? `
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M0 1.75A.75.75 0 01.75 1h4.253c1.227 0 2.317.59 3 1.501A3.744 3.744 0 0111.006 1h4.245a.75.75 0 01.75.75v10.5a.75.75 0 01-.75.75h-4.507a2.25 2.25 0 00-1.591.659l-.622.621a.75.75 0 01-1.06 0l-.622-.621A2.25 2.25 0 005.258 13H.75a.75.75 0 01-.75-.75V1.75zm8.755 3a2.25 2.25 0 012.25-2.25H14.5v9h-3.757c-.71 0-1.4.201-1.992.572l.004-7.322zm-1.504 7.324l.004-5.073-.002-2.253A2.25 2.25 0 005.003 2.5H1.5v9h3.757a3.75 3.75 0 011.994.574z"></path></svg>
${computed.plugins.traffic.views.count} views in last two weeks
</div>` : ""
}
</section>
</div>
</section>

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 22 KiB