From adbf8af2f3eb3b61fa342a3f86e991ea4183c546 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 1 Aug 2019 13:31:48 +0100 Subject: [PATCH] Refactor UploadEventLogger.statusWithBar(String,Long=>String,Long,Long,Long) (#128) * [domain] UploadEventListener refactoring * [domain] UploadEventListener rewrite --- .../thorp/core/UnversionedMirrorArchive.scala | 10 +++-- .../thorp/core/DummyStorageService.scala | 2 +- .../thorp/domain/UploadEventListener.scala | 34 ++++++++------ .../thorp/domain/UploadEventLogger.scala | 44 ++++++++++--------- .../kemitix/thorp/storage/api/Storage.scala | 8 ++-- .../kemitix/thorp/storage/aws/S3Storage.scala | 4 +- .../kemitix/thorp/storage/aws/Uploader.scala | 10 +++-- .../aws/AmazonS3ClientTestFixture.scala | 4 +- .../thorp/storage/aws/UploaderTest.scala | 14 +++--- 9 files changed, 71 insertions(+), 59 deletions(-) diff --git a/core/src/main/scala/net/kemitix/thorp/core/UnversionedMirrorArchive.scala b/core/src/main/scala/net/kemitix/thorp/core/UnversionedMirrorArchive.scala index 9f4edea..5096846 100644 --- a/core/src/main/scala/net/kemitix/thorp/core/UnversionedMirrorArchive.scala +++ b/core/src/main/scala/net/kemitix/thorp/core/UnversionedMirrorArchive.scala @@ -33,10 +33,12 @@ case class UnversionedMirrorArchive(syncTotals: SyncTotals) bucket: Bucket, localFile: LocalFile ) = - Storage.upload( - localFile, - bucket, - UploadEventListener(localFile, index, syncTotals, totalBytesSoFar)) + Storage.upload(localFile, + bucket, + UploadEventListener.Settings(localFile, + index, + syncTotals, + totalBytesSoFar)) } object UnversionedMirrorArchive { diff --git a/core/src/test/scala/net/kemitix/thorp/core/DummyStorageService.scala b/core/src/test/scala/net/kemitix/thorp/core/DummyStorageService.scala index fe20de5..b82a9bf 100644 --- a/core/src/test/scala/net/kemitix/thorp/core/DummyStorageService.scala +++ b/core/src/test/scala/net/kemitix/thorp/core/DummyStorageService.scala @@ -23,7 +23,7 @@ case class DummyStorageService(s3ObjectData: S3ObjectsData, override def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener, + uploadEventListener: UploadEventListener.Settings, ): UIO[StorageQueueEvent] = { val (remoteKey, md5Hash) = uploadFiles(localFile.file) UIO(StorageQueueEvent.UploadQueueEvent(remoteKey, md5Hash)) diff --git a/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventListener.scala b/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventListener.scala index 48e1cbf..32e5021 100644 --- a/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventListener.scala +++ b/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventListener.scala @@ -1,26 +1,32 @@ package net.kemitix.thorp.domain import net.kemitix.thorp.domain.UploadEvent.RequestEvent -import net.kemitix.thorp.domain.UploadEventLogger.logRequestCycle +import net.kemitix.thorp.domain.UploadEventLogger.{ + RequestCycle, + logRequestCycle +} -case class UploadEventListener( - localFile: LocalFile, - index: Int, - syncTotals: SyncTotals, - totalBytesSoFar: Long -) { +object UploadEventListener { + + case class Settings( + localFile: LocalFile, + index: Int, + syncTotals: SyncTotals, + totalBytesSoFar: Long + ) var bytesTransferred = 0L - def listener: UploadEvent => Unit = { + def listener(settings: Settings): UploadEvent => Unit = { case e: RequestEvent => bytesTransferred += e.transferred - logRequestCycle(localFile, - e, - bytesTransferred, - index, - syncTotals, - totalBytesSoFar) + logRequestCycle( + RequestCycle(settings.localFile, + bytesTransferred, + settings.index, + settings.syncTotals, + settings.totalBytesSoFar)) case _ => () } + } diff --git a/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventLogger.scala b/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventLogger.scala index 692a271..6305212 100644 --- a/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventLogger.scala +++ b/domain/src/main/scala/net/kemitix/thorp/domain/UploadEventLogger.scala @@ -1,33 +1,39 @@ package net.kemitix.thorp.domain -import net.kemitix.thorp.domain.SizeTranslation.sizeInEnglish import net.kemitix.thorp.domain.Terminal._ -import net.kemitix.thorp.domain.UploadEvent.RequestEvent import scala.io.AnsiColor._ -trait UploadEventLogger { +object UploadEventLogger { - def logRequestCycle( + case class RequestCycle( localFile: LocalFile, - event: RequestEvent, bytesTransferred: Long, index: Int, syncTotals: SyncTotals, totalBytesSoFar: Long - ): Unit = { - val remoteKey = localFile.remoteKey.key - val fileLength = localFile.file.length + ) + + def logRequestCycle(requestCycle: RequestCycle): Unit = { + val remoteKey = requestCycle.localFile.remoteKey.key + val fileLength = requestCycle.localFile.file.length val statusHeight = 7 - if (bytesTransferred < fileLength) + if (requestCycle.bytesTransferred < fileLength) println( s"${GREEN}Uploading:$RESET $remoteKey$eraseToEndOfScreen\n" + - statusWithBar(" File", sizeInEnglish, bytesTransferred, fileLength) + - statusWithBar("Files", l => l.toString, index, syncTotals.count) + - statusWithBar(" Size", - sizeInEnglish, - bytesTransferred + totalBytesSoFar, - syncTotals.totalSizeBytes) + + statusWithBar(" File", + SizeTranslation.sizeInEnglish, + requestCycle.bytesTransferred, + fileLength) + + statusWithBar("Files", + l => l.toString, + requestCycle.index, + requestCycle.syncTotals.count) + + statusWithBar( + " Size", + SizeTranslation.sizeInEnglish, + requestCycle.bytesTransferred + requestCycle.totalBytesSoFar, + requestCycle.syncTotals.totalSizeBytes) + s"${Terminal.cursorPrevLine(statusHeight)}") } @@ -35,15 +41,11 @@ trait UploadEventLogger { label: String, format: Long => String, current: Long, - max: Long, - pre: Long = 0 + max: Long ): String = { val percent = f"${(current * 100) / max}%2d" s"$GREEN$label:$RESET ($percent%) ${format(current)} of ${format(max)}" + - (if (pre > 0) s" (pre-synced ${format(pre)}" - else "") + s"$eraseLineForward\n" + + s"$eraseLineForward\n" + progressBar(current, max, Terminal.width) } } - -object UploadEventLogger extends UploadEventLogger diff --git a/storage-api/src/main/scala/net/kemitix/thorp/storage/api/Storage.scala b/storage-api/src/main/scala/net/kemitix/thorp/storage/api/Storage.scala index ad5df92..fc6f15a 100644 --- a/storage-api/src/main/scala/net/kemitix/thorp/storage/api/Storage.scala +++ b/storage-api/src/main/scala/net/kemitix/thorp/storage/api/Storage.scala @@ -20,7 +20,7 @@ object Storage { def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener, + listenerSettings: UploadEventListener.Settings, ): ZIO[Storage with Config, Nothing, StorageQueueEvent] def copy( @@ -56,7 +56,7 @@ object Storage { override def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener + listenerSettings: UploadEventListener.Settings ): ZIO[Storage, Nothing, StorageQueueEvent] = uploadResult @@ -98,9 +98,9 @@ object Storage { final def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener + listenerSettings: UploadEventListener.Settings ): ZIO[Storage with Config, Nothing, StorageQueueEvent] = - ZIO.accessM(_.storage upload (localFile, bucket, uploadEventListener)) + ZIO.accessM(_.storage upload (localFile, bucket, listenerSettings)) final def copy( bucket: Bucket, diff --git a/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/S3Storage.scala b/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/S3Storage.scala index 7ba57e9..8e7aaa2 100644 --- a/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/S3Storage.scala +++ b/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/S3Storage.scala @@ -27,10 +27,10 @@ object S3Storage { override def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener, + listenerSettings: UploadEventListener.Settings, ): ZIO[Config, Nothing, StorageQueueEvent] = Uploader.upload(transferManager)( - Uploader.Request(localFile, bucket, uploadEventListener)) + Uploader.Request(localFile, bucket, listenerSettings)) override def copy(bucket: Bucket, sourceKey: RemoteKey, diff --git a/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/Uploader.scala b/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/Uploader.scala index fd01565..e25ebe6 100644 --- a/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/Uploader.scala +++ b/storage-aws/src/main/scala/net/kemitix/thorp/storage/aws/Uploader.scala @@ -21,7 +21,7 @@ trait Uploader { case class Request( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener + uploadEventListener: UploadEventListener.Settings ) def upload(transferManager: => AmazonTransferManager)( @@ -72,11 +72,13 @@ trait Uploader { metadata } - private def progressListener: UploadEventListener => ProgressListener = - uploadEventListener => + private def progressListener + : UploadEventListener.Settings => ProgressListener = + listenerSettings => new ProgressListener { override def progressChanged(progressEvent: ProgressEvent): Unit = - uploadEventListener.listener(eventHandler(progressEvent)) + UploadEventListener.listener(listenerSettings)( + eventHandler(progressEvent)) private def eventHandler: ProgressEvent => UploadEvent = progressEvent => { diff --git a/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/AmazonS3ClientTestFixture.scala b/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/AmazonS3ClientTestFixture.scala index de9d240..5f65a50 100644 --- a/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/AmazonS3ClientTestFixture.scala +++ b/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/AmazonS3ClientTestFixture.scala @@ -32,10 +32,10 @@ trait AmazonS3ClientTestFixture extends MockFactory { override def upload( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener, + listenerSettings: UploadEventListener.Settings, ): ZIO[Config, Nothing, StorageQueueEvent] = Uploader.upload(transferManager)( - Uploader.Request(localFile, bucket, uploadEventListener)) + Uploader.Request(localFile, bucket, listenerSettings)) override def copy( bucket: Bucket, diff --git a/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/UploaderTest.scala b/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/UploaderTest.scala index 271add8..a4891e2 100644 --- a/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/UploaderTest.scala +++ b/storage-aws/src/test/scala/net/kemitix/thorp/storage/aws/UploaderTest.scala @@ -33,8 +33,8 @@ class UploaderTest extends FreeSpec with MockFactory { val inProgress = new AmazonUpload.InProgress { override def waitForUploadResult: UploadResult = uploadResult } - val uploadEventListener = - UploadEventListener(localFile, 0, SyncTotals(1, 0, 0), 0) + val listenerSettings = + UploadEventListener.Settings(localFile, 0, SyncTotals(1, 0, 0), 0) "when no error" in { val expected = Right(UploadQueueEvent(remoteKey, aHash)) @@ -46,7 +46,7 @@ class UploaderTest extends FreeSpec with MockFactory { invoke(fixture.amazonS3TransferManager)( localFile, bucket, - uploadEventListener + listenerSettings ) assertResult(expected)(result) } @@ -64,7 +64,7 @@ class UploaderTest extends FreeSpec with MockFactory { invoke(fixture.amazonS3TransferManager)( localFile, bucket, - uploadEventListener + listenerSettings ) assertResult(expected)(result) } @@ -82,7 +82,7 @@ class UploaderTest extends FreeSpec with MockFactory { invoke(fixture.amazonS3TransferManager)( localFile, bucket, - uploadEventListener + listenerSettings ) assertResult(expected)(result) } @@ -90,14 +90,14 @@ class UploaderTest extends FreeSpec with MockFactory { def invoke(transferManager: AmazonTransferManager)( localFile: LocalFile, bucket: Bucket, - uploadEventListener: UploadEventListener + listenerSettings: UploadEventListener.Settings ) = { type TestEnv = Config val testEnv: TestEnv = Config.Live new DefaultRuntime {}.unsafeRunSync { Uploader .upload(transferManager)( - Uploader.Request(localFile, bucket, uploadEventListener)) + Uploader.Request(localFile, bucket, listenerSettings)) .provide(testEnv) }.toEither }