Commit e7ffe708 authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

added cluster changes notifications service

parent df690d07
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
package controllers

import models.ElasticServer
import models.commons.Indices
import models.commons.{Indices, Nodes}

import scala.concurrent.ExecutionContext.Implicits.global

class CommonsController extends BaseController {
@@ -12,6 +13,12 @@ class CommonsController extends BaseController {
    }
  }

  def nodes = process { (request, client) =>
    client.getNodes(ElasticServer(request.host, request.authentication)).map { response =>
      Status(response.status)(Nodes(response.body))
    }
  }

  def getIndexSettings = process { (request, client) =>
    client.getIndexSettings(request.get("index"), ElasticServer(request.host, request.authentication)) map { response =>
      Status(response.status)(response.body)
+5 −0
Original line number Diff line number Diff line
@@ -148,6 +148,11 @@ trait ElasticClient {
    execute(s"${target.host}$path", "GET", None, target.authentication)
  }

  def getNodes(target: ElasticServer) = {
    val path = s"/_cat/nodes?format=json"
    execute(s"${target.host}$path", "GET", None, target.authentication)
  }

  def analyzeTextByField(index: String, field: String, text: String, target: ElasticServer) = {
    val path = s"/$index/_analyze"
    val body = Json.obj("text" -> text, "field" -> field).toString()
+11 −0
Original line number Diff line number Diff line
package models.commons

import play.api.libs.json.{JsArray, JsString, JsValue}

object Nodes {

  def apply(data: JsValue) = JsArray(data.as[JsArray].value.collect {
    case node => (node \ "name").as[JsString]
  })

}
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ POST /analysis/analyze/field @controllers.AnalysisContr

# Commons
POST        /commons/indices                          @controllers.CommonsController.indices
POST        /commons/nodes                            @controllers.CommonsController.nodes
POST        /commons/get_index_settings               @controllers.CommonsController.getIndexSettings
POST        /commons/get_index_mapping                @controllers.CommonsController.getIndexMappings
POST        /commons/get_node_stats                   @controllers.CommonsController.getNodeStats
+74 −0
Original line number Diff line number Diff line
@@ -1390,6 +1390,76 @@ angular.module('cerebro').factory('AlertService', function() {
  return this;
});

angular.module('cerebro').factory('ClusterChangesService', [
  '$rootScope', 'AlertService', 'RefreshService', 'DataService',
  function($rootScope, AlertService, RefreshService, DataService) {

    var indices;
    var nodes;

    var process = function() {
      var successIndices = function(currentIndices) {
        if (indices) {
          var created = difference(currentIndices, indices);
          var deleted = difference(indices, currentIndices);
          if (created.length > 0) {
            info(created, ' indices created');
          }
          if (deleted.length > 0) {
            warn(deleted, ' indices deleted');
          }
        }
        indices = currentIndices;
      };
      DataService.getIndices(successIndices, angular.noop);

      var successNodes = function(currentNodes) {
        if (nodes) {
          var joined = difference(currentNodes, nodes);
          var left = difference(nodes, currentNodes);
          if (joined.length > 0) {
            info(joined, ' nodes joined the cluster');
          }
          if (left.length > 0) {
            warn(left, ' nodes left the cluster');
          }
        }
        nodes = currentNodes;
      };
      DataService.getNodes(successNodes, angular.noop);
    };

    var difference = function(set1, set2) {
      return set1.filter(function(s) {
        return set2.indexOf(s) < 0;
      });
    };

    var info = function(elements, text) {
      AlertService.info(elements.length + text, elements.join(',\n'));
    };

    var warn = function(elements, text) {
      AlertService.warn(elements.length + text, elements.join(',\n'));
    };

    $rootScope.$watch(
      function() {
        return RefreshService.lastUpdate();
      },
      function() {
        console.log('rpocessing!');
        process();
      },
      true
    );

    return this;
  }]
);

angular.module('cerebro').run(['ClusterChangesService', angular.noop]);

angular.module('cerebro').factory('DataService', ['$rootScope', '$timeout',
  '$http', '$location', 'RefreshService', 'AlertService',
  function($rootScope, $timeout, $http, $location, RefreshService,
@@ -1490,6 +1560,10 @@ angular.module('cerebro').factory('DataService', ['$rootScope', '$timeout',
      request('/commons/indices', {}, success, error);
    };

    this.getNodes = function(success, error) {
      request('/commons/nodes', {}, success, error);
    };

    this.getIndexSettings = function(index, success, error) {
      request('/commons/get_index_settings', {index: index}, success, error);
    };
Loading