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.



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

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

Instance Method Details

#configure(conf) ⇒ Object



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
61
# File 'lib/fluent/plugin/in_deskcom.rb', line 31

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

  @tick = @interval * 60

  @stored_time = load_store_file
  @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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/fluent/plugin/in_deskcom.rb', line 123

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

#get_streamObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fluent/plugin/in_deskcom.rb', line 83

def get_stream()
  page = 0
  if @input_api == 'cases' then
    begin
      page = page + 1
      cases = Desk.cases(:since_updated_at => @stored_time, :page => page, :per_page => @per_page)
      # Sleep for rate limit
      # ToDo: Check body "Too Many Requests" and Sleep
      sleep(2)

      cases.each do |c|
        get_content(c)
      end
      $log.info "Case total entry: #{cases.total_entries} page: #{page}"
    end while (cases.total_entries/@per_page.to_f).ceil > page
  elsif @input_api == 'replies'
    begin
      page = page + 1
      cases = Desk.cases(:since_updated_at => @stored_time, :page => page, :per_page => @per_page)        
      # Sleep for rate limit
      # ToDo: Check body "Too Many Requests" and Sleep
      sleep(2)

      cases.each do |c|
        Desk.case_replies(c.id).each do |r|
          # Sleep for rate limit
          # ToDo: Check body "Too Many Requests" and Sleep
          sleep(2)
          
          r[:case_id] = c.id
          get_content(r) if c.count > 0
        end
      end
      $log.info "Case total entry include reply #{cases.total_entries} page: #{page}"
    end while (cases.total_entries/@per_page.to_f).ceil > page
  end
rescue => e
  $log.error "deskcom run: #{e.message}"
end

#load_store_fileObject

> int



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fluent/plugin/in_deskcom.rb', line 162

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



74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/in_deskcom.rb', line 74

def run
  while true
    @started_time = Time.now.to_i
    get_stream
    save_store_file unless !@store_file
    sleep @tick
  end
end

#save_store_fileObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fluent/plugin/in_deskcom.rb', line 178

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



68
69
70
71
72
# File 'lib/fluent/plugin/in_deskcom.rb', line 68

def shutdown
  super
  @thread.terminate
  @thread.join
end

#startObject



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

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