Add new plugin habits
This commit is contained in:
57
src/plugins/habits/index.mjs
Normal file
57
src/plugins/habits/index.mjs
Normal 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()
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user