Skip to content

Commit

Permalink
Added count column to server entity
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Dec 30, 2023
1 parent 7bc318a commit 1af5089
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DebugDaoImpl(private val database: OutlineDatabase) : DebugDao {
BuildConfig.DEBUG_SERVER1,
BuildConfig.DEBUG_SERVER2,
).forEachIndexed { index, url ->
serverQueries.insert(ServerEntity(url, true, "Server ${index + 1}", null))
serverQueries.insert(ServerEntity(url, true, "Server ${index + 1}", null, null))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/sirekanyan/outline/api/OutlineApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class OutlineApi {

suspend fun getServer(server: Server): Server {
val name = request(HttpMethod.Get, server, "server").body<ServerNameResponse>().name
return Server(server.id, server.insecure, name, server.traffic)
return Server(server.id, server.insecure, name, server.traffic, server.count)
}

suspend fun renameServer(server: Server, name: String) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/sirekanyan/outline/api/model/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fun List<KeyEntity>.fromEntities(server: Server): List<Key> =

fun List<KeyWithServerEntity>.fromEntities(): List<Key> =
map { entity ->
val server = Server(entity.serverId, entity.insecure, entity.serverName, entity.serverTraffic)
val server = Server(entity.serverId, entity.insecure, entity.serverName, entity.serverTraffic, entity.serverCount)
Key(server, entity.id, entity.url, entity.name, entity.traffic)
}

Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/org/sirekanyan/outline/api/model/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import kotlinx.parcelize.Parcelize
import org.sirekanyan.outline.db.model.ServerEntity

fun createServerEntity(url: String, insecure: Boolean): Server =
Server(url, insecure, name = "", traffic = null)
Server(url, insecure, name = "", traffic = null, count = null)

fun ServerEntity.fromEntity(): Server =
Server(id, insecure, name, traffic)
Server(id, insecure, name, traffic, count)

fun List<ServerEntity>.fromEntities(): List<Server> =
map(ServerEntity::fromEntity)

fun Server.toEntity(): ServerEntity =
ServerEntity(id, insecure, name, traffic)
ServerEntity(id, insecure, name, traffic, count)

fun List<Server>.toEntities(): List<ServerEntity> =
map(Server::toEntity)
Expand All @@ -26,6 +26,7 @@ class Server(
val insecure: Boolean,
val name: String,
val traffic: Long?,
val count: Long?,
) : Parcelable {

fun getHost(): String =
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/org/sirekanyan/outline/db/ServerDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class ServerDao(private val database: OutlineDatabase) {
queries.insert(server)
}

fun update(id: String, traffic: Long) {
queries.update(id, traffic)
fun update(id: String, traffic: Long, count: Long) {
queries.transaction {
queries.updateTraffic(id, traffic)
queries.updateCount(id, count)
}
}

fun insertAll(servers: List<ServerEntity>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class KeyRepository(
withContext(IO) {
val keys = api.getKeys(server)
keyDao.update(server, keys.toEntities())
serverDao.update(server.id, keys.sumOf { it.traffic ?: 0 })
serverDao.update(
id = server.id,
traffic = keys.sumOf { it.traffic ?: 0 },
count = keys.size.toLong(),
)
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/sqldelight/migrations/3.sqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE ServerEntity ADD COLUMN count INTEGER;

DROP VIEW IF EXISTS KeyWithServerEntity;
CREATE VIEW KeyWithServerEntity AS
SELECT k.*, s.insecure, s.name serverName, s.traffic serverTraffic, s.count serverCount
FROM KeyEntity k, ServerEntity s
WHERE k.serverId = s.id;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS KeyEntity (
);

CREATE VIEW IF NOT EXISTS KeyWithServerEntity AS
SELECT k.*, s.insecure, s.name serverName, s.traffic serverTraffic
SELECT k.*, s.insecure, s.name serverName, s.traffic serverTraffic, s.count serverCount
FROM KeyEntity k, ServerEntity s
WHERE k.serverId = s.id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ CREATE TABLE IF NOT EXISTS ServerEntity (
id TEXT NOT NULL PRIMARY KEY,
insecure INTEGER AS kotlin.Boolean NOT NULL DEFAULT 0,
name TEXT NOT NULL DEFAULT '',
traffic INTEGER
traffic INTEGER,
count INTEGER
);

selectAll:
Expand All @@ -11,9 +12,12 @@ SELECT * FROM ServerEntity ORDER BY id;
insert:
INSERT OR REPLACE INTO ServerEntity VALUES ?;

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

updateCount:
UPDATE ServerEntity SET count = ?2 WHERE id = ?1;

delete:
DELETE FROM ServerEntity WHERE id = ?;

Expand Down

0 comments on commit 1af5089

Please sign in to comment.