Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Remove DOT format from stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Zimmer committed Sep 5, 2022
1 parent 15969d0 commit e6ddb58
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 296 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ LABEL org.opencontainers.image.vendor="neuland – Büro für Informatik GmbH"
LABEL org.opencontainers.image.licenses="Apache-2.0"
LABEL org.opencontainers.image.title="bandwhichd-server"
LABEL org.opencontainers.image.description="bandwhichd server collecting measurements and calculating statistics"
LABEL org.opencontainers.image.version="0.6.0-rc8"
LABEL org.opencontainers.image.version="0.6.0-rc9"
USER guest
ENTRYPOINT ["/opt/java/openjdk/bin/java"]
CMD ["-jar", "/opt/bandwhichd-server.jar"]
EXPOSE 8080
STOPSIGNAL SIGTERM
COPY --from=build --chown=root:root /tmp/bandwhichd-server/target/scala-3.1.3/bandwhichd-server-assembly-0.6.0-rc8.jar /opt/bandwhichd-server.jar
COPY --from=build --chown=root:root /tmp/bandwhichd-server/target/scala-3.1.3/bandwhichd-server-assembly-0.6.0-rc9.jar /opt/bandwhichd-server.jar
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lazy val root = (project in file("."))
.settings(
organization := "de.neuland-bfi",
name := "bandwhichd-server",
version := "0.6.0-rc8",
version := "0.6.0-rc9",
scalaVersion := "3.1.3",
Compile / scalaSource := baseDirectory.value / "src" / "main" / "scala",
Test / scalaSource := baseDirectory.value / "src" / "test" / "scala",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package de.neuland.bandwhichd.server.adapter.in.v1.stats

import de.neuland.bandwhichd.server.domain.stats.*
import de.neuland.bandwhichd.server.lib.dot.*
import io.circe.Json

import java.util.concurrent.atomic.AtomicReference
import io.circe.{Encoder, Json}

object StatsCodecs {
val circeEncoder: io.circe.Encoder[MonitoredStats] =
val encoder: Encoder[MonitoredStats] =
(stats: MonitoredStats) =>
Json.obj(
"hosts" -> Json.fromFields(
Expand Down Expand Up @@ -40,32 +37,4 @@ object StatsCodecs {
})
)
)

val dotEncoder
: de.neuland.bandwhichd.server.lib.dot.codec.Encoder[MonitoredStats] =
(stats: MonitoredStats) => {

val nodeStatements: Seq[Node] =
stats.allHosts.toSeq.map(host =>
Node(
id = NodeId(host.hostId.uuid.toString),
attributes = Seq(
Attribute.Label(host.host.toString)
)
)
)
val edgeStatements: Seq[Edge] =
stats.connections.toSeq
.map(connection =>
Edge(
idA = NodeId(connection._1.uuid.toString),
idB = NodeId(connection._2.uuid.toString)
)
)

Graph(
`type` = Directed,
statements = nodeStatements ++ edgeStatements
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import cats.implicits.*
import de.neuland.bandwhichd.server.application.StatsApplicationService
import de.neuland.bandwhichd.server.domain.measurement.Timing
import de.neuland.bandwhichd.server.domain.stats.*
import de.neuland.bandwhichd.server.lib.dot.Dot
import de.neuland.bandwhichd.server.lib.http4s.Helpers
import de.neuland.bandwhichd.server.lib.http4s.dot.DotHttp4s
import de.neuland.bandwhichd.server.lib.time.cats.TimeContext
import io.circe.Json
import io.circe.syntax.*
Expand Down Expand Up @@ -40,53 +38,10 @@ class StatsController[F[_]: Async](
response <- {
val statsWithinMonitoredNetworks: MonitoredStats =
monitoredStats.withoutHostsOutsideOfMonitoredNetworks
if (useDotInsteadOfJson(request.headers)) {
import de.neuland.bandwhichd.server.lib.http4s.dot.DotHttp4s.dotEntityEncoder
val dot: Dot = StatsCodecs.dotEncoder(statsWithinMonitoredNetworks)
Ok(dot)
} else {
import org.http4s.circe.CirceEntityEncoder.circeEntityEncoder
val json: Json =
statsWithinMonitoredNetworks.asJson(StatsCodecs.circeEncoder)
Ok(json)
}
import org.http4s.circe.CirceEntityEncoder.circeEntityEncoder
val json: Json =
statsWithinMonitoredNetworks.asJson(StatsCodecs.encoder)
Ok(json)
}
} yield response

private def useDotInsteadOfJson(headers: Headers) = {
val maybeAcceptHeader: Option[Accept] =
headers.get(
Header.Select.recurringHeadersWithMerge(
org.http4s.headers.Accept.headerSemigroupInstance,
org.http4s.headers.Accept.headerInstance
)
)

maybeAcceptHeader.fold(false) { acceptHeader =>

val mediaRangeAndQValues: NonEmptyList[MediaRangeAndQValue] =
acceptHeader.values

val maybeDotMediaRangeAndQValue: Option[MediaRangeAndQValue] =
mediaRangeAndQValues.find { mediaRangeAndQValue =>
DotHttp4s.mediaType.satisfiedBy(mediaRangeAndQValue.mediaRange)
}

maybeDotMediaRangeAndQValue.fold(false) { dotMediaRangeAndQValue =>

val maybeJsonMediaRangeAndQValue =
mediaRangeAndQValues.find { mediaRangeAndQValue =>
MediaType.application.json.satisfiedBy(
mediaRangeAndQValue.mediaRange
)
}

maybeJsonMediaRangeAndQValue.fold(true) { jsonMediaRangeAndQValue =>
dotMediaRangeAndQValue.qValue.compare(
jsonMediaRangeAndQValue.qValue
) >= 0
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import de.neuland.bandwhichd.server.domain.measurement.{Measurement, Timing}
import de.neuland.bandwhichd.server.domain.stats.Stats.Bundle
import de.neuland.bandwhichd.server.lib.time.Interval
import de.neuland.bandwhichd.server.lib.time.cats.TimeContext
import fs2.Stream

import java.nio.charset.StandardCharsets.UTF_8
import java.time.temporal.ChronoUnit.HOURS
Expand Down
20 changes: 0 additions & 20 deletions src/main/scala/de/neuland/bandwhichd/server/lib/dot/Dot.scala

This file was deleted.

This file was deleted.

90 changes: 0 additions & 90 deletions src/main/scala/de/neuland/bandwhichd/server/lib/dot/fs2/BC.scala

This file was deleted.

This file was deleted.

Loading

0 comments on commit e6ddb58

Please sign in to comment.