Class: Blocktrain::Query

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

Direct Known Subclasses

Aggregation

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Query

Returns a new instance of Query.



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

def initialize(options = {})
  @memory_addresses = [options.fetch(:memory_addresses, nil)].flatten.compact
  @signals = [options[:signals]].flatten.compact

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

  @limit = options.fetch(:limit, 100)
end

Instance Method Details

#address_queryObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/blocktrain/query.rb', line 27

def address_query
  # Look up memory addresses directly if specified
  unless @memory_addresses == []
    build_query(@memory_addresses)
  else
    # No query if there isn't a signal specified
    return nil if @signals == []
    lookups = Lookups.instance.lookups
    @signals = @signals.map { |s| lookups[s] }
    build_query(@signals)
  end
end

#bodyObject



81
82
83
84
85
86
# File 'lib/blocktrain/query.rb', line 81

def body
  {
    query: query,
    size: @limit
  }
end

#build_query(addresses) ⇒ Object



40
41
42
43
# File 'lib/blocktrain/query.rb', line 40

def build_query(addresses)
  addresses.map! { |a| "memoryAddress:#{a}" }
  addresses.join(" OR ")
end

#filtered_filterObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/blocktrain/query.rb', line 64

def filtered_filter
  {
    bool: {
      must: [
        {
          range: {
            timeStamp: {
              gte: @from,
              lte: @to
            }
          }
        }
      ]
    }
  }
end

#filtered_queryObject



54
55
56
57
58
59
60
61
62
# File 'lib/blocktrain/query.rb', line 54

def filtered_query
  q = address_query
  return {} if q.nil?
  {
    query_string: {
      query: q
    }
  }
end

#hitsObject



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

def hits
  result['hits']['total']
end

#parse_datetime(datetime) ⇒ Object



22
23
24
25
# File 'lib/blocktrain/query.rb', line 22

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

#queryObject



45
46
47
48
49
50
51
52
# File 'lib/blocktrain/query.rb', line 45

def query
  {
    filtered: {
      query: filtered_query,
      filter: filtered_filter
    }
  }
end

#resultsObject



14
15
16
# File 'lib/blocktrain/query.rb', line 14

def results
  result['hits']['hits']
end