Add new metrics and handle errors gracefully

- Handle plugins errors gracefully
- Add issue comments and organization metrics
This commit is contained in:
lowlighter
2020-10-12 19:12:35 +02:00
parent d744865b02
commit d76b7e29ff
9 changed files with 205 additions and 76 deletions

View File

@@ -16,28 +16,33 @@
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 (!Array.isArray(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 = {...lines}
console.debug(`metrics/plugins/lines/${login} > ${JSON.stringify(computed.plugins.lines)}`)
solve()
pending.push(new Promise(async (solve, reject) => {
try {
//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 (!Array.isArray(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 = {...lines}
console.debug(`metrics/plugins/lines/${login} > ${JSON.stringify(computed.plugins.lines)}`)
solve()
}
catch (error) {
reject(error)
}
}))
}

View File

@@ -15,19 +15,32 @@
console.debug(`metrics/plugins/pagespeed/${login} > started`)
//Plugin execution
pending.push(new Promise(async solve => {
//Format url if needed
if (!/^https?:[/][/]/.test(url))
url = `https://${url}`
//Load scores from API
const scores = new Map()
await Promise.all(["performance", "accessibility", "best-practices", "seo"].map(async category => {
const {score, title} = (await axios.get(`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?category=${category}&url=${url}&key=${token}`)).data.lighthouseResult.categories[category]
scores.set(category, {score, title})
}))
//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()
pending.push(new Promise(async (solve, reject) => {
try {
//Format url if needed
if (!/^https?:[/][/]/.test(url))
url = `https://${url}`
//Load scores from API
const scores = new Map()
await Promise.all(["performance", "accessibility", "best-practices", "seo"].map(async category => {
const {score, title} = (await axios.get(`https://www.googleapis.com/pagespeedonline/v5/runPagespeed?category=${category}&url=${url}&key=${token}`)).data.lighthouseResult.categories[category]
scores.set(category, {score, title})
}))
//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()
}
catch (error) {
//Thrown when token is incorrect
if ((error.response)&&(error.response.status)) {
computed.plugins.pagespeed = {url, error:`PageSpeed token error (code ${error.response.status})`}
console.debug(`metrics/plugins/traffic/${login} > ${error.response.status}`)
solve()
return
}
console.log(error)
reject(error)
}
}))
}

View File

@@ -16,18 +16,30 @@
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()
pending.push(new Promise(async (solve, reject) => {
try {
//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()
}
catch (error) {
//Thrown when token has unsufficient permissions
if (error.status === 403) {
computed.plugins.traffic = {error:`Insufficient token rights`}
console.debug(`metrics/plugins/traffic/${login} > ${error.status}`)
solve()
return
}
reject(error)
}
}))
}