Class: Fluent::DeskcomInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_deskcom.rb

Constant Summary collapse

OUTPUT_FORMAT_TYPE =

un-support yet: nest flat

%w(simple)
INPUT_API_TYPE =

un-support yet: brand article reply ~

%w(cases replies)
DEFAULT_PER_PAGE =
50

Instance Method Summary collapse

Constructor Details

#initializeDeskcomInput

Returns a new instance of DeskcomInput.



22
23
24
25
26
27
# File 'lib/fluent/plugin/in_deskcom.rb', line 22

def initialize
  super
  require 'desk'
  require 'yaml'
  require 'pathname'
end

Instance Method Details

#configure(conf) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/in_deskcom.rb', line 30

def configure(conf)
  super
  if !OUTPUT_FORMAT_TYPE.include?(@output_format)
    raise Fluent::ConfigError, "output_format value undefined #{@output_format}"
  end

  if !INPUT_API_TYPE.include?(@input_api)
    raise Fluent::ConfigError, "input_api value undefined #{@input_api}"
  end

  if !@consumer_key || !@consumer_secret || !@oauth_token || !@oauth_token_secret
    raise Fluent::ConfigError, "missing values in consumer_key or consumer_secret or oauth_token or oauth_token_secret"
  end

  if !@store_file
    $log.warn("stored_time_file path is missing")
  end


  @stored_time = load_store_file
  @started_time = Time.now.to_i
  @per_page = DEFAULT_PER_PAGE

  Desk.configure do |config|
    config.subdomain          = @subdomain
    config.consumer_key       = @consumer_key
    config.consumer_secret    = @consumer_secret
    config.oauth_token        = @oauth_token
    config.oauth_token_secret = @oauth_token_secret
  end
end

#get_content(status) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fluent/plugin/in_deskcom.rb', line 98

def get_content(status)
  case @output_format
  when 'simple'
    record = Hash.new
    status.each_pair do |k,v|
      # @stored_time <= store data's updated time < @started_time
      if (k == 'updated_at') then
        at_time = Time.parse(v).to_i
        if (at_time >= @started_time) || (at_time < @stored_time) then
          next
        end
      end

      if (!@time_column.nil? && k == "#{@time_column}") then
        @time_value = Time.parse(v).to_i rescue nil
      end

      if (k == '_links') then
        next
      end

      if v.kind_of? Hashie::Deash then
        record.store(k, v.to_json)
      else
        record.store(k, v)
      end
    end
  end

  if !@time_value.nil? then
    Engine.emit(@tag, @time_value, record)
  else
    Engine.emit(@tag, @started_time,  record)
  end
rescue => e
  $log.error "deskcom get_content: #{e.message}"
end

#load_store_fileObject

> int



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fluent/plugin/in_deskcom.rb', line 137

def load_store_file
  begin
    f = Pathname.new(@store_file)
    stored_time = 0
    f.open('r') do |f|
      stored = YAML.load_file(f)
      stored_time = stored[:time].to_i
    end
    $log.info "deskcom: Load #{@store_file}: #{@stored_time}"
  rescue => e
    $log.warn "deskcom: Can't load store_file #{e.message}"
    return 0
  end
  return stored_time
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/plugin/in_deskcom.rb', line 71

def run
  page = 1
  if @input_api == 'cases' then
    begin
      cases = Desk.cases(:since_updated_at => @stored_time, :page => page, :per_page => @per_page)
      cases.each do |c|
        get_content(c)
      end
      page = page + 1
    end while cases.total_entries > (@per_page*page)
  elsif @input_api == 'replies'
    begin
      cases = Desk.cases(:since_updated_at => @stored_time, :page => page, :per_page => @per_page)
      cases.each do |c|
        Desk.case_replies(c.id).each do |r|
          r[:case_id] = c.id
          get_content(r) if c.count > 0
        end
      end
      page = page + 1
    end while cases.total_entries > (@per_page*page)
  end
  save_store_file unless !@store_file
rescue => e
  $log.error "deskcom run: #{e.message}"
end

#save_store_fileObject



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/plugin/in_deskcom.rb', line 153

def save_store_file
  begin
    f = Pathname.new(@store_file)
    f.open('w') do |f|
      data = {:time => @started_time}
      YAML.dump(data, f)
    end
    $log.info "deskcom: Save started_time: #{@started_time} to #{@store_file}"
  rescue => e
    $log.warn "deskcom: Can't save store_file #{e.message}"
  end
end

#shutdownObject



67
68
69
# File 'lib/fluent/plugin/in_deskcom.rb', line 67

def shutdown
  @thread.kill
end

#startObject



62
63
64
65
# File 'lib/fluent/plugin/in_deskcom.rb', line 62

def start
  super
  @thread = Thread.new(&method(:run))
end