Add new plugin habits

This commit is contained in:
lowlighter
2020-10-13 23:00:05 +02:00
parent e9e23d6028
commit 7ff3a7f46b
16 changed files with 221 additions and 25 deletions

View File

@@ -0,0 +1,57 @@
//Setup
export default function ({login, rest, computed, pending, q}, {enabled = false, from = 50} = {}) {
//Check if plugin is enabled and requirements are met
if (!enabled)
return computed.plugins.habits = null
if (!q.habits)
return computed.plugins.habits = null
console.debug(`metrics/plugins/habits/${login} > started`)
//Plugin execution
pending.push(new Promise(async solve => {
try {
//Initialization
const habits = {commits:{hour:NaN, hours:{}}, indents:{style:"", spaces:0, tabs:0}}
//Get user recent commits from events
const events = await rest.activity.listEventsForAuthenticatedUser({username:login, per_page:from})
const commits = events.data
.filter(({type}) => type === "PushEvent")
.filter(({actor}) => actor.login === login)
//Commit hour
{
//Compute commit hours
const hours = commits.map(({created_at}) => (new Date(created_at)).getHours())
for (const hour of hours)
habits.commits.hours[hour] = (habits.commits.hours[hour] || 0) + 1
//Compute hour with most commits
habits.commits.hour = hours.length ? Object.entries(habits.commits.hours).sort(([an, a], [bn, b]) => b - a).map(([hour, occurence]) => hour)[0] : NaN
}
//Indent style
{
//Retrieve edited files
const edited = await Promise.allSettled(commits
.flatMap(({payload}) => payload.commits).map(commit => commit.url)
.map(async commit => (await rest.request(commit)).data.files)
)
//Attemp to guess whether tabs or spaces are used from patch
edited
.filter(({status}) => status === "fulfilled")
.map(({value}) => value)
.flatMap(files => files.flatMap(file => (file.patch||"").match(/(?<=^[+])((?:\t)|(?: )) /gm)||[]))
.forEach(indent => habits.indents[/^\t/.test(indent) ? "tabs" : "spaces"]++)
//Compute indent style
habits.indents.style = habits.indents.spaces > habits.indents.tabs ? "spaces" : habits.indents.tabs > habits.indents.spaces ? "tabs" : ""
}
//Save results
computed.plugins.habits = habits
console.debug(`metrics/plugins/habits/${login} > ${JSON.stringify(computed.plugins.habits)}`)
solve()
}
catch (error) {
//Generic error
computed.plugins.habits = {error:`An error occured`}
console.debug(error)
solve()
}
}))
}

View File

@@ -1,10 +1,12 @@
//Imports
import habits from "./habits/index.mjs"
import lines from "./lines/index.mjs"
import pagespeed from "./pagespeed/index.mjs"
import traffic from "./traffic/index.mjs"
//Exports
export default {
habits,
lines,
pagespeed,
traffic,

View File

@@ -16,7 +16,7 @@
console.debug(`metrics/plugins/lines/${login} > started`)
//Plugin execution
pending.push(new Promise(async (solve, reject) => {
pending.push(new Promise(async solve => {
try {
//Get contributors stats from repositories
const lines = {added:0, deleted:0}
@@ -41,7 +41,10 @@
solve()
}
catch (error) {
reject(error)
//Generic error
computed.plugins.pagespeed = {error:`An error occured`}
console.debug(error)
solve()
}
}))
}

View File

@@ -15,7 +15,7 @@
console.debug(`metrics/plugins/pagespeed/${login} > started`)
//Plugin execution
pending.push(new Promise(async (solve, reject) => {
pending.push(new Promise(async solve => {
try {
//Format url if needed
if (!/^https?:[/][/]/.test(url))
@@ -36,11 +36,12 @@
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
return solve()
}
console.log(error)
reject(error)
//Generic error
computed.plugins.pagespeed = {error:`An error occured`}
console.debug(error)
solve()
}
}))
}

View File

@@ -16,7 +16,7 @@
console.debug(`metrics/plugins/traffic/${login} > started`)
//Plugin execution
pending.push(new Promise(async (solve, reject) => {
pending.push(new Promise(async solve => {
try {
//Get views stats from repositories
const views = {count:0, uniques:0}
@@ -36,10 +36,12 @@
if (error.status === 403) {
computed.plugins.traffic = {error:`Insufficient token rights`}
console.debug(`metrics/plugins/traffic/${login} > ${error.status}`)
solve()
return
return solve()
}
reject(error)
//Generic error
computed.plugins.traffic = {error:`An error occured`}
console.debug(error)
solve()
}
}))
}