Skip to content

Commit

Permalink
Fix commentary generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kunyavskiy authored and Kostya Bats committed Sep 10, 2024
1 parent 8c22c40 commit 7148b97
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.icpclive.util.completeOrThrow
import org.icpclive.cds.util.getLogger
import org.icpclive.controllers.PresetsController
import org.icpclive.data.*
import org.icpclive.service.analytics.AnalyticsGenerator
import kotlin.time.Duration

sealed class AnalyticsAction {
Expand All @@ -38,7 +37,7 @@ sealed class AnalyticsAction {
}


class AnalyticsService(private val generator: AnalyticsGenerator) : Service {
class AnalyticsService : Service {
private val internalActions = MutableSharedFlow<AnalyticsAction>()
private var contestInfo: ContestInfo? = null
private val messages = mutableMapOf<AnalyticsMessageId, AnalyticsMessage>()
Expand Down Expand Up @@ -219,7 +218,7 @@ class AnalyticsService(private val generator: AnalyticsGenerator) : Service {
merge(
subscriberFlow.map { Subscribe },
actionFlow,
generator.getFlow(flow).map { ContestUpdate(it.state.lastEvent) }
flow.map { ContestUpdate(it.state.lastEvent) }
).collect { event ->
when (event) {
is ContestUpdate -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.*
import org.icpclive.Config
import org.icpclive.api.CurrentTeamState
import org.icpclive.cds.ContestUpdate
import org.icpclive.cds.adapters.generateCommentary
import org.icpclive.cds.api.OptimismLevel
import org.icpclive.cds.scoreboard.calculateScoreboard
import org.icpclive.cds.util.getLogger
Expand All @@ -23,8 +24,10 @@ fun CoroutineScope.launchServices(loader: Flow<ContestUpdate>) {
.onEach { log.info { "Start loading data" } }
.take(1)
}
val commentaryGenerator = AnalyticsGenerator(Config.analyticsTemplatesFile)
val normalScoreboardState = loader
.calculateScoreboard(OptimismLevel.NORMAL)
.generateCommentary(commentaryGenerator::getMessages)
.buffer(Int.MAX_VALUE)
.shareIn(this, starter)

Expand All @@ -50,7 +53,7 @@ fun CoroutineScope.launchServices(loader: Flow<ContestUpdate>) {
launchService(QueueService())
launchService(ScoreboardService())
launchService(StatisticsService())
launchService(AnalyticsService(AnalyticsGenerator(Config.analyticsTemplatesFile)))
launchService(AnalyticsService())
launchService(ExternalRunsService())
launchService(TeamSpotlightService(teamInteresting = teamInterestingFlow))
launchService(RegularLoggingService())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.icpclive.service.analytics

import kotlinx.coroutines.flow.*
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
Expand All @@ -15,39 +14,29 @@ import java.nio.file.Path
import kotlin.io.path.inputStream

class AnalyticsGenerator(jsonTemplatePath: Path?) {
private val messagesTemplates_ = jsonTemplatePath ?.let { Json.decodeFromStream<JsonAnalyticTemplates>(it.inputStream()) }

fun getFlow(
scoreboardFlow: Flow<ContestStateWithScoreboard>,
) = flow {
log.info { "Analytics generator service is started" }
val runs = mutableMapOf<RunId, RunAnalyse>()
scoreboardFlow.collect {
when (val event = it.state.lastEvent) {
is InfoUpdate, is CommentaryMessagesUpdate -> emit(it)
is RunUpdate -> {
emit(it)
if (messagesTemplates_ == null) return@collect
val run = event.newInfo
if (run.isHidden) return@collect
val info = it.state.infoAfterEvent ?: return@collect
val analysis = runs.processRun(run, it.toScoreboardDiff(true)) ?: return@collect

val team = info.teams[run.teamId] ?: return@collect
val problem = info.problems[run.problemId] ?: return@collect
val message = CommentaryMessage(
"_analytics_by_run_${run.id}".toCommentaryMessageId(),
getMessage(messagesTemplates_, analysis, team, problem),
analysis.creationTime,
run.time,
listOf(team.id),
listOf(run.id),
tags = getTags(analysis),
)
// TODO: emit(AnalyticsUpdate(message))
}
}
}
private val messagesTemplates_ = jsonTemplatePath?.let { Json.decodeFromStream<JsonAnalyticTemplates>(it.inputStream()) }
private val runs = mutableMapOf<RunId, RunAnalyse>()

fun getMessages(state: ContestStateWithScoreboard): List<CommentaryMessage> {
val event = state.state.lastEvent as? RunUpdate ?: return emptyList()
if (messagesTemplates_ == null) return emptyList()
val run = event.newInfo
if (run.isHidden) return emptyList()
val info = state.state.infoAfterEvent ?: return emptyList()
val analysis = runs.processRun(run, state.toScoreboardDiff(true)) ?: return emptyList()

val team = info.teams[run.teamId] ?: return emptyList()
val problem = info.problems[run.problemId] ?: return emptyList()
val message = CommentaryMessage(
"_analytics_by_run_${run.id}".toCommentaryMessageId(),
getMessage(messagesTemplates_, analysis, team, problem),
analysis.creationTime,
run.time,
listOf(team.id),
listOf(run.id),
tags = getTags(analysis),
)
return listOf(message)
}

private fun getMessage(messagesTemplates: JsonAnalyticTemplates, analyse: RunAnalyse, team: TeamInfo, problem: ProblemInfo): String {
Expand Down
4 changes: 4 additions & 0 deletions src/cds/core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public final class org/icpclive/cds/adapters/AutoFinalizeKt {
public static final fun finalContestState (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class org/icpclive/cds/adapters/CommentaryAdapterKt {
public static final fun generateCommentary (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
}

public final class org/icpclive/cds/adapters/ContestStateKt {
public static final fun applyEvent (Lorg/icpclive/cds/api/ContestState;Lorg/icpclive/cds/ContestUpdate;)Lorg/icpclive/cds/api/ContestState;
public static final fun contestState (Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.icpclive.cds.adapters

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.transform
import org.icpclive.cds.CommentaryMessagesUpdate
import org.icpclive.cds.api.CommentaryMessage
import org.icpclive.cds.api.ContestState
import org.icpclive.cds.scoreboard.ContestStateWithScoreboard


public fun Flow<ContestStateWithScoreboard>.generateCommentary(
generator: (ContestStateWithScoreboard) -> List<CommentaryMessage>
) : Flow<ContestStateWithScoreboard> {
var contestState: ContestState? = null
return transform {
contestState = contestState.applyEvent(it.state.lastEvent)
emit(
ContestStateWithScoreboard(
state = contestState!!,
scoreboardRowsAfter = it.scoreboardRowsAfter,
scoreboardRowsBefore = it.scoreboardRowsBefore,
scoreboardRowsChanged = it.scoreboardRowsChanged,
rankingBefore = it.rankingAfter,
rankingAfter = it.rankingBefore,
lastSubmissionTime = it.lastSubmissionTime
)
)
for (message in generator(it)) {
val event = CommentaryMessagesUpdate(message)
contestState = contestState.applyEvent(event)
emit(
ContestStateWithScoreboard(
state = contestState!!,
scoreboardRowsAfter = it.scoreboardRowsAfter,
scoreboardRowsBefore = it.scoreboardRowsAfter,
scoreboardRowsChanged = emptyList(),
rankingBefore = it.rankingAfter,
rankingAfter = it.rankingAfter,
lastSubmissionTime = it.lastSubmissionTime
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public fun ContestStateWithScoreboard.toScoreboardDiff(snapshot: Boolean) : Scor
*/
public class ContestStateWithScoreboard internal constructor(
public val state: ContestState,
private val scoreboardRowsAfter: Map<TeamId, ScoreboardRow>,
private val scoreboardRowsBefore: Map<TeamId, ScoreboardRow>,
internal val scoreboardRowsAfter: Map<TeamId, ScoreboardRow>,
internal val scoreboardRowsBefore: Map<TeamId, ScoreboardRow>,
public val scoreboardRowsChanged: List<TeamId>,
public val rankingBefore: Ranking,
public val rankingAfter: Ranking,
Expand Down

0 comments on commit 7148b97

Please sign in to comment.