mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
merge: B-M4e Android usage/storage dashboards + settings
# Conflicts: # android/app/src/main/kotlin/app/hapi/companion/Navigation.kt
This commit is contained in:
@@ -37,6 +37,7 @@ import app.hapi.protocol.wire.SkillsResponse
|
||||
import app.hapi.protocol.wire.SlashCommandsResponse
|
||||
import app.hapi.protocol.wire.SpawnResponse
|
||||
import app.hapi.protocol.wire.SpawnSessionRequest
|
||||
import app.hapi.protocol.wire.SqliteStorageUsageResponse
|
||||
import app.hapi.protocol.wire.SteerQueuedMessageResponse
|
||||
import app.hapi.protocol.wire.TranscriptionProvidersResponse
|
||||
import app.hapi.protocol.wire.TranscriptionResponse
|
||||
@@ -44,6 +45,7 @@ import app.hapi.protocol.wire.UnregisterDeviceRequest
|
||||
import app.hapi.protocol.wire.UpdateSessionSummaryRequest
|
||||
import app.hapi.protocol.wire.UploadFileRequest
|
||||
import app.hapi.protocol.wire.UploadFileResponse
|
||||
import app.hapi.protocol.wire.UsageSummaryResponse
|
||||
import app.hapi.protocol.wire.VisibilityRequest
|
||||
import java.io.IOException
|
||||
import kotlin.coroutines.resume
|
||||
@@ -593,6 +595,26 @@ class HapiApi(
|
||||
DeleteUploadRequest(path).toJsonBody(),
|
||||
)
|
||||
|
||||
// ---------------------------------------------------- usage & storage --
|
||||
// Owner-only dashboards: both 403 unless the JWT namespace is `default`
|
||||
// (`docs/api/client-contract/rest.md#usage--storage-owner-only`).
|
||||
|
||||
/**
|
||||
* `GET /api/usage/summary?range=7d|30d|all&timeZone=<IANA>` — token-usage
|
||||
* dashboard aggregates. The hub validates [timeZone] (400 when invalid)
|
||||
* and buckets `daily` by that zone's calendar days.
|
||||
*/
|
||||
suspend fun getUsageSummary(range: String = "7d", timeZone: String = "UTC"): UsageSummaryResponse {
|
||||
val target = url("api", "usage", "summary")
|
||||
.addQueryParameter("range", range)
|
||||
.addQueryParameter("timeZone", timeZone)
|
||||
return request("GET", target.build())
|
||||
}
|
||||
|
||||
/** `GET /api/storage/sqlite` — hub db/wal/shm file sizes. */
|
||||
suspend fun getSqliteStorageUsage(): SqliteStorageUsageResponse =
|
||||
request("GET", url("api", "storage", "sqlite").build())
|
||||
|
||||
// ------------------------------------------------------------- devices --
|
||||
|
||||
/** `POST /api/devices/register` (upsert) — FCM contract (`native-companion-contract.md`). */
|
||||
|
||||
@@ -283,6 +283,48 @@ class HapiApiTest {
|
||||
|
||||
// --------------------------------------------------- health & binaries --
|
||||
|
||||
@Test
|
||||
fun `usage summary sends range and timeZone and decodes with auth`() {
|
||||
server.enqueue(
|
||||
ok(
|
||||
"""
|
||||
{"range":{"from":null,"to":1755600000000},
|
||||
"totals":{"inputTokens":10,"outputTokens":2,"cacheReadTokens":4,"cacheCreationTokens":1,
|
||||
"totalTokens":12,"uncachedTokens":6,"requests":3,"sessions":2},
|
||||
"daily":[],"byAgent":[],"byModel":[],"updatedAt":1755600000000}
|
||||
""".trimIndent()
|
||||
)
|
||||
)
|
||||
|
||||
val summary = runBlocking { session.api.getUsageSummary(range = "all", timeZone = "Asia/Shanghai") }
|
||||
|
||||
val request = server.takeRequest()
|
||||
val url = requireNotNull(request.requestUrl)
|
||||
assertEquals("/api/usage/summary", url.encodedPath)
|
||||
assertEquals("all", url.queryParameter("range"))
|
||||
assertEquals("Asia/Shanghai", url.queryParameter("timeZone"))
|
||||
assertEquals("Bearer $jwt", request.getHeader("Authorization"))
|
||||
assertNull(summary.range.from)
|
||||
assertEquals(12L, summary.totals.totalTokens)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sqlite storage usage decodes and a 403 surfaces as coded ApiError`() {
|
||||
server.enqueue(
|
||||
ok("""{"path":"/x/hapi.db","databaseBytes":100,"walBytes":20,"shmBytes":4,"totalBytes":124}""")
|
||||
)
|
||||
val storage = runBlocking { session.api.getSqliteStorageUsage() }
|
||||
assertEquals("/api/storage/sqlite", server.takeRequest().path)
|
||||
assertEquals(124L, storage.totalBytes)
|
||||
|
||||
server.enqueue(
|
||||
MockResponse().setResponseCode(403)
|
||||
.setBody("""{"error":"Storage usage is only available to the hub owner"}""")
|
||||
)
|
||||
val error = assertFailsWith<ApiError> { runBlocking { session.api.getSqliteStorageUsage() } }
|
||||
assertEquals(403, error.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `health is fetched without an authorization header`() {
|
||||
server.enqueue(ok("""{"status":"ok","protocolVersion":1,"capabilities":{"titleSuggestion":true,"brandNew":true}}"""))
|
||||
|
||||
Reference in New Issue
Block a user