Skip to content

Commit

Permalink
Merge pull request #49 from hmrc/BDOG-108
Browse files Browse the repository at this point in the history
Bdog 108
  • Loading branch information
colin-lamed committed Feb 5, 2019
2 parents 3b8fcc0 + 322cedf commit 1d56ee9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,33 @@ class DependencyExplorerController @Inject()(
def landing: Action[AnyContent] =
Action.async { implicit request =>
service.getGroupArtefacts.map { groupArtefacts =>
Ok(page(form, groupArtefacts, searchResults = None))
Ok(page(form.fill(SearchForm("", "", "", "0.0.0")), groupArtefacts, searchResults = None, pieData = None))
}
}


def search =
Action.async { implicit request =>
service.getGroupArtefacts.flatMap { groupArtefacts =>
def pageWithError(msg: String) = page(form.bindFromRequest().withGlobalError(msg), groupArtefacts, searchResults = None)
def pageWithError(msg: String) = page(form.bindFromRequest().withGlobalError(msg), groupArtefacts, searchResults = None, pieData = None)
form
.bindFromRequest()
.fold(
hasErrors = formWithErrors => Future.successful(BadRequest(page(formWithErrors, groupArtefacts, searchResults = None))),
hasErrors = formWithErrors => Future.successful(BadRequest(page(formWithErrors, groupArtefacts, searchResults = None, pieData = None))),
success = query => {
(for {
versionOp <- EitherT.fromOption[Future](VersionOp.parse(query.versionOp), BadRequest(pageWithError("Invalid version op")))
version <- EitherT.fromOption[Future](Version.parse(query.version), BadRequest(pageWithError("Invalid version")))
results <- EitherT.right[Result] {
service
.getServicesWithDependency(query.group, query.artefact, versionOp, version)
}
} yield Ok(page(form.bindFromRequest(), groupArtefacts, Some(results)))
}
pieData = DependencyExplorerController.PieData(
"Version spread",
results
.groupBy(r => s"${r.depGroup}:${r.depArtefact}:${r.depVersion}")
.map(r => r._1 -> r._2.size))
} yield Ok(page(form.bindFromRequest(), groupArtefacts, Some(results), Some(pieData)))
).merge
}
)
Expand Down Expand Up @@ -92,5 +97,10 @@ class DependencyExplorerController @Inject()(
"version" -> Forms.text
)(SearchForm.apply)(SearchForm.unapply)
)

}

object DependencyExplorerController {
case class PieData(
title : String,
results: Map[String, Int])
}
41 changes: 38 additions & 3 deletions app/views/DependencyExplorerPage.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*@

@import views.html.helper.CSRF
@import play.api.mvc.Call
@import uk.gov.hmrc.cataloguefrontend.connector.model.{GroupArtefacts, ServiceWithDependency, VersionOp}
@import uk.gov.hmrc.cataloguefrontend.DependencyExplorerController.PieData
@import uk.gov.hmrc.cataloguefrontend.ViewMessages
@import uk.gov.hmrc.cataloguefrontend.routes
@import uk.gov.hmrc.cataloguefrontend.service.SearchByUrlService.FrontendRoutes
Expand All @@ -26,13 +26,15 @@

@(form : Form[_],
groupArtefacts : List[GroupArtefacts],
searchResults : Option[Seq[ServiceWithDependency]])(
searchResults : Option[Seq[ServiceWithDependency]],
pieData : Option[PieData])(
implicit request : Request[_],
messagesProvider: MessagesProvider)

@standard_layout("Dependency Explorer", "search") {
<header>
<h1 id="search-service-header">Dependency Explorer</h1>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</header>

<div id="service-list">
Expand All @@ -54,7 +56,6 @@ <h1 id="search-service-header">Dependency Explorer</h1>
action = routes.DependencyExplorerController.search,
'class -> "form-inline",
'id -> "search-by-dependency-form") {
@helper.CSRF.formField
<table padding="1">
<tr>
<td valign="top">
Expand Down Expand Up @@ -106,6 +107,9 @@ <h1 id="search-service-header">Dependency Explorer</h1>
}
case Some(searchResults) => {
<p>Found @searchResults.size results</p>

<div id="chart_div" align="center"></div>

<table id="search-results" class="table table-striped">
<thead>
<tr>
Expand Down Expand Up @@ -183,4 +187,35 @@ <h1 id="search-service-header">Dependency Explorer</h1>
}
}
</script>

<script type="text/javascript">
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {

@pieData match {
case None => {}
case Some(pd) => {

var data = new google.visualization.DataTable();
var x = Math.floor((Math.random() * 10) + 1);
data.addColumn('string', 'Version');
data.addColumn('number', 'Count');
data.addRows([
@for(r <- pd.results) {
['@r._1', @r._2],
}
]);

var options = { 'title' : '@pd.title',
'width' : "80%",
'height' : 200,
'chartArea': { 'width': '100%',
'height': '100%'}};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
}
</script>
}
2 changes: 1 addition & 1 deletion conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ POST /search uk.gov.hmrc.cataloguefro
GET /dependencies/:name uk.gov.hmrc.cataloguefrontend.DependenciesController.service(name)

GET /dependencyexplorer uk.gov.hmrc.cataloguefrontend.DependencyExplorerController.landing
POST /dependencyexplorer uk.gov.hmrc.cataloguefrontend.DependencyExplorerController.search
GET /dependencyexplorer/results uk.gov.hmrc.cataloguefrontend.DependencyExplorerController.search

0 comments on commit 1d56ee9

Please sign in to comment.