diff --git a/lib/upload-manager.js b/lib/upload-manager.js index 4d8b3bd..552b72b 100644 --- a/lib/upload-manager.js +++ b/lib/upload-manager.js @@ -137,7 +137,9 @@ class UploadManager extends EventEmitter { _recordTransferred(entry, bytes, total) { if (!Number.isFinite(bytes) || bytes < 0) return; const next = Math.min(bytes, total); - this.transferredBytes += Math.max(0, next - entry.bytesUploaded); + const delta = Math.max(0, next - entry.bytesUploaded); + this.transferredBytes += delta; + if (delta > 0) this.emit('statistics-transfer', { hoster: entry.hoster, bytes: delta }); entry.bytesUploaded = next; entry.phase = next >= total ? 'awaiting-confirmation' : 'uploading'; } @@ -431,6 +433,7 @@ class UploadManager extends EventEmitter { const batchId = `batch-${Date.now()}`; const results = new Map(); // filePath -> { name, size, results: [] } this._batchResults = results; + this._batchTaskCount = tasks.length; this._additionalPromises = []; // Track jobs added mid-batch via addJobs() const DEDUP_CHUNK = 200; @@ -472,7 +475,7 @@ class UploadManager extends EventEmitter { this._emitStats(); const files = Array.from(results.values()); - const total = tasks.length; + const total = this._batchTaskCount; const succeeded = files.reduce((count, file) => count + file.results.filter((result) => result.status === 'done').length, 0); const skipped = files.reduce((count, file) => count + file.results.filter((result) => result.status === 'skipped').length, 0); @@ -549,6 +552,7 @@ class UploadManager extends EventEmitter { }; results.get(task.file).results.push(result); + this.emit('statistics-outcome', { id: uploadId, hoster: task.hoster, status }); }; const emitFinalStatus = (status, payload = {}) => { @@ -1557,6 +1561,7 @@ class UploadManager extends EventEmitter { } this._additionalPromises.push(this._runJob(task, results, signal)); addResult.added++; + this._batchTaskCount++; } return addResult; } diff --git a/lib/upload-statistics.js b/lib/upload-statistics.js new file mode 100644 index 0000000..51a8ed7 --- /dev/null +++ b/lib/upload-statistics.js @@ -0,0 +1,186 @@ +const fs = require('node:fs/promises'); +const path = require('node:path'); + +const HOSTERS = Object.freeze({ 'doodstream.com': 'Doodstream', 'voe.sx': 'VOE', 'vidmoly.me': 'Vidmoly', 'byse.sx': 'Byse', 'clouddrop.cc': 'CloudDrop' }); +const RANGES = new Set(['session', 'today', 'last24', 'week', 'month', 'all']); +const MINUTE = 60000; +const DAY = 86400000; +const empty = () => ({ bytes: 0, trackedBytes: 0, completed: 0, failed: 0, activeMs: 0, providers: {} }); +const count = value => Number.isFinite(value) && value >= 0 ? value : 0; +const dayKey = time => { + const date = new Date(time); + return [date.getFullYear(), String(date.getMonth() + 1).padStart(2, '0'), String(date.getDate()).padStart(2, '0')].join('-'); +}; +function add(target, delta, hoster) { + for (const key of ['bytes', 'trackedBytes', 'completed', 'failed', 'activeMs']) target[key] = count(target[key]) + count(delta[key]); + if (Object.hasOwn(HOSTERS, hoster)) { + const provider = target.providers[hoster] ||= { bytes: 0, completed: 0, failed: 0 }; + for (const key of ['bytes', 'completed', 'failed']) provider[key] += count(delta[key]); + } +} +function merge(target, source) { + add(target, source); + for (const [hoster, bucket] of Object.entries(source.providers || {})) { + if (!Object.hasOwn(HOSTERS, hoster)) continue; + const provider = target.providers[hoster] ||= { bytes: 0, completed: 0, failed: 0 }; + for (const key of ['bytes', 'completed', 'failed']) provider[key] += count(bucket[key]); + } +} +function validState(value) { + return value?.version === 1 && value.days && value.minutes && value.total?.providers && Number.isFinite(value.startedAt); +} + +class UploadStatistics { + constructor(filePath, { now = Date.now } = {}) { + this.filePath = filePath; + this.now = now; + this.session = empty(); + this.sessionStartedAt = now(); + this.seen = new Set(); + this.activeSince = null; + this.writeChain = Promise.resolve(); + this.revision = 0; + this.savedRevision = -1; + this.ready = false; + this.error = ''; + } + + async init(history = []) { + let present = false; + for (const name of [this.filePath, this.filePath + '.bak']) { + try { + const value = JSON.parse(await fs.readFile(name, 'utf8')); + if (!validState(value)) throw new Error('Ungültiges Statistikformat'); + this.state = value; + this.savedRevision = 0; + break; + } catch (error) { + if (error.code !== 'ENOENT') present = true; + } + } + if (!this.state && present) throw new Error('Statistikdatei und Sicherung sind nicht lesbar'); + if (!this.state) { + this.state = { version: 1, startedAt: this.now(), historyImported: false, total: empty(), days: {}, minutes: {} }; + for (const batch of Array.isArray(history) ? history : []) { + const time = Date.parse(batch?.timestamp); + if (!Number.isFinite(time) || time > this.now()) continue; + for (const file of batch.files || []) { + for (const result of file.results || []) { + if (!Object.hasOwn(HOSTERS, result.hoster) || !['done', 'error'].includes(result.status)) continue; + this.record(result.hoster, { bytes: result.status === 'done' ? count(file.size) : 0, completed: Number(result.status === 'done'), failed: Number(result.status === 'error') }, time, false); + this.state.historyImported = true; + this.state.startedAt = Math.min(this.state.startedAt, time); + } + } + } + } + this.ready = true; + await this.flush(); + } + + record(hoster, delta, time = this.now(), session = true) { + if (session) { + delta = { ...delta, trackedBytes: count(delta.bytes) }; + add(this.session, delta, hoster); + } + add(this.state.total, delta, hoster); + add(this.state.days[dayKey(time)] ||= empty(), delta, hoster); + if (time >= this.now() - 2 * DAY) add(this.state.minutes[Math.floor(time / MINUTE)] ||= empty(), delta, hoster); + this.revision++; + } + + transfer(hoster, bytes) { + if (!this.ready || !Object.hasOwn(HOSTERS, hoster) || !Number.isFinite(bytes) || bytes <= 0) return; + this.record(hoster, { bytes }); + } + + outcome({ id, hoster, status }) { + if (!this.ready || !id || !Object.hasOwn(HOSTERS, hoster) || !['done', 'error'].includes(status) || this.seen.has(id)) return; + this.seen.add(id); + this.record(hoster, { completed: Number(status === 'done'), failed: Number(status === 'error') }); + } + + activity(active) { + if (!this.ready) return; + const now = this.now(); + if (this.activeSince !== null) { + let start = this.activeSince; + while (start < now) { + const end = Math.min(now, (Math.floor(start / MINUTE) + 1) * MINUTE); + this.record(null, { activeMs: end - start }, start); + start = end; + } + } + this.activeSince = active ? now : null; + } + + finishBatch() { + this.activity(false); + this.seen.clear(); + } + + snapshot(range = 'session') { + if (!RANGES.has(range)) throw new Error('Ungültiger Statistikzeitraum'); + if (!this.ready) throw new Error('Statistik noch nicht verfügbar'); + this.activity(this.activeSince !== null); + const total = empty(); + if (range === 'session') merge(total, this.session); + else if (range === 'all') merge(total, this.state.total); + else if (range === 'last24') { + const cutoff = Math.floor((this.now() - DAY) / MINUTE); + for (const [minute, bucket] of Object.entries(this.state.minutes)) if (Number(minute) > cutoff && Number(minute) <= Math.floor(this.now() / MINUTE)) merge(total, bucket); + } else { + const date = new Date(this.now()); + date.setDate(date.getDate() - (range === 'month' ? 29 : range === 'week' ? 6 : 0)); + const start = dayKey(date.getTime()); + const end = dayKey(this.now()); + for (const [day, bucket] of Object.entries(this.state.days)) if (day >= start && day <= end) merge(total, bucket); + } + const outcomes = total.completed + total.failed; + return { ...total, range, startedAt: range === 'session' ? this.sessionStartedAt : this.state.startedAt, + historyImported: range !== 'session' && this.state.historyImported, + successRate: outcomes ? total.completed / outcomes * 100 : null, + averageSpeedBps: total.activeMs > 0 ? total.trackedBytes * 1000 / total.activeMs : null, + persistenceError: this.error, + providers: Object.entries(HOSTERS).map(([id, label]) => ({ id, label, ...(total.providers[id] || { bytes: 0, completed: 0, failed: 0 }) })) }; + } + + async reset(scope) { + if (!['session', 'all', 'errors'].includes(scope)) throw new Error('Ungültiger Statistikreset'); + if (!this.ready) throw new Error('Statistik noch nicht verfügbar'); + this.activity(this.activeSince !== null); + if (scope === 'session' || scope === 'all') { this.session = empty(); this.sessionStartedAt = this.now(); } + if (scope === 'all') this.state = { version: 1, startedAt: this.now(), historyImported: false, total: empty(), days: {}, minutes: {} }; + if (scope === 'errors') { + for (const bucket of [this.session, this.state.total, ...Object.values(this.state.days), ...Object.values(this.state.minutes)]) { + bucket.failed = 0; + for (const provider of Object.values(bucket.providers)) provider.failed = 0; + } + } + this.revision++; + await this.flush(); + } + + flush() { + const task = this.writeChain.catch(() => {}).then(async () => { + if (!this.ready || this.savedRevision === this.revision) return; + const cutoff = Math.floor((this.now() - 2 * DAY) / MINUTE); + for (const minute of Object.keys(this.state.minutes)) if (Number(minute) < cutoff) delete this.state.minutes[minute]; + const revision = this.revision; + const data = JSON.stringify(this.state); + await fs.mkdir(path.dirname(this.filePath), { recursive: true }); + const handle = await fs.open(this.filePath + '.tmp', 'w'); + try { await handle.writeFile(data, 'utf8'); await handle.sync(); } finally { await handle.close(); } + try { + const previous = await fs.readFile(this.filePath, 'utf8'); + if (validState(JSON.parse(previous))) await fs.writeFile(this.filePath + '.bak', previous, 'utf8'); + } catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; } + await fs.rename(this.filePath + '.tmp', this.filePath); + this.savedRevision = revision; + this.error = ''; + }); + this.writeChain = task; + return task.catch(error => { this.error = 'Statistik konnte nicht gespeichert werden'; throw error; }); + } +} +module.exports = { UploadStatistics, HOSTERS }; diff --git a/main.js b/main.js index 7ae5bbd..cfb681e 100644 --- a/main.js +++ b/main.js @@ -45,6 +45,7 @@ const { selectOrphanTmps } = require('./lib/orphan-tmp'); const { sanitizeConfig, buildSupportBundleText, collectSecretValues, redactLogText, valueScrub, collectFile, collectFileSnapshot, REDACTED } = require('./lib/support-bundle'); const { buildWebhookRequest, isAllAborted } = require('./lib/webhook-notify'); const stats = require('./lib/stats'); +const { UploadStatistics } = require('./lib/upload-statistics'); const { createCollectors } = require('./lib/diagnostics-collectors'); const { buildSessionReport, buildSessionReportCsv } = require('./lib/session-report'); const { inspectImportEntries, inspectReadableImportPath } = require('./lib/import-preflight'); @@ -160,6 +161,9 @@ let dropTargetWindow = null; let tray = null; let _cachedLogSettings = null; const configStore = new ConfigStore(app); +const uploadStatistics = new UploadStatistics(path.join(app.getPath('userData'), 'upload-statistics.json')); +let statisticsReady; +let statisticsTimer; const mcpUpdate = new McpUpdate({ directory: app.getPath('userData'), version: () => app.getVersion(), canUpdate: () => mcpService?.capabilities().includes('updates') === true, @@ -296,6 +300,7 @@ async function waitForConfigStoreWrites() { await Promise.all([...cloudLogWrites]); await cloudLogSync.flushLocal(); await configStore.drainWrites(); + await uploadStatistics.flush(); await configStore.drainAutomationCompletionWrites(); } @@ -2010,6 +2015,10 @@ async function runAutomaticUpdateCheck(forceRefresh) { app.whenReady().then(async () => { await mcpUpdate.load().catch(() => logError('MCP update state unavailable')); if (!_hasSingleInstanceLock) return; + statisticsReady = configStore.loadHistory().then(history => uploadStatistics.init(history)); + await statisticsReady.catch(error => debugLog(`statistics init failed: ${error.message}`)); + statisticsTimer = setInterval(() => { void uploadStatistics.flush().catch(error => debugLog(`statistics save failed: ${error.message}`)); }, 5000); + statisticsTimer.unref(); startupDiagnostics.write('application-ready', { hardwareAcceleration: app.isHardwareAccelerationEnabled(), gpuFeatures: app.getGPUFeatureStatus() }); void app.getGPUInfo('basic').then(info => { const devices = Array.isArray(info.gpuDevice) ? info.gpuDevice.map(device => ({ @@ -2109,6 +2118,7 @@ app.on('will-quit', () => { if (quitTeardownStarted) return; quitTeardownStarted = true; systemMetrics.stop(); + clearInterval(statisticsTimer); void mcpService?.stop(); folderMonitorRendererReadyGeneration = null; if (updateCheckInterval) clearInterval(updateCheckInterval); @@ -2241,6 +2251,18 @@ ipcMain.handle('save-config', async (_event, config) => { return true; }); +ipcMain.handle('get-statistics', async (_event, range) => { + await statisticsReady; + return uploadStatistics.snapshot(range); +}); + +ipcMain.handle('reset-statistics', async (_event, scope) => { + assertConfigWriteAllowed(); + await statisticsReady; + await uploadStatistics.reset(scope); + return true; +}); + ipcMain.handle('get-history', () => { return configStore.loadHistory(); }); @@ -2824,6 +2846,12 @@ ipcMain.handle('start-upload', async (_event, payload) => { onError: error => debugLog(`automation completion ledger failed: ${error.message}`) }); + uploadManager.on('statistics-transfer', ({ hoster, bytes }) => { + uploadStatistics.activity(true); + uploadStatistics.transfer(hoster, bytes); + }); + uploadManager.on('statistics-outcome', data => uploadStatistics.outcome(data)); + uploadManager.on('progress', (data) => { if (data.status !== 'uploading') { debugLog(`progress: ${data.fileName} ${data.hoster} ${data.status} ${data.error || ''}`); @@ -2862,6 +2890,7 @@ ipcMain.handle('start-upload', async (_event, payload) => { uploadManager.on('stats', (data) => { try { if (!data || typeof data !== 'object') return; + uploadStatistics.activity(data.activeJobs > 0 && data.state !== 'idle'); safeSend('upload-stats', data); if (data.state === 'uploading' && data.activeJobs > 0) { const speedMb = ((Number(data.globalSpeedKbs) || 0) / 1000).toFixed(1); @@ -2939,6 +2968,8 @@ ipcMain.handle('start-upload', async (_event, payload) => { // create a fresh manager which the trailing `uploadManager = null` then // orphans (cancel/addJobs see null, the new batch keeps running invisibly). uploadManager.on('batch-done', async (summary) => { + uploadStatistics.finishBatch(); + void uploadStatistics.flush().catch(error => debugLog(`statistics save failed: ${error.message}`)); summary = stats.mergeSkippedIntoSummary(summary, skippedJobs); let automationCompletionsPersisted = true; try { await _thisManager._automationCompletionWriter?.flush(); } catch (error) { diff --git a/preload.js b/preload.js index c405025..2d0eef8 100644 --- a/preload.js +++ b/preload.js @@ -28,6 +28,8 @@ ipcRenderer.on('folder-monitor:renderer-generation', (_event, generation) => { }); contextBridge.exposeInMainWorld('api', { + getStatistics: range => ipcRenderer.invoke('get-statistics', range), + resetStatistics: scope => ipcRenderer.invoke('reset-statistics', scope), mcpStatus: () => ipcRenderer.invoke('mcp:status'), mcpConfigure: args => ipcRenderer.invoke('mcp:configure', args), mcpRotate: () => ipcRenderer.invoke('mcp:rotate'), diff --git a/renderer/app.js b/renderer/app.js index 2364ed1..067b590 100644 --- a/renderer/app.js +++ b/renderer/app.js @@ -42,6 +42,7 @@ function refreshLocalizedRuntimeUi() { if (hint && activeRecentTab) hint.textContent = localizeUiText(activeRecentTab.dataset.panel === 'statsTab' ? 'Upload-Statistiken' : 'Zuletzt erzeugte Upload-Links'); renderManagedOnlineBackups(); renderManagedOnlineBackupRefreshIssue(); + window.UploadStatisticsView?.localize(); } // Dropdown options for "Add Account" modal: value -> label @@ -1698,6 +1699,7 @@ function _isHistoryTabActive() { syncTabIndicator(tab); const activeSidebarButton = nextView?.querySelector('.view-sidebar-navigation > .view-sidebar-item.active, .settings-navigation .settings-nav-button.active'); _syncSidebarIndicator(activeSidebarButton, true); + if (tab.dataset.view === 'history') window.UploadStatisticsView?.refresh(true); if (tab.dataset.view === 'history' && (_historyDirty || !_historyEverLoaded)) { loadHistory(); } @@ -6048,6 +6050,7 @@ function setAccountSidebarFilter(value) { function setHistorySidebarFilter(value) { if (!['all', 'success', 'error', 'skipped'].includes(value)) return; + window.UploadStatisticsView?.showHistory(); historySidebarFilter = value; _syncSidebarFilterButtons('[data-history-filter]', 'historyFilter', value); const container = document.getElementById('historyContainer'); diff --git a/renderer/i18n.js b/renderer/i18n.js index e3658fe..ca632cb 100644 --- a/renderer/i18n.js +++ b/renderer/i18n.js @@ -4,6 +4,52 @@ if (root) root.I18n = api; })(typeof window !== 'undefined' ? window : globalThis, function () { const pairs = [ + ['Seit Beginn der Datenverkehrserfassung', 'Since traffic recording began'], + ["Ungültiges Statistikformat","Invalid statistics format"], + ["Statistikdatei und Sicherung sind nicht lesbar","Statistics file and backup cannot be read"], + ["Ungültiger Statistikzeitraum","Invalid statistics period"], + ["Statistik noch nicht verfügbar","Statistics not available yet"], + ["Ungültiger Statistikreset","Invalid statistics reset"], + ["Statistiken","Statistics"], + ["Auswertung","Analysis"], + ["Statistik-Zeitraum","Statistics period"], + ["Statistik-Dashboard","Statistics dashboard"], + ["Statistiken zurücksetzen","Reset statistics"], + ["Heute","Today"], + ["Letzte 24 Stunden","Last 24 hours"], + ["Sieben Tage","Seven days"], + ["30 Tage","30 days"], + ["Sitzung zurücksetzen","Reset session"], + ["Gesamt zurücksetzen","Reset all"], + ["Fehler zurücksetzen","Reset errors"], + ["Datenmenge","Data transferred"], + ["Erfolgsquote","Success rate"], + ["Durchschnitt","Average speed"], + ["Bandbreitenverlauf","Bandwidth history"], + ["Live · letzte 60 Sekunden","Live · last 60 seconds"], + ["Daten","Data"], + ["Ergebnisse","Results"], + ["Upload-Nutzung je Hoster","Upload usage by host"], + ["Statistiken werden geladen…","Loading statistics…"], + ["Erfolgreiche Uploads werden je Hoster gezählt. Die Datenmenge enthält Wiederholungen.","Successful uploads are counted per host. Data transferred includes retries."], + ["Enthält vorhandenen Verlauf. Frühere Wiederholungen und aktive Uploadzeit sind nicht vollständig erfasst.","Includes existing history. Earlier retries and active upload time were not fully recorded."], + ["Lokale Upload-Statistik. Wiederholungen zählen zum Datenverkehr; abgebrochene und übersprungene Uploads nicht zur Erfolgsquote.","Local upload statistics. Traffic includes retries; success rate excludes cancelled and skipped uploads."], + ["Erfolgreiche Uploads je Hoster","Successful uploads per host"], + ["Erfolgreich / erfolgreich und fehlgeschlagen","Successful / successful and failed"], + ["Keine vollständige aktive Uploadzeit verfügbar","Complete active upload time unavailable"], + ["Daten / aktive Uploadzeit","Data / active upload time"], + ["Endgültig fehlgeschlagene Uploads","Permanently failed uploads"], + ["fertig","completed"], + ["Statistiken konnten nicht geladen werden. Erneut versuchen.","Statistics could not be loaded. Try again."], + ["Statistik konnte nicht gespeichert werden","Statistics could not be saved"], + ["Statistikwerte dieser Sitzung zurücksetzen? Uploads, Dateien und Verlauf bleiben erhalten.","Reset statistics for this session? Uploads, files and history are preserved."], + ["Alle gespeicherten Statistikwerte zurücksetzen? Uploads, Dateien und Verlauf bleiben erhalten.","Reset all saved statistics? Uploads, files and history are preserved."], + ["Fehlerzähler in allen Statistikzeiträumen zurücksetzen? Uploads und Verlauf bleiben unverändert.","Reset error counts in all statistics periods? Uploads and history remain unchanged."], + ["Statistiken zurücksetzen?","Reset statistics?"], + ["Zurücksetzen","Reset"], + ["Statistiken konnten nicht zurückgesetzt werden. Erneut versuchen.","Statistics could not be reset. Try again."], + ["Upload-Bandbreitenverlauf der letzten 60 Sekunden","Upload bandwidth history for the last 60 seconds"], + ["Kein Upload-Datenverkehr in den letzten 60 Sekunden","No upload traffic in the last 60 seconds"], ['MCP-Fernzugriff', 'MCP remote access'], ['MCP-Einstellungen werden gespeichert…', 'Saving MCP settings…'], ['MCP-Änderungen noch nicht gespeichert', 'MCP changes have not been saved yet'], diff --git a/renderer/index.html b/renderer/index.html index 9da0968..2e95d40 100644 --- a/renderer/index.html +++ b/renderer/index.html @@ -7,6 +7,7 @@