Skip to content

Commit

Permalink
fix signal setup
Browse files Browse the repository at this point in the history
- fix get signal by power
- fix empty signal after at least one exists
  • Loading branch information
makeevrserg committed Jul 12, 2024
1 parent 61aae45 commit f96c8e7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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?"
)
)
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) }
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}
Expand Down

0 comments on commit f96c8e7

Please sign in to comment.