From ee7eb10faae19b95c94b326c9c90ad3d8741d997 Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Sat, 21 Sep 2013 02:08:15 +0100 Subject: [PATCH] Columnar list view introduced. TODO: Add tests once migrated to new serverside stack. --- src/main/groovy/server.groovy | 74 +++++++++++++++++++++++++++++----- src/main/templates/list_2.gtpl | 9 +++++ 2 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 src/main/templates/list_2.gtpl diff --git a/src/main/groovy/server.groovy b/src/main/groovy/server.groovy index 70250ee7..d30d74a1 100644 --- a/src/main/groovy/server.groovy +++ b/src/main/groovy/server.groovy @@ -17,10 +17,10 @@ import groovy.text.SimpleTemplateEngine import org.vertx.groovy.core.http.RouteMatcher -import org.vertx.java.core.json.JsonObject final GVM_VERSION = '@GVM_VERSION@' final VERTX_VERSION = '@VERTX_VERSION@' +final COLUMN_LENGTH = 15 // // datasource configuration @@ -116,19 +116,74 @@ rm.get("/candidates/:candidate/list") { req -> def candidate = req.params['candidate'] def current = req.params['current'] ?: '' def installed = req.params['installed'] ? req.params['installed'].tokenize(',') : [] - def gtplFile = 'build/templates/list.gtpl' as File def cmd = [action:"find", collection:"versions", matcher:[candidate:candidate], keys:["version":1], sort:["version":-1]] vertx.eventBus.send("mongo-persistor", cmd){ msg -> def available = msg.body.results.collect { it.version } - def local = installed.findAll { ! available.contains(it) } - def binding = [candidate:candidate, available:available, current:current, installed:installed, local:local] - def template = templateEngine.createTemplate(gtplFile).make(binding) - addPlainTextHeader req - req.response.end template.toString() + + def combined = combine(available, installed) + def local = determineLocal(available, installed) + + def content = prepareListView(combined, current, installed, local, COLUMN_LENGTH) + + def binding = [candidate: candidate, content:content] + def gtplFile = 'build/templates/list_2.gtpl' as File + def templateText = templateEngine.createTemplate(gtplFile).make(binding).toString() + + addPlainTextHeader req + req.response.end templateText } } +private prepareListView(combined, current, installed, local, colLength){ + def output = "" + for (i in (0..(colLength-1))){ + def versionColumn1 = prepareVersion(combined[i], current, installed, local) + def versionColumn2 = prepareVersion(combined[i+(colLength*1)], current, installed, local) + def versionColumn3 = prepareVersion(combined[i+(colLength*2)], current, installed, local) + def versionColumn4 = prepareVersion(combined[i+(colLength*3)], current, installed, local) + output += "${pad(versionColumn1)} ${pad(versionColumn2)} ${pad(versionColumn3)} ${pad(versionColumn4)}\n" + } + output +} + +private prepareVersion(version, current, installed, local){ + def isCurrent = (current == version) + def isInstalled = installed.contains(version) + def isLocalOnly = local.contains(version) + decorateVersion(version, isCurrent, isInstalled, isLocalOnly) +} + +private decorateVersion(version, isCurrent, isInstalled, isLocalOnly) { + " ${markCurrent(isCurrent)} ${markStatus(isInstalled, isLocalOnly)} ${version ?: ''}" +} + +private pad(col, width=20) { + (col ?: "").take(width).padRight(width) +} + +private markCurrent(isCurrent){ + isCurrent ? '>' : ' ' +} + + +private markStatus(isInstalled, isLocalOnly){ + if(isInstalled && isLocalOnly) '+' + else if(isInstalled) '*' + else ' ' +} + +private determineLocal(available, installed){ + installed.findAll { ! available.contains(it) } +} + +private combine(available, installed){ + def combined = [] as TreeSet + combined.addAll installed + combined.addAll available + combined.toList().reverse() +} + def validationHandler = { req -> def candidate = req.params['candidate'] def version = req.params['version'] @@ -231,7 +286,4 @@ private log(command, candidate, version, req){ def port = System.getenv('PORT') ?: 8080 def host = System.getenv('PORT') ? '0.0.0.0' : 'localhost' println "Starting vertx on $host:$port" -vertx.createHttpServer().requestHandler(rm.asClosure()).listen(port as int, host) - - - +vertx.createHttpServer().requestHandler(rm.asClosure()).listen(port as int, host) \ No newline at end of file diff --git a/src/main/templates/list_2.gtpl b/src/main/templates/list_2.gtpl new file mode 100644 index 00000000..f19dfcb0 --- /dev/null +++ b/src/main/templates/list_2.gtpl @@ -0,0 +1,9 @@ +================================================================================ +Available ${candidate.capitalize()} Versions +================================================================================ +${content} +================================================================================ ++ - local version +* - installed +> - currently in use +================================================================================