Skip to content

Commit

Permalink
fix: Fixed the issue of repeated subscriptions to OpenAI usage when s…
Browse files Browse the repository at this point in the history
…witching OpenAI configurations.

(cherry picked from commit d840cd6)
  • Loading branch information
Yubyf committed Jul 27, 2023
1 parent 375c1a9 commit 719514c
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -100,6 +101,7 @@ class OpenAIRepository @Inject internal constructor(
private val _openAIConfigsFlow = MutableStateFlow(openAIConfigs)
val openAIConfigsFlow = _openAIConfigsFlow.asStateFlow()

private var openAIUsageJob: Job? = null
private val _openAIUsageFlow = MutableStateFlow<OpenAIUsage?>(null)
val openAIUsageFlow = _openAIUsageFlow.asStateFlow()

Expand Down Expand Up @@ -169,15 +171,17 @@ class OpenAIRepository @Inject internal constructor(
set(year, month, 1, 0, 0)
}.timeInMillis
}
openAIUsageDao.getUsageByApiKeyStream(apiKey, startDate).onEach { usages ->
usages.groupBy(OpenAIUsageEntity::model).mapValues { (_, usageEntities) ->
usageEntities.sumOf { it.tokens }
}.toList().let { usage ->
_openAIUsageFlow.update {
OpenAIUsage(apiKey, usage.sumOf { it.second }, usage)
openAIUsageJob?.cancel()
openAIUsageJob =
openAIUsageDao.getUsageByApiKeyStream(apiKey, startDate).onEach { usages ->
usages.groupBy(OpenAIUsageEntity::model).mapValues { (_, usageEntities) ->
usageEntities.sumOf { it.tokens }
}.toList().let { usage ->
_openAIUsageFlow.update {
OpenAIUsage(apiKey, usage.sumOf { it.second }, usage)
}
}
}
}.launchIn(CoroutineScope(dispatcher))
}.launchIn(CoroutineScope(dispatcher))
}
}.launchIn(CoroutineScope(dispatcher))
}
Expand Down

0 comments on commit 719514c

Please sign in to comment.