Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Menezes committed Apr 3, 2016
0 parents commit bb3b859
Show file tree
Hide file tree
Showing 69 changed files with 6,681 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Extracted from https://github.com/ulrich/macaron-factory/blob/master/.gitignore
# Ignore all dotfiles...
.*
# except for .gitignore
!.gitignore

# Ignore Play! working directory #
db
eclipse
lib
log
logs
modules
precompiled
project/project
project/target
target
tmp
test-result
server.pid
*.iml
*.eml
activator-*.sbt
node_modules
65 changes: 65 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module.exports = function(grunt) {

grunt.initConfig({
clean: {
dist: {
src: ['_site/dist']
}
},
watch: {
scripts: {
files: ['src/**/*.*', 'src/*.*'],
tasks: ['build'],
options: {
spawn: false
}
}
},
copy: {
main: {
files: [
]
}
},
concat: {
vendorjs: {
src: [
],
dest: 'public/lib.js'
},
vendorcss: {
src: [
],
dest: 'public/css/lib.css'
},
appjs: {
src: [
'src/main.js',
'src/*/*.js'
],
dest: 'public/app.js'
},
},
jshint: {
cerebro: {
src: [

]
}
},
qunit: {
all: []
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks("grunt-jscs");
grunt.registerTask('dev', ['watch'])
grunt.registerTask('build',
['clean', 'copy', 'concat' ]);
};
12 changes: 12 additions & 0 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controllers

import play.api.mvc.{Action, Controller}
import play.api.http.MimeTypes

object Application extends Controller {

def index = Action {
Ok(views.html.Index())
}

}
33 changes: 33 additions & 0 deletions app/controllers/ClusterOverviewController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package controllers

import elastic.ElasticClient._
import models.overview.ClusterOverview
import play.api.mvc.{Action, Controller}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


object ClusterOverviewController extends Controller {

def index = Action.async {
request => {
val host = request.queryString.getOrElse("host", Seq("http://localhost:9200")).head
try {
val response = Future.sequence(
Seq(clusterState(host), nodesStats(host), indicesStats(host), clusterSettings(host), aliases(host), clusterHealth(host), nodes(host), main(host))
).map { f =>
new ClusterOverview(f(0).body, f(1).body, f(2).body, f(3).body, f(4).body, f(5).body, f(6).body, f(7).body).json
}.recover {
case e =>
throw e
}
response.map(Ok(_))
} catch {
case _ => Future.successful(Status(500)(s"Cannot connect to $host"))
}
}

}

}
20 changes: 20 additions & 0 deletions app/controllers/HostsController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controllers

import play.api.Play
import play.api.libs.json.{JsArray, JsString}
import play.api.mvc.{Action, Controller}


class HostsController extends Controller {

def index = Action {
request => {
val hosts = Play.current.configuration.getConfigSeq("hosts") match {
case Some(a) => a.map { b => b.getString("host").get }
case None => Seq()
}
Ok(JsArray(hosts.map(JsString(_))))
}
}

}
10 changes: 10 additions & 0 deletions app/controllers/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controllers

import controllers.elasticsearch.ElasticsearchController
import elastic.ElasticClient

object Main extends ElasticsearchController {

def index = processRequest(ElasticClient.main(_))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/ClearIndexCacheController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class ClearIndexCacheController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.clearIndexCache(indices, _))

}
10 changes: 10 additions & 0 deletions app/controllers/elasticsearch/CloseIndexController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controllers.elasticsearch


import elastic.ElasticClient

class CloseIndexController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.closeIndex(indices, _))

}
10 changes: 10 additions & 0 deletions app/controllers/elasticsearch/DeleteIndexController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controllers.elasticsearch


import elastic.ElasticClient

class DeleteIndexController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.deleteIndex(indices, _))

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class DisableShardAllocationController extends ElasticsearchController {

def index() = processRequest(ElasticClient.disableShardAllocation(_))

}
27 changes: 27 additions & 0 deletions app/controllers/elasticsearch/ElasticsearchController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package controllers.elasticsearch

