Skip to content

Commit

Permalink
BDOG-3170 Tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Jul 2, 2024
1 parent 341db98 commit 3d2d504
Show file tree
Hide file tree
Showing 60 changed files with 349 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BobbyRulesTrendController @Inject() (
page(
form.fill(SearchForm(rules = Seq.empty, from = LocalDate.now().minusYears(2) , to = LocalDate.now())),
allRules,
flags = SlugInfoFlag.values,
flags = SlugInfoFlag.values.toSeq,
data = None
)
)
Expand All @@ -68,17 +68,18 @@ class BobbyRulesTrendController @Inject() (
allRules <- configConnector.bobbyRules()
.map(_.libraries)
.map(_.sortBy(-_.from.toEpochDay))
flags = SlugInfoFlag.values.toSeq
pageWithError = (msg: String) =>
page(
form.bindFromRequest().withGlobalError(msg),
allRules,
flags = SlugInfoFlag.values,
flags,
data = None
)
res <- form
.bindFromRequest()
.fold(
hasErrors = formWithErrors => Future.successful(BadRequest(page(formWithErrors, allRules, flags = SlugInfoFlag.values, data = None))),
hasErrors = formWithErrors => Future.successful(BadRequest(page(formWithErrors, allRules, flags, data = None))),
success = query =>
(for
violations <- EitherT.right[Result](serviceDeps.getHistoricBobbyRuleViolations(query.rules.toList, query.from, query.to))
Expand All @@ -87,7 +88,7 @@ class BobbyRulesTrendController @Inject() (
Ok(page(
form.bindFromRequest(),
allRules,
flags = SlugInfoFlag.values,
flags,
Some(
countData
.groupBy { case ((_, e), _) => e }
Expand Down
4 changes: 2 additions & 2 deletions app/uk/gov/hmrc/cataloguefrontend/CatalogueController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class CatalogueController @Inject() (
serviceMetricsConnector.nonPerformantQueriesForService(serviceName)
else
Future.successful(Seq.empty)
envDatas <- Environment.valuesAsSeq
envDatas <- Environment.values.toSeq
.traverse: env =>
val deployedVersions = deployments.filter(_.environment == env).map(_.version)
// a single environment may have multiple versions during a deployment
Expand All @@ -169,7 +169,7 @@ class CatalogueController @Inject() (
case Some(version) =>
for
repoModules <- serviceDependenciesConnector.getRepositoryModules(repositoryName, version)
shutterStates <- ShutterType.valuesAsSeq.foldLeftM[Future, Seq[ShutterState]](Seq.empty): (acc, shutterType) =>
shutterStates <- ShutterType.values.toSeq.foldLeftM[Future, Seq[ShutterState]](Seq.empty): (acc, shutterType) =>
shutterService
.getShutterStates(shutterType, env, Some(serviceName))
.map(acc ++ _)
Expand Down
9 changes: 5 additions & 4 deletions app/uk/gov/hmrc/cataloguefrontend/JdkVersionController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import cats.implicits._
import play.api.mvc.MessagesControllerComponents
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.TeamsAndRepositoriesConnector
import uk.gov.hmrc.cataloguefrontend.model.{SlugInfoFlag, TeamName}
import uk.gov.hmrc.cataloguefrontend.model.{SlugInfoFlag, TeamName, given}
import uk.gov.hmrc.cataloguefrontend.service.DependenciesService
import uk.gov.hmrc.cataloguefrontend.util.Parser
import uk.gov.hmrc.cataloguefrontend.view.html.{JdkAcrossEnvironmentsPage, JdkVersionPage}
import uk.gov.hmrc.internalauth.client.FrontendAuthComponents
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController
Expand All @@ -46,18 +47,18 @@ class JdkVersionController @Inject() (
BasicAuthAction.async { implicit request =>
for
teams <- teamsAndRepositoriesConnector.allTeams()
selectedFlag = SlugInfoFlag.parse(flag.toLowerCase).getOrElse(SlugInfoFlag.Latest)
selectedFlag = Parser[SlugInfoFlag].parse(flag.toLowerCase).getOrElse(SlugInfoFlag.Latest)
selectedTeamName = teamName.flatMap(n => teams.find(_.name == n)).map(_.name)
jdkVersions <- dependenciesService.getJdkVersions(selectedFlag, selectedTeamName)
yield Ok(jdkVersionPage(jdkVersions.sortBy(j => (j.version, j.serviceName)), SlugInfoFlag.values, teams, selectedFlag, selectedTeamName))
yield Ok(jdkVersionPage(jdkVersions.sortBy(j => (j.version, j.serviceName)), SlugInfoFlag.values.toSeq, teams, selectedFlag, selectedTeamName))
}

def compareAllEnvironments(teamName: Option[TeamName]) =
BasicAuthAction.async { implicit request =>
for
teams <- teamsAndRepositoriesConnector.allTeams()
selectedTeamName = teamName.flatMap(n => teams.find(_.name == n)).map(_.name)
envs <- SlugInfoFlag.values.traverse(env => dependenciesService.getJdkCountsForEnv(env, selectedTeamName))
envs <- SlugInfoFlag.values.toSeq.traverse(env => dependenciesService.getJdkCountsForEnv(env, selectedTeamName))
jdks = envs.flatMap(_.usage.keys).distinct.sortBy(_._1)
yield Ok(jdkAcrossEnvironmentsPage(envs, jdks, teams, selectedTeamName))
}
Expand Down
9 changes: 5 additions & 4 deletions app/uk/gov/hmrc/cataloguefrontend/SbtVersionController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import cats.implicits._
import play.api.mvc.MessagesControllerComponents
import uk.gov.hmrc.cataloguefrontend.auth.CatalogueAuthBuilders
import uk.gov.hmrc.cataloguefrontend.connector.TeamsAndRepositoriesConnector
import uk.gov.hmrc.cataloguefrontend.model.{SlugInfoFlag, TeamName}
import uk.gov.hmrc.cataloguefrontend.model.{SlugInfoFlag, TeamName, given}
import uk.gov.hmrc.cataloguefrontend.service.DependenciesService
import uk.gov.hmrc.cataloguefrontend.util.Parser
import uk.gov.hmrc.cataloguefrontend.view.html.{SbtVersionPage, SbtAcrossEnvironmentsPage}
import uk.gov.hmrc.internalauth.client.FrontendAuthComponents
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController
Expand All @@ -46,18 +47,18 @@ class SbtVersionController @Inject()(
BasicAuthAction.async { implicit request =>
for
teams <- teamsAndRepositoriesConnector.allTeams()
selectedFlag = SlugInfoFlag.parse(flag.toLowerCase).getOrElse(SlugInfoFlag.Latest)
selectedFlag = Parser[SlugInfoFlag].parse(flag.toLowerCase).getOrElse(SlugInfoFlag.Latest)
selectedTeam = teamName.flatMap(n => teams.find(_.name == n))
sbtVersions <- dependenciesService.getSbtVersions(selectedFlag, selectedTeam.map(_.name))
yield Ok(sbtVersionPage(sbtVersions.sortBy(s => (s.version, s.serviceName)), SlugInfoFlag.values, teams, selectedFlag, selectedTeam))
yield Ok(sbtVersionPage(sbtVersions.sortBy(s => (s.version, s.serviceName)), SlugInfoFlag.values.toSeq, teams, selectedFlag, selectedTeam))
}

def compareAllEnvironments(teamName: Option[TeamName]) =
BasicAuthAction.async { implicit request =>
for
teams <- teamsAndRepositoriesConnector.allTeams()
selectedTeam = teamName.flatMap(n => teams.find(_.name == n))
envs <- SlugInfoFlag.values.traverse(env => dependenciesService.getSbtCountsForEnv(env, selectedTeam.map(_.name)))
envs <- SlugInfoFlag.values.toSeq.traverse(env => dependenciesService.getSbtCountsForEnv(env, selectedTeam.map(_.name)))
sbts = envs.flatMap(_.usage.keys).distinct.sorted
yield Ok(sbtAcrossEnvironmentsPage(envs, sbts, teams, selectedTeam))
}
Expand Down
18 changes: 16 additions & 2 deletions app/uk/gov/hmrc/cataloguefrontend/binders/Binders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package uk.gov.hmrc.cataloguefrontend.binders

import cats.implicits._
import play.api.data.FormError
import play.api.data.format.Formatter
import play.api.mvc.{PathBindable, QueryStringBindable}

import java.time.{Instant, LocalDate}
Expand Down Expand Up @@ -52,10 +55,21 @@ object Binders:
* This function provides `andThen` semantics
*/
def pathBindableFromString[T](parse: String => Either[String, T], asString: T => String)(using strBinder: PathBindable[String]): PathBindable[T] =
new PathBindable[T] {
new PathBindable[T]:
override def bind(key: String, value: String): Either[String, T] =
parse(value)

override def unbind(key: String, value: T): String =
asString(value)
}

def formFormatFromString[T](parse: String => Either[String, T], asString: T => String): Formatter[T] =
new Formatter[T]:
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], T] =
data
.get(key)
.map(_.trim) match
case Some(s) if s.nonEmpty => parse(s).leftMap(err => Seq(FormError(key, err)))
case _ => Left(Seq(FormError(key, s"$key is missing")))

override def unbind(key: String, value: T): Map[String, String] =
Map(key -> asString(value))
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ object BuildDeployApiConnector:

given Parser[PrototypeStatus] = Parser.parser(PrototypeStatus.values)

enum PrototypeStatus(val asString: String, val displayString: String) extends FromString derives Ordering, Reads, PathBindable:
enum PrototypeStatus(
override val asString: String,
val displayString : String
) extends FromString
derives Ordering, Reads, PathBindable:
case Running extends PrototypeStatus(asString = "running" , displayString = "Running" )
case Stopped extends PrototypeStatus(asString = "stopped" , displayString = "Stopped" )
case Undetermined extends PrototypeStatus(asString = "undetermined", displayString = "Undetermined")

object PrototypeStatus extends FromStringEnum[PrototypeStatus]

case class PrototypeDetails(
url : Option[String],
status: PrototypeStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class ResourceUsageConnector @Inject() (
acc.lastOption match
case Some(previous) =>
Environment
.valuesAsSeq.map: env =>
.values.map: env =>
env -> resourceUsages
.find(_.environment == env).map(_.deploymentSize)
.orElse(previous.values.get(env))
.getOrElse(DeploymentSize.empty)
.toMap
case None =>
Environment
.valuesAsSeq.map: env =>
.values.map: env =>
env -> resourceUsages
.find(_.environment == env).map(_.deploymentSize)
.getOrElse(DeploymentSize.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,40 @@ import FromStringEnum._

given Parser[RepoType] = Parser.parser(RepoType.values)

enum RepoType(val asString: String) extends FromString derives Ordering, Reads, Writes, FormFormat:
enum RepoType(
override val asString: String
) extends FromString
derives Ordering, Reads, Writes, FormFormat:
case Service extends RepoType("Service" )
case Library extends RepoType("Library" )
case Prototype extends RepoType("Prototype")
case Test extends RepoType("Test" )
case Other extends RepoType("Other" )

object RepoType extends FromStringEnum[RepoType]

given Parser[ServiceType] = Parser.parser(ServiceType.values)

enum ServiceType(val asString: String, val displayString: String) extends FromString derives Ordering, Reads, Writes, FormFormat, QueryStringBindable:
case Frontend extends ServiceType(asString = "frontend", displayString = "Service (Frontend)")
case Backend extends ServiceType(asString = "backend" , displayString = "Service (Backend)")

object ServiceType extends FromStringEnum[ServiceType]
enum ServiceType(
override val asString: String,
val displayString : String
) extends FromString
derives Ordering, Reads, Writes, FormFormat, QueryStringBindable:
case Frontend extends ServiceType(asString = "frontend", displayString = "Frontend")
case Backend extends ServiceType(asString = "backend" , displayString = "Backend")

given Parser[Tag] = Parser.parser(Tag.values)

enum Tag(val asString: String, val displayString: String) extends FromString derives Ordering, Reads:
enum Tag(
override val asString: String,
val displayString : String
) extends FromString
derives Ordering, Reads:
case AdminFrontend extends Tag(asString = "admin" , displayString = "Admin Frontend" )
case Api extends Tag(asString = "api" , displayString = "API" )
case BuiltOffPlatform extends Tag(asString = "built-off-platform", displayString = "Built Off Platform")
case Maven extends Tag(asString = "maven" , displayString = "Maven" )
case Stub extends Tag(asString = "stub" , displayString = "Stub" )


object Tag extends FromStringEnum[Tag]

case class Link(
name : String,
displayName: String,
Expand Down Expand Up @@ -103,13 +108,14 @@ object BuildData:

given Parser[BuildJobType] = Parser.parser(BuildJobType.values)

enum BuildJobType(val asString: String) extends FromString derives Ordering, Reads:
enum BuildJobType(
override val asString: String
) extends FromString
derives Ordering, Reads:
case Job extends BuildJobType("job" )
case Pipeline extends BuildJobType("pipeline" )
case PullRequest extends BuildJobType("pull-request")

object BuildJobType extends FromStringEnum[BuildJobType]

case class JenkinsJob(
name : String,
jenkinsURL : String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.cataloguefrontend.connector.model

import cats.implicits._
import play.api.libs.functional.syntax._
import play.api.libs.json.{JsError, JsResult, JsSuccess, JsValue, Reads, __}
import play.api.libs.json.{JsResult, JsString, JsValue, Reads, __}
import uk.gov.hmrc.cataloguefrontend.model.SlugInfoFlag

import java.time.LocalDate
Expand All @@ -39,23 +39,21 @@ private object DataFormat:

def dataReads[A: Reads]: Reads[Map[(BobbyRule, SlugInfoFlag), A]] =
given Reads[BobbyRule] = BobbyRule.reads
summon[Reads[List[(JsValue, Map[String, A])]]]
.flatMap: x =>
val y = x // List
.flatTraverse: (k1, v1) =>
v1
.toList.traverse: (k2, v2) =>
SlugInfoFlag.parse(k2) match
case Some(flag) =>
JsSuccess:
summon[Reads[List[(JsValue, Map[JsString, A])]]]
.flatMap: l =>
_ =>
l
.flatTraverse: (k1, v1) =>
v1
.toList.traverse: (k2, v2) =>
summon[Reads[SlugInfoFlag]].reads(k2)
.map: flag =>
( ( k1.as[BobbyRule]
, flag
)
, v2
)
case None =>
JsError(__, s"Invalid SlugInfoFlag $k2")
_ => y.map(_.toMap)
.map(_.toMap)

object BobbyRulesSummary:
val reads: Reads[BobbyRulesSummary] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ object GroupArtefacts {

given Parser[DependencyScope] = Parser.parser(DependencyScope.values)

enum DependencyScope(val asString: String) extends FromString derives Ordering, Reads, Writes:
enum DependencyScope(
override val asString: String
) extends FromString
derives Ordering, Reads, Writes:
case Compile extends DependencyScope("compile" )
case Provided extends DependencyScope("provided")
case Test extends DependencyScope("test" )
Expand All @@ -205,6 +208,3 @@ enum DependencyScope(val asString: String) extends FromString derives Ordering,
asString match
case "it" => "Integration Test"
case other => other.capitalize


object DependencyScope extends FromStringEnum[DependencyScope]
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CostController @Inject() (
val configEstimateCosts = String.format("%.0f", configList.map(_.deploymentSize.totalSlots.costGbp(costEstimateConfig)).sum)

val slotsAndInstances =
Environment.valuesAsSeq.flatMap: env =>
Environment.values.flatMap: env =>
Seq(
s"slots.{${env.asString}}" -> configList.find(_.environment == env).map(_.deploymentSize.slots.toString).getOrElse(""),
s"instances.{${env.asString}}" -> configList.find(_.environment == env).map(_.deploymentSize.instances.toString).getOrElse("")
Expand Down
7 changes: 4 additions & 3 deletions app/uk/gov/hmrc/cataloguefrontend/cost/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ package object cost:

given Parser[Zone] = Parser.parser(Zone.values)

enum Zone(val asString: String) extends FromString derives Ordering, Reads:
enum Zone(
override val asString: String
) extends FromString
derives Ordering, Reads:
case Protected extends Zone("protected" )
case Public extends Zone("public" )
case ProtectedRate extends Zone("protected-rate" )
Expand All @@ -81,8 +84,6 @@ package object cost:
def displayName: String =
asString.capitalize

object Zone extends FromStringEnum[Zone]

object DeploymentConfig:
val reads: Reads[DeploymentConfig] =
( (__ \ "name" ).read[ServiceName ](ServiceName.format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h1 class="page-heading mt-4">
<tr>
<th rowspan="2"><button role="button" id="name" data-sort="repo-name" class="fw-bold sort no-border">
Application Name</button></th>
@Environment.valuesAsSeq.map { environment =>
@Environment.values.map { environment =>
<th colspan="2" style="text-align: center">@environment.toString</th>
}
<th rowspan="2" style="text-align: center">
Expand All @@ -87,7 +87,7 @@ <h1 class="page-heading mt-4">
</th>
</tr>
<tr>
@Environment.valuesAsSeq.map(_ =>
@Environment.values.map(_ =>
<th>Slots</th>
<th>Instances</th>
)
Expand All @@ -99,7 +99,7 @@ <h1 class="page-heading mt-4">
<td class="repo-name" id="row@{
index
}_name"><a id="link-to-@serviceName-page" href="/repositories/@serviceName.asString">@serviceName.asString</a></td>
@Environment.valuesAsSeq.map { env =>
@Environment.values.map { env =>
<td style="text-align: center">@configs.find(_.environment == env).map(_.deploymentSize.slots)</td>
<td style="text-align: center">@configs.find(_.environment == env).map(_.deploymentSize.instances)</td>
}
Expand Down
Loading

0 comments on commit 3d2d504

Please sign in to comment.