refactor: optimize train record merging logic

This commit is contained in:
Nedifinita
2025-08-19 18:06:02 +08:00
parent 8894a73999
commit 0f98b6bcf7
2 changed files with 42 additions and 31 deletions

View File

@@ -503,7 +503,7 @@ class MainActivity : ComponentActivity() {
private fun handleTrainInfo(jsonData: JSONObject) { private fun handleTrainInfo(jsonData: JSONObject) {
Log.d(TAG, "Received train data=${jsonData.toString().take(50)}...") Log.d(TAG, "Received train data=${jsonData.toString()}...")
runOnUiThread { runOnUiThread {
try { try {

View File

@@ -297,12 +297,12 @@ class TrainRecordManager(private val context: Context) {
} }
private fun processRecordsForMerging(records: List<TrainRecord>, settings: MergeSettings): List<MergedTrainRecord> { private fun processRecordsForMerging(records: List<TrainRecord>, settings: MergeSettings): List<MergedTrainRecord> {
val currentTime = Date() val validRecords = settings.timeWindow.seconds?.let { windowSeconds ->
val validRecords = records.filter { record -> val currentTime = Date()
settings.timeWindow.seconds?.let { windowSeconds -> records.filter { record ->
(currentTime.time - record.timestamp.time) / 1000 <= windowSeconds (currentTime.time - record.timestamp.time) / 1000 <= windowSeconds
} ?: true }
} } ?: records
return when (settings.groupBy) { return when (settings.groupBy) {
GroupBy.TRAIN_OR_LOCO -> processTrainOrLocoMerging(validRecords) GroupBy.TRAIN_OR_LOCO -> processTrainOrLocoMerging(validRecords)
@@ -317,11 +317,14 @@ class TrainRecordManager(private val context: Context) {
groupedRecords.mapNotNull { (groupKey, groupRecords) -> groupedRecords.mapNotNull { (groupKey, groupRecords) ->
if (groupRecords.size >= 2) { if (groupRecords.size >= 2) {
val sortedRecords = groupRecords.sortedBy { it.timestamp } val latestRecord = if (groupRecords.size > 1) {
val latestRecord = sortedRecords.maxByOrNull { it.timestamp }!! groupRecords.maxByOrNull { it.timestamp } ?: groupRecords.last()
} else {
groupRecords.last()
}
MergedTrainRecord( MergedTrainRecord(
groupKey = groupKey, groupKey = groupKey,
records = sortedRecords, records = groupRecords.toList(),
latestRecord = latestRecord latestRecord = latestRecord
) )
} else null } else null
@@ -331,7 +334,9 @@ class TrainRecordManager(private val context: Context) {
} }
private fun processTrainOrLocoMerging(records: List<TrainRecord>): List<MergedTrainRecord> { private fun processTrainOrLocoMerging(records: List<TrainRecord>): List<MergedTrainRecord> {
val groups = mutableListOf<MutableList<TrainRecord>>() val trainGroups = mutableMapOf<String, MutableList<TrainRecord>>()
val locoGroups = mutableMapOf<String, MutableList<TrainRecord>>()
val mergedGroups = mutableSetOf<MutableList<TrainRecord>>()
records.forEach { record -> records.forEach { record ->
val train = record.train.trim() val train = record.train.trim()
@@ -341,38 +346,44 @@ class TrainRecordManager(private val context: Context) {
return@forEach return@forEach
} }
var foundGroup: MutableList<TrainRecord>? = null var targetGroup: MutableList<TrainRecord>? = null
for (group in groups) { if (train.isNotEmpty() && train != "<NUL>") {
val shouldMerge = group.any { existingRecord -> targetGroup = trainGroups[train]
val existingTrain = existingRecord.train.trim()
val existingLoco = existingRecord.loco.trim()
(train.isNotEmpty() && train != "<NUL>" && train == existingTrain) ||
(loco.isNotEmpty() && loco != "<NUL>" && loco == existingLoco)
}
if (shouldMerge) {
foundGroup = group
break
}
} }
if (foundGroup != null) { if (targetGroup == null && loco.isNotEmpty() && loco != "<NUL>") {
foundGroup.add(record) targetGroup = locoGroups[loco]
}
if (targetGroup != null) {
targetGroup.add(record)
if (train.isNotEmpty() && train != "<NUL>") {
trainGroups[train] = targetGroup
}
if (loco.isNotEmpty() && loco != "<NUL>") {
locoGroups[loco] = targetGroup
}
} else { } else {
groups.add(mutableListOf(record)) val newGroup = mutableListOf(record)
mergedGroups.add(newGroup)
if (train.isNotEmpty() && train != "<NUL>") {
trainGroups[train] = newGroup
}
if (loco.isNotEmpty() && loco != "<NUL>") {
locoGroups[loco] = newGroup
}
} }
} }
return groups.mapNotNull { groupRecords -> return mergedGroups.mapNotNull { groupRecords ->
if (groupRecords.size >= 2) { if (groupRecords.size >= 2) {
val sortedRecords = groupRecords.sortedBy { it.timestamp } val latestRecord = groupRecords.maxByOrNull { it.timestamp } ?: groupRecords.lastOrNull() ?: return@mapNotNull null
val latestRecord = sortedRecords.maxByOrNull { it.timestamp }!!
val groupKey = "${latestRecord.train}_OR_${latestRecord.loco}" val groupKey = "${latestRecord.train}_OR_${latestRecord.loco}"
MergedTrainRecord( MergedTrainRecord(
groupKey = groupKey, groupKey = groupKey,
records = sortedRecords, records = groupRecords.toList(),
latestRecord = latestRecord latestRecord = latestRecord
) )
} else null } else null