Skip to content

Commit

Permalink
add backend status debug overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
chrimaeon committed May 21, 2023
1 parent ece2786 commit 9695bcd
Show file tree
Hide file tree
Showing 19 changed files with 575 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

import java.text.CharacterIterator
import java.text.StringCharacterIterator
import kotlin.math.abs
import kotlin.math.sign

actual fun Long.humanReadableSize(): String {
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this)
if (absB < 1024) {
return "$this B"
}
var value = absB
val ci: CharacterIterator = StringCharacterIterator("KMGTPE")
var i = 40
while (i >= 0 && absB > 0xfffccccccccccccL shr i) {
value = value shr 10
ci.next()
i -= 10
}
value *= sign(this.toDouble()).toLong()
return String.format("%.2f %cB", value / 1024.0, ci.current())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ package com.cmgapps.common.curriculumvitae.data.network

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.plugins.websocket.webSocket
import io.ktor.client.plugins.websocket.ws
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsChannel
import io.ktor.http.HttpMethod
import io.ktor.http.URLProtocol
import io.ktor.http.Url
import io.ktor.http.appendPathSegments
Expand All @@ -21,7 +20,6 @@ import io.ktor.websocket.readText
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

class CvApiService(private val client: HttpClient, private val baseUrl: Url) {
Expand Down Expand Up @@ -51,14 +49,14 @@ class CvApiService(private val client: HttpClient, private val baseUrl: Url) {

@OptIn(ExperimentalCoroutinesApi::class)
fun getApiStatus(): Flow<Status> = flow {
client.webSocket(
HttpMethod.Get,
baseUrl.host,
baseUrl.port,
"status",
client.ws(
baseUrl.toString(),
request = {
url.protocol =
if (baseUrl.protocol == URLProtocol.HTTPS) URLProtocol.WSS else URLProtocol.WS
url {
appendPathSegments("status")
protocol =
if (url.protocol == URLProtocol.HTTPS) URLProtocol.WSS else URLProtocol.WS
}
},
) {
while (!incoming.isClosedForReceive) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

expect fun Long.humanReadableSize(): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

import kotlin.math.pow
import kotlin.test.Test
import kotlin.test.assertEquals

class HumanReadableSizeShould {

@Test
fun get_zero() {
assertEquals("0 B", 0L.humanReadableSize())
}

@Test
fun get_negative() {
assertEquals("-54 B", (-54L).humanReadableSize())
}

@Test
fun get_bytes() {
assertEquals("1000 B", 1000L.humanReadableSize())
}

@Test
fun get_kilobytes() {
assertEquals("1.00 KB", 2f.pow(10).toLong().humanReadableSize())
}

@Test
fun get_arbitrary_kilobytes() {
assertEquals("4.88 KB", 5000L.humanReadableSize())
}

@Test
fun get_megabytes() {
assertEquals("1.00 MB", 2f.pow(20).toLong().humanReadableSize())
}

@Test
fun get_gigabytes() {
assertEquals("1.00 GB", 2f.pow(30).toLong().humanReadableSize())
}

@Test
fun get_terabytes() {
assertEquals("1.00 TB", 2f.pow(40).toLong().humanReadableSize())
}

@Test
fun get_petabytes() {
assertEquals("1.00 PB", 2F.pow(50).toLong().humanReadableSize())
}

@Test
fun get_exabytes() {
assertEquals("1.00 EB", 2F.pow(60).toLong().humanReadableSize())
}

@Test
fun get_max() {
assertEquals("8.00 EB", Long.MAX_VALUE.humanReadableSize())
}

@Test
fun get_near_max() {
assertEquals("8.00 EB", (Long.MAX_VALUE - 1).humanReadableSize())
}

@Test
fun get_min() {
assertEquals("-8.00 EB", Long.MIN_VALUE.humanReadableSize())
}

@Test
fun get_near_minimum() {
assertEquals("-8.00 EB", (Long.MIN_VALUE + 2).humanReadableSize())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

import platform.Foundation.NSNumber
import platform.Foundation.NSNumberFormatter
import platform.Foundation.NSNumberFormatterDecimalStyle
import kotlin.math.abs
import kotlin.math.sign

actual fun Long.humanReadableSize(): String {
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this)
if (absB < 1024) {
return "$this B"
}
var value = absB
val ci = "KMGTPE"
var ciIndex = 0
var i = 40
while (i >= 0 && absB > 0xfffccccccccccccL shr i) {
value = value shr 10
if (++ciIndex > ci.lastIndex) {
break
}
i -= 10
}
value *= sign(this.toDouble()).toLong()
val formatter = NSNumberFormatter().apply {
numberStyle = NSNumberFormatterDecimalStyle
maximumFractionDigits = 2u
minimumFractionDigits = 2u
}
return "${formatter.stringFromNumber(NSNumber(value / 1024.0))} ${ci[ciIndex]}B"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

import kotlin.math.abs
import kotlin.math.sign

actual fun Long.humanReadableSize(): String {
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this)
if (absB < 1024) {
return "$this B"
}
var value = absB
val ci = "KMGTPE"
var ciIndex = 0
var i = 40
while (i >= 0 && absB > 0xfffccccccccccccL shr i) {
value = value shr 10

if (++ciIndex > ci.length - 1) {
break
}
i -= 10
}
value *= sign(this.toDouble()).toLong()
return "${(value / 1024.0).asDynamic().toFixed(2)} ${ci[ciIndex]}B"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.cmgapps.common.curriculumvitae.format

import java.text.CharacterIterator
import java.text.StringCharacterIterator
import kotlin.math.abs
import kotlin.math.sign

actual fun Long.humanReadableSize(): String {
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this)
if (absB < 1024) {
return "$this B"
}
var value = absB
val ci: CharacterIterator = StringCharacterIterator("KMGTPE")
var i = 40
while (i >= 0 && absB > 0xfffccccccccccccL shr i) {
value = value shr 10
ci.next()
i -= 10
}
value *= sign(this.toDouble()).toLong()
return String.format("%.2f %cB", value / 1024.0, ci.current())
}
1 change: 1 addition & 0 deletions desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kotlin {
implementation(compose.desktop.currentOs)
implementation(compose.uiTooling)
implementation(libs.koin.core)
implementation(libs.koin.compose.mpp)
implementation(libs.ktor.client.java)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.coroutines.swing)
Expand Down
Loading

0 comments on commit 9695bcd

Please sign in to comment.