这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions test/test-cases/get-sync-progress-test-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ module.exports = (
assert.propertyVal(res.body, 'id', 5)
assert.isObject(res.body.result)
assert.isNumber(res.body.result.progress)
assert.isNumber(res.body.result.syncStartedAt)
assert.isNumber(res.body.result.spentTime)

assert.isOk(
res.body.result.syncStartedAt === null ||
Number.isInteger(res.body.result.syncStartedAt)
)
assert.isOk(
res.body.result.spentTime === null ||
Number.isInteger(res.body.result.spentTime)
)
if (Number.isFinite(res.body.result.syncStartedAt)) {
Number.isInteger(res.body.result.spentTime)
}

if (
!Number.isFinite(res.body.result.syncStartedAt) ||
!Number.isFinite(res.body.result.progress) ||
res.body.result.progress <= 0 ||
res.body.result.progress > 100
Expand Down
2 changes: 2 additions & 0 deletions workers/loc.api/sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Sync {
}
}

this.progress.deactivateSyncTimeEstimate()

try {
await this.redirectRequestsToApi({ isRedirected: false })
} catch (err) {
Expand Down
85 changes: 77 additions & 8 deletions workers/loc.api/sync/progress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Progress extends EventEmitter {
this.logger = logger

this._syncStartedAt = null
this._progressEmitterInterval = null
this._progressEmitterIntervalMs = 1000
this._progress = null
this._hasNotProgressChanged = true
this._leftTime = null
this._prevEstimatedLeftTime = Date.now()
}

async setProgress (progress) {
Expand All @@ -43,18 +49,16 @@ class Progress extends EventEmitter {
const _progress = isError
? progress.toString()
: progress
this._hasNotProgressChanged = this._progress !== _progress
this._progress = _progress

try {
await this.dao.updateRecordOf(
this.TABLES_NAMES.PROGRESS,
{ value: JSON.stringify(_progress) }
)
const estimatedSyncTime = this._estimateSyncTime({
progress: _progress
})

this.emit(estimatedSyncTime)
await this.wsEventEmitter.emitProgress(() => estimatedSyncTime)
await this._emitProgress()
} catch (e) {
this.logger.error(
`PROGRESS:SYNC:SET: ${e.stack || e}`
Expand Down Expand Up @@ -94,19 +98,22 @@ class Progress extends EventEmitter {

activateSyncTimeEstimate () {
this._syncStartedAt = Date.now()
this._launchProgressEmitterInterval()

return this
}

deactivateSyncTimeEstimate () {
clearInterval(this._progressEmitterInterval)
this._syncStartedAt = null

return this
}

async _estimateSyncTime (params) {
const {
progress
progress,
hasNotProgressChanged
} = params ?? {}

const syncStartedAt = this._getSyncStartedAt()
Expand All @@ -124,7 +131,7 @@ class Progress extends EventEmitter {
}
}

const spentTime = nowMts - syncStartedAt
const spentTime = Math.floor(nowMts - syncStartedAt)

if (
!Number.isFinite(progress) ||
Expand All @@ -139,7 +146,12 @@ class Progress extends EventEmitter {
}
}

const leftTime = (spentTime / progress) * (100 - progress)
const leftTime = this._calcLeftTime({
progress,
nowMts,
spentTime,
hasNotProgressChanged
})

return {
progress,
Expand All @@ -149,9 +161,66 @@ class Progress extends EventEmitter {
}
}

_calcLeftTime (params) {
const {
progress,
nowMts,
spentTime,
hasNotProgressChanged
} = params ?? {}

if (!hasNotProgressChanged) {
this._prevEstimatedLeftTime = nowMts
this._leftTime = Math.floor((spentTime / progress) * (100 - progress))

return this._leftTime
}
if (!Number.isFinite(this._leftTime)) {
this._prevEstimatedLeftTime = nowMts

return this._leftTime
}

const leftTime = Math.floor(this._leftTime - (nowMts - this._prevEstimatedLeftTime))
this._prevEstimatedLeftTime = nowMts
this._leftTime = leftTime > 0
? leftTime
: null

return this._leftTime
}

_getSyncStartedAt () {
return this._syncStartedAt ?? null
}

_launchProgressEmitterInterval () {
clearInterval(this._progressEmitterInterval)

this._progressEmitterInterval = setInterval(async () => {
await this._emitProgress({
hasNotProgressChanged: this._hasNotProgressChanged
})
}, this._progressEmitterIntervalMs).unref()
}

async _emitProgress (opts) {
try {
const { hasNotProgressChanged } = opts ?? {}

const estimatedSyncTime = this._estimateSyncTime({
progress: this._progress,
hasNotProgressChanged
})

this.emit(estimatedSyncTime)
await this.wsEventEmitter.emitProgress(() => estimatedSyncTime)
} catch (e) {
this.logger.error(
`PROGRESS:SYNC:EMITTING: ${e.stack || e}`
)
}
}
}

decorateInjectable(Progress, depsTypes)
Expand Down