Class: Blocktrain::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/blocktrain/aggregation.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Aggregation

Returns a new instance of Aggregation.



3
4
5
6
7
8
9
10
11
# File 'lib/blocktrain/aggregation.rb', line 3

def initialize(options = {})
  @lookups = Lookups.instance.lookups
  @car = options[:car]

  @from = parse_datetime(options.fetch(:from, '2015-09-01T00:00:00'))
  @to = parse_datetime(options.fetch(:to, '2015-09-02T00:00:00'))

  @interval = options.fetch(:interval, '10m')
end

Instance Method Details

#address_queryObject



22
23
24
25
26
27
28
# File 'lib/blocktrain/aggregation.rb', line 22

def address_query
  if @car.nil?
    @lookups['car_codes'].map { |car, code| "memoryAddress:#{code}" }.join(' OR ')
  else
    "memoryAddress:#{@lookups['car_codes'][@car]}"
  end
end

#bodyObject



57
58
59
60
61
62
63
# File 'lib/blocktrain/aggregation.rb', line 57

def body
  {
    query: query,
    size: 0,
    aggregations: aggs,
  }
end

#parse_datetime(datetime) ⇒ Object



17
18
19
20
# File 'lib/blocktrain/aggregation.rb', line 17

def parse_datetime(datetime)
  utc = Time.parse(datetime).utc
  return utc.to_i * 1000
end

#queryObject



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
# File 'lib/blocktrain/aggregation.rb', line 30

def query
  {
    filtered: {
      query: {
        query_string: {
          analyze_wildcard: true,
          query: address_query
        }
      },
      filter: {
        bool: {
          must: [
            {
              range: {
                timeStamp: {
                  gte: @from,
                  lte: @to
                }
              }
            }
          ]
        }
      }
    }
  }
end

#resultsObject



13
14
15
# File 'lib/blocktrain/aggregation.rb', line 13

def results
  Client.results(body)['aggregations']
end