Class: DynamoAutoscale::LocalDataPoll

Inherits:
Poller
  • Object
show all
Includes:
Logger
Defined in:
lib/dynamo-autoscale/local_data_poll.rb

Instance Attribute Summary

Attributes inherited from Poller

#filters, #tables

Instance Method Summary collapse

Methods included from Logger

included, logger, #logger, logger=

Methods inherited from Poller

#dispatch, #run

Constructor Details

#initialize(*args) ⇒ LocalDataPoll

Returns a new instance of LocalDataPoll.



5
6
7
8
# File 'lib/dynamo-autoscale/local_data_poll.rb', line 5

def initialize *args
  super(*args)
  @cache = Hash.new { |h, k| h[k] = {} }
end

Instance Method Details

#poll(tables, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dynamo-autoscale/local_data_poll.rb', line 10

def poll tables, &block
  if tables.nil?
    tables = ["*"]
  end

  tables.each do |table_name|
    unless @cache[table_name].empty?
      @cache[table_name].each do |day, table_day_data|
        block.call(table_name, table_day_data)
      end
    else
      file = "#{table_name}.json"

      Dir[File.join(DynamoAutoscale.data_dir, '*')].each do |day_path|
        Dir[File.join(day_path, file)].each do |table_path|
          data = JSON.parse(File.read(table_path)).symbolize_keys

          if data[:consumed_writes].nil? or data[:consumed_reads].nil?
            logger.warn "[ld_poller] Lacking data for table #{table_name}. Skipping."
            next
          end

          # All this monstrosity below is doing is parsing the time keys in
          # the nested hash from strings into Time objects. Hash mapping
          # semantics are weird, hence why this looks ridiculous.
          data = Hash[data.map do |key, ts|
            [
              key,
              Hash[ts.map do |t, d|
                [Time.parse(t), d]
              end],
            ]
          end]

          @cache[table_name][day_path] = data

          block.call(table_name, data)
        end
      end
    end
  end
end