Class: Blocktrain::Aggregation

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

Constant Summary collapse

CAR_CODES =
{
  'A' => '2E64930W',
  'B' => '2E64932W',
  'C' => '2E64934W',
  'D' => '2E64936W'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Aggregation

Returns a new instance of Aggregation.



11
12
13
14
15
16
17
18
# File 'lib/blocktrain/aggregation.rb', line 11

def initialize(options = {})
  @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



29
30
31
32
33
34
35
# File 'lib/blocktrain/aggregation.rb', line 29

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

#aggsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/blocktrain/aggregation.rb', line 65

def aggs
  {
     'weight_chart' => {
       'date_histogram' => {
         'field' => 'timeStamp',
         'interval' => @interval,
         'pre_zone' => '+01:00',
         'pre_zone_adjust_large_interval' =>true,
         'min_doc_count' => 1,
         'extended_bounds' => {
           'min' =>@from,
           'max' =>@to
         }
      },
      'aggregations' => {
        'weight' => {
          'avg' => {
            'field' => 'value'
          }
        }
      }
    },
  }
end

#bodyObject



90
91
92
93
94
95
96
# File 'lib/blocktrain/aggregation.rb', line 90

def body
  {
    'query' => query,
    'size' =>0,
    'aggregations' => aggs,
  }
end

#parse_datetime(datetime) ⇒ Object



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

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

#queryObject



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

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

#resultsObject



20
21
22
# File 'lib/blocktrain/aggregation.rb', line 20

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