Skip to content

Fix installation/update progress reports to the frontend #2683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2023
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
73 changes: 43 additions & 30 deletions src/backend/storeManagers/gog/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ interface tmpProgressMap {
[key: string]: InstallProgress
}

const defaultTmpProgress = {
bytes: '',
eta: '',
percent: undefined,
diskSpeed: undefined,
downSpeed: undefined
function defaultTmpProgress() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these default object was inlined before and extracted here, but it needs to be inside a function so we always return a new object

the current code without a function passes the same object around and it's getting updated, it doesn't stay i this initial state

return {
bytes: '',
eta: '',
percent: undefined,
diskSpeed: undefined,
downSpeed: undefined
}
}
const tmpProgress: tmpProgressMap = {}

Expand All @@ -165,48 +167,59 @@ export function onInstallOrUpdateOutput(
totalDownloadSize = -1
) {
if (!Object.hasOwn(tmpProgress, appName)) {
tmpProgress[appName] = defaultTmpProgress
tmpProgress[appName] = defaultTmpProgress()
}
const progress = tmpProgress[appName]

// parse log for percent
const percentMatch = data.match(/Progress: (\d+\.\d+) /m)
if (!progress.percent) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these checks were there in the original code, we need them to make sure we update only specific fields of the progress object

const percentMatch = data.match(/Progress: (\d+\.\d+) /m)

tmpProgress[appName].percent = !Number.isNaN(Number(percentMatch?.at(1)))
? Number(percentMatch?.at(1))
: undefined
progress.percent = !Number.isNaN(Number(percentMatch?.at(1)))
? Number(percentMatch?.at(1))
: undefined
}

// parse log for eta
const etaMatch = data.match(/ETA: (\d\d:\d\d:\d\d)/m)
tmpProgress[appName].eta =
etaMatch && etaMatch?.length >= 2 ? etaMatch[1] : ''
if (progress.eta === '') {
const etaMatch = data.match(/ETA: (\d\d:\d\d:\d\d)/m)
progress.eta = etaMatch && etaMatch?.length >= 2 ? etaMatch[1] : ''
}

// parse log for game download progress
const bytesMatch = data.match(/Downloaded: (\S+) MiB/m)
tmpProgress[appName].bytes =
bytesMatch && bytesMatch?.length >= 2 ? `${bytesMatch[1]}MB` : ''
if (progress.bytes === '') {
const bytesMatch = data.match(/Downloaded: (\S+) MiB/m)
progress.bytes =
bytesMatch && bytesMatch?.length >= 2 ? `${bytesMatch[1]}MB` : ''
}

// parse log for download speed
const downSpeedMBytes = data.match(/Download\t- (\S+.) MiB/m)
tmpProgress[appName].downSpeed = !Number.isNaN(Number(downSpeedMBytes?.at(1)))
? Number(downSpeedMBytes?.at(1))
: undefined
if (!progress.downSpeed) {
const downSpeedMBytes = data.match(/Download\t- (\S+.) MiB/m)
progress.downSpeed = !Number.isNaN(Number(downSpeedMBytes?.at(1)))
? Number(downSpeedMBytes?.at(1))
: undefined
}

// parse disk write speed
const diskSpeedMBytes = data.match(/Disk\t- (\S+.) MiB/m)
tmpProgress[appName].diskSpeed = !Number.isNaN(Number(diskSpeedMBytes?.at(1)))
? Number(diskSpeedMBytes?.at(1))
: undefined
if (!progress.diskSpeed) {
const diskSpeedMBytes = data.match(/Disk\t- (\S+.) MiB/m)
progress.diskSpeed = !Number.isNaN(Number(diskSpeedMBytes?.at(1)))
? Number(diskSpeedMBytes?.at(1))
: undefined
}

// only send to frontend if all values are updated
if (
Object.values(tmpProgress[appName]).every(
Object.values(progress).every(
(value) => !(value === undefined || value === '')
)
) {
logInfo(
[
`Progress for ${getGameInfo(appName).title}:`,
`${tmpProgress[appName].percent}%/${tmpProgress[appName].bytes}/${tmpProgress[appName].eta}`.trim(),
`Down: ${tmpProgress[appName].downSpeed}MB/s / Disk: ${tmpProgress[appName].diskSpeed}MB/s`
`${progress.percent}%/${progress.bytes}/${progress.eta}`.trim(),
`Down: ${progress.downSpeed}MB/s / Disk: ${progress.diskSpeed}MB/s`
],
LogPrefix.Gog
)
Expand All @@ -215,11 +228,11 @@ export function onInstallOrUpdateOutput(
appName: appName,
runner: 'gog',
status: action,
progress: tmpProgress[appName]
progress: progress
})

// reset
tmpProgress[appName] = defaultTmpProgress
tmpProgress[appName] = defaultTmpProgress()
}
}

