add dashboard
This commit is contained in:
@@ -53,7 +53,9 @@ function scriptFor(root) {
|
||||
return null
|
||||
}
|
||||
|
||||
function available() { return !!scriptFor(roots[0]) }
|
||||
// sizes can always be measured; cleanup candidates need cleanup.ps1
|
||||
function available() { return true }
|
||||
const cleanupAvailable = () => !!scriptFor(roots[0])
|
||||
|
||||
function psArgs(root, extra) {
|
||||
return ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', scriptFor(root), '-Root', root, ...extra, '-Json']
|
||||
@@ -66,25 +68,72 @@ function parseJson(stdout) {
|
||||
return JSON.parse(text.slice(start))
|
||||
}
|
||||
|
||||
async function dirSizeMB(dir) {
|
||||
async function dirBytes(dir) {
|
||||
// robocopy /L only lists; fast and copes with long node_modules paths
|
||||
const r = await run('robocopy', [dir, 'C:\\__nul__', '/L', '/S', '/NJH', '/BYTES', '/FP', '/NC', '/NDL', '/NFL', '/XJ', '/R:0', '/W:0'], { timeout: 10 * 60 * 1000 })
|
||||
const line = r.stdout.split(/\r?\n/).find((l) => /^\s*Bytes\s*:/.test(l))
|
||||
const m = line && line.match(/:\s*(\d+)/)
|
||||
return m ? Math.round(+m[1] / 1048576) : null
|
||||
return m ? +m[1] : null
|
||||
}
|
||||
|
||||
const MB = (b) => Math.round((b / 1048576) * 10) / 10
|
||||
|
||||
// Size of a folder split by its direct subfolders (one robocopy run each, so the
|
||||
// total costs the same as one run over the whole folder). Files directly in the
|
||||
// folder are added up as "(Dateien)". -> { bytes, children: [{ name, mb, bytes }] }
|
||||
async function folderSplit(dir) {
|
||||
let entries = []
|
||||
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { return { bytes: 0, children: [] } }
|
||||
let fileBytes = 0
|
||||
const dirs = []
|
||||
for (const e of entries) {
|
||||
const full = path.join(dir, e.name)
|
||||
if (e.isFile()) { try { fileBytes += fs.statSync(full).size } catch {} }
|
||||
else if (e.isDirectory() && !e.isSymbolicLink()) {
|
||||
// junctions look like directories: skip them like robocopy /XJ does
|
||||
try { if (fs.lstatSync(full).isSymbolicLink()) continue } catch { continue }
|
||||
dirs.push(e.name)
|
||||
}
|
||||
}
|
||||
const children = []
|
||||
await pool(dirs, 4, async (name) => {
|
||||
const b = await dirBytes(path.join(dir, name))
|
||||
if (b) children.push({ name, bytes: b })
|
||||
})
|
||||
if (fileBytes) children.push({ name: '(Dateien)', bytes: fileBytes, files: true })
|
||||
children.sort((a, b) => b.bytes - a.bytes)
|
||||
return { bytes: children.reduce((s, c) => s + c.bytes, 0), children }
|
||||
}
|
||||
|
||||
// Tree for the treemap: subfolders of the project, the biggest ones (>= 5 % and
|
||||
// >= 50 MB, at most 3) split once more. Tiny entries are merged.
|
||||
async function sizeTree(dir) {
|
||||
const top = await folderSplit(dir)
|
||||
const big = top.children.filter((c) => !c.files && c.bytes >= top.bytes * 0.05 && c.bytes >= 50 * 1048576).slice(0, 3)
|
||||
for (const c of big) c.children = (await folderSplit(path.join(dir, c.name))).children
|
||||
const tidy = (list, total) => {
|
||||
const keep = [], small = { name: '(kleinere)', bytes: 0, merged: 0 }
|
||||
for (const c of list) {
|
||||
if (c.bytes < total * 0.002 && list.length > 12) { small.bytes += c.bytes; small.merged++ }
|
||||
else keep.push({ name: c.name, mb: MB(c.bytes), ...(c.files ? { files: true } : {}), ...(c.children ? { children: tidy(c.children, c.bytes) } : {}) })
|
||||
}
|
||||
if (small.merged) keep.push({ name: `(${small.merged} kleinere)`, mb: MB(small.bytes), merged: true })
|
||||
return keep.slice(0, 60)
|
||||
}
|
||||
return { bytes: top.bytes, tree: tidy(top.children, top.bytes) }
|
||||
}
|
||||
|
||||
async function scanProject(root, folder) {
|
||||
const dir = path.join(root, folder)
|
||||
const [sizeMB, cand] = await Promise.all([
|
||||
dirSizeMB(dir),
|
||||
const [split, cand] = await Promise.all([
|
||||
sizeTree(dir),
|
||||
scriptFor(root) ? run('powershell.exe', psArgs(root, ['-Project', folder, '-Tier', ALL_TIERS]), { timeout: 10 * 60 * 1000 }) : null,
|
||||
])
|
||||
const candidates = cand ? parseJson(cand.stdout) : []
|
||||
const deletable = { Cache: 0, Release: 0, Output: 0 }
|
||||
for (const c of candidates) if (!c.Skip) deletable[c.Tier] += c.MB
|
||||
const d = (all[rootKey(root)] ??= {})
|
||||
d[folder] = { at: new Date().toISOString(), sizeMB, candidates, deletable }
|
||||
d[folder] = { at: new Date().toISOString(), sizeMB: Math.round(split.bytes / 1048576), candidates, deletable, tree: split.tree }
|
||||
save()
|
||||
return d[folder]
|
||||
}
|
||||
@@ -118,6 +167,18 @@ async function clean(root, folder, paths) {
|
||||
|
||||
function get(root, folder) { return all[rootKey(root)]?.[folder] ?? null }
|
||||
|
||||
// A project was renamed/moved: keep its scan result. Cleanup paths are
|
||||
// "<folder>\..." relative to the projects folder, so they get the new name.
|
||||
function rename(oldRoot, oldFolder, newRoot, newFolder) {
|
||||
const d = get(oldRoot, oldFolder)
|
||||
if (!d) return
|
||||
delete all[rootKey(oldRoot)][oldFolder]
|
||||
const prefix = oldFolder + path.sep
|
||||
d.candidates = (d.candidates ?? []).map((c) => (typeof c.Path === 'string' && c.Path.startsWith(prefix) ? { ...c, Path: newFolder + path.sep + c.Path.slice(prefix.length) } : c))
|
||||
;(all[rootKey(newRoot)] ??= {})[newFolder] = d
|
||||
save()
|
||||
}
|
||||
|
||||
// items: [{ root, folder }]
|
||||
function totals(items) {
|
||||
let sizeMB = 0, cacheMB = 0, allMB = 0, scanned = 0
|
||||
@@ -132,4 +193,4 @@ function totals(items) {
|
||||
return { sizeMB, cacheMB, allMB, scanned }
|
||||
}
|
||||
|
||||
module.exports = { init, startScan, clean, get, totals, state, available, busy }
|
||||
module.exports = { init, startScan, clean, get, rename, totals, state, available, cleanupAvailable, busy }
|
||||
|
||||
Reference in New Issue
Block a user