Skip to content

Commit

Permalink
fix: schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwinter committed Mar 12, 2024
1 parent a1c9616 commit c561364
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.aamdigital.aambackendservice.report.sqs

import com.aamdigital.aambackendservice.error.InvalidArgumentException
import com.aamdigital.aambackendservice.report.core.QueryStorage
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.http.MediaType
import org.springframework.stereotype.Service
Expand All @@ -21,6 +23,8 @@ class SqsQueryStorage(
@Qualifier("sqs-client") private val sqsClient: WebClient,
private val schemaService: SqsSchemaService,
) : QueryStorage {
private val logger = LoggerFactory.getLogger(javaClass)

override fun executeQuery(query: QueryRequest): Mono<QueryResult> {
val schemaPath = schemaService.getSchemaPath()
return schemaService.updateSchema()
Expand All @@ -31,14 +35,22 @@ class SqsQueryStorage(
.body(BodyInserters.fromValue(query))
.accept(MediaType.APPLICATION_JSON)
.exchangeToMono { response ->
response.bodyToMono(List::class.java)
.map {
QueryResult(result = it)
}
if (response.statusCode().is2xxSuccessful) {
response.bodyToMono(List::class.java)
.map {
QueryResult(result = it)
}
} else {
response.bodyToMono(String::class.java)
.flatMap {
logger.error("[SqsQueryStorage] Invalid response from SQS: $it")
Mono.error(InvalidArgumentException(it))
}
}
}
}
.doOnError {
println(it)
logger.error("[SqsQueryStorage]: ${it.localizedMessage}", it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.aamdigital.aambackendservice.report.sqs

import com.aamdigital.aambackendservice.couchdb.core.CouchDbStorage
import com.aamdigital.aambackendservice.crypto.core.CryptoService
import com.aamdigital.aambackendservice.domain.EntityAttribute
import com.aamdigital.aambackendservice.domain.EntityConfig
import com.aamdigital.aambackendservice.domain.EntityType
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.stereotype.Service
import org.springframework.util.LinkedMultiValueMap
import reactor.core.publisher.Mono
import java.security.MessageDigest

data class AppConfigAttribute(
val dataType: String,
Expand Down Expand Up @@ -49,13 +52,27 @@ data class SqlOptions(

data class SqsSchema(
val language: String = "sqlite",
val configVersion: String,
val sql: SqlObject,
)
) {
var configVersion: String

init {
configVersion = generateConfigVersion()
}

@OptIn(ExperimentalStdlibApi::class)
fun generateConfigVersion(): String {
val md = MessageDigest.getInstance("SHA-256")
val input = jacksonObjectMapper().writeValueAsString(sql).toByteArray()
val bytes = md.digest(input)
return bytes.toHexString()
}
}

@Service
class SqsSchemaService(
private val couchDbStorage: CouchDbStorage,
private val cryptoService: CryptoService,
) {

companion object {
Expand Down Expand Up @@ -112,16 +129,13 @@ class SqsSchemaService(
Mono.just(Unit)
}
}
.doOnError {
println(it)
}
}

private fun mapToSqsSchema(entityConfig: EntityConfig): SqsSchema {
val tables = entityConfig.entities.map { entityType ->
val attributes = entityType.attributes
.filter {
it.type == "file"
it.type != "file"
}
.map {
EntityAttribute(
Expand All @@ -139,7 +153,6 @@ class SqsSchemaService(
}

return SqsSchema(
configVersion = "",
sql = SqlObject(
tables = tables,
options = SqlOptions(
Expand Down Expand Up @@ -175,7 +188,7 @@ class SqsSchemaService(

private fun parseEntityConfig(entityKey: String, config: AppConfigEntry): EntityType {
return EntityType(
label = config.label ?: entityKey.split(":").first(),
label = config.label ?: entityKey.split(":")[1],
attributes = config.attributes.orEmpty().map {
EntityAttribute(
name = it.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ class ReportCalculationRepository(
body = calculation
)
}.map { data }
.doOnError {
println("err")
}
}

fun fetchData(calculationReference: DomainReference): Mono<Optional<ReportData>> {
Expand Down

0 comments on commit c561364

Please sign in to comment.