Improvements languages indepth analysis (#329)

This commit is contained in:
Simon Lecoq
2021-05-26 22:07:09 +02:00
committed by GitHub
parent 87b5ed3f5c
commit 9c4b709f36
9 changed files with 91 additions and 76 deletions

View File

@@ -1,12 +1,12 @@
/**Indepth analyzer */
export async function indepth({login, data, imports}, {skipped}) {
export async function indepth({login, data, imports, repositories}, {skipped}) {
//Check prerequisites
if (!await imports.which("github-linguist"))
throw new Error("Feature requires github-linguist")
//Compute repositories stats from fetched repositories
const results = {total:0, stats:{}}
for (const repository of data.user.repositories.nodes) {
const results = {total:0, lines:{}, stats:{}}
for (const repository of repositories) {
//Skip repository if asked
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
console.debug(`metrics/compute/${login}/plugins > languages > skipped repository ${repository.owner.login}/${repository.name}`)
@@ -52,7 +52,7 @@ export async function recent({login, data, imports, rest, account}, {skipped}) {
//Get user recent activity
console.debug(`metrics/compute/${login}/plugins > languages > querying api`)
const commits = [], days = 14, pages = 3, results = {total:0, stats:{}}
const commits = [], days = 14, pages = 3, results = {total:0, lines:{}, stats:{}}
try {
for (let page = 1; page <= pages; page++) {
console.debug(`metrics/compute/${login}/plugins > languages > loading page ${page}`)
@@ -110,8 +110,6 @@ export async function recent({login, data, imports, rest, account}, {skipped}) {
console.debug(`metrics/compute/${login}/plugins > languages > cleaning temp dir ${path}`)
await imports.fs.rmdir(path, {recursive:true})
}
console.log(results)
return results
}
@@ -121,8 +119,6 @@ async function analyze({login, imports}, {results, path}) {
console.debug(`metrics/compute/${login}/plugins > languages > indepth > running linguist`)
const files = Object.fromEntries(Object.entries(JSON.parse(await imports.run("github-linguist --json", {cwd:path}, {log:false}))).flatMap(([lang, files]) => files.map(file => [file, lang])))
console.log(files)
//Processing diff
const per_page = 10
console.debug(`metrics/compute/${login}/plugins > languages > indepth > checking git log`)
@@ -149,9 +145,10 @@ async function analyze({login, imports}, {results, path}) {
if (!lang)
continue
//Added line marker
if (/^[+]\s(?<line>[\s\S]+)$/.test(line)) {
const size = Buffer.byteLength(line.match(/^[+]\s(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8")
if (/^[+]\s*(?<line>[\s\S]+)$/.test(line)) {
const size = Buffer.byteLength(line.match(/^[+]\s*(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8")
results.stats[lang] = (results.stats[lang] ?? 0) + size
results.lines[lang] = (results.lines[lang] ?? 0) + 1
results.total += size
}
}

View File

@@ -31,7 +31,8 @@ export default async function({login, data, imports, q, rest, account}, {enabled
console.debug(`metrics/compute/${login}/plugins > languages > custom colors ${JSON.stringify(colors)}`)
//Unique languages
const unique = new Set(data.user.repositories.nodes.flatMap(repository => repository.languages.edges.map(({node:{name}}) => name))).size
const repositories = [...data.user.repositories.nodes, ...data.user.repositoriesContributedTo.nodes]
const unique = new Set(repositories.flatMap(repository => repository.languages.edges.map(({node:{name}}) => name))).size
//Iterate through user's repositories and retrieve languages data
console.debug(`metrics/compute/${login}/plugins > languages > processing ${data.user.repositories.nodes.length} repositories`)
@@ -59,17 +60,18 @@ export default async function({login, data, imports, q, rest, account}, {enabled
//Indepth mode
if (indepth) {
console.debug(`metrics/compute/${login}/plugins > languages > switching to indepth mode (this may take some time)`)
Object.assign(languages, await indepth_analyzer({login, data, imports}, {skipped}))
Object.assign(languages, await indepth_analyzer({login, data, imports, repositories}, {skipped}))
}
//Compute languages stats
for (const {section, stats = {}, total = 0} of [{section:"favorites", stats:languages.stats, total:languages.total}, {section:"recent", ...languages["stats.recent"]}]) {
for (const {section, stats = {}, lines = {}, total = 0} of [{section:"favorites", stats:languages.stats, lines:languages.lines, total:languages.total}, {section:"recent", ...languages["stats.recent"]}]) {
console.debug(`metrics/compute/${login}/plugins > languages > computing stats ${section}`)
languages[section] = Object.entries(stats).filter(([name]) => !ignored.includes(name.toLocaleLowerCase())).sort(([_an, a], [_bn, b]) => b - a).slice(0, limit).map(([name, value]) => ({name, value, size:value, color:languages.colors[name], x:0})).filter(({value}) => value / total > threshold)
const visible = {total:Object.values(languages[section]).map(({size}) => size).reduce((a, b) => a + b, 0)}
for (let i = 0; i < languages[section].length; i++) {
languages[section][i].value /= visible.total
languages[section][i].x = (languages[section][i - 1]?.x ?? 0) + (languages[section][i - 1]?.value ?? 0)
languages[section][i].lines = lines[languages[section][i].name] ?? 0
if ((colors[i]) && (!colors[languages[section][i].name.toLocaleLowerCase()]))
languages[section][i].color = colors[i]
}

View File

@@ -73,6 +73,7 @@ inputs:
values:
- bytes-size # Languages total size written in bytes
- percentage # Languages proportions in %
- lines # Estimation of lines of code (plugin_languages_indepth must be enabled)
default: ""
example: bytes-size, percentage