Просмотр исходного кода

Added count column to server entity

Vadik Sirekanyan 2 лет назад
Родитель
Сommit
1af5089f3f

+ 1 - 1
app/src/debug/kotlin/org/sirekanyan/outline/db/DebugDaoImpl.kt

@@ -23,7 +23,7 @@ class DebugDaoImpl(private val database: OutlineDatabase) : DebugDao {
                 BuildConfig.DEBUG_SERVER1,
                 BuildConfig.DEBUG_SERVER1,
                 BuildConfig.DEBUG_SERVER2,
                 BuildConfig.DEBUG_SERVER2,
             ).forEachIndexed { index, url ->
             ).forEachIndexed { index, url ->
-                serverQueries.insert(ServerEntity(url, true, "Server ${index + 1}", null))
+                serverQueries.insert(ServerEntity(url, true, "Server ${index + 1}", null, null))
             }
             }
         }
         }
     }
     }

+ 1 - 1
app/src/main/java/org/sirekanyan/outline/api/OutlineApi.kt

@@ -63,7 +63,7 @@ class OutlineApi {
 
 
     suspend fun getServer(server: Server): Server {
     suspend fun getServer(server: Server): Server {
         val name = request(HttpMethod.Get, server, "server").body<ServerNameResponse>().name
         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) {
     suspend fun renameServer(server: Server, name: String) {

+ 1 - 1
app/src/main/java/org/sirekanyan/outline/api/model/Key.kt

@@ -17,7 +17,7 @@ fun List<KeyEntity>.fromEntities(server: Server): List<Key> =
 
 
 fun List<KeyWithServerEntity>.fromEntities(): List<Key> =
 fun List<KeyWithServerEntity>.fromEntities(): List<Key> =
     map { entity ->
     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)
         Key(server, entity.id, entity.url, entity.name, entity.traffic)
     }
     }
 
 

+ 4 - 3
app/src/main/java/org/sirekanyan/outline/api/model/Server.kt

@@ -6,16 +6,16 @@ import kotlinx.parcelize.Parcelize
 import org.sirekanyan.outline.db.model.ServerEntity
 import org.sirekanyan.outline.db.model.ServerEntity
 
 
 fun createServerEntity(url: String, insecure: Boolean): Server =
 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 =
 fun ServerEntity.fromEntity(): Server =
-    Server(id, insecure, name, traffic)
+    Server(id, insecure, name, traffic, count)
 
 
 fun List<ServerEntity>.fromEntities(): List<Server> =
 fun List<ServerEntity>.fromEntities(): List<Server> =
     map(ServerEntity::fromEntity)
     map(ServerEntity::fromEntity)
 
 
 fun Server.toEntity(): ServerEntity =
 fun Server.toEntity(): ServerEntity =
-    ServerEntity(id, insecure, name, traffic)
+    ServerEntity(id, insecure, name, traffic, count)
 
 
 fun List<Server>.toEntities(): List<ServerEntity> =
 fun List<Server>.toEntities(): List<ServerEntity> =
     map(Server::toEntity)
     map(Server::toEntity)
@@ -26,6 +26,7 @@ class Server(
     val insecure: Boolean,
     val insecure: Boolean,
     val name: String,
     val name: String,
     val traffic: Long?,
     val traffic: Long?,
+    val count: Long?,
 ) : Parcelable {
 ) : Parcelable {
 
 
     fun getHost(): String =
     fun getHost(): String =

+ 5 - 2
app/src/main/java/org/sirekanyan/outline/db/ServerDao.kt

@@ -20,8 +20,11 @@ class ServerDao(private val database: OutlineDatabase) {
         queries.insert(server)
         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>) {
     fun insertAll(servers: List<ServerEntity>) {

+ 5 - 1
app/src/main/java/org/sirekanyan/outline/repository/KeyRepository.kt

@@ -30,7 +30,11 @@ class KeyRepository(
         withContext(IO) {
         withContext(IO) {
             val keys = api.getKeys(server)
             val keys = api.getKeys(server)
             keyDao.update(server, keys.toEntities())
             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(),
+            )
         }
         }
     }
     }
 
 

+ 7 - 0
app/src/main/sqldelight/migrations/3.sqm

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

+ 1 - 1
app/src/main/sqldelight/org/sirekanyan/outline/db/model/KeyEntity.sq

@@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS KeyEntity (
 );
 );
 
 
 CREATE VIEW IF NOT EXISTS KeyWithServerEntity AS
 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
     FROM KeyEntity k, ServerEntity s
     WHERE k.serverId = s.id;
     WHERE k.serverId = s.id;
 
 

+ 6 - 2
app/src/main/sqldelight/org/sirekanyan/outline/db/model/ServerEntity.sq

@@ -2,7 +2,8 @@ CREATE TABLE IF NOT EXISTS ServerEntity (
   id TEXT NOT NULL PRIMARY KEY,
   id TEXT NOT NULL PRIMARY KEY,
   insecure INTEGER AS kotlin.Boolean NOT NULL DEFAULT 0,
   insecure INTEGER AS kotlin.Boolean NOT NULL DEFAULT 0,
   name TEXT NOT NULL DEFAULT '',
   name TEXT NOT NULL DEFAULT '',
-  traffic INTEGER
+  traffic INTEGER,
+  count INTEGER
 );
 );
 
 
 selectAll:
 selectAll:
@@ -11,9 +12,12 @@ SELECT * FROM ServerEntity ORDER BY id;
 insert:
 insert:
 INSERT OR REPLACE INTO ServerEntity VALUES ?;
 INSERT OR REPLACE INTO ServerEntity VALUES ?;
 
 
-update:
+updateTraffic:
 UPDATE ServerEntity SET traffic = ?2 WHERE id = ?1;
 UPDATE ServerEntity SET traffic = ?2 WHERE id = ?1;
 
 
+updateCount:
+UPDATE ServerEntity SET count = ?2 WHERE id = ?1;
+
 delete:
 delete:
 DELETE FROM ServerEntity WHERE id = ?;
 DELETE FROM ServerEntity WHERE id = ?;