Class: Flydata::Preference::DataEntryPreference

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/preference/data_entry_preference.rb

Constant Summary collapse

CONFS_HOME =
File.join(FLYDATA_HOME, 'confs')
CUSTOM_CONFIG_PARAMS =
{
  RedshiftMysqlDataEntry: ::Fluent::MysqlBinlogFlydataInputPreference::CUSTOM_CONFIG_PARAMS
}

Class Method Summary collapse

Class Method Details

.conf_exists?(data_entry) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
# File 'lib/flydata/preference/data_entry_preference.rb', line 70

def conf_exists?(data_entry)
  File.exists?(conf_path(data_entry))
end

.conf_name(data_entry) ⇒ Object


74
75
76
# File 'lib/flydata/preference/data_entry_preference.rb', line 74

def conf_name(data_entry)
  "#{data_entry['name']}.conf"
end

.conf_path(data_entry) ⇒ Object


66
67
68
# File 'lib/flydata/preference/data_entry_preference.rb', line 66

def conf_path(data_entry)
  "#{File.join(CONFS_HOME, data_entry['name'])}.conf"
end

.conf_template_name(data_entry) ⇒ Object


86
87
88
89
# File 'lib/flydata/preference/data_entry_preference.rb', line 86

def conf_template_name(data_entry)
  type = ActiveSupport::Inflector.underscore(data_entry['type'])
  "#{type}.conf.tmpl"
end

.conf_template_path(data_entry) ⇒ Object


82
83
84
# File 'lib/flydata/preference/data_entry_preference.rb', line 82

def conf_template_path(data_entry)
  File.join(FLYDATA_TMPL_DIR, conf_template_name(data_entry))
end

.configurable?(data_entry) ⇒ Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/flydata/preference/data_entry_preference.rb', line 78

def configurable?(data_entry)
  File.exists?(conf_template_path(data_entry))
end

.copy_template(data_entry) ⇒ Object


55
56
57
58
59
60
61
62
63
64
# File 'lib/flydata/preference/data_entry_preference.rb', line 55

def copy_template(data_entry)
  return unless File.exists?(conf_template_path(data_entry))
  FileUtils.mkdir_p(CONFS_HOME)
  FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry)+".tmpl")

  # Do not overwrite a current conf file
  unless File.exists?(conf_path(data_entry))
    FileUtils.cp(conf_template_path(data_entry), conf_path(data_entry))
  end
end

.create(type) ⇒ Object


91
92
93
94
# File 'lib/flydata/preference/data_entry_preference.rb', line 91

def create(type)
  custom_config_params = CUSTOM_CONFIG_PARAMS[type]
  return nil unless custom_config_params
end

.filter_data_entry(de) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flydata/preference/data_entry_preference.rb', line 32

def filter_data_entry(de)
  configurable_params = CUSTOM_CONFIG_PARAMS[de['type'].to_sym]
  return de if configurable_params.nil? or configurable_params.empty?

  configurable_params.each do |pref_name, param_info|
    # skip if the data entry doesn't have the information
    de_pref = de[pref_name.to_s]
    next unless de_pref

    param_info.each do |param_name, options|
      next if options.nil? or options.empty?
      param_name_str = param_name.to_s
      next if de_pref[param_name_str].nil? or de_pref[param_name_str].empty?

      options.each do |option_name, option_value|
        de_pref[param_name_str] = Fluent::DataEntryPreferenceConfigurable.replace_value_with_option(
          param_name_str, de_pref[param_name_str], option_name, option_value, key: de['data_port_key'])
      end
    end
  end
end

.load_conf(data_entry) ⇒ Object

data_entry must be hash


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flydata/preference/data_entry_preference.rb', line 14

def load_conf(data_entry)
  path = conf_path(data_entry)
  raise "Conf file does not exist. path:#{path}" unless File.exists?(path)
  custom_conf = YAML::load(File.open(path, 'r'))
  data_entry.each_key do |k|
    v = custom_conf[k]
    case v
    when Hash
      data_entry[k].merge!(v)
    when NilClass
    else
      data_entry[k] = v
    end
  end
  filter_data_entry(data_entry)
  data_entry
end