Refactor UploadEventLogger.statusWithBar(String,Long=>String,Long,Long,Long) (#128)

* [domain] UploadEventListener refactoring

* [domain] UploadEventListener rewrite
This commit is contained in:
Paul Campbell 2019-08-01 13:31:48 +01:00 committed by GitHub
parent 4a54038933
commit adbf8af2f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 71 additions and 59 deletions

View file

@ -33,10 +33,12 @@ case class UnversionedMirrorArchive(syncTotals: SyncTotals)
bucket: Bucket, bucket: Bucket,
localFile: LocalFile localFile: LocalFile
) = ) =
Storage.upload( Storage.upload(localFile,
localFile, bucket,
bucket, UploadEventListener.Settings(localFile,
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar)) index,
syncTotals,
totalBytesSoFar))
} }
object UnversionedMirrorArchive { object UnversionedMirrorArchive {

View file

@ -23,7 +23,7 @@ case class DummyStorageService(s3ObjectData: S3ObjectsData,
override def upload( override def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener, uploadEventListener: UploadEventListener.Settings,
): UIO[StorageQueueEvent] = { ): UIO[StorageQueueEvent] = {
val (remoteKey, md5Hash) = uploadFiles(localFile.file) val (remoteKey, md5Hash) = uploadFiles(localFile.file)
UIO(StorageQueueEvent.UploadQueueEvent(remoteKey, md5Hash)) UIO(StorageQueueEvent.UploadQueueEvent(remoteKey, md5Hash))

View file

@ -1,26 +1,32 @@
package net.kemitix.thorp.domain package net.kemitix.thorp.domain
import net.kemitix.thorp.domain.UploadEvent.RequestEvent 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( object UploadEventListener {
localFile: LocalFile,
index: Int, case class Settings(
syncTotals: SyncTotals, localFile: LocalFile,
totalBytesSoFar: Long index: Int,
) { syncTotals: SyncTotals,
totalBytesSoFar: Long
)
var bytesTransferred = 0L var bytesTransferred = 0L
def listener: UploadEvent => Unit = { def listener(settings: Settings): UploadEvent => Unit = {
case e: RequestEvent => case e: RequestEvent =>
bytesTransferred += e.transferred bytesTransferred += e.transferred
logRequestCycle(localFile, logRequestCycle(
e, RequestCycle(settings.localFile,
bytesTransferred, bytesTransferred,
index, settings.index,
syncTotals, settings.syncTotals,
totalBytesSoFar) settings.totalBytesSoFar))
case _ => () case _ => ()
} }
} }

View file

@ -1,33 +1,39 @@
package net.kemitix.thorp.domain package net.kemitix.thorp.domain
import net.kemitix.thorp.domain.SizeTranslation.sizeInEnglish
import net.kemitix.thorp.domain.Terminal._ import net.kemitix.thorp.domain.Terminal._
import net.kemitix.thorp.domain.UploadEvent.RequestEvent
import scala.io.AnsiColor._ import scala.io.AnsiColor._
trait UploadEventLogger { object UploadEventLogger {
def logRequestCycle( case class RequestCycle(
localFile: LocalFile, localFile: LocalFile,
event: RequestEvent,
bytesTransferred: Long, bytesTransferred: Long,
index: Int, index: Int,
syncTotals: SyncTotals, syncTotals: SyncTotals,
totalBytesSoFar: Long 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 val statusHeight = 7
if (bytesTransferred < fileLength) if (requestCycle.bytesTransferred < fileLength)
println( println(
s"${GREEN}Uploading:$RESET $remoteKey$eraseToEndOfScreen\n" + s"${GREEN}Uploading:$RESET $remoteKey$eraseToEndOfScreen\n" +
statusWithBar(" File", sizeInEnglish, bytesTransferred, fileLength) + statusWithBar(" File",
statusWithBar("Files", l => l.toString, index, syncTotals.count) + SizeTranslation.sizeInEnglish,
statusWithBar(" Size", requestCycle.bytesTransferred,
sizeInEnglish, fileLength) +
bytesTransferred + totalBytesSoFar, statusWithBar("Files",
syncTotals.totalSizeBytes) + l => l.toString,
requestCycle.index,
requestCycle.syncTotals.count) +
statusWithBar(
" Size",
SizeTranslation.sizeInEnglish,
requestCycle.bytesTransferred + requestCycle.totalBytesSoFar,
requestCycle.syncTotals.totalSizeBytes) +
s"${Terminal.cursorPrevLine(statusHeight)}") s"${Terminal.cursorPrevLine(statusHeight)}")
} }
@ -35,15 +41,11 @@ trait UploadEventLogger {
label: String, label: String,
format: Long => String, format: Long => String,
current: Long, current: Long,
max: Long, max: Long
pre: Long = 0
): String = { ): String = {
val percent = f"${(current * 100) / max}%2d" val percent = f"${(current * 100) / max}%2d"
s"$GREEN$label:$RESET ($percent%) ${format(current)} of ${format(max)}" + s"$GREEN$label:$RESET ($percent%) ${format(current)} of ${format(max)}" +
(if (pre > 0) s" (pre-synced ${format(pre)}" s"$eraseLineForward\n" +
else "") + s"$eraseLineForward\n" +
progressBar(current, max, Terminal.width) progressBar(current, max, Terminal.width)
} }
} }
object UploadEventLogger extends UploadEventLogger

View file

@ -20,7 +20,7 @@ object Storage {
def upload( def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener, listenerSettings: UploadEventListener.Settings,
): ZIO[Storage with Config, Nothing, StorageQueueEvent] ): ZIO[Storage with Config, Nothing, StorageQueueEvent]
def copy( def copy(
@ -56,7 +56,7 @@ object Storage {
override def upload( override def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener listenerSettings: UploadEventListener.Settings
): ZIO[Storage, Nothing, StorageQueueEvent] = ): ZIO[Storage, Nothing, StorageQueueEvent] =
uploadResult uploadResult
@ -98,9 +98,9 @@ object Storage {
final def upload( final def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener listenerSettings: UploadEventListener.Settings
): ZIO[Storage with Config, Nothing, StorageQueueEvent] = ): ZIO[Storage with Config, Nothing, StorageQueueEvent] =
ZIO.accessM(_.storage upload (localFile, bucket, uploadEventListener)) ZIO.accessM(_.storage upload (localFile, bucket, listenerSettings))
final def copy( final def copy(
bucket: Bucket, bucket: Bucket,

View file

@ -27,10 +27,10 @@ object S3Storage {
override def upload( override def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener, listenerSettings: UploadEventListener.Settings,
): ZIO[Config, Nothing, StorageQueueEvent] = ): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)( Uploader.upload(transferManager)(
Uploader.Request(localFile, bucket, uploadEventListener)) Uploader.Request(localFile, bucket, listenerSettings))
override def copy(bucket: Bucket, override def copy(bucket: Bucket,
sourceKey: RemoteKey, sourceKey: RemoteKey,

View file

@ -21,7 +21,7 @@ trait Uploader {
case class Request( case class Request(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener uploadEventListener: UploadEventListener.Settings
) )
def upload(transferManager: => AmazonTransferManager)( def upload(transferManager: => AmazonTransferManager)(
@ -72,11 +72,13 @@ trait Uploader {
metadata metadata
} }
private def progressListener: UploadEventListener => ProgressListener = private def progressListener
uploadEventListener => : UploadEventListener.Settings => ProgressListener =
listenerSettings =>
new ProgressListener { new ProgressListener {
override def progressChanged(progressEvent: ProgressEvent): Unit = override def progressChanged(progressEvent: ProgressEvent): Unit =
uploadEventListener.listener(eventHandler(progressEvent)) UploadEventListener.listener(listenerSettings)(
eventHandler(progressEvent))
private def eventHandler: ProgressEvent => UploadEvent = private def eventHandler: ProgressEvent => UploadEvent =
progressEvent => { progressEvent => {

View file

@ -32,10 +32,10 @@ trait AmazonS3ClientTestFixture extends MockFactory {
override def upload( override def upload(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener, listenerSettings: UploadEventListener.Settings,
): ZIO[Config, Nothing, StorageQueueEvent] = ): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)( Uploader.upload(transferManager)(
Uploader.Request(localFile, bucket, uploadEventListener)) Uploader.Request(localFile, bucket, listenerSettings))
override def copy( override def copy(
bucket: Bucket, bucket: Bucket,

View file

@ -33,8 +33,8 @@ class UploaderTest extends FreeSpec with MockFactory {
val inProgress = new AmazonUpload.InProgress { val inProgress = new AmazonUpload.InProgress {
override def waitForUploadResult: UploadResult = uploadResult override def waitForUploadResult: UploadResult = uploadResult
} }
val uploadEventListener = val listenerSettings =
UploadEventListener(localFile, 0, SyncTotals(1, 0, 0), 0) UploadEventListener.Settings(localFile, 0, SyncTotals(1, 0, 0), 0)
"when no error" in { "when no error" in {
val expected = val expected =
Right(UploadQueueEvent(remoteKey, aHash)) Right(UploadQueueEvent(remoteKey, aHash))
@ -46,7 +46,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)( invoke(fixture.amazonS3TransferManager)(
localFile, localFile,
bucket, bucket,
uploadEventListener listenerSettings
) )
assertResult(expected)(result) assertResult(expected)(result)
} }
@ -64,7 +64,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)( invoke(fixture.amazonS3TransferManager)(
localFile, localFile,
bucket, bucket,
uploadEventListener listenerSettings
) )
assertResult(expected)(result) assertResult(expected)(result)
} }
@ -82,7 +82,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)( invoke(fixture.amazonS3TransferManager)(
localFile, localFile,
bucket, bucket,
uploadEventListener listenerSettings
) )
assertResult(expected)(result) assertResult(expected)(result)
} }
@ -90,14 +90,14 @@ class UploaderTest extends FreeSpec with MockFactory {
def invoke(transferManager: AmazonTransferManager)( def invoke(transferManager: AmazonTransferManager)(
localFile: LocalFile, localFile: LocalFile,
bucket: Bucket, bucket: Bucket,
uploadEventListener: UploadEventListener listenerSettings: UploadEventListener.Settings
) = { ) = {
type TestEnv = Config type TestEnv = Config
val testEnv: TestEnv = Config.Live val testEnv: TestEnv = Config.Live
new DefaultRuntime {}.unsafeRunSync { new DefaultRuntime {}.unsafeRunSync {
Uploader Uploader
.upload(transferManager)( .upload(transferManager)(
Uploader.Request(localFile, bucket, uploadEventListener)) Uploader.Request(localFile, bucket, listenerSettings))
.provide(testEnv) .provide(testEnv)
}.toEither }.toEither
} }