Skip to content

Commit

Permalink
Add new metrics and handle errors gracefully
Browse files Browse the repository at this point in the history
- Handle plugins errors gracefully
- Add issue comments and organization metrics
  • Loading branch information
lowlighter committed Oct 12, 2020
1 parent d744865 commit d76b7e2
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 76 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ Assuming your username is `my-github-user`, you can embed your metrics in your p
<summary>💬 Restrictions and fair use</summary>

Since GitHub API has rate limitations and to avoid abuse, the shared instance has the following limitations :
* Images are cached for 2 hours
* Images are cached for 1 hour
* Your generated metrics **won't** be updated during this amount of time
* If you enable or disable plugins by changing the url parameters, you'll need to wait before changes are applied
* You're limited to 3 requests per 2 hours
* Restriction **does not** apply to already cached users metrics, including your own
* If you enable or disable plugins in url parameters, you'll need to wait for cache expiration before these changes are applied
* A rate limiter prevents new metrics generation when reached, but it **does not** affect already cached users metrics, including your own
* Most of plugins are not available
* PageSpeed plugin can be enabled by passing `?pagespeed=1`, but metrics generation can take up some time

Expand Down
70 changes: 60 additions & 10 deletions action/dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 27 additions & 22 deletions src/plugins/lines/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}))
}

41 changes: 27 additions & 14 deletions src/plugins/pagespeed/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}))
}
38 changes: 25 additions & 13 deletions src/plugins/traffic/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}))
}
6 changes: 6 additions & 0 deletions src/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,11 @@ query Metrics {
following {
totalCount
}
issueComments {
totalCount
}
organizations {
totalCount
}
}
}
7 changes: 7 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
margin: 0 8px;
fill: #959da5;
}
.field.error {
color: #cb2431;
}
.field.error svg {
fill: #cb2431;

}

/* Displays */
.row {
Expand Down
Loading

0 comments on commit d76b7e2

Please # to comment.