Sfoglia il codice sorgente

perbaikan migration call partner

masarifyuli 2 settimane fa
parent
commit
81da7a5478

+ 10 - 1
config/general-setting.yml

@@ -182,4 +182,13 @@ redirectUrl:
 - http://localhost:4200/oauth2redirect
 - https://cmpd.telmessenger.com/oauth2redirect
 - https://app.insomnia.rest/oauth/redirect
-dataKey: RMyvjTZXDMBnCr3jymw9oHGUUzIJTHCULv9x%2B6xE4De56nu2OlXjkTWuk5%2B3QLfyE5PJQUBl9SIAuZOGmpIJBTHvNBFOUN%2BtwT0FzDUIKfD%2BdOcU4lJzNzo9mc2cMilw
+dataKey: aY6tKotSoUoMIi7JiTBZq%2B9DncnROCQuIFA4CyJXUMuktp4W0u5C9eZjKuE%2FC8LaLa7mh1RZlROXD10B%2BNuLQLMWT5KP1ibNurybHs0Ced49gXYJZlqA%2BmuDNpA9DXLe
+
+#database: 
+#  type: sqlserver
+#  host: 127.0.0.1
+#  port: 1433
+#  name: dbname
+#  username: sa
+#  password: password
+#  properties: encrypt=true;trustServerCertificate=true;

+ 2 - 1
config/migration.yml

@@ -54,7 +54,7 @@ migration:
     table: trunk
     history: trunkhistory
     group: null
-    unique: code
+    unique: code;pbx_uid
     attribute:
       id: history.trunk_id
       code: trunk_code
@@ -68,6 +68,7 @@ migration:
     table: costcenter
     history: costcenterhistory
     group: null
+    unique: code
     attribute:
       id: history.costcenter_id
       code: costcenter_code

+ 97 - 41
src/main/kotlin/com/datacomsolusindo/migration/data/FinalizedDataService.kt

@@ -17,6 +17,9 @@ import java.util.*
 import kotlin.math.log
 import kotlin.time.measureTimedValue
 
