diff --git a/app/assets/javascripts/alert.js b/app/assets/javascripts/alert.js index f400adb..53b21d7 100644 --- a/app/assets/javascripts/alert.js +++ b/app/assets/javascripts/alert.js @@ -1,6 +1,7 @@ (function(){ "use strict"; - var POLLING_INTERVAL = 1 * 1000; + var POLLING_INTERVAL = 3 * 1000; + var POLLING_URL = "/polling/alerts"; $(function(){ var alert = new Vue({ @@ -11,33 +12,30 @@ created: function(){ var self = this; - setInterval(function(){ - self.fetchData().then(function(alerts){ + var fetch = function(){ + self.fetchAlertsData().then(function(alerts){ self.alerts = alerts; }); - }, POLLING_INTERVAL); + }; + fetch(); + setInterval(fetch, POLLING_INTERVAL); }, computed: { + alertsCount: { + $get: function(){ return this.alerts.length; } + }, hasAlerts: { - $get: function(){ - return this.alerts.length > 0; - } + $get: function(){ return this.alertsCount > 0; } } }, methods: { - fetchData: function() { - // TODO: fetch from Rails app + fetchAlertsData: function() { return new Promise(function(resolve, reject) { - resolve([ - { - "text": "dummy: " + (new Date).toString(), - }, - { - "text": "dummy: " + (Math.random()).toString(), - }, - ]); + $.getJSON(POLLING_URL, function(data){ + resolve(data); + }).fail(reject); }); } } diff --git a/app/assets/stylesheets/common.css.scss b/app/assets/stylesheets/common.css.scss index b1c4800..2e3b8b5 100644 --- a/app/assets/stylesheets/common.css.scss +++ b/app/assets/stylesheets/common.css.scss @@ -27,3 +27,13 @@ label { #page-wrapper { padding-bottom: 2em; } + + +// expand alert area width +.navbar-top-links .dropdown-messages, +.navbar-top-links .dropdown-tasks, +.navbar-top-links .dropdown-alerts { + // width: 310px; + width: 400px; + min-width: 0; +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3d74a6f..d505276 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception helper_method :current_user helper_method :current_locale + helper_method :installing_gem?, :installing_gems, :uninstalling_gem?, :uninstalling_gems before_action :login_required before_action :set_locale @@ -21,6 +22,26 @@ class ApplicationController < ActionController::Base I18n.locale end + def installing_gem? + installing_gems.length > 0 + end + + def installing_gems + Plugin::WORKING.find_all do |data| + data[:type] == :install && data[:state] == :running + end.map{|data| data[:plugin]} || [] + end + + def uninstalling_gem? + uninstalling_gems.length > 0 + end + + def uninstalling_gems + Plugin::WORKING.find_all do |data| + data[:type] == :uninstall && data[:state] == :running + end.map{|data| data[:plugin]} || [] + end + private def find_fluentd diff --git a/app/controllers/polling_controller.rb b/app/controllers/polling_controller.rb new file mode 100644 index 0000000..cb70e98 --- /dev/null +++ b/app/controllers/polling_controller.rb @@ -0,0 +1,20 @@ +class PollingController < ApplicationController + def alerts + alerts = [] + installing_gems.each do |plugin| + target = plugin.gem_name.dup + target << "(#{plugin.version})" if plugin.version + alerts << { + text: I18n.t('terms.installing', target: target) + } + end + uninstalling_gems.each do |plugin| + target = plugin.gem_name.dup + target << "(#{plugin.version})" if plugin.version + alerts << { + text: I18n.t('terms.uninstalling', target: target) + } + end + render json: alerts + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9332f5b..9ab688d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,26 +5,6 @@ module ApplicationHelper Plugin.gemfile_changed? end - def installing_gem? - installing_gems.length > 0 - end - - def installing_gems - Plugin::WORKING.find_all do |data| - data[:type] == :install && data[:state] == :running - end.map{|data| data[:plugin]} || [] - end - - def uninstalling_gem? - uninstalling_gems.length > 0 - end - - def uninstalling_gems - Plugin::WORKING.find_all do |data| - data[:type] == :uninstall && data[:state] == :running - end.map{|data| data[:plugin]} || [] - end - def has_td_agent_system? File.exist?("/etc/init.d/td-agent") end diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index 16dd54a..56ad605 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -24,6 +24,8 @@ en: new: New setup: "Setup %{target}" install_it: "Install %{target}" + installing: "Installing: %{target}" + uninstalling: "Uninstalling: %{target}" plugins: common: &plugin_common diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 8350c3a..8dbf6da 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -24,6 +24,8 @@ ja: new: 新規作成 setup: "%{target}をセットアップ" install_it: "%{target}をインストール" + installing: "インストール中: %{target}" + uninstalling: "アンインストール中: %{target}" plugins: common: &plugin_common diff --git a/config/routes.rb b/config/routes.rb index bc2d9ac..ed64061 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,7 +25,12 @@ Rails.application.routes.draw do end end + resource :user, only: [:show, :edit, :update] + get "misc" => "misc#show" get "misc/information" - resource :user, only: [:show, :edit, :update] + + namespace :polling do + get "alerts" + end end diff --git a/spec/controllers/polling_controller_spec.rb b/spec/controllers/polling_controller_spec.rb new file mode 100644 index 0000000..f76a2de --- /dev/null +++ b/spec/controllers/polling_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe PollingController do + +end