Skip to content

Commit

Permalink
Backend - add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Dec 15, 2023
1 parent bb563d4 commit 9ac14e7
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package fr.gouv.dgampa.rapportnav

import io.sentry.Sentry
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.boot.runApplication

import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
class RapportNavApplication

fun main(args: Array<String>) {
runApplication<RapportNavApplication>(*args)

val ctx = runApplication<RapportNavApplication>(*args)

val isSentryEnabled: String? = ctx.environment.getProperty("sentry.enabled")
val sentryDsn: String? = ctx.environment.getProperty("sentry.dsn")

if (isSentryEnabled == "true") {
Sentry.init { options ->
options.dsn = sentryDsn
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0
}
}

try {
throw Exception("This is a test.")
} catch (e: Exception) {
Sentry.captureException(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,52 @@ import com.fasterxml.jackson.databind.ObjectMapper
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.ExtendedEnvMissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.EnvMission
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionSourceEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionTypeEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.envActions.*
import fr.gouv.dgampa.rapportnav.domain.use_cases.user.GetControlUnitsForUser
import io.sentry.Sentry
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.GeometryFactory
import org.n52.jackson.datatype.jts.JtsModule
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Instant
import java.time.ZonedDateTime
import java.util.*


@UseCase
class GetEnvMissionById(
private val mapper: ObjectMapper
) {
fun execute(missionId: Int): ExtendedEnvMissionEntity {
fun execute(missionId: Int): ExtendedEnvMissionEntity? {

val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(
URI.create(
"https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions/$missionId"
try {
val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(
URI.create(
"https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions/$missionId"
)
)
)
.build();
.build();

val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val response = client.send(request, HttpResponse.BodyHandlers.ofString())

mapper.registerModule(JtsModule())
mapper.registerModule(JtsModule())

val envMission = mapper.readValue(response.body(), object : TypeReference<EnvMission>() {})
return ExtendedEnvMissionEntity.fromEnvMission(envMission)
} catch (e: Exception) {
Sentry.captureException(e)
return null
}

val envMission = mapper.readValue(response.body(), object : TypeReference<EnvMission>() {})
return ExtendedEnvMissionEntity.fromEnvMission(envMission)

// val controlTheme1 = ThemeEntity(
// theme = "Rejets illicites",
Expand All @@ -51,6 +67,7 @@ class GetEnvMissionById(
// actionStartDateTimeUtc = ZonedDateTime.parse("2022-02-16T04:50:09Z"),
// actionEndDateTimeUtc = ZonedDateTime.parse("2022-02-16T06:50:09Z"),
// themes = listOf(controlTheme1),
// geom = GeometryFactory().createPoint(Coordinate(52.4, 14.2)),
// actionNumberOfControls = 5,
// actionTargetType = ActionTargetTypeEnum.VEHICLE,
// vehicleType = VehicleTypeEnum.VESSEL,
Expand Down Expand Up @@ -80,6 +97,7 @@ class GetEnvMissionById(
// actionEndDateTimeUtc = ZonedDateTime.parse("2022-02-19T06:50:09Z"),
// themes = listOf(controlTheme1),
// actionNumberOfControls = 10,
// geom = GeometryFactory().createPoint(Coordinate(52.4, 14.2)),
// actionTargetType = null,
// vehicleType = null,
// observations = null,
Expand Down Expand Up @@ -107,27 +125,14 @@ class GetEnvMissionById(
// actionStartDateTimeUtc = ZonedDateTime.parse("2022-02-21T04:50:09Z"),
// actionEndDateTimeUtc = ZonedDateTime.parse("2022-02-21T06:50:09Z"),
// themes = listOf(controlTheme2),
// geom = GeometryFactory().createPoint(Coordinate(52.4, 14.2)),
// actionNumberOfControls = 8,
// actionTargetType = ActionTargetTypeEnum.INDIVIDUAL,
// vehicleType = VehicleTypeEnum.VESSEL,
// observations = null,
// isSafetyEquipmentAndStandardsComplianceControl = true,
// isComplianceWithWaterRegulationsControl = true,
// infractions = listOf(
// InfractionEntity(
// id = "12343",
// natinf = listOf("2593", "27773"),
// registrationNumber = "CCCCCD",
// companyName = "dummy company",
// relevantCourt = "tribunal",
// infractionType = InfractionTypeEnum.WAITING,
// formalNotice = FormalNoticeEnum.PENDING,
// toProcess = true,
// controlledPersonIdentity = "Jean Robert",
// vesselType = VesselTypeEnum.COMMERCIAL,
// vesselSize = VesselSizeEnum.LESS_THAN_12m,
// )
// ),
// infractions = listOf(),
// )
//// val envMissionActionSurveillance = EnvActionSurveillanceEntity(
//// id = UUID.randomUUID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.EnvMission
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionSourceEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionTypeEnum
import io.sentry.Sentry
import org.n52.jackson.datatype.jts.JtsModule
import org.slf4j.LoggerFactory
import org.springframework.web.util.UriUtils
Expand All @@ -30,67 +31,72 @@ class GetEnvMissions(private val mapper: ObjectMapper) {
controlUnits: List<Int>? = null
): List<MissionEntity> {

val uriBuilder = StringBuilder("https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions")

// Append query parameters
uriBuilder.append("?")

// Append optional parameters
startedAfterDateTime?.let {
uriBuilder.append(
"startedAfterDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
startedBeforeDateTime?.let {
uriBuilder.append(
"startedBeforeDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
pageNumber?.let { uriBuilder.append("pageNumber=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }
pageSize?.let { uriBuilder.append("pageSize=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }

// Append controlUnits if present
controlUnits?.let {
for (controlUnit in it) {
uriBuilder.append("controlUnits=${UriUtils.encodeQueryParam(controlUnit.toString(), "UTF-8")}&")
try {
val uriBuilder = StringBuilder("https://monitorenv.din.developpement-durable.gouv.fr/api/v1/missions")

// Append query parameters
uriBuilder.append("?")

// Append optional parameters
startedAfterDateTime?.let {
uriBuilder.append(
"startedAfterDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
startedBeforeDateTime?.let {
uriBuilder.append(
"startedBeforeDateTime=${
UriUtils.encodeQueryParam(
it.toString(),
"UTF-8"
)
}&"
)
}
pageNumber?.let { uriBuilder.append("pageNumber=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }
pageSize?.let { uriBuilder.append("pageSize=${UriUtils.encodeQueryParam(it.toString(), "UTF-8")}&") }

// Append controlUnits if present
controlUnits?.let {
for (controlUnit in it) {
uriBuilder.append("controlUnits=${UriUtils.encodeQueryParam(controlUnit.toString(), "UTF-8")}&")
}
}
}

// Remove trailing "&" if present
if (uriBuilder.endsWith("&")) {
uriBuilder.deleteCharAt(uriBuilder.length - 1)
}
// Remove trailing "&" if present
if (uriBuilder.endsWith("&")) {
uriBuilder.deleteCharAt(uriBuilder.length - 1)
}

// Build the URI
val uri = URI.create(uriBuilder.toString())
// Build the URI
val uri = URI.create(uriBuilder.toString())

val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(uri)
.build();
val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(uri)
.build();

val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val response = client.send(request, HttpResponse.BodyHandlers.ofString())

mapper.registerModule(JtsModule())
mapper.registerModule(JtsModule())

val envMissions: List<EnvMission> =
mapper.readValue(response.body(), object : TypeReference<List<EnvMission>>() {})
val envMissions: List<EnvMission> =
mapper.readValue(response.body(), object : TypeReference<List<EnvMission>>() {})

val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }
val missions = envMissions.map { MissionEntity(envMission = ExtendedEnvMissionEntity.fromEnvMission(it)) }

logger.info("Found ${missions.size} missions ")
logger.info("Found ${missions.size} missions ")

return missions
return missions
} catch (e: Exception) {
Sentry.captureException(e)
return listOf()
}

// val mission1 = EnvMission(
// id = 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.fish.fishActions.*
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.action.ExtendedFishActionEntity
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.action.AttachControlsToActionControl
import io.sentry.Sentry
import org.n52.jackson.datatype.jts.JtsModule
import java.net.URI
import java.net.http.HttpClient
Expand All @@ -20,32 +21,37 @@ class GetFishActionsByMissionId(
private val mapper: ObjectMapper
) {
fun execute(missionId: Int): List<ExtendedFishActionEntity> {

val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(
URI.create(
"https://monitorfish.din.developpement-durable.gouv.fr/api/v1/mission_actions?missionId=$missionId"
try {
val client: HttpClient = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(
URI.create(
"https://monitorfish.din.developpement-durable.gouv.fr/api/v1/mission_actions?missionId=$missionId"
)
)
)
.build();
.build();


val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val response = client.send(request, HttpResponse.BodyHandlers.ofString())

mapper.registerModule(JtsModule())
mapper.registerModule(JtsModule())

val fishActions = mapper.readValue(response.body(), object : TypeReference<List<MissionAction>>() {})
val actions = fishActions.map {
var action = ExtendedFishActionEntity.fromMissionAction(it)
action = attachControlsToActionControl.toFishAction(
actionId = it.id?.toString(),
action = action
)
action
val fishActions = mapper.readValue(response.body(), object : TypeReference<List<MissionAction>>() {})
val actions = fishActions.map {
var action = ExtendedFishActionEntity.fromMissionAction(it)
action = attachControlsToActionControl.toFishAction(
actionId = it.id?.toString(),
action = action
)
action
}
return actions
} catch (e: Exception) {
Sentry.captureException(e)
return listOf()
}
return actions

//
// val gearControlInstance = GearControl()
// gearControlInstance.gearCode = "ABC123"
// gearControlInstance.gearName = "Example Gear"
Expand Down Expand Up @@ -78,8 +84,13 @@ class GetFishActionsByMissionId(
// flightGoals = listOf(),
// logbookInfractions = listOf(),
// gearInfractions = listOf(),
// speciesInfractions = listOf(),
// otherInfractions = listOf(),
// speciesInfractions = listOf(
// SpeciesInfraction(),
// SpeciesInfraction()
// ),
// otherInfractions = listOf(
// OtherInfraction()
// ),
// controlUnits = listOf(),
// isDeleted = false,
// isAdministrativeControl = true,
Expand Down Expand Up @@ -124,8 +135,13 @@ class GetFishActionsByMissionId(
// actionType = MissionActionType.SEA_CONTROL,
// actionDatetimeUtc = ZonedDateTime.parse("2022-02-18T18:50:09Z"),
// flightGoals = listOf(),
// logbookInfractions = listOf(),
// gearInfractions = listOf(),
// logbookInfractions = listOf(
// LogbookInfraction(infractionType = InfractionType.WITHOUT_RECORD),
// LogbookInfraction(infractionType = InfractionType.WITH_RECORD)
// ),
// gearInfractions = listOf(
// GearInfraction(infractionType = InfractionType.WITH_RECORD)
// ),
// speciesInfractions = listOf(),
// otherInfractions = listOf(),
// controlUnits = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GetEnvActionByIdAndMissionId(

// TODO fetch controrls

return mission.actions?.firstOrNull { it?.controlAction?.action?.id == id }?.let {
return mission?.actions?.firstOrNull { it?.controlAction?.action?.id == id }?.let {
attachControlsToActionControl.toEnvAction(
actionId = id.toString(),
action = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class MissionController(
}

@QueryMapping
fun mission(@Argument missionId: Int): Mission {
val envMission = getEnvMissionById.execute(missionId = missionId)
fun mission(@Argument missionId: Int): Mission? {
val envMission = getEnvMissionById.execute(missionId = missionId) ?: return null

val fishMissionActions = getFishActionsByMissionId.execute(missionId = missionId)
val navMission = getNavMissionById.execute(missionId = missionId)

Expand Down
Loading

0 comments on commit 9ac14e7

Please sign in to comment.