Files
ProjectDashboard/lib/activity.js

109 lines
4.4 KiB
JavaScript
Raw Normal View History

2026-11-12 09:32:23 +01:00
// "Last change" of a project = newest modified file inside it, ignoring
// folders that change by themselves (node_modules, build output, caches, .git).
// The folder's own mtime is useless for this (it only changes when direct
// children are added/removed). Bounded walk, cached for a few minutes.
const fs = require('fs')
const path = require('path')
2026-11-12 14:03:53 +01:00
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
2026-11-12 09:32:23 +01:00
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 }
2026-11-12 14:03:53 +01:00
// 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
2026-11-12 09:32:23 +01:00
function scan(projectPath) {
let best = 0
let bestFile = ''
let seen = 0
2026-11-12 14:03:53 +01:00
// 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
2026-11-12 09:32:23 +01:00
const stack = [{ dir: projectPath, depth: 0 }]
while (stack.length) {
const { dir, depth } = stack.pop()
let entries
try { entries = fs.readdirSync(dir, { withFileTypes: true }) } catch { continue }
for (const e of entries) {
if (++seen > MAX_ENTRIES) return finish()
const full = path.join(dir, e.name)
if (e.isDirectory()) {
if (depth < MAX_DEPTH && !SKIP.test(e.name)) stack.push({ dir: full, depth: depth + 1 })
} else if (e.isFile() && !IGNORE_FILES.test(e.name)) {
let m = 0
try { m = fs.statSync(full).mtimeMs } catch { continue }
if (m > best) { best = m; bestFile = full }
2026-11-12 14:03:53 +01:00
if (m >= since && m <= Date.now() + 86400000) { const k = dayOf(m); days[k] = (days[k] ?? 0) + 1 }
2026-11-12 09:32:23 +01:00
}
}
}
return finish()
function finish() {
2026-11-12 14:03:53 +01:00
return best ? { at: new Date(best).toISOString(), file: path.relative(projectPath, bestFile), partial: seen > MAX_ENTRIES, days } : null
2026-11-12 09:32:23 +01:00
}
}
2026-11-12 14:03:53 +01:00
// 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 ?? {}
}
2026-11-12 09:32:23 +01:00
// 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 = []
const queued = new Set()
let running = false
function enqueue(projectPath) {
if (queued.has(projectPath)) return
queued.add(projectPath)
queue.push(projectPath)
if (!running) { running = true; setImmediate(work) }
}
function work() {
const p = queue.shift()
if (!p) { running = false; return }
queued.delete(p)
try { cache.set(p, { value: scan(p), at: Date.now() }) } catch {}
setImmediate(work)
}
// -> { at, file, partial } | null (empty project) | undefined (not known yet);
// a stale or missing value is refreshed in the background.
function lastChange(projectPath) {
const c = cache.get(projectPath)
if (!c || Date.now() - c.at > TTL_MS) enqueue(projectPath)
return c ? c.value : undefined
}
function warm(projectPaths) { for (const p of projectPaths) enqueue(p) }
function pending() { return queue.length + (running ? 1 : 0) }
function forget(projectPath) { cache.delete(projectPath) }
2026-11-12 14:03:53 +01:00
function clear() { cache.clear(); gitCache.clear(); queue.length = 0; queued.clear() }
2026-11-12 09:32:23 +01:00
2026-11-12 14:03:53 +01:00
module.exports = { lastChange, warm, pending, forget, clear, gitDays, filesPerDay, dayOf }