Expand Down
69 changes: 40 additions & 29 deletions src/backend/storeManagers/legendary/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,14 @@ interface tmpProgressMap {
[key: string]: InstallProgress
}

const defaultTmpProgress = {
bytes: '',
eta: '',
percent: undefined,
diskSpeed: undefined,
downSpeed: undefined
function defaultTmpProgres() {
return {
bytes: '',
eta: '',
percent: undefined,
diskSpeed: undefined,
downSpeed: undefined
}
}
const tmpProgress: tmpProgressMap = {}

Expand All @@ -382,55 +384,64 @@ export function onInstallOrUpdateOutput(
}

if (!Object.hasOwn(tmpProgress, appName)) {
tmpProgress[appName] = defaultTmpProgress
tmpProgress[appName] = defaultTmpProgres()
}

const progress = tmpProgress[appName]

// parse log for eta
const etaMatch = data.match(/ETA: (\d\d:\d\d:\d\d)/m)
tmpProgress[appName].eta =
etaMatch && etaMatch?.length >= 2 ? etaMatch[1] : ''
if (progress.eta === '') {
const etaMatch = data.match(/ETA: (\d\d:\d\d:\d\d)/m)
progress.eta = etaMatch && etaMatch?.length >= 2 ? etaMatch[1] : ''
}

// parse log for game download progress
const bytesMatch = data.match(/Downloaded: (\S+.) MiB/m)
tmpProgress[appName].bytes =
bytesMatch && bytesMatch?.length >= 2 ? `${bytesMatch[1]}MB` : ''
if (progress.bytes === '') {
const bytesMatch = data.match(/Downloaded: (\S+.) MiB/m)
progress.bytes =
bytesMatch && bytesMatch?.length >= 2 ? `${bytesMatch[1]}MB` : ''
}

// parse log for download speed
const downSpeedMBytes = data.match(/Download\t- (\S+.) MiB/m)
tmpProgress[appName].downSpeed = !Number.isNaN(Number(downSpeedMBytes?.at(1)))
? Number(downSpeedMBytes?.at(1))
: undefined
if (!progress.downSpeed) {
const downSpeedMBytes = data.match(/Download\t- (\S+.) MiB/m)
progress.downSpeed = !Number.isNaN(Number(downSpeedMBytes?.at(1)))
? Number(downSpeedMBytes?.at(1))
: undefined
}

// parse disk write speed
const diskSpeedMBytes = data.match(/Disk\t- (\S+.) MiB/m)
tmpProgress[appName].diskSpeed = !Number.isNaN(Number(diskSpeedMBytes?.at(1)))
? Number(diskSpeedMBytes?.at(1))
: undefined
if (!progress.diskSpeed) {
const diskSpeedMBytes = data.match(/Disk\t- (\S+.) MiB/m)
progress.diskSpeed = !Number.isNaN(Number(diskSpeedMBytes?.at(1)))
? Number(diskSpeedMBytes?.at(1))
: undefined
}

// original is in bytes, convert to MiB with 2 decimals
totalDownloadSize = Math.round((totalDownloadSize / 1024 / 1024) * 100) / 100

// calculate percentage
if (tmpProgress[appName].bytes !== '') {
const downloaded = parseFloat(tmpProgress[appName].bytes)
if (progress.bytes !== '') {
const downloaded = parseFloat(progress.bytes)
const downloadCache = totalDownloadSize - currentDownloadSize[appName]
const totalDownloaded = downloaded + downloadCache
const newPercent =
Math.round((totalDownloaded / totalDownloadSize) * 10000) / 100
tmpProgress[appName].percent = newPercent >= 0 ? newPercent : undefined
progress.percent = newPercent >= 0 ? newPercent : undefined
}

// only send to frontend if all values are updated
if (
Object.values(tmpProgress[appName]).every(
Object.values(progress).every(
(value) => !(value === undefined || value === '')
)
) {
logInfo(
[
`Progress for ${getGameInfo(appName).title}:`,
`${tmpProgress[appName].percent}%/${tmpProgress[appName].bytes}/${tmpProgress[appName].eta}`.trim(),
`Down: ${tmpProgress[appName].downSpeed}MB/s / Disk: ${tmpProgress[appName].diskSpeed}MB/s`
`${progress.percent}%/${progress.bytes}/${progress.eta}`.trim(),
`Down: ${progress.downSpeed}MB/s / Disk: ${progress.diskSpeed}MB/s`
],
LogPrefix.Legendary
)
Expand All @@ -439,11 +450,11 @@ export function onInstallOrUpdateOutput(
appName: appName,
runner: 'legendary',
status: action,
progress: tmpProgress[appName]
progress: progress
})

// reset
tmpProgress[appName] = defaultTmpProgress
tmpProgress[appName] = defaultTmpProgres()
}
}

Expand Down