import elastic.ElasticResponse
import play.api.Logger
import play.api.mvc.{Action, Controller}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait ElasticsearchController extends Controller {

protected val logger = Logger("elastic")

def processRequest(f: (String => Future[ElasticResponse])) = Action.async {
request =>
val host = request.queryString.getOrElse("host", Seq("http://localhost:9200")).head
try {
val response = f(host)
response.map { r =>
Status(r.status)(r.body)
}
} catch {
case _ => Future.successful(Status(500)(s"Cannot connect to $host"))
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class EnableShardAllocationController extends ElasticsearchController {

def index() = processRequest(ElasticClient.enableShardAllocation(_))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/GetIndexMapping.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class GetIndexMapping extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.getIndexMapping(indices, _))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/GetIndexSettings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class GetIndexSettings extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.getIndexSettings(indices, _))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/NodeStatsController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class NodeStatsController extends ElasticsearchController {

def index(nodes: String) = processRequest(ElasticClient.nodesStats(nodes, _))

}
10 changes: 10 additions & 0 deletions app/controllers/elasticsearch/OpenIndexController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controllers.elasticsearch


import elastic.ElasticClient

class OpenIndexController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.openIndex(indices, _))

}
12 changes: 12 additions & 0 deletions app/controllers/elasticsearch/OptimizeIndexController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package controllers.elasticsearch

import elastic.ElasticClient
import play.api.mvc.{Action, Controller}

import scala.concurrent.ExecutionContext.Implicits.global

class OptimizeIndexController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.optimizeIndex(indices, _))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/PutClusterSettings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class PutClusterSettings extends ElasticsearchController {

def index = processRequest(ElasticClient.putClusterSettings("", _))

}
9 changes: 9 additions & 0 deletions app/controllers/elasticsearch/RefreshIndexController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controllers.elasticsearch

import elastic.ElasticClient

class RefreshIndexController extends ElasticsearchController {

def index(indices: String) = processRequest(ElasticClient.refreshIndex(indices, _))

}
74 changes: 74 additions & 0 deletions app/elastic/ElasticClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package elastic

import play.api.Play.current
import play.api.libs.ws.WS
import play.api.mvc.Results.EmptyContent

trait ElasticClient {

def main(host: String) =
ElasticResponse(WS.url(s"$host").get())

def clusterState(host: String) =
ElasticResponse(WS.url(s"$host/_cluster/state/master_node,routing_table,routing_nodes,blocks").get())

def indicesStats(host: String) =
ElasticResponse(WS.url(s"$host/_stats/docs,store").get())

def nodesStats(host: String) =
ElasticResponse(WS.url(s"$host/_nodes/stats/jvm,fs,os,process").get())

def nodesStats(node: String, host: String) =
ElasticResponse(WS.url(s"$host/_nodes/$node/stats?human").get())

def clusterSettings(host: String) =
ElasticResponse(WS.url(s"$host/_cluster/settings").get())

def aliases(host: String) =
ElasticResponse(WS.url(s"$host/_aliases").get())

def clusterHealth(host: String) =
ElasticResponse(WS.url(s"$host/_cluster/health").get())

def nodes(host: String) =
ElasticResponse(WS.url(s"$host/_nodes/_all/os,jvm").get())

def closeIndex(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_close").post(EmptyContent()))

def openIndex(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_open").post(EmptyContent()))

def refreshIndex(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_refresh").post(EmptyContent()))

def optimizeIndex(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_optimize").post(EmptyContent()))

def clearIndexCache(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_cache/clear").post(EmptyContent()))

def deleteIndex(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index").delete())

def getIndexSettings(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_settings").get())

def getIndexMapping(index: String, host: String) =
ElasticResponse(WS.url(s"$host/$index/_mapping").get())

def putClusterSettings(settings: String, host: String) =
ElasticResponse(WS.url(s"$host/_cluster/settings").put(settings))

private def allocationSettings(value: String) =
s"""{"transient": {"cluster": {"routing": {"allocation": {"enable": \"$value\"}}}}}"""

def enableShardAllocation(host: String) =
putClusterSettings(allocationSettings("all"), host)

def disableShardAllocation(host: String) =
putClusterSettings(allocationSettings("none"), host)

}

object ElasticClient extends ElasticClient
Loading

0 comments on commit bb3b859

Please sign in to comment.