-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix empty content * refactor and fix orders * fix wrong signal inheritance * add simple test for files * fix failed signals * fix sort by signal count * up version 0.8.0
- Loading branch information
1 parent
9ceea4c
commit cf68c08
Showing
17 changed files
with
568 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ org.gradle.parallel=true | |
makeevrserg.project.name=IRDBBackend | ||
makeevrserg.project.url=https://github.com/flipperdevices/IRDB-Backend | ||
makeevrserg.project.group=com.flipperdevices.ifrmvp.backend | ||
makeevrserg.project.version.string=0.7.2 | ||
makeevrserg.project.version.string=0.8.0 | ||
makeevrserg.project.description=Api for IfrSample | ||
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected] | ||
# Java | ||
|
10 changes: 10 additions & 0 deletions
10
modules/core/src/main/kotlin/com/flipperdevices/ifrmvp/backend/core/logging/Loggable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.flipperdevices.ifrmvp.backend.core.logging | ||
|
||
interface Loggable { | ||
fun info(msg: () -> String) | ||
fun debug(msg: () -> String) | ||
fun error(throwable: Throwable? = null, msg: () -> String) | ||
|
||
class Default(tag: String) : Loggable by Slf4jLoggable(tag) | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
modules/core/src/main/kotlin/com/flipperdevices/ifrmvp/backend/core/logging/Slf4jLoggable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.flipperdevices.ifrmvp.backend.core.logging | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
class Slf4jLoggable(tag: String) : Loggable { | ||
private val logger: Logger = LoggerFactory.getLogger(tag) | ||
|
||
override fun info(msg: () -> String) { | ||
logger.debug(msg.invoke()) | ||
} | ||
|
||
override fun debug(msg: () -> String) { | ||
logger.debug(msg.invoke()) | ||
} | ||
|
||
override fun error(throwable: Throwable?, msg: () -> String) { | ||
if (throwable == null) logger.error(msg.invoke()) | ||
else logger.error(msg.invoke(), throwable) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...ain/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/IncludedFilesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.flipperdevices.ifrmvp.backend.route.signal.data | ||
|
||
import com.flipperdevices.ifrmvp.backend.core.logging.Loggable | ||
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileTable | ||
import com.flipperdevices.ifrmvp.backend.db.signal.table.InfraredFileToSignalTable | ||
import com.flipperdevices.ifrmvp.backend.model.SignalRequestModel | ||
import com.flipperdevices.ifrmvp.backend.route.signal.data.model.IncludedFile | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.Join | ||
import org.jetbrains.exposed.sql.JoinType | ||
import org.jetbrains.exposed.sql.SortOrder | ||
import org.jetbrains.exposed.sql.alias | ||
import org.jetbrains.exposed.sql.andWhere | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
class IncludedFilesRepository( | ||
private val database: Database | ||
) : Loggable by Loggable.Default("IncludedFilesRepository") { | ||
|
||
/** | ||
* This is a list of files, which contains every instance of [successSignalIds] | ||
* aka list<SignalID>().containsAll(successSignal1,successSignal2) | ||
*/ | ||
private fun getWhiteListedFileIds(successSignalIds: List<Long>): List<Long> { | ||
var join: Join? = null | ||
successSignalIds.forEachIndexed { i, id -> | ||
join = (join ?: InfraredFileToSignalTable) | ||
.join( | ||
otherTable = InfraredFileToSignalTable.alias("F$i"), | ||
onColumn = InfraredFileToSignalTable.infraredFileId, | ||
otherColumn = InfraredFileToSignalTable.alias("F$i")[InfraredFileToSignalTable.infraredFileId], | ||
joinType = JoinType.INNER | ||
) | ||
} | ||
var query = join | ||
?.select(InfraredFileToSignalTable.infraredFileId) | ||
?.withDistinct(true) | ||
successSignalIds.forEachIndexed { index, id -> | ||
query = if (index == 0) { | ||
query?.where { InfraredFileToSignalTable.signalId eq id } | ||
} else { | ||
query?.andWhere { | ||
InfraredFileToSignalTable.alias("F$index")[InfraredFileToSignalTable.signalId] eq id | ||
} | ||
} | ||
} | ||
return when { | ||
successSignalIds.isEmpty() -> emptyList() | ||
else -> transaction(database) { | ||
query?.map { it[InfraredFileToSignalTable.infraredFileId].value }.orEmpty() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* If any signal is failed then we skip this file | ||
*/ | ||
private fun getBlackListedFileIds(failedSignalIds: List<Long>): List<Long> { | ||
return when { | ||
failedSignalIds.isEmpty() -> emptyList() | ||
else -> transaction(database) { | ||
InfraredFileToSignalTable.select(InfraredFileToSignalTable.infraredFileId) | ||
.withDistinct(true) | ||
.where { InfraredFileToSignalTable.signalId inList failedSignalIds } | ||
.map { it[InfraredFileToSignalTable.infraredFileId].value } | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Keep only those files, in which signals have been successfully passed | ||
* | ||
* SELECT INFRARED_FILE."id", INFRARED_FILE."signal_count" | ||
* FROM INFRARED_FILE JOIN INFRARED_FILE_TO_SIGNAL on INFRARED_FILE_TO_SIGNAL."infrared_file_id" = INFRARED_FILE."id" | ||
* group by INFRARED_FILE."id" | ||
* order by INFRARED_FILE."signal_count" desc | ||
*/ | ||
suspend fun findIncludedFiles(signalRequestModel: SignalRequestModel): List<IncludedFile> { | ||
info { "#findIncludedFiles invoked" } | ||
|
||
val excludedFileIds = getBlackListedFileIds( | ||
failedSignalIds = signalRequestModel.failedResults | ||
.map(SignalRequestModel.SignalResultData::signalId) | ||
) | ||
info { "#findIncludedFiles excludedFileIds: $excludedFileIds" } | ||
val includedFileIds = getWhiteListedFileIds( | ||
successSignalIds = signalRequestModel.successResults | ||
.map(SignalRequestModel.SignalResultData::signalId) | ||
) | ||
info { "#findIncludedFiles includedFileIds: $includedFileIds" } | ||
return transaction(database) { | ||
InfraredFileTable | ||
// Main query | ||
.select(InfraredFileTable.id, InfraredFileTable.signalCount) | ||
.groupBy(InfraredFileTable.id, InfraredFileTable.signalCount) | ||
.orderBy(InfraredFileTable.signalCount to SortOrder.DESC) | ||
.where { InfraredFileTable.brandId eq signalRequestModel.brandId } | ||
.let { nextQuery -> | ||
if (includedFileIds.isEmpty()) nextQuery | ||
else nextQuery.andWhere { InfraredFileTable.id inList includedFileIds } | ||
} | ||
//[3273, 3282, 3283, 3286] | ||
.let { nextQuery -> | ||
if (excludedFileIds.isEmpty()) nextQuery | ||
else nextQuery.andWhere { InfraredFileTable.id notInList excludedFileIds } | ||
} | ||
.map { | ||
val file = IncludedFile( | ||
fileId = it[InfraredFileTable.id].value, | ||
signalCount = it[InfraredFileTable.signalCount] | ||
) | ||
file | ||
}.also { | ||
|
||
debug { "#got files: ${it.map { it.fileId }}" } | ||
} | ||
} | ||
} | ||
|
||
suspend fun findFallbackFile(signalRequestModel: SignalRequestModel): IncludedFile? { | ||
// Getting last available file | ||
return findIncludedFiles( | ||
signalRequestModel = when { | ||
signalRequestModel.skippedResults.isNotEmpty() -> { | ||
signalRequestModel.copy( | ||
skippedResults = signalRequestModel.skippedResults.dropLast(1) | ||
) | ||
} | ||
|
||
signalRequestModel.failedResults.isNotEmpty() -> { | ||
signalRequestModel.copy( | ||
failedResults = signalRequestModel.failedResults.dropLast(1) | ||
) | ||
} | ||
|
||
else -> { | ||
signalRequestModel.copy( | ||
successResults = signalRequestModel.successResults.dropLast(1) | ||
) | ||
} | ||
}, | ||
).firstOrNull() | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
...in/com/flipperdevices/ifrmvp/backend/route/signal/data/InstantCategoryConfigRepository.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
...pi/src/main/kotlin/com/flipperdevices/ifrmvp/backend/route/signal/data/OrderRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.flipperdevices.ifrmvp.backend.route.signal.data | ||
|
||
import com.flipperdevices.ifrmvp.backend.db.signal.table.SignalNameAliasTable | ||
import com.flipperdevices.ifrmvp.backend.model.CategoryConfiguration | ||
import com.flipperdevices.ifrmvp.backend.model.CategoryType | ||
import com.flipperdevices.ifrmvp.backend.model.SignalRequestModel | ||
import com.flipperdevices.ifrmvp.generator.config.device.api.DeviceKeyNamesProvider.Companion.getKey | ||
import com.flipperdevices.ifrmvp.generator.config.device.api.any.AnyDeviceKeyNamesProvider | ||
import com.flipperdevices.ifrmvp.parser.util.ParserPathResolver | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.orWhere | ||
import org.jetbrains.exposed.sql.selectAll | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
class OrderRepository(private val database: Database) { | ||
fun findOrder( | ||
signalRequestModel: SignalRequestModel, | ||
categoryType: CategoryType, | ||
recursionLevel: Int | ||
): CategoryConfiguration.OrderModel? { | ||
val skippedKeys = transaction(database) { | ||
SignalNameAliasTable | ||
.selectAll() | ||
.where { SignalNameAliasTable.id inList signalRequestModel.skippedResults.map(SignalRequestModel.SignalResultData::signalId) } | ||
.orWhere { SignalNameAliasTable.id inList signalRequestModel.successResults.map(SignalRequestModel.SignalResultData::signalId) } | ||
// .orWhere { SignalNameAliasTable.id inList signalRequestModel.failedResults.map(SignalRequestModel.SignalResultData::signalId) } | ||
.mapNotNull { | ||
val keyName = it[SignalNameAliasTable.signalName] | ||
AnyDeviceKeyNamesProvider.getKey(keyName) | ||
}.distinct() | ||
} | ||
|
||
return ParserPathResolver | ||
.categoryConfiguration(categoryType.folderName) | ||
.orders | ||
.filter { !skippedKeys.contains(it.key) } | ||
.getOrNull(recursionLevel) | ||
} | ||
} |
Oops, something went wrong.