Skip to content

Commit

Permalink
feat: ensure better api format alignments to official specs again (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwinter authored Aug 7, 2024
1 parent c86abde commit 2d5311a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.aamdigital.aambackendservice.reporting.report.core.ReportingStorage
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationRequest
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationResult
import com.aamdigital.aambackendservice.reporting.reportcalculation.core.CreateReportCalculationUseCase
import com.aamdigital.aambackendservice.reporting.reportcalculation.dto.ReportCalculationData
import com.aamdigital.aambackendservice.reporting.reportcalculation.dto.ReportCalculationDto
import com.aamdigital.aambackendservice.reporting.storage.DefaultReportStorage
import org.springframework.core.io.buffer.DataBuffer
Expand All @@ -27,6 +28,7 @@ import reactor.kotlin.core.publisher.toFlux
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.jvm.optionals.getOrElse


@RestController
Expand Down Expand Up @@ -114,27 +116,41 @@ class ReportCalculationController(
val fileContent = reportingStorage
.fetchData(DomainReference(id = calculationId))

val prefix = """
{
"id": "${calculationId}_data.json",
"report": "removed",
"calculation": "$calculationId",
"data":
""".trimIndent().toByteArray()
val prefixBuffer = DefaultDataBufferFactory().allocateBuffer(prefix.size)
prefixBuffer.write(prefix)

val suffix = """
}
""".trimIndent().toByteArray()
val suffixBuffer = DefaultDataBufferFactory().allocateBuffer(suffix.size)
suffixBuffer.write(suffix)

return@flatMap Flux.concat(
Flux.just(prefixBuffer),
fileContent,
Flux.just(suffixBuffer),
)
reportingStorage.fetchCalculation(DomainReference(calculationId))
.toFlux()
.flatMap {

val calculation = it.getOrElse {
return@flatMap Flux.error { NotFoundException("No data available") }
}

val prefix = """
{
"id": "${calculationId}_data.json",
"report": {
"id": "${calculation.report.id}"
},
"calculation": {
"id": "$calculationId"
},
"dataHash": "${calculation.attachments["data.json"]?.digest}",
"data":
""".trimIndent().toByteArray()
val prefixBuffer = DefaultDataBufferFactory().allocateBuffer(prefix.size)
prefixBuffer.write(prefix)

val suffix = """
}
""".trimIndent().toByteArray()
val suffixBuffer = DefaultDataBufferFactory().allocateBuffer(suffix.size)
suffixBuffer.write(suffix)

Flux.concat(
Flux.just(prefixBuffer),
fileContent,
Flux.just(suffixBuffer),
)
}
}
}

Expand All @@ -153,6 +169,15 @@ class ReportCalculationController(
startDate = it.calculationStarted,
endDate = it.calculationCompleted,
args = it.args,
attachments = it.attachments,
data = toReportCalculationData(it),
)

private fun toReportCalculationData(it: ReportCalculation): ReportCalculationData? {
val attachment = it.attachments["data.json"] ?: return null
return ReportCalculationData(
contentType = attachment.contentType,
hash = attachment.digest,
length = attachment.length
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.aamdigital.aambackendservice.reporting.reportcalculation.dto

import com.aamdigital.aambackendservice.couchdb.dto.AttachmentMetaData
import com.aamdigital.aambackendservice.domain.DomainReference
import com.aamdigital.aambackendservice.reporting.domain.ReportCalculationStatus

Expand All @@ -14,7 +13,13 @@ data class ReportCalculationDto(
var startDate: String? = null,
var endDate: String? = null,
var args: Map<String, String>,
var attachments: Map<String, AttachmentMetaData> = emptyMap(),
var data: ReportCalculationData?,
)

data class ReportCalculationData(
val contentType: String,
val hash: String,
val length: Number,
)

/**
Expand Down
46 changes: 32 additions & 14 deletions docs/api-specs/reporting-api-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
description: |-
API to manage reports that provide data calculated based on any entities of the Aam Digital system
and offer notifications when data of such reports changes.
version: 1.0.0-alpha.3
version: 1.0.0-alpha.4
servers:
- url: https://{customerId}.aam-digital.net/api/v1/reporting
description: Developer Instance for testing
Expand Down Expand Up @@ -482,15 +482,32 @@ components:
id:
type: string
format: uuid
start_date:
report:
type: object
properties:
id:
type: string
format: uuid
status:
type: string
description: Current status of the run
enum:
- PENDING
- RUNNING
- FINISHED_SUCCESS
- FINISHED_ERROR
startDate:
type: string
description: The Date, the calculation starts processing the query
example: date
end_date:
endDate:
type: string
description: The Date, the calculation finished processing and the data is available
example: date
nullable: true
params:
args:
type: object
description: Input arguments will be injected into the sql query
properties:
from:
type: string
Expand All @@ -502,23 +519,24 @@ components:
description: optional end date filtering data included in the report. If no date is given here, all data (possibly filtered by the "from" date) is included. The field considered for date filtering are defined in each report's query specifically.
format: date
nullable: true
status:
type: string
description: Current status of the run
enum:
- PENDING
- RUNNING
- FINISHED_SUCCESS
- FINISHED_ERROR
data:
type: object
properties:
contentType:
type: string
hash:
type: string
length:
type: number

ReportData:
type: object
properties:
reportId:
calculation:
type: string
format: uuid
data:
type: object
type: array

# notification

Expand Down

0 comments on commit 2d5311a

Please sign in to comment.