-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize backend timeline calculation
- Loading branch information
1 parent
524b9cb
commit 7a03642
Showing
17 changed files
with
211 additions
and
242 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/backend-api/src/main/kotlin/org/icpclive/api/TimeLineRunInfo.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,26 @@ | ||
package org.icpclive.api | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import org.icpclive.cds.api.ProblemId | ||
import org.icpclive.cds.util.serializers.DurationInMillisecondsSerializer | ||
import kotlin.time.Duration | ||
|
||
@Serializable | ||
sealed class TimeLineRunInfo { | ||
@Serializable | ||
@SerialName("ICPC") | ||
public data class ICPC( | ||
@Serializable(with = DurationInMillisecondsSerializer::class) val time: Duration, | ||
val problemId: ProblemId, | ||
val isAccepted: Boolean, | ||
val shortName: String) : TimeLineRunInfo() | ||
|
||
@Serializable | ||
@SerialName("IOI") | ||
public data class IOI(@Serializable(with = DurationInMillisecondsSerializer::class) val time: Duration, val problemId: ProblemId, val score: Double) : TimeLineRunInfo() | ||
|
||
@Serializable | ||
@SerialName("IN_PROGRESS") | ||
public data class InProgress(@Serializable(with = DurationInMillisecondsSerializer::class) val time: Duration, val problemId: ProblemId) : TimeLineRunInfo() | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/backend/src/main/kotlin/org/icpclive/service/TimelineService.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,66 @@ | ||
package org.icpclive.service | ||
|
||
import kotlinx.collections.immutable.* | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.* | ||
import org.icpclive.api.TimeLineRunInfo | ||
import org.icpclive.cds.* | ||
import org.icpclive.cds.adapters.* | ||
import org.icpclive.cds.api.* | ||
import org.icpclive.api.TimeLineRunInfo.* | ||
import org.icpclive.cds.scoreboard.* | ||
import org.icpclive.cds.utils.TeamRunsStorage | ||
import org.icpclive.data.DataBus | ||
|
||
internal class TimelineService : Service { | ||
override fun CoroutineScope.runOn(flow: Flow<ContestStateWithScoreboard>) { | ||
DataBus.timelineFlow.complete(flow.map { it.state.lastEvent }.timelineFlow().stateIn(this, SharingStarted.Eagerly, emptyMap())) | ||
} | ||
|
||
private fun Flow<ContestUpdate>.timelineFlow() = flow { | ||
var rows = persistentMapOf<TeamId, List<TimeLineRunInfo>>() | ||
val runsByTeamId = TeamRunsStorage() | ||
contestState().collect { state -> | ||
for (team in runsByTeamId.applyEvent(state)) { | ||
val newRow = runsByTeamId.getRuns(team).toTimeLine() | ||
val oldRow = rows[team] | ||
if (newRow != oldRow) { // optimization: avoid identity change, if no real change | ||
rows = rows.put(team, newRow) | ||
} | ||
} | ||
emit(rows) | ||
} | ||
} | ||
|
||
private fun List<RunInfo>?.toTimeLine() : List<TimeLineRunInfo> { | ||
val acceptedProblems = mutableSetOf<ProblemId>() | ||
return (this ?: emptyList()).mapNotNull { | ||
when (val result = it.result) { | ||
is RunResult.ICPC -> { | ||
if (!acceptedProblems.contains(it.problemId)) { | ||
if (result.verdict.isAccepted) { | ||
acceptedProblems.add(it.problemId) | ||
} | ||
ICPC(it.time, it.problemId, result.verdict.isAccepted, result.verdict.shortName) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
is RunResult.IOI -> { | ||
if (result.difference > 0) { | ||
IOI(it.time, it.problemId, result.scoreAfter) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
is RunResult.InProgress -> { | ||
InProgress(it.time, it.problemId) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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
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
Oops, something went wrong.