{"version":3,"file":"Eta.mjs","sources":["../../../lib/upload/uploader/Eta.ts"],"sourcesContent":["/*!\n * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { TypedEventTarget } from 'typescript-event-target'\nimport { formatFileSize } from '../../utils/fileSize.ts'\n\nexport const EtaStatus = Object.freeze({\n\tIdle: 0,\n\tPaused: 1,\n\tRunning: 2,\n})\n\ntype TEtaStatus = typeof EtaStatus[keyof typeof EtaStatus]\n\ninterface EtaOptions {\n\t/** Low pass filter cutoff time for smoothing the speed */\n\tcutoffTime?: number\n\t/** Total number of bytes to be expected */\n\ttotal?: number\n\t/** Start the estimation directly */\n\tstart?: boolean\n}\n\nexport interface EtaEventsMap {\n\tpause: CustomEvent\n\treset: CustomEvent\n\tresume: CustomEvent\n\tupdate: CustomEvent\n}\n\nexport class Eta extends TypedEventTarget<EtaEventsMap> {\n\t/** Bytes done */\n\tprivate _done: number = 0\n\t/** Total bytes to do */\n\tprivate _total: number = 0\n\t/** Current progress (cached) as interval [0,1] */\n\tprivate _progress: number = 0\n\t/** Status of the ETA */\n\tprivate _status: TEtaStatus = EtaStatus.Idle\n\t/** Time of the last update */\n\tprivate _startTime: number = -1\n\t/** Total elapsed time for current ETA */\n\tprivate _elapsedTime: number = 0\n\t/** Current speed in bytes per second */\n\tprivate _speed: number = -1\n\t/** Expected duration to finish in seconds */\n\tprivate _eta: number = Infinity\n\n\t/**\n\t * Cutoff time for the low pass filter of the ETA.\n\t * A higher value will consider more history information for calculation,\n\t * and thus suppress spikes of the speed,\n\t * but will make the overall resposiveness slower.\n\t */\n\tprivate _cutoffTime = 2.5\n\n\tpublic constructor(options: EtaOptions = {}) {\n\t\tsuper()\n\t\tif (options.start) {\n\t\t\tthis.resume()\n\t\t}\n\t\tif (options.total) {\n\t\t\tthis.update(0, options.total)\n\t\t}\n\t\tthis._cutoffTime = options.cutoffTime ?? 2.5\n\t}\n\n\t/**\n\t * Add more transferred bytes.\n\t *\n\t * @param done Additional bytes done.\n\t */\n\tpublic add(done: number): void {\n\t\tthis.update(this._done + done)\n\t}\n\n\t/**\n\t * Update the transmission state.\n\t *\n\t * @param done The new value of transferred bytes.\n\t * @param total Optionally also update the total bytes we expect.\n\t */\n\tpublic update(done: number, total?: number): void {\n\t\tif (this.status !== EtaStatus.Running) {\n\t\t\treturn\n\t\t}\n\t\tif (total && total > 0) {\n\t\t\tthis._total = total\n\t\t}\n\n\t\tconst deltaDone = done - this._done\n\t\tconst deltaTime = (Date.now() - this._startTime) / 1000\n\n\t\tthis._startTime = Date.now()\n\t\tthis._elapsedTime += deltaTime\n\t\tthis._done = done\n\t\tthis._progress = this._done / this._total\n\n\t\t// Only update speed when the history is large enough so we can estimate it\n\t\tconst historyNeeded = this._cutoffTime + deltaTime\n\t\tif (this._elapsedTime > historyNeeded) {\n\t\t\t// Filter the done bytes using a low pass filter to suppress speed spikes\n\t\t\tconst alpha = deltaTime / (deltaTime + (1 / this._cutoffTime))\n\t\t\tconst filtered = (this._done - deltaDone) + (1 - alpha) * deltaDone\n\t\t\t// bytes per second - filtered\n\t\t\tthis._speed = Math.round(filtered / this._elapsedTime)\n\t\t} else if (this._speed === -1 && this._elapsedTime > deltaTime) {\n\t\t\t// special case when uploading with high speed\n\t\t\t// it could be that the upload is finished before we reach the curoff time\n\t\t\t// so we already give an estimation\n\t\t\tconst remaining = this._total - done\n\t\t\tconst eta = remaining / (done / this._elapsedTime)\n\t\t\t// Only set the ETA when we either already set it for a previous update\n\t\t\t// or when the special case happened that we are in fast upload and we only got a couple of seconds for the whole upload\n\t\t\t// meaning we are below 2x the cutoff time.\n\t\t\tif (this._eta !== Infinity || eta <= 2 * this._cutoffTime) {\n\t\t\t\t// We only take a couple of seconds so we set the eta to the current ETA using current speed.\n\t\t\t\t// But we do not set the speed because we do not want to trigger the real ETA calculation below\n\t\t\t\t// and especially because the speed would be very spiky (we still have no filters in place).\n\t\t\t\tthis._eta = eta\n\t\t\t}\n\t\t}\n\n\t\t// Update the eta if we have valid speed information (prevent divide by zero)\n\t\tif (this._speed > 0) {\n\t\t\t// Estimate transfer of remaining bytes with current average speed\n\t\t\tthis._eta = Math.round((this._total - this._done) / this._speed)\n\t\t}\n\n\t\tthis.dispatchTypedEvent('update', new CustomEvent('update', { cancelable: false }))\n\t}\n\n\tpublic reset(): void {\n\t\tthis._done = 0\n\t\tthis._total = 0\n\t\tthis._progress = 0\n\t\tthis._elapsedTime = 0\n\t\tthis._eta = Infinity\n\t\tthis._speed = -1\n\t\tthis._startTime = -1\n\t\tthis._status = EtaStatus.Idle\n\t\tthis.dispatchTypedEvent('reset', new CustomEvent('reset'))\n\t}\n\n\t/**\n\t * Pause the ETA calculation.\n\t */\n\tpublic pause(): void {\n\t\tif (this._status === EtaStatus.Running) {\n\t\t\tthis._status = EtaStatus.Paused\n\t\t\tthis._elapsedTime += (Date.now() - this._startTime) / 1000\n\t\t\tthis.dispatchTypedEvent('pause', new CustomEvent('pause'))\n\t\t}\n\t}\n\n\t/**\n\t * Resume the ETA calculation.\n\t */\n\tpublic resume(): void {\n\t\tif (this._status !== EtaStatus.Running) {\n\t\t\tthis._startTime = Date.now()\n\t\t\tthis._status = EtaStatus.Running\n\t\t\tthis.dispatchTypedEvent('resume', new CustomEvent('resume'))\n\t\t}\n\t}\n\n\t/**\n\t * Status of the Eta (paused, active, idle).\n\t */\n\tpublic get status(): TEtaStatus {\n\t\treturn this._status\n\t}\n\n\t/**\n\t * Progress (percent done)\n\t */\n\tpublic get progress(): number {\n\t\treturn Math.round(this._progress * 10000) / 100\n\t}\n\n\t/**\n\t * Estimated time in seconds.\n\t * If the time is not yet estimated, it will return `Infinity`.\n\t */\n\tpublic get time(): number {\n\t\treturn this._eta\n\t}\n\n\t/**\n\t * Transfer speed in bytes per second.\n\t * Returns `-1` if not yet estimated.\n\t */\n\tpublic get speed(): number {\n\t\treturn this._speed\n\t}\n\n\t/**\n\t * Get the speed in human readable format using file sizes like 10KB/s.\n\t * Returns the empty string if not yet estimated.\n\t */\n\tpublic get speedReadable(): string {\n\t\treturn this._speed > 0\n\t\t\t? `${formatFileSize(this._speed, true)}∕s`\n\t\t\t: ''\n\t}\n}\n"],"names":[],"mappings":";;AAAA;AAAA;AAAA;AAAA;AAQO,MAAM,YAAY,OAAO,OAAO;AAAA,EACtC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,SAAS;AACV,CAAC;AAoBM,MAAM,YAAY,iBAA+B;AAAA;AAAA,EAE/C,QAAgB;AAAA;AAAA,EAEhB,SAAiB;AAAA;AAAA,EAEjB,YAAoB;AAAA;AAAA,EAEpB,UAAsB,UAAU;AAAA;AAAA,EAEhC,aAAqB;AAAA;AAAA,EAErB,eAAuB;AAAA;AAAA,EAEvB,SAAiB;AAAA;AAAA,EAEjB,OAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQf,cAAc;AAAA,EAEf,YAAY,UAAsB,IAAI;AAC5C,UAAA;AACA,QAAI,QAAQ,OAAO;AAClB,WAAK,OAAA;AAAA,IACN;AACA,QAAI,QAAQ,OAAO;AAClB,WAAK,OAAO,GAAG,QAAQ,KAAK;AAAA,IAC7B;AACA,SAAK,cAAc,QAAQ,cAAc;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,IAAI,MAAoB;AAC9B,SAAK,OAAO,KAAK,QAAQ,IAAI;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,OAAO,MAAc,OAAsB;AACjD,QAAI,KAAK,WAAW,UAAU,SAAS;AACtC;AAAA,IACD;AACA,QAAI,SAAS,QAAQ,GAAG;AACvB,WAAK,SAAS;AAAA,IACf;AAEA,UAAM,YAAY,OAAO,KAAK;AAC9B,UAAM,aAAa,KAAK,IAAA,IAAQ,KAAK,cAAc;AAEnD,SAAK,aAAa,KAAK,IAAA;AACvB,SAAK,gBAAgB;AACrB,SAAK,QAAQ;AACb,SAAK,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