+val temporaryPrepareCallTransactionCallTo: MutableMap<String, Map<String, Any?>?> = mutableMapOf()
+val temporaryPrepareCallTransactionCallFrom: MutableMap<String, String?> = mutableMapOf()
+
 @Service
 class FinalizedDataService(
     private val cpDecrypt: CpDecrypt,
@@ -310,43 +313,82 @@ class FinalizedDataService(
     private fun findUidByCode(table: String, value: Any, field: String = "code", select: String = "uid"): String? {
         return if (value.toString().isBlank()) null else {
             val tableName = General.toTableName(table)
-            temporaryDataByCode["$tableName;$select;$value"] ?: run {
+            val keyCode = "$tableName;$select;$value"
+            if (temporaryDataByCode.any { a -> a.key == keyCode }) {
+                temporaryDataByCode[keyCode]
+            } else {
                 try {
-                    val fd = if (table == "trunk") " AND subscribed_no != 'new trunk' " else " "
+                    val fd = if (table == "trunk") " AND (subscribed_no != 'new trunk' OR subscribed_no IS NULL) " else " "
                     apiService.transaction { em ->
                         val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field $fd")
                             .setParameter(field, value.toString())
                             .resultList
                             .first() as String?
-                        temporaryDataByCode["$tableName;$select;$value"] = result
+                        temporaryDataByCode[keyCode] = result
                         result
                     }
                 } catch (e: Exception) {
+                    temporaryDataByCode[keyCode] = null
                     logger.info("failed find uid $tableName $field $value")
                     null
                 }
             }
+//            temporaryDataByCode["$tableName;$select;$value"] ?: run {
+//                try {
+//                    val fd = if (table == "trunk") " AND subscribed_no != 'new trunk' " else " "
+//                    apiService.transaction { em ->
+//                        val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field $fd")
+//                            .setParameter(field, value.toString())
+//                            .resultList
+//                            .first() as String?
+//                        temporaryDataByCode["$tableName;$select;$value"] = result
+//                        result
+//                    }
+//                } catch (e: Exception) {
+//                    logger.info("failed find uid $tableName $field $value")
+//                    null
+//                }
+//            }
         }
     }
 
     private fun findCodeByUid(table: String, value: Any, field: String = "uid", select: String = "code"): String? {
         return if (value.toString().isBlank()) null else {
             val tableName = General.toTableName(table)
-            temporaryDataByCode["$tableName;$select;$value"] ?: run {
+            val keyCode = "$tableName;$select;$value"
+            if (temporaryDataByCode.any { a -> a.key == keyCode }) {
+                temporaryDataByCode[keyCode]
+            } else {
                 try {
                     apiService.transaction { em ->
                         val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field")
                             .setParameter(field, value.toString())
                             .resultList
                             .first() as String?
-                        temporaryDataByCode["$tableName;$select;$value"] = result
+                        temporaryDataByCode[keyCode] = result
                         result
                     }
                 } catch (e: Exception) {
+                    temporaryDataByCode[keyCode] = null
                     logger.info("failed find code $tableName $field $value")
                     null
                 }
             }
+//            temporaryDataByCode["$tableName;$select;$value"] ?: run {
+//                try {
+//                    apiService.transaction { em ->
+//                        val result = em.createNativeQuery("SELECT $select FROM $tableName WHERE $field = :$field")
+//                            .setParameter(field, value.toString())
+//                            .resultList
+//                            .first() as String?
+//                        temporaryDataByCode["$tableName;$select;$value"] = result
+//                        result
+//                    }
+//                } catch (e: Exception) {
+//                    logger.info("failed find code $tableName $field $value")
+//                    null
+//                }
+//            }
         }
     }
 
@@ -500,6 +542,7 @@ class FinalizedDataService(
 
             1
         } catch (e: Exception) {
+            println(Util.mapper.writeValueAsString(map))
             logger.error("failed insert data $table", e)
             0
         }
@@ -522,7 +565,7 @@ class FinalizedDataService(
         val seg8 = data["duration"]
         val seg9 = data["accessNumber"] ?: ""
         val seg10 = ""
-        val seg11 = data["trunk_id"]?.let { findCodeByUid("trunk", it) ?: "" }
+        val seg11 = data["trunk_id"]?.let { findCodeByUid("trunk", it) ?: "" } ?: ""
         val seg12 = ""
         val seg13 = data["number"] ?: ""
         val seg14 = data["callerNumber"] ?: ""
@@ -586,49 +629,62 @@ class FinalizedDataService(
     }
 
     private fun getCallTo(number: String): Map<String, Any?>? {
-        return try {
-            apiService.transaction { em ->
-                val hlr = em.createNativeQuery(
-                    "SELECT TOP 1 prefix, provider_uid, area_uid, phone_type, zone_uid, domain \n" +
-                            "FROM hlr\n" +
+        val keyCode = "getCall-$number"
+        return if (temporaryPrepareCallTransactionCallTo.any { a -> a.key == keyCode })
+            temporaryPrepareCallTransactionCallTo[keyCode]
+        else {
+            try {
+                apiService.transaction { em ->
+                    val hlr = em.createNativeQuery(
+                        "SELECT TOP 1 prefix, provider_uid, area_uid, phone_type, zone_uid, domain \n" +
+                                "FROM hlr\n" +
 //                            "WHERE '$number' LIKE prefix + '%'\n" +
-                            "WHERE '$number' LIKE prefix + '%'\n" +
-                            "ORDER BY LEN(prefix) DESC"
-                ).singleResult as Array<Any?>
-                mapOf(
-                    "prefix" to hlr[0].toString(),
-                    "provider_to_uid" to hlr[1].toString(),
-                    "area_uid" to hlr[2].toString(),
-                    "phone_type" to hlr[3].toString(),
-                    "zone_uid" to hlr[4].toString(),
-                    "domain" to hlr[5].toString()
-                )
+                                "WHERE '$number' LIKE prefix + '%'\n" +
+                                "ORDER BY LEN(prefix) DESC"
+                    ).singleResult as Array<Any?>
+                    val result = mapOf(
+                        "prefix" to hlr[0].toString(),
+                        "provider_to_uid" to hlr[1].toString(),
+                        "area_uid" to hlr[2].toString(),
+                        "phone_type" to hlr[3].toString(),
+                        "zone_uid" to hlr[4].toString(),
+                        "domain" to hlr[5].toString()
+                    )
+                    temporaryPrepareCallTransactionCallTo[keyCode] = result
+                    result
+                }
+            } catch (e: Exception) {
+                temporaryPrepareCallTransactionCallTo[keyCode] = null
+                logger.info("failed get call to attribute number $number")
+                null
             }
-        } catch (e: Exception) {
-            logger.info("failed get call to attribute number $number")
-//            logger.error("failed get call to attribute number $number", e)
-            null
         }
     }
 
     private fun getCallFrom(areaUid: String, trunkCode: String?, pbxCode: String?): String? {
-        return try {
-            val trunk = trunkCode?.let { findUidByCode("trunk", it, select = "provider_uid") }
-            val pbx = pbxCode?.let { findUidByCode("pbx", it, select = "provider_uid") }
+        val keyCode = "getCallFrom-$areaUid-${trunkCode ?: "#"}-${pbxCode ?: "#"}"
+        return if (temporaryPrepareCallTransactionCallFrom.any { a -> a.key == keyCode })
+            temporaryPrepareCallTransactionCallFrom[keyCode]
+        else {
+            try {
+                val trunk = trunkCode?.let { findUidByCode("trunk", it, select = "provider_uid") }
+                val pbx = pbxCode?.let { findUidByCode("pbx", it, select = "provider_uid") }
 
-            apiService.transaction { em ->
-                val hlr = em.createNativeQuery(
-                    "SELECT TOP 1 domain \n" +
-                            "FROM hlr\n" +
-//                            "WHERE '$number' LIKE prefix + '%'\n" +
-                            "WHERE area_uid = '$areaUid' AND provider_uid = '${trunk ?: pbx ?: "#uid"}' \n"
-                ).singleResult as Array<Any?>
-                hlr[0].toString()
+                apiService.transaction { em ->
+                    val hlr = em.createNativeQuery(
+                        "SELECT TOP 1 domain \n" +
+                                "FROM hlr\n" +
+                                "WHERE area_uid = '$areaUid' AND provider_uid = '${trunk ?: pbx ?: "#uid"}' \n"
+                    ).singleResult as Array<Any?>
+                    val result = hlr[0].toString()
+                    temporaryPrepareCallTransactionCallFrom[keyCode] = result
+                    result
+                }
+            } catch (e: Exception) {
+                temporaryPrepareCallTransactionCallFrom[keyCode] = null
+                logger.info("failed get call from attribute area ${areaUid} trunk $trunkCode pbx $pbxCode")
+                null
             }
-        } catch (e: Exception) {
-            logger.info("failed get call from attribute trunk $trunkCode pbx $pbxCode")
-//            logger.error("failed get call to attribute number $number", e)
-            null
         }
     }
 

+ 10 - 8
src/main/kotlin/com/datacomsolusindo/migration/service/MigrationFileService.kt

@@ -18,6 +18,7 @@ import java.nio.charset.StandardCharsets
 import java.nio.file.Files
 import java.nio.file.Path
 import java.nio.file.Paths
+import java.nio.file.StandardCopyOption
 import java.time.LocalDateTime
 import java.time.format.DateTimeFormatter
 import kotlin.time.measureTimedValue
@@ -50,23 +51,25 @@ class MigrationFileService(
         return if (ext.isNotEmpty()) "${base}_$key.$ext" else "${base}_$key"
     }
 
-    fun renameToTemp(path: Path) {
+    fun renameToTemp(path: Path): Path? {
         val time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
         val newName = "temp_${time}_${path.fileName}"
         val target = path.parent.resolve(newName)
-//        Files.move(path, target, StandardCopyOption.ATOMIC_MOVE)
+        return Files.move(path, target, StandardCopyOption.REPLACE_EXISTING)
     }
 
     fun execute() {
         val migrationFile = prepareFile()
         runBlocking {
             migrationFile.map {
-                renameToTemp(it.migrationFile)
+                val pathMigrationFile = renameToTemp(it.migrationFile)
+                val pathHistoryFile = it.historyFile?.let { i -> renameToTemp(i) }
+                val pathGroupFile = it.groupFile?.let { i -> renameToTemp(i) }
                 val (migrationData, historyData, groupData) =
                     prepareMigrationData(
-                        it.migrationFile.toFile(),
-                        it.historyFile?.toFile(),
-                        it.groupFile?.toFile()
+                        pathMigrationFile!!.toFile(),
+                        pathHistoryFile?.toFile(),
+                        pathGroupFile?.toFile()
                     )
 
                 it.rootData = migrationData
@@ -112,7 +115,7 @@ class MigrationFileService(
                             f.fileName.toString().endsWith(toHistoryName(filename, "history"))
                         },
                         groupFile = additionalFiles.firstOrNull { f ->
-                            f.fileName.toString().endsWith(toHistoryName(filename, "history"))
+                            f.fileName.toString().endsWith(toHistoryName(filename, "group"))
                         }
                     )
                 )
@@ -163,7 +166,6 @@ class MigrationFileService(
 
         return if (file.name.contains("trunk_history")) {
             val validTrunk = result.value.filter { f -> f.get("trunk_number")?.toString()?.let { t -> t != "new trunk" } ?: true }
-            validTrunk.forEach { println(Util.mapper.writeValueAsString(it)) }
             validTrunk
         } else result.value
 

+ 2 - 1
src/main/kotlin/com/datacomsolusindo/migration/service/WorkerMigrationService.kt

@@ -54,7 +54,8 @@ class WorkerMigrationService(
                 if (attribute.table == "calltransaction") {
                     insertDataService.updateRawData()
                 }
-                val data = migrationData.second//.take(100)
+                val data = migrationData.second
+            //.take(100)
                 try {
                     val chunkData = data.chunked(500)
                     logger.info("migration table ${attribute.table} with total data ${data.size} and chunk data ${chunkData.size}")