Skip to content

Commit d146bbc

Browse files
authored
feat(plugins/languages): various improvements (lowlighter#985)
1 parent f2c56da commit d146bbc

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

source/plugins/languages/analyzers.mjs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//Imports
12
import linguist from "linguist-js"
23

34
/**Indepth analyzer */
@@ -29,12 +30,12 @@ export async function indepth({login, data, imports, repositories, gpg}, {skippe
2930
finally {
3031
//Cleaning
3132
console.debug(`metrics/compute/${login}/plugins > languages > indepth > cleaning ${path}`)
32-
await imports.fs.rm(path, {recursive:true, force:true})
33+
await imports.fs.rm(path, {recursive:true, force:true}).catch(error => console.debug(`metrics/compute/${login}/plugins > languages > indepth > failed to clean ${path} (${error})`))
3334
}
3435
}
3536

3637
//Compute repositories stats from fetched repositories
37-
const results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:0, verified:{signature:0}}
38+
const results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:{lines:0, bytes:0, commits:0}, verified:{signature:0}}
3839
for (const repository of repositories) {
3940
//Skip repository if asked
4041
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
@@ -67,7 +68,7 @@ export async function indepth({login, data, imports, repositories, gpg}, {skippe
6768
finally {
6869
//Cleaning
6970
console.debug(`metrics/compute/${login}/plugins > languages > indepth > cleaning temp dir ${path}`)
70-
await imports.fs.rm(path, {recursive:true, force:true})
71+
await imports.fs.rm(path, {recursive:true, force:true}).catch(error => console.debug(`metrics/compute/${login}/plugins > languages > indepth > failed to clean ${path} (${error})`))
7172
}
7273
}
7374
solve(results)
@@ -85,7 +86,7 @@ export async function recent({login, data, imports, rest, account}, {skipped = [
8586

8687
//Get user recent activity
8788
console.debug(`metrics/compute/${login}/plugins > languages > querying api`)
88-
const commits = [], pages = Math.ceil(load / 100), results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:0, days}
89+
const commits = [], pages = Math.ceil(load / 100), results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:{lines:0, bytes:0, commits:0}, days}
8990
try {
9091
for (let page = 1; page <= pages; page++) {
9192
console.debug(`metrics/compute/${login}/plugins > languages > loading page ${page}`)
@@ -134,7 +135,7 @@ export async function recent({login, data, imports, rest, account}, {skipped = [
134135
await imports.fs.mkdir(path, {recursive:true})
135136
await Promise.all(patches.map(async ({name, directory, patch}) => {
136137
await imports.fs.mkdir(imports.paths.join(path, directory), {recursive:true})
137-
imports.fs.writeFile(imports.paths.join(path, directory, name), patch)
138+
await imports.fs.writeFile(imports.paths.join(path, directory, name), patch)
138139
}))
139140

140141
//Process temporary repositories
@@ -170,7 +171,7 @@ export async function recent({login, data, imports, rest, account}, {skipped = [
170171
finally {
171172
//Cleaning
172173
console.debug(`metrics/compute/${login}/plugins > languages > cleaning temp dir ${path}`)
173-
await imports.fs.rm(path, {recursive:true, force:true})
174+
await imports.fs.rm(path, {recursive:true, force:true}).catch(error => console.debug(`metrics/compute/${login}/plugins > languages > indepth > failed to clean ${path} (${error})`))
174175
}
175176
solve(results)
176177
})
@@ -199,7 +200,7 @@ async function analyze({login, imports, data}, {results, path, categories = ["pr
199200
try {
200201
console.debug(`metrics/compute/${login}/plugins > languages > indepth > processing commits ${page * per_page} from ${(page + 1) * per_page}`)
201202
let empty = true, file = null, lang = null
202-
await imports.spawn("git", ["log", ...data.shared["commits.authoring"].map(authoring => `--author="${authoring}"`), "--regexp-ignore-case", "--format=short", "--patch", `--max-count=${per_page}`, `--skip=${page * per_page}`], {cwd:path}, {
203+
await imports.spawn("git", ["log", ...data.shared["commits.authoring"].map(authoring => `--author="${authoring}"`), "--regexp-ignore-case", "--format=short", "--no-merges", "--patch", `--max-count=${per_page}`, `--skip=${page * per_page}`], {cwd:path}, {
203204
stdout(line) {
204205
try {
205206
//Unflag empty output
@@ -226,8 +227,8 @@ async function analyze({login, imports, data}, {results, path, categories = ["pr
226227
//File marker
227228
if (/^[+]{3}\sb[/](?<file>[\s\S]+)$/.test(line)) {
228229
file = `${path}/${line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file}`.replace(/\\/g, "/")
229-
lang = files[file] ?? null
230-
if ((lang) && (!categories.includes(languageResults[lang].type)))
230+
lang = files[file] ?? "<unknown>"
231+
if ((lang) && (lang !== "<unknown>") && (!categories.includes(languageResults[lang].type)))
231232
lang = null
232233
edited.add(file)
233234
return
@@ -238,9 +239,15 @@ async function analyze({login, imports, data}, {results, path, categories = ["pr
238239
//Added line marker
239240
if (/^[+]\s*(?<line>[\s\S]+)$/.test(line)) {
240241
const size = Buffer.byteLength(line.match(/^[+]\s*(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8")
241-
results.stats[lang] = (results.stats[lang] ?? 0) + size
242-
results.lines[lang] = (results.lines[lang] ?? 0) + 1
243242
results.total += size
243+
if (lang === "<unknown>") {
244+
results.missed.lines++
245+
results.missed.bytes += size
246+
}
247+
else {
248+
results.stats[lang] = (results.stats[lang] ?? 0) + size
249+
results.lines[lang] = (results.lines[lang] ?? 0) + 1
250+
}
244251
}
245252
}
246253
catch (error) {
@@ -255,7 +262,7 @@ async function analyze({login, imports, data}, {results, path, categories = ["pr
255262
}
256263
catch {
257264
console.debug(`metrics/compute/${login}/plugins > languages > indepth > an error occured on page ${page}, skipping...`)
258-
results.missed += per_page
265+
results.missed.commits += per_page
259266
}
260267
}
261268
await Promise.allSettled(pending)
@@ -278,7 +285,7 @@ if (/languages.analyzers.mjs$/.test(process.argv[1])) {
278285

279286
//Prepare call
280287
const imports = await import("../../app/metrics/utils.mjs")
281-
const results = {total:0, lines:{}, colors:{}, stats:{}, missed:0}
288+
const results = {total:0, lines:{}, colors:{}, stats:{}, missed:{lines:0, bytes:0, commits:0}}
282289
console.debug = log => /exited with code null/.test(log) ? console.error(log.replace(/^.*--max-count=(?<step>\d+) --skip=(?<start>\d+).*$/, (_, step, start) => `error: skipped commits ${start} from ${Number(start) + Number(step)}`)) : null
283290

284291
//Analyze repository

source/plugins/languages/index.mjs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export default async function({login, data, imports, q, rest, account}, {enabled
1212
//Context
1313
let context = {mode:"user"}
1414
if (q.repo) {
15-
console.debug(`metrics/compute/${login}/plugins > activity > switched to repository mode`)
15+
console.debug(`metrics/compute/${login}/plugins > languages > switched to repository mode`)
1616
context = {...context, mode:"repository"}
1717
}
1818

1919
//Load inputs
20-
let {ignored, skipped, colors, aliases, details, threshold, limit, indepth, "analysis.timeout":timeout, sections, categories, "recent.categories":_recent_categories, "recent.load":_recent_load, "recent.days":_recent_days} = imports.metadata.plugins.languages.inputs({
20+
let {ignored, skipped, other, colors, aliases, details, threshold, limit, indepth, "analysis.timeout":timeout, sections, categories, "recent.categories":_recent_categories, "recent.load":_recent_load, "recent.days":_recent_days} = imports.metadata.plugins.languages.inputs({
2121
data,
2222
account,
2323
q,
@@ -102,7 +102,7 @@ export default async function({login, data, imports, q, rest, account}, {enabled
102102
const existingColors = languages.colors
103103
Object.assign(languages, await indepth_analyzer({login, data, imports, repositories, gpg}, {skipped, categories, timeout}))
104104
Object.assign(languages.colors, existingColors)
105-
console.debug(`metrics/compute/${login}/plugins > languages > indepth analysis missed ${languages.missed} commits`)
105+
console.debug(`metrics/compute/${login}/plugins > languages > indepth analysis missed ${languages.missed.commits} commits`)
106106
}
107107
catch (error) {
108108
console.debug(`metrics/compute/${login}/plugins > languages > ${error}`)
@@ -125,10 +125,20 @@ export default async function({login, data, imports, q, rest, account}, {enabled
125125
}
126126

127127
//Compute languages stats
128-
for (const {section, stats = {}, lines = {}, total = 0} of [{section:"favorites", stats:languages.stats, lines:languages.lines, total:languages.total}, {section:"recent", ...languages["stats.recent"]}]) {
128+
for (const {section, stats = {}, lines = {}, missed = {bytes:0}, total = 0} of [{section:"favorites", stats:languages.stats, lines:languages.lines, total:languages.total, missed:languages.missed}, {section:"recent", ...languages["stats.recent"]}]) {
129129
console.debug(`metrics/compute/${login}/plugins > languages > computing stats ${section}`)
130-
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
131-
)
130+
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)
131+
if (other) {
132+
let value = indepth ? missed.bytes : Object.entries(stats).filter(([name]) => !Object.values(languages[section]).map(({name}) => name).includes(name)).reduce((a, [_, b]) => a + b, 0)
133+
if (value) {
134+
if (languages[section].length === limit) {
135+
const {size} = languages[section].pop()
136+
value += size
137+
}
138+
//dprint-ignore-next-line
139+
languages[section].push({name:"Other", value, size:value, get lines() { return missed.lines }, set lines(_) { }, x:0}) //eslint-disable-line brace-style, no-empty-function, max-statements-per-line
140+
}
141+
}
132142
const visible = {total:Object.values(languages[section]).map(({size}) => size).reduce((a, b) => a + b, 0)}
133143
for (let i = 0; i < languages[section].length; i++) {
134144
const {name} = languages[section][i]

source/plugins/languages/metadata.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ inputs:
4848
type: string
4949
default: 0%
5050

51+
plugin_languages_other:
52+
description: |
53+
Group unknown, ignored and over-limit languages into a single "Other" category
54+
55+
If this option is enabled, "Other" category will not be subject to `plugin_languages_threshold`.
56+
It will be automatically hidden if empty.
57+
type: boolean
58+
default: no
59+
5160
plugin_languages_colors:
5261
description: Custom languages colors
5362
type: array

source/templates/classic/partials/languages.ejs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,32 @@
2020
<% } else { const width = 460 * (1 + large) %>
2121
<% if (section === "recently-used") { %>
2222
<small>
23-
estimation from <%= plugins.languages["stats.recent"]?.files %> edited file<%= s(plugins.languages["stats.recent"]?.files) %> from <%= plugins.languages["stats.recent"]?.commits %> commit<%= s(plugins.languages["stats.recent"]?.commits) %> over last <%= plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days %> day<%= s(plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days) %>
23+
<% if (languages.length) { %>
24+
estimation from <%= f(plugins.languages["stats.recent"]?.total) %>b of code in <%= plugins.languages["stats.recent"]?.files %> edited file<%= s(plugins.languages["stats.recent"]?.files) %> across <%= plugins.languages["stats.recent"]?.commits %> commit<%= s(plugins.languages["stats.recent"]?.commits) %> over last <%= plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days %> day<%= s(plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days) %>
25+
<% } else { %>
26+
No recent push activity found over last <%= plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days %> day<%= s(plugins.languages["stats.recent"]?.latest ?? plugins.languages["stats.recent"]?.days) %>
27+
<% } %>
2428
</small>
2529
<% } else if ((section === "most-used")&&(plugins.languages.indepth)) { %>
2630
<small>
27-
estimation from <%= plugins.languages.files %> edited file<%= s(plugins.languages.files) %> from <%= plugins.languages.commits %> commit<%= s(plugins.languages.commits) %>
31+
<% if (languages.length) { %>
32+
estimation from <%= f(plugins.languages.total) %>b of code in <%= plugins.languages.files %> edited file<%= s(plugins.languages.files) %> across <%= plugins.languages.commits %> commit<%= s(plugins.languages.commits) %>
33+
<% } else { %>
34+
No push activity found
35+
<% } %>
2836
</small>
2937
<% } %>
30-
<svg class="bar" xmlns="http://www.w3.org/2000/svg" width="<%= width %>" height="8">
31-
<mask id="languages-bar">
32-
<rect x="0" y="0" width="<%= width %>" height="8" fill="white" rx="5"/>
33-
</mask>
34-
<rect mask="url(#languages-bar)" x="0" y="0" width="<%= languages.length ? 0 : width %>" height="8" fill="#d1d5da"/>
35-
<% for (const {name, value, color, x} of languages) { %>
36-
<rect mask="url(#languages-bar)" x="<%= x*width %>" y="0" width="<%= value*width %>" height="8" fill="<%= color ?? "#959DA5" %>"/>
37-
<% } %>
38-
</svg>
38+
<% if (languages.length) { %>
39+
<svg class="bar" xmlns="http://www.w3.org/2000/svg" width="<%= width %>" height="8">
40+
<mask id="languages-bar">
41+
<rect x="0" y="0" width="<%= width %>" height="8" fill="white" rx="5"/>
42+
</mask>
43+
<rect mask="url(#languages-bar)" x="0" y="0" width="<%= languages.length ? 0 : width %>" height="8" fill="#d1d5da"/>
44+
<% for (const {name, value, color, x} of languages) { %>
45+
<rect mask="url(#languages-bar)" x="<%= x*width %>" y="0" width="<%= value*width %>" height="8" fill="<%= color ?? "#959DA5" %>"/>
46+
<% } %>
47+
</svg>
48+
<% } %>
3949
<% if (plugins.languages.details.length) { const rows = large ? [0, 1, 2, 3] : (plugins.languages.details.length > 2) ? [0] : [0, 1] %>
4050
<div class="row fill-width">
4151
<% for (const row of rows) { %>

0 commit comments

Comments
 (0)