Skip to content

Commit

Permalink
[TH2-5004] Refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Aug 2, 2023
1 parent 6121dff commit 871f45a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 58 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ dependencies {
testImplementation('org.jetbrains.kotlin:kotlin-reflect') {
because('mockk needs it')
}
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.9.3"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testImplementation "org.junit.jupiter:junit-jupiter-params:5.10.0"

testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}

tasks.withType(KotlinCompile).configureEach {
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/com/exactpro/th2/rptdataprovider/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ abstract class Context<B, G, RM, PM>(
val timeout: Long = configuration.responseTimeout.value.toLong(),
val cacheTimeout: Long = configuration.serverCacheTimeout.value.toLong(),

val jacksonMapper: ObjectMapper = jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(SimpleModule("backward_compatibility").apply {
addSerializer(Instant::class.java, InstantBackwardCompatibilitySerializer)
})
.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES),
val jacksonMapper: ObjectMapper = JACKSON_MAPPER,

val cradleManager: CradleManager,

Expand Down Expand Up @@ -126,6 +120,15 @@ abstract class Context<B, G, RM, PM>(
val searchEventsHandler: SearchEventsHandler = SearchEventsHandler(this)

companion object {
@JvmStatic
protected val JACKSON_MAPPER: ObjectMapper = jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(SimpleModule("backward_compatibility").apply {
addSerializer(Instant::class.java, InstantBackwardCompatibilitySerializer)
})
.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

@JvmStatic
protected fun cacheControlConfig(timeout: Int, enableCaching: Boolean): CacheControl {
return if (enableCaching) {
Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/com/exactpro/th2/rptdataprovider/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,15 @@ fun MessageFilter.toGroupedMessageFilter(group: String?): GroupedMessageFilter =
}
}.build()

@OptIn(DelicateCoroutinesApi::class)
suspend fun <T> logTime(methodName: String, lambda: suspend () -> T): T? {
return withContext(coroutineContext) {
var result: T?
var result: T?

logger.debug { "cradle: $methodName is starting" }
logger.debug { "cradle: $methodName is starting" }

measureTimeMillis { result = lambda.invoke() }
.also { logger.debug { "cradle: $methodName took ${it}ms" } }
measureTimeMillis { result = lambda.invoke() }
.also { logger.debug { "cradle: $methodName took ${it}ms" } }

result
}
return result
}

data class Metrics(
Expand Down
20 changes: 4 additions & 16 deletions src/main/kotlin/com/exactpro/th2/rptdataprovider/ProtoContext.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 Exactpro (Exactpro Systems Limited)
* Copyright 2023 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,18 +45,12 @@ import com.exactpro.th2.rptdataprovider.handlers.SearchMessagesHandler
import com.exactpro.th2.rptdataprovider.producers.EventProducer
import com.exactpro.th2.rptdataprovider.producers.MessageProducer
import com.exactpro.th2.rptdataprovider.producers.ProtoMessageProducer
import com.exactpro.th2.rptdataprovider.serialization.InstantBackwardCompatibilitySerializer
import com.exactpro.th2.rptdataprovider.server.ServerType
import com.exactpro.th2.rptdataprovider.services.cradle.CradleService
import com.exactpro.th2.rptdataprovider.services.rabbitmq.RabbitMqService
import com.exactpro.th2.rptdataprovider.services.rabbitmq.ProtoRabbitMqService
import com.fasterxml.jackson.databind.DeserializationFeature
import com.exactpro.th2.rptdataprovider.services.rabbitmq.RabbitMqService
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.ktor.http.*
import java.time.Instant
import io.ktor.http.CacheControl

@Suppress("MemberVisibilityCanBePrivate")
class ProtoContext(
Expand All @@ -67,13 +61,7 @@ class ProtoContext(
timeout: Long = configuration.responseTimeout.value.toLong(),
cacheTimeout: Long = configuration.serverCacheTimeout.value.toLong(),

jacksonMapper: ObjectMapper = jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(SimpleModule("backward_compatibility").apply {
addSerializer(Instant::class.java, InstantBackwardCompatibilitySerializer)
})
.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES),
jacksonMapper: ObjectMapper = JACKSON_MAPPER,

cradleManager: CradleManager,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 Exactpro (Exactpro Systems Limited)
* Copyright 2023 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,18 +45,12 @@ import com.exactpro.th2.rptdataprovider.handlers.TransportSearchMessagesHandler
import com.exactpro.th2.rptdataprovider.producers.EventProducer
import com.exactpro.th2.rptdataprovider.producers.MessageProducer
import com.exactpro.th2.rptdataprovider.producers.TransportMessageProducer
import com.exactpro.th2.rptdataprovider.serialization.InstantBackwardCompatibilitySerializer
import com.exactpro.th2.rptdataprovider.server.ServerType
import com.exactpro.th2.rptdataprovider.services.cradle.CradleService
import com.exactpro.th2.rptdataprovider.services.rabbitmq.RabbitMqService
import com.exactpro.th2.rptdataprovider.services.rabbitmq.TransportRabbitMqService
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.ktor.http.*
import java.time.Instant
import io.ktor.http.CacheControl

@Suppress("MemberVisibilityCanBePrivate")
class TransportContext(
Expand All @@ -67,13 +61,7 @@ class TransportContext(
timeout: Long = configuration.responseTimeout.value.toLong(),
cacheTimeout: Long = configuration.serverCacheTimeout.value.toLong(),

jacksonMapper: ObjectMapper = jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(SimpleModule("backward_compatibility").apply {
addSerializer(Instant::class.java, InstantBackwardCompatibilitySerializer)
})
.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES),
jacksonMapper: ObjectMapper = JACKSON_MAPPER,

cradleManager: CradleManager,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.exactpro.th2.rptdataprovider.entities.filters.info.FilterSpecialType.
import com.exactpro.th2.rptdataprovider.entities.filters.info.Parameter
import com.exactpro.th2.rptdataprovider.entities.responses.BaseEventEntity
import com.exactpro.th2.rptdataprovider.services.cradle.CradleService
import java.util.*
import java.util.Locale

class EventBodyFilter private constructor(
private var body: List<String>, override var negative: Boolean = false, override var conjunct: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.exactpro.th2.rptdataprovider.entities.filters.info.FilterParameterTyp
import com.exactpro.th2.rptdataprovider.entities.filters.info.Parameter
import com.exactpro.th2.rptdataprovider.entities.responses.BaseEventEntity
import com.exactpro.th2.rptdataprovider.services.cradle.CradleService
import java.util.*
import java.util.Locale

class EventNameFilter private constructor(
private var name: List<String>, override var negative: Boolean = false, override var conjunct: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ class MessageBatchDecoder<B, G, RM, PM>(
val pipelineMessage = previousComponent!!.pollMessage()

if (pipelineMessage is PipelineCodecRequest<*, *, *, *>) {
@Suppress("UNCHECKED_CAST")
pipelineMessage as PipelineCodecRequest<B, G, RM, PM>

val protocol = pipelineMessage.codecRequest.protocol

if (isImage(protocol)) {
sendToChannel(
PipelineDecodedBatch(
(pipelineMessage as PipelineCodecRequest<B, G, RM, PM>).also {
pipelineMessage.also {
it.info.startParseMessage = System.currentTimeMillis()
},
CodecBatchResponse(CompletableDeferred(value = null)),
Expand All @@ -101,7 +102,7 @@ class MessageBatchDecoder<B, G, RM, PM>(
pipelineMessage.codecRequest.groupsCount.toLong()
)

@Suppress("UNCHECKED_CAST") val codecRequest: CodecBatchRequest<B, G, PM> = (pipelineMessage as PipelineCodecRequest<B, G, RM, PM>).codecRequest
val codecRequest: CodecBatchRequest<B, G, PM> = pipelineMessage.codecRequest
val result = PipelineDecodedBatch(
pipelineMessage.also {
it.info.startParseMessage = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CradleService(configuration: Configuration, cradleManager: CradleManager)
logger.debug { "Start searching group batches by $it" }
}
storage.getGroupedMessageBatchesAsync(groupedMessageFilter).await().asSequence()
.map { batch ->
.mapNotNull { batch ->
batch.messages.asSequence().filter { message ->
filter.sessionAlias == message.sessionAlias
&& filter.direction == message.direction
Expand All @@ -120,7 +120,7 @@ class CradleService(configuration: Configuration, cradleManager: CradleManager)
}.toList()
.run {
if (isEmpty()) {
StoredMessageBatch()
null
} else {
StoredMessageBatch(
this,
Expand All @@ -129,8 +129,7 @@ class CradleService(configuration: Configuration, cradleManager: CradleManager)
)
}
}
}.filterNot(StoredMessageBatch::isEmpty)
.asIterable()
}.asIterable()

} ?: listOf()
} else {
Expand Down

0 comments on commit 871f45a

Please sign in to comment.