Skip to content

Commit

Permalink
Improved TransactionDownloader.progress (take #2).
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Le Ponner <eric.leponner@icloud.com>
  • Loading branch information
ericleponner committed Nov 9, 2022
1 parent e7151d1 commit 487bd93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/utils/downloader/EntityDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export abstract class EntityDownloader<E, R> {
private entities: E[] = []

private readonly downloadedCountRef = ref(0)
private readonly firstDownloadedEntityRef: Ref<E|null> = ref(null)
private readonly lastDownloadedEntityRef: Ref<E|null> = ref(null)
private readonly drainedRef = ref(false)
private readonly stateRef: Ref<DownloaderState> = ref(DownloaderState.Fresh)
Expand Down Expand Up @@ -66,6 +67,8 @@ export abstract class EntityDownloader<E, R> {

public downloadedCount: ComputedRef<number>
= computed(() => this.downloadedCountRef.value)
public firstDownloadedEntity: ComputedRef<E|null>
= computed(() => this.firstDownloadedEntityRef.value)
public lastDownloadedEntity: ComputedRef<E|null>
= computed(() => this.lastDownloadedEntityRef.value)
public drained: ComputedRef<boolean>
Expand Down Expand Up @@ -128,6 +131,8 @@ export abstract class EntityDownloader<E, R> {
= this.entities.concat(newEntities)
this.downloadedCountRef.value
= this.entities.length
this.firstDownloadedEntityRef.value
= this.entities.length >= 1 ? this.entities[0] : null
this.lastDownloadedEntityRef.value
= this.entities.length >= 1 ? this.entities[this.entities.length - 1] : null

Expand Down
15 changes: 10 additions & 5 deletions src/utils/downloader/TransactionDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,24 @@ export class TransactionDownloader extends EntityDownloader<Transaction, Transac
const startTime = this.startDate.getTime()
const endTime = this.endDate != null ? this.endDate.getTime() : this.now.getTime()

const firstEntity = this.firstDownloadedEntity.value
const firstTimestamp = firstEntity?.consensus_timestamp ?? null
const firstTime = firstTimestamp !== null ? timestampToMillis(firstTimestamp) : endTime
const lastEntity = this.lastDownloadedEntity.value
const lastTimestamp = lastEntity?.consensus_timestamp ?? null
const lastTime = lastTimestamp !== null ? timestampToMillis(lastTimestamp) : endTime

/*
| remaining | done |
--------+-----------------------+-------------------------------+--------> now
startTime lastTime endTime
| remaining | already | nothing |
| to download | downloaded | here |
--------+-----------------------+------------------------+---------+--------> now
startTime lastTime firstTime endTime
*/

const progress = lastTime !== null ? (endTime - lastTime) / (endTime - startTime) : 0
result = Math.round(progress * 100) / 100
const progress = firstTime !== null && lastTime !== null
? (firstTime - lastTime) / (firstTime - startTime) : 0
result = Math.round(progress * 1000) / 1000
}

return result
Expand Down

0 comments on commit 487bd93

Please sign in to comment.