feat: update overdue analytics to display calculated hours instead of activity counts
This commit is contained in:
@@ -9,7 +9,7 @@ data class AnalyticsState(
|
||||
val timelineDates: List<Date> = emptyList(),
|
||||
val plannedProgressPoints: List<Float> = emptyList(),
|
||||
val realizedProgressPoints: List<Float> = emptyList(),
|
||||
val overdueByTag: List<Pair<String, Int>> = emptyList(),
|
||||
val overdueBySector: List<Pair<String, Int>> = emptyList(),
|
||||
val overdueByTag: List<Pair<String, Double>> = emptyList(),
|
||||
val overdueBySector: List<Pair<String, Double>> = emptyList(),
|
||||
val selectedSetorSCurve: String = "Todos"
|
||||
)
|
||||
|
||||
@@ -235,12 +235,14 @@ fun AnalyticsScreen(
|
||||
|
||||
// Modal 1: Listagem das OSs Atrasadas por Grupo (TAG ou Setor)
|
||||
selectedOverdueGroup?.let { group ->
|
||||
val topSectors = analyticsState.overdueBySector.take(3).map { it.first }
|
||||
val topSectors = analyticsState.overdueBySector.map { it.first }
|
||||
val topTags = analyticsState.overdueByTag.take(9).map { it.first }
|
||||
OverdueActivitiesListModal(
|
||||
group = group,
|
||||
allActivities = activities,
|
||||
projectEnd = analyticsState.projectEnd,
|
||||
topSectors = topSectors,
|
||||
topTags = topTags,
|
||||
referenceDate = referenceDate,
|
||||
onDismiss = { selectedOverdueGroup = null },
|
||||
onActivityClick = { activity ->
|
||||
@@ -649,7 +651,7 @@ fun OverdueAnalyticsCard(
|
||||
|
||||
@Composable
|
||||
private fun OverdueBarChartContent(
|
||||
displayList: List<Pair<String, Int>>,
|
||||
displayList: List<Pair<String, Double>>,
|
||||
emptyMessage: String,
|
||||
onItemClick: (String) -> Unit = {}
|
||||
) {
|
||||
@@ -669,7 +671,7 @@ private fun OverdueBarChartContent(
|
||||
)
|
||||
}
|
||||
} else {
|
||||
val maxCount = displayList.maxOfOrNull { it.second } ?: 1
|
||||
val maxCount = displayList.maxOfOrNull { it.second } ?: 1.0
|
||||
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
@@ -684,6 +686,11 @@ private fun OverdueBarChartContent(
|
||||
else -> Color(0xFF9C27B0) // Purple
|
||||
}
|
||||
|
||||
val fillRatio = if (maxCount > 0) (count / maxCount).toFloat().coerceIn(0f, 1f) else 0f
|
||||
val formattedHours = if (count <= 0.0) "0h"
|
||||
else if (count % 1.0 == 0.0) "${count.toInt()}h"
|
||||
else String.format(java.util.Locale.US, "%.1fh", count)
|
||||
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
@@ -710,7 +717,7 @@ private fun OverdueBarChartContent(
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(count.toFloat() / maxCount.toFloat())
|
||||
.fillMaxWidth(fillRatio)
|
||||
.fillMaxHeight()
|
||||
.background(barColor, shape = RoundedCornerShape(4.dp))
|
||||
)
|
||||
@@ -718,13 +725,13 @@ private fun OverdueBarChartContent(
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
// Count number
|
||||
// Count number formatted in hours
|
||||
Text(
|
||||
text = count.toString(),
|
||||
text = formattedHours,
|
||||
fontSize = 12.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = BrandBlack,
|
||||
modifier = Modifier.width(20.dp),
|
||||
modifier = Modifier.width(42.dp),
|
||||
textAlign = TextAlign.End
|
||||
)
|
||||
}
|
||||
@@ -742,12 +749,13 @@ fun OverdueActivitiesListModal(
|
||||
group: SelectedOverdueGroup,
|
||||
allActivities: List<MaintenanceActivity>,
|
||||
projectEnd: Date,
|
||||
topSectors: List<String>,
|
||||
topSectors: List<String> = emptyList(),
|
||||
topTags: List<String> = emptyList(),
|
||||
referenceDate: Date = Date(),
|
||||
onDismiss: () -> Unit,
|
||||
onActivityClick: (MaintenanceActivity) -> Unit
|
||||
) {
|
||||
val filteredActivities = remember(group, allActivities, referenceDate, topSectors) {
|
||||
val filteredActivities = remember(group, allActivities, referenceDate, topSectors, topTags) {
|
||||
allActivities.filter { act ->
|
||||
val isNotCompleted = act.percentualExecucao < 100
|
||||
val endDate = parseFullDateTime(act.dataFim) ?: parseDateOnly(act.dataFim)
|
||||
@@ -757,7 +765,14 @@ fun OverdueActivitiesListModal(
|
||||
|
||||
when (group.type) {
|
||||
OverdueGroupType.TAG -> {
|
||||
if (group.label == "Sem TAG") act.tag.isBlank() else act.tag == group.label
|
||||
val actTag = if (act.tag.isBlank()) "Sem TAG" else act.tag
|
||||
if (group.label == "Outros") {
|
||||
!topTags.contains(actTag)
|
||||
} else if (group.label == "Sem TAG") {
|
||||
act.tag.isBlank()
|
||||
} else {
|
||||
act.tag == group.label
|
||||
}
|
||||
}
|
||||
OverdueGroupType.SETOR -> {
|
||||
val actSetor = if (act.setor.isBlank()) "Sem Setor" else act.setor
|
||||
@@ -770,7 +785,10 @@ fun OverdueActivitiesListModal(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.sortedWith(
|
||||
compareByDescending<MaintenanceActivity> { it.duracaoHorasCalculada }
|
||||
.thenBy { it.dataFimMillis }
|
||||
)
|
||||
}
|
||||
|
||||
AlertDialog(
|
||||
|
||||
@@ -819,45 +819,31 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
isNotCompleted && endMs < Long.MAX_VALUE && endMs < referenceDateMs
|
||||
}
|
||||
|
||||
val overdueByTag = overdueActivities
|
||||
val overdueByTagList = overdueActivities
|
||||
.groupBy { if (it.tag.isBlank()) "Sem TAG" else it.tag }
|
||||
.mapValues { it.value.size }
|
||||
.mapValues { entry -> entry.value.sumOf { it.duracaoHorasCalculada } }
|
||||
.toList()
|
||||
.sortedByDescending { it.second }
|
||||
.sortedWith(compareByDescending<Pair<String, Double>> { it.second }.thenBy { it.first })
|
||||
|
||||
val displayLimit = 4
|
||||
val finalOverdueByTag = if (overdueByTag.size <= displayLimit) {
|
||||
overdueByTag
|
||||
val finalOverdueByTag = if (overdueByTagList.size <= 9) {
|
||||
overdueByTagList
|
||||
} else {
|
||||
val top = overdueByTag.take(displayLimit - 1)
|
||||
val othersCount = overdueByTag.drop(displayLimit - 1).sumOf { it.second }
|
||||
top + listOf("Outros" to othersCount)
|
||||
val top = overdueByTagList.take(9)
|
||||
val othersHours = overdueByTagList.drop(9).sumOf { it.second }
|
||||
top + listOf("Outros" to othersHours)
|
||||
}
|
||||
|
||||
val overdueBySectorMap = overdueActivities
|
||||
.groupBy { if (it.setor.isBlank()) "Sem Setor" else it.setor }
|
||||
.mapValues { it.value.size }
|
||||
.mapValues { entry -> entry.value.sumOf { it.duracaoHorasCalculada } }
|
||||
|
||||
val finalOverdueBySector = if (overdueActivities.isEmpty()) {
|
||||
emptyList()
|
||||
} else {
|
||||
val plantSectors = activities
|
||||
.map { if (it.setor.isBlank()) "Sem Setor" else it.setor }
|
||||
.distinct()
|
||||
|
||||
val fullOverdueBySector = plantSectors
|
||||
.map { sector -> sector to (overdueBySectorMap[sector] ?: 0) }
|
||||
.sortedWith(compareByDescending<Pair<String, Int>> { it.second }.thenBy { it.first })
|
||||
|
||||
val displayLimit = 4
|
||||
if (fullOverdueBySector.size <= displayLimit) {
|
||||
fullOverdueBySector
|
||||
} else {
|
||||
val top = fullOverdueBySector.take(displayLimit - 1)
|
||||
val othersCount = fullOverdueBySector.drop(displayLimit - 1).sumOf { it.second }
|
||||
top + listOf("Outros" to othersCount)
|
||||
}
|
||||
}
|
||||
val finalOverdueBySector = plantSectors
|
||||
.map { sector -> sector to (overdueBySectorMap[sector] ?: 0.0) }
|
||||
.sortedWith(compareByDescending<Pair<String, Double>> { it.second }.thenBy { it.first })
|
||||
|
||||
return AnalyticsState(
|
||||
teamMetrics = teamMetrics,
|
||||
|
||||
@@ -479,19 +479,19 @@ class ExampleRobolectricTest {
|
||||
|
||||
// 1. Current Date (July 2026): 0 or 1 overdue OS because CSV activities are in Aug/Sep 2026
|
||||
val overdueJuly = state.overdueBySector.sumOf { it.second }
|
||||
assertTrue("Expected overdueJuly <= 1, got $overdueJuly", overdueJuly <= 1)
|
||||
assertTrue("Expected overdueJuly <= 10.0, got $overdueJuly", overdueJuly <= 10.0)
|
||||
|
||||
// 2. Simulated Future Date (August 20, 2026): OSs due before Aug 20 start appearing as overdue
|
||||
val aug20 = com.example.data.model.parseFullDateTime("20/08/26 18:00")!!
|
||||
val stateAug20 = viewModel.calculateAnalyticsState(activitiesList, emptyList(), referenceDateOverride = aug20)
|
||||
val overdueAug20 = stateAug20.overdueBySector.sumOf { it.second }
|
||||
assertTrue("Expected overdue activities on Aug 20, 2026, got: $overdueAug20", overdueAug20 > 0)
|
||||
assertTrue("Expected overdue activities on Aug 20, 2026, got: $overdueAug20", overdueAug20 > 0.0)
|
||||
|
||||
// 3. Simulated Future Date (September 15, 2026): All incomplete activities in project are overdue
|
||||
val sep15 = com.example.data.model.parseFullDateTime("15/09/26 18:00")!!
|
||||
val stateSep15 = viewModel.calculateAnalyticsState(activitiesList, emptyList(), referenceDateOverride = sep15)
|
||||
val overdueSep15 = stateSep15.overdueBySector.sumOf { it.second }
|
||||
assertTrue("Expected most CSV activities to be overdue on Sep 15, got: $overdueSep15", overdueSep15 > 400)
|
||||
assertTrue("Expected most CSV activities to be overdue on Sep 15, got: $overdueSep15", overdueSep15 > 400.0)
|
||||
|
||||
val targetOs = db.maintenanceDao().getAllActivitiesList().firstOrNull()?.numeroOs ?: "1024"
|
||||
repository.updateProgress(
|
||||
|
||||
Reference in New Issue
Block a user