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,
localFile: LocalFile
) =
Storage.upload(
localFile,
bucket,
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar))
Storage.upload(localFile,
bucket,
UploadEventListener.Settings(localFile,
index,
syncTotals,
totalBytesSoFar))
}
object UnversionedMirrorArchive {

View file

@ -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))

View file

@ -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 _ => ()
}
}

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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 => {

View file

@ -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,

View file

@ -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
}