diff --git a/app/assets/javascripts/setting_format.js b/app/assets/javascripts/setting_format.js new file mode 100644 index 0000000..ae5cbcc --- /dev/null +++ b/app/assets/javascripts/setting_format.js @@ -0,0 +1,15 @@ +(function(){ + "use strict"; + + $(function(){ + var $select = $('#setting-format select'); + if($select.length === 0) return; + + var $options = $('#setting-format-options'); + console.log($options); + debugger; + $select.on("change", function(ev){ + }); + }); +})(); + diff --git a/app/assets/javascripts/vue/in_tail_format.js b/app/assets/javascripts/vue/in_tail_format.js new file mode 100644 index 0000000..5cba3e7 --- /dev/null +++ b/app/assets/javascripts/vue/in_tail_format.js @@ -0,0 +1,31 @@ +(function(){ + "use strict"; + + $(function(){ + if($('#in_tail_format').length === 0) return; + + new Vue({ + el: "#in_tail_format", + paramAttributes: ["formatOptions", "initialSelected"], + data: { + // v-model: format + }, + + created: function(){ + this.formatOptions = JSON.parse(this.formatOptions); + this.formats = Object.keys(this.formatOptions); + this.format = this.initialSelected; + }, + + computed: { + options: function(){ + return this.formatOptions[this.format]; + } + }, + + methods: { + } + }); + }); +})(); + diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index ea1def4..9e07d87 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -4,15 +4,7 @@ class ApiController < ApplicationController end def file_preview - return empty_json unless params[:file] - return empty_json unless File.exists? params[:file] - file = params[:file] - sample = File.read(file, 1024) || "" - sample2 = sample.force_encoding('ascii-8bit').encode('us-ascii', :undef => :replace, :invalid => :replace, :replace => "") - return empty_json if sample != sample2 # maybe binary file - - reader = FileReverseReader.new(File.open(file)) - render json: reader.enum_for(:each_line).to_a.first(10).reverse + render json: file_tail(params[:file]) || [] end def empty_json diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9aa3e6d..5bbfd3a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base helper_method :current_user helper_method :current_locale helper_method :installing_gem?, :installing_gems, :uninstalling_gem?, :uninstalling_gems + helper_method :file_tail before_action :login_required before_action :set_locale @@ -72,4 +73,15 @@ class ApplicationController < ActionController::Base end I18n.locale = prefer end + + def file_tail(path, limit = 10) + return unless path + return unless File.exists? path + sample = File.read(path, 1024) || "" + sample2 = sample.force_encoding('ascii-8bit').encode('us-ascii', :undef => :replace, :invalid => :replace, :replace => "") + return if sample != sample2 # maybe binary file + + reader = FileReverseReader.new(File.open(path)) + reader.enum_for(:each_line).to_a.first(limit).reverse + end end diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index a130797..2377021 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -25,17 +25,18 @@ class Fluentd::SettingsController < ApplicationController }) end + def in_tail_after_format + @setting = Fluentd::Setting::InTail.new(setting_params) + end + def in_tail_confirm @setting = Fluentd::Setting::InTail.new(setting_params) - unless @setting.valid? - return render "in_tail_after_file_choose" - end end def in_tail_finish @setting = Fluentd::Setting::InTail.new(setting_params) unless @setting.valid? - return render "in_tail_after_file_choose" + return render "in_tail_after_format" end File.open(@fluentd.agent.config_file, "a") do |f| # TODO: should update by agent class f.write "\n" @@ -48,6 +49,6 @@ class Fluentd::SettingsController < ApplicationController private def setting_params - params.require(:setting).permit(:path, :tag, :rotate_wait, :pos_file, :read_from_head, :refresh_interval) + params.require(:setting).permit(:path, :format, *Fluentd::Setting::InTail.known_formats, :tag, :rotate_wait, :pos_file, :read_from_head, :refresh_interval) end end diff --git a/app/models/fluentd/setting.rb b/app/models/fluentd/setting.rb index 5d19526..8410298 100644 --- a/app/models/fluentd/setting.rb +++ b/app/models/fluentd/setting.rb @@ -8,6 +8,46 @@ class Fluentd validates :tag, presence: true #validates :format, presence: true + def self.known_formats + { + :regexp => [], + :apache2 => [:time_format], + :nginx => [:time_format], + :syslog => [:time_format], + :tsv => [:keys, :time_key], + :csv => [:keys, :time_key], + :ltsv => [:delimiter, :time_key], + :json => [:time_key], + :multiline => [:format_firstline, *(1..20).map{|n| "format#{n}".to_sym}], + } + end + attr_accessor *known_formats.values.flatten.compact + + def known_formats + self.class.known_formats + end + + def guess_format + case path + when /\.json$/ + :json + when /\.csv$/ + :csv + when /\.tsv$/ + :tsv + when /\.ltsv$/ + :ltsv + when /nginx/ + :nginx + when /apache/ + :apache2 + when %r|/var/log| + :syslog + else + :regexp + end + end + def to_conf <<-XML.strip_heredoc.gsub(/^[ ]+\n/m, "") diff --git a/app/views/fluentd/settings/_form.html.haml b/app/views/fluentd/settings/_form.html.haml new file mode 100644 index 0000000..20eec38 --- /dev/null +++ b/app/views/fluentd/settings/_form.html.haml @@ -0,0 +1,38 @@ + +- @setting.errors.full_messages.each do |e| + %div.alert.alert-danger= e + +%div.form-group + = f.label :path + = f.hidden_field :path + = f.text_field :path, class: "form-control", disabled: true +%div.form-group + = f.label :format + = f.hidden_field :format + = f.text_field :format, class: "form-control", disabled: true +- @setting.known_formats[@setting.format.to_sym].each do |key| + %label= key + = f.hidden_field key + = @setting.send(key) + %br +%div.form-group + = f.label :tag + = f.text_field :tag, class: "form-control" +.well.well-sm + %h4{"data-toggle" => "collapse", "href" => "#advanced-setting"} + = icon('fa-caret-down') + = t('terms.advanced_setting') + #advanced-setting.collapse + %div.form-group + = f.label :rotate_wait + = f.text_field :rotate_wait, class: "form-control" + %div.form-group + = f.label :pos_file + = f.text_field :pos_file, class: "form-control" + %div.form-group + = f.label :read_from_head + = f.check_box :read_from_head, class: "form-control" + %div.form-group + = f.label :refresh_interval + = f.text_field :refresh_interval, class: "form-control" + diff --git a/app/views/fluentd/settings/in_tail_after_file_choose.html.haml b/app/views/fluentd/settings/in_tail_after_file_choose.html.haml index 057a404..fcce562 100644 --- a/app/views/fluentd/settings/in_tail_after_file_choose.html.haml +++ b/app/views/fluentd/settings/in_tail_after_file_choose.html.haml @@ -2,7 +2,7 @@ = link_to t('fluentd.settings.restart_from_first'), in_tail_fluentd_setting_path -= form_for(@setting, as: "setting", url: in_tail_confirm_fluentd_setting_path(@fluentd)) do |f| += form_for(@setting, as: "setting", url: in_tail_after_format_fluentd_setting_path(@fluentd)) do |f| - @setting.errors.full_messages.each do |e| %div.alert.alert-danger= e @@ -10,27 +10,9 @@ = f.label :path = f.hidden_field :path = f.text_field :path, class: "form-control", disabled: true - %div.form-group - = f.label :tag - = f.text_field :tag, class: "form-control" - .well.well-sm - %h4{"data-toggle" => "collapse", "href" => "#advanced-setting"} - = icon('fa-caret-down') - = t('terms.advanced_setting') - #advanced-setting.collapse - %div.form-group - = f.label :rotate_wait - = f.text_field :rotate_wait, class: "form-control" - %div.form-group - = f.label :pos_file - = f.text_field :pos_file, class: "form-control" - %div.form-group - = f.label :read_from_head - = f.check_box :read_from_head, class: "form-control" - %div.form-group - = f.label :refresh_interval - = f.text_field :refresh_interval, class: "form-control" + = render partial: "shared/vue/in_tail_format", locals: { formats: @setting.known_formats, initialSelected: f.object.format || @setting.guess_format } + %pre= file_tail(@setting.path).join("\n") %p = f.submit t('terms.next'), class: "btn btn-primary" diff --git a/app/views/fluentd/settings/in_tail_after_format.html.haml b/app/views/fluentd/settings/in_tail_after_format.html.haml new file mode 100644 index 0000000..c5429b3 --- /dev/null +++ b/app/views/fluentd/settings/in_tail_after_format.html.haml @@ -0,0 +1,9 @@ +- page_title t(".page_title") + += link_to t('fluentd.settings.restart_from_first'), in_tail_fluentd_setting_path + += form_for(@setting, as: "setting", url: in_tail_confirm_fluentd_setting_path(@fluentd)) do |f| + = render partial: "form", locals: { f: f } + + %p + = f.submit t('terms.next'), class: "btn btn-primary" diff --git a/app/views/fluentd/settings/in_tail_confirm.html.haml b/app/views/fluentd/settings/in_tail_confirm.html.haml index 672d606..d8a39a8 100644 --- a/app/views/fluentd/settings/in_tail_confirm.html.haml +++ b/app/views/fluentd/settings/in_tail_confirm.html.haml @@ -3,33 +3,7 @@ = link_to t('fluentd.settings.restart_from_first'), in_tail_fluentd_setting_path = form_for(@setting, as: "setting", url: in_tail_finish_fluentd_setting_path(@fluentd)) do |f| - - @setting.errors.full_messages.each do |e| - %div.alert.alert-danger= e - - %div.form-group - = f.label :path - = f.hidden_field :path - = f.text_field :path, class: "form-control", disabled: true - %div.form-group - = f.label :tag - = f.text_field :tag, class: "form-control" - .well.well-sm - %h4{"data-toggle" => "collapse", "href" => "#advanced-setting"} - = icon('fa-caret-down') - = t('terms.advanced_setting') - #advanced-setting.collapse - %div.form-group - = f.label :rotate_wait - = f.text_field :rotate_wait, class: "form-control" - %div.form-group - = f.label :pos_file - = f.text_field :pos_file, class: "form-control" - %div.form-group - = f.label :read_from_head - = f.check_box :read_from_head, class: "form-control" - %div.form-group - = f.label :refresh_interval - = f.text_field :refresh_interval, class: "form-control" + = render partial: "form", locals: { f: f } %pre= @setting.to_conf diff --git a/app/views/shared/vue/_in_tail_format.html.erb b/app/views/shared/vue/_in_tail_format.html.erb new file mode 100644 index 0000000..8d70772 --- /dev/null +++ b/app/views/shared/vue/_in_tail_format.html.erb @@ -0,0 +1,16 @@ +
+
+ + +
+
+ + +
+ +
+ <%= raw t('fluentd.settings.in_tail_option_guide') %> +
+
diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index c21fef6..8558cd6 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -85,6 +85,7 @@ en: config_file: Config file page_title: "%{label}" setup_in_tail: Setup in_tail + finish: Update config form: <<: *fluentd_common index: @@ -102,6 +103,10 @@ en: fluentd_info: Setting info recent_errors: "Recent %{count} Errors" settings: + restart_from_first: Restart from first + in_tail_option_guide: | + See in_tail Plugin or + Fluentular for more details. show: <<: *fluentd_common edit: @@ -112,10 +117,11 @@ en: in_tail_after_file_choose: <<: *fluentd_common page_title: "Setup in_tail | Other config" + in_tail_after_format: + <<: *fluentd_common in_tail_confirm: <<: *fluentd_common page_title: "Setup in_tail | Confirmation" - finish: Update config misc: common: &misc_common diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index c8d17b9..dcbc273 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -84,6 +84,8 @@ ja: setting: 設定 config_file: 設定ファイル page_title: "%{label}" + setup_in_tail: in_tailセットアップ + finish: 設定する form: <<: *fluentd_common index: @@ -102,6 +104,9 @@ ja: recent_errors: "最新 %{count}件のエラー" settings: restart_from_first: 最初からやり直す + in_tail_option_guide: | + in_tailプラグインの解説ページや + Fluentularもご参照ください。 show: <<: *fluentd_common edit: @@ -112,10 +117,11 @@ ja: in_tail_after_file_choose: <<: *fluentd_common page_title: "in_tailセットアップ | その他の設定" + in_tail_after_format: + <<: *fluentd_common in_tail_confirm: <<: *fluentd_common page_title: "in_tailセットアップ | 確認" - finish: 設定する misc: common: &misc_common diff --git a/config/routes.rb b/config/routes.rb index 19be54a..09fd07e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ Rails.application.routes.draw do resource :setting, only: [:show, :edit, :update], module: :fluentd do get "in_tail" post "in_tail_after_file_choose" + post "in_tail_after_format" post "in_tail_confirm" post "in_tail_finish" end