Skip to content

Commit

Permalink
Removed unnecessary metrics api call
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Dec 30, 2023
1 parent 2dcc950 commit b9c99e1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/org/sirekanyan/outline/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class App : Application() {
private val database by lazy {
OutlineDatabase(AndroidSqliteDriver(OutlineDatabase.Schema, this, "outline.db"))
}
val serverRepository by lazy { ServerRepository(api, ServerDao(database)) }
val keyRepository by lazy { KeyRepository(api, KeyDao(database)) }
private val serverDao = ServerDao(database)
val serverRepository by lazy { ServerRepository(api, serverDao) }
val keyRepository by lazy { KeyRepository(api, KeyDao(database), serverDao) }
val prefsDao by lazy { KeyValueDao(database) }
val debugDao: DebugDao by lazy { DebugDaoImpl(database) }

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/org/sirekanyan/outline/api/OutlineApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class OutlineApi {

suspend fun getServer(server: Server): Server {
val name = request(HttpMethod.Get, server, "server").body<ServerNameResponse>().name
val transferMetrics = getTransferMetrics(server)?.bytesTransferredByUserId
return Server(server.id, server.insecure, name, transferMetrics?.values?.sum())
return Server(server.id, server.insecure, name, server.traffic)
}

suspend fun renameServer(server: Server, name: String) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/db/ServerDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ServerDao(private val database: OutlineDatabase) {
queries.insert(server)
}

fun update(id: String, traffic: Long) {
queries.update(id, traffic)
}

fun insertAll(servers: List<ServerEntity>) {
queries.transaction {
servers.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import org.sirekanyan.outline.api.model.Server
import org.sirekanyan.outline.api.model.fromEntities
import org.sirekanyan.outline.api.model.toEntities
import org.sirekanyan.outline.db.KeyDao
import org.sirekanyan.outline.db.ServerDao
import org.sirekanyan.outline.db.model.KeyWithServerEntity

class KeyRepository(private val api: OutlineApi, private val keyDao: KeyDao) {
class KeyRepository(
private val api: OutlineApi,
private val keyDao: KeyDao,
private val serverDao: ServerDao,
) {

fun observeKeys(server: Server): Flow<List<Key>> =
keyDao.observe(server).mapToList(IO).map { it.fromEntities(server) }
Expand All @@ -25,6 +30,7 @@ class KeyRepository(private val api: OutlineApi, private val keyDao: KeyDao) {
withContext(IO) {
val keys = api.getKeys(server)
keyDao.update(server, keys.toEntities())
serverDao.update(server.id, keys.sumOf { it.traffic ?: 0 })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ SELECT * FROM ServerEntity ORDER BY id;
insert:
INSERT OR REPLACE INTO ServerEntity VALUES ?;

update:
UPDATE ServerEntity SET traffic = ?2 WHERE id = ?1;

delete:
DELETE FROM ServerEntity WHERE id = ?;

Expand Down

0 comments on commit b9c99e1

Please sign in to comment.