diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1b4eb5b..81d3386 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ kotlin-version = "2.0.0" # https://github.com/JetBrains/kotlin kotlin-coroutines = "1.8.1" kotlin-serialization-json = "1.7.0" # https://github.com/Kotlin/kotlinx.serialization -android-gradle = "8.4.2" # https://developer.android.com/studio/releases/gradle-plugin +android-gradle = "8.2.0" # https://developer.android.com/studio/releases/gradle-plugin # Shadow shadow = "7.1.2" # https://github.com/johnrengelman/shadow diff --git a/modules/ui-generator/src/main/kotlin/com/flipperdevices/ifrmvp/parser/UiGeneratorImpl.kt b/modules/ui-generator/src/main/kotlin/com/flipperdevices/ifrmvp/parser/UiGeneratorImpl.kt index 1402e24..c783107 100644 --- a/modules/ui-generator/src/main/kotlin/com/flipperdevices/ifrmvp/parser/UiGeneratorImpl.kt +++ b/modules/ui-generator/src/main/kotlin/com/flipperdevices/ifrmvp/parser/UiGeneratorImpl.kt @@ -24,9 +24,9 @@ class UiGeneratorImpl : UiGenerator { val keyIdentifier = IfrKeyIdentifier.Name(signal.name) IfrButton( data = when { - signal.name.contains("pwr", true) - || signal.name.contains("power", true) - || signal.name.contains("on", true) -> { + signal.name.contains("pwr", true) || + signal.name.contains("power", true) || + signal.name.contains("on", true) -> { IconButtonData( keyIdentifier = keyIdentifier, iconId = IconButtonData.IconType.POWER diff --git a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/CounterRepository.kt b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/CounterRepository.kt index eb57472..d62b13f 100644 --- a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/CounterRepository.kt +++ b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/CounterRepository.kt @@ -3,7 +3,6 @@ package com.flipperdevices.ifrmvp.backend.route.signal.data import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalOrderTable internal class CounterRepository { - fun countOrders(ifrFileId: Long): Long { return SignalOrderTable .select(SignalOrderTable.id) diff --git a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/DefaultSignalRepository.kt b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/DefaultSignalRepository.kt index dbe799d..57daa10 100644 --- a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/DefaultSignalRepository.kt +++ b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/DefaultSignalRepository.kt @@ -27,11 +27,18 @@ internal class DefaultSignalRepository { when { count == 0 -> andWhere { SignalTable.name.eq("power") - SignalTable.name.eq("on") + .or(SignalTable.name.eq("on")) .or(SignalTable.name.like("%power%")) .or(SignalTable.name.like("%on%")) } + count >= 1 -> andWhere { + SignalTable.name.neq("power") + .or(SignalTable.name.neq("on")) + .or(SignalTable.name.notLike("%power%")) + .or(SignalTable.name.notLike("%on%")) + } + else -> this } } @@ -44,23 +51,32 @@ internal class DefaultSignalRepository { ifrFile: IfrFileModel, signalRequestModel: SignalRequestModel, categorySingularDisplayName: String, + successCount: Long, ): SignalResponseModel { val signalModel = getSignal(ifrFile.id, signalRequestModel) - return if (signalModel == null) { - SignalResponseModel(ifrFileModel = ifrFile) - } else { - SignalResponseModel( - signalResponse = SignalResponse( - signalModel = signalModel, - data = SignalResponse.Data( - type = ButtonData.ButtonType.TEXT.name, - iconId = null, - text = signalModel.name - ), - categoryName = categorySingularDisplayName, - message = "Does $categorySingularDisplayName respond to button?" + return when { + signalModel == null && successCount == 0L -> { + SignalResponseModel() + } + + signalModel == null -> { + SignalResponseModel(ifrFileModel = ifrFile) + } + + else -> { + SignalResponseModel( + signalResponse = SignalResponse( + signalModel = signalModel, + data = SignalResponse.Data( + type = ButtonData.ButtonType.TEXT.name, + iconId = null, + text = signalModel.name + ), + categoryName = categorySingularDisplayName, + message = "Does $categorySingularDisplayName respond to button?" + ) ) - ) + } } } } diff --git a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/IrFileRepository.kt b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/IrFileRepository.kt index 004391c..94c5a43 100644 --- a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/IrFileRepository.kt +++ b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/IrFileRepository.kt @@ -8,7 +8,7 @@ import org.jetbrains.exposed.sql.selectAll internal class IrFileRepository { - fun getIrFile(signalRequestModel: SignalRequestModel): IfrFileModel? { + fun getNextIrFile(signalRequestModel: SignalRequestModel): IfrFileModel? { return IfrFileTable .selectAll() .andWhere { IfrFileTable.id.notInList(signalRequestModel.failedResults.map { it.ifrFileId }) } @@ -24,4 +24,22 @@ internal class IrFileRepository { ) }.firstOrNull() } + + fun getMostSuccessfulIfrFile(signalRequestModel: SignalRequestModel): IfrFileModel? { + val id = signalRequestModel.successResults + .groupBy { it.ifrFileId } + .maxByOrNull { it.value.size } + ?.key + return IfrFileTable + .selectAll() + .where { IfrFileTable.id eq id } + .map { + IfrFileModel( + id = it[IfrFileTable.id].value, + categoryId = it[IfrFileTable.categoryId].value, + brandId = it[IfrFileTable.brandId].value, + fileName = it[IfrFileTable.fileName] + ) + }.firstOrNull() + } } diff --git a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/presentation/SignalRouteRegistry.kt b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/presentation/SignalRouteRegistry.kt index c6295b3..9102777 100644 --- a/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/presentation/SignalRouteRegistry.kt +++ b/web-api/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/presentation/SignalRouteRegistry.kt @@ -23,51 +23,65 @@ internal class SignalRouteRegistry( private val signalByOrderRepository: SignalByOrderRepository ) : RouteRegistry { + private fun getCategorySingularDisplayName(categoryId: Long): String { + return CategoryMetaTable + .select(CategoryMetaTable.singularDisplayName) + .where { CategoryMetaTable.categoryId eq categoryId } + .limit(1) + .map { it[CategoryMetaTable.singularDisplayName] } + .firstOrNull() + ?: error("Could not find category $categoryId") + } + private fun getSignal(signalRequestModel: SignalRequestModel): SignalResponseModel { return transaction(database) { // Looking for ifr file id with non-failed results // If file not found then we don't have signals - val ifrFile = irFileRepository - .getIrFile(signalRequestModel) - ?: return@transaction SignalResponseModel() + val ifrFile = irFileRepository.getNextIrFile(signalRequestModel) + if (ifrFile == null) { + val mostSuccessIfrFile = irFileRepository.getMostSuccessfulIfrFile(signalRequestModel) + return@transaction SignalResponseModel(ifrFileModel = mostSuccessIfrFile) + } val orderCount = counterRepository.countOrders(ifrFile.id) + val successCount = signalRequestModel .successResults .count { it.ifrFileId == ifrFile.id } .toLong() - val categorySingularDisplayName = CategoryMetaTable - .select(CategoryMetaTable.singularDisplayName) - .where { CategoryMetaTable.categoryId eq signalRequestModel.categoryId } - .limit(1) - .map { it[CategoryMetaTable.singularDisplayName] } - .firstOrNull() ?: return@transaction SignalResponseModel() + + val categorySingularDisplayName = getCategorySingularDisplayName(signalRequestModel.categoryId) println("Success count: $successCount; Orders count: $orderCount") println("IfrFileId: ${ifrFile.id} category: ${ifrFile.categoryId} brand: ${ifrFile.brandId}") - if (orderCount == 1L && successCount >= 1) { - return@transaction SignalResponseModel(ifrFileModel = ifrFile) - } - // If orders empty, getting just by signal - // If orders not empty, getting by orders - if (orderCount == 0L) { - if (successCount >= MAX_SUCCESS_ROW) { - return@transaction SignalResponseModel(ifrFileModel = ifrFile) + return@transaction when { + orderCount == 1L && successCount >= 1 -> { + SignalResponseModel(ifrFileModel = ifrFile) + } + + orderCount == 0L && successCount >= MAX_SUCCESS_ROW -> { + SignalResponseModel(ifrFileModel = ifrFile) + } + + orderCount == 0L -> { + defaultSignalRepository.getDefaultSignal( + ifrFile = ifrFile, + signalRequestModel = signalRequestModel, + categorySingularDisplayName = categorySingularDisplayName, + successCount = successCount + ) + } + + else -> { + signalByOrderRepository.getSignalByOrder( + signalRequestModel = signalRequestModel, + ifrFile = ifrFile, + orderCount = orderCount, + successCount = successCount, + categorySingularDisplayName = categorySingularDisplayName, + ) } - return@transaction defaultSignalRepository.getDefaultSignal( - ifrFile = ifrFile, - signalRequestModel = signalRequestModel, - categorySingularDisplayName = categorySingularDisplayName - ) - } else { - return@transaction signalByOrderRepository.getSignalByOrder( - signalRequestModel = signalRequestModel, - ifrFile = ifrFile, - orderCount = orderCount, - successCount = successCount, - categorySingularDisplayName = categorySingularDisplayName, - ) } } }