add dashboard
This commit is contained in:
@@ -5,17 +5,27 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const SKIP = /^(node_modules|\.git|\.gradle|\.gradle-local|Library|Temp|Logs|obj|bin|build|builds|dist|out|release|publish|\.venv.*|venv|env|__pycache__|\.cache|\.expo|\.next|\.idea|\.vs|\.vscode|output|CMakeFiles|_deps|\.dart_tool|\.cxx|\.externalNativeBuild|cmake-build-.*)$/i
|
||||
const SKIP = /^(node_modules|\.git|\.gradle|\.gradle-local|Library|Temp|Logs|obj|bin|build|builds|dist|out|release|releases|publish|\.venv.*|venv|env|__pycache__|\.cache|\.expo|\.next|\.idea|\.vs|\.vscode|output|CMakeFiles|_deps|\.dart_tool|\.cxx|\.externalNativeBuild|cmake-build-.*)$/i
|
||||
const IGNORE_FILES = /^(\.dashboard\.json|thumbs\.db|desktop\.ini|\.ds_store)$/i
|
||||
const MAX_ENTRIES = 30000
|
||||
const MAX_DEPTH = 10
|
||||
const TTL_MS = 5 * 60 * 1000
|
||||
const cache = new Map() // projectPath -> { value, at }
|
||||
|
||||
// local calendar day "YYYY-MM-DD" (the heatmap shows the user's days, not UTC)
|
||||
const dayOf = (ms) => {
|
||||
const d = new Date(ms)
|
||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
||||
}
|
||||
const YEAR_MS = 371 * 86400000
|
||||
|
||||
function scan(projectPath) {
|
||||
let best = 0
|
||||
let bestFile = ''
|
||||
let seen = 0
|
||||
// files changed per day within the last year (each file counts on the day of its last change)
|
||||
const days = {}
|
||||
const since = Date.now() - YEAR_MS
|
||||
const stack = [{ dir: projectPath, depth: 0 }]
|
||||
while (stack.length) {
|
||||
const { dir, depth } = stack.pop()
|
||||
@@ -30,15 +40,37 @@ function scan(projectPath) {
|
||||
let m = 0
|
||||
try { m = fs.statSync(full).mtimeMs } catch { continue }
|
||||
if (m > best) { best = m; bestFile = full }
|
||||
if (m >= since && m <= Date.now() + 86400000) { const k = dayOf(m); days[k] = (days[k] ?? 0) + 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
return finish()
|
||||
function finish() {
|
||||
return best ? { at: new Date(best).toISOString(), file: path.relative(projectPath, bestFile), partial: seen > MAX_ENTRIES } : null
|
||||
return best ? { at: new Date(best).toISOString(), file: path.relative(projectPath, bestFile), partial: seen > MAX_ENTRIES, days } : null
|
||||
}
|
||||
}
|
||||
|
||||
// Commits per day of the last year (git log), cached like the scan.
|
||||
const { run } = require('./proc')
|
||||
const gitCache = new Map() // projectPath -> { value, at }
|
||||
async function gitDays(projectPath) {
|
||||
const c = gitCache.get(projectPath)
|
||||
if (c && Date.now() - c.at < TTL_MS) return c.value
|
||||
let value = {}
|
||||
if (fs.existsSync(path.join(projectPath, '.git'))) {
|
||||
const r = await run('git', ['-C', projectPath, 'log', '--since=1.year.ago', '--format=%ad', '--date=short-local'], { timeout: 15000 })
|
||||
for (const line of r.stdout.split(/\r?\n/)) { const k = line.trim(); if (/^\d{4}-\d{2}-\d{2}$/.test(k)) value[k] = (value[k] ?? 0) + 1 }
|
||||
}
|
||||
gitCache.set(projectPath, { value, at: Date.now() })
|
||||
return value
|
||||
}
|
||||
|
||||
// Files changed per day (undefined while the background scan hasn't reached the project).
|
||||
function filesPerDay(projectPath) {
|
||||
const v = lastChange(projectPath)
|
||||
return v === undefined ? undefined : v?.days ?? {}
|
||||
}
|
||||
|
||||
// Scans run in the background, one project per tick, so the server stays
|
||||
// responsive; the list always gets the latest known value right away.
|
||||
const queue = []
|
||||
@@ -71,6 +103,6 @@ function lastChange(projectPath) {
|
||||
function warm(projectPaths) { for (const p of projectPaths) enqueue(p) }
|
||||
function pending() { return queue.length + (running ? 1 : 0) }
|
||||
function forget(projectPath) { cache.delete(projectPath) }
|
||||
function clear() { cache.clear(); queue.length = 0; queued.clear() }
|
||||
function clear() { cache.clear(); gitCache.clear(); queue.length = 0; queued.clear() }
|
||||
|
||||
module.exports = { lastChange, warm, pending, forget, clear }
|
||||
module.exports = { lastChange, warm, pending, forget, clear, gitDays, filesPerDay, dayOf }
|
||||
|
||||
Reference in New Issue
Block a user