Class: Blocktrain::Query
- Inherits:
-
Object
- Object
- Blocktrain::Query
- Defined in:
- lib/blocktrain/query.rb
Direct Known Subclasses
Instance Method Summary collapse
- #address_query ⇒ Object
- #body ⇒ Object
- #build_query(addresses) ⇒ Object
- #filtered_filter ⇒ Object
- #filtered_query ⇒ Object
- #hits ⇒ Object
-
#initialize(options = {}) ⇒ Query
constructor
A new instance of Query.
- #parse_datetime(datetime) ⇒ Object
- #query ⇒ Object
- #results ⇒ Object
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( = {}) @memory_addresses = [.fetch(:memory_addresses, nil)].flatten.compact @signals = [[:signals]].flatten.compact @from = parse_datetime(.fetch(:from, '2015-09-01T00:00:00')) @to = parse_datetime(.fetch(:to, '2015-09-02T00:00:00')) @limit = .fetch(:limit, 100) end |
Instance Method Details
#address_query ⇒ Object
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 |
#body ⇒ Object
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_filter ⇒ Object
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_query ⇒ Object
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 |
#hits ⇒ Object
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 |
#query ⇒ Object
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 |
#results ⇒ Object
14 15 16 |
# File 'lib/blocktrain/query.rb', line 14 def results result['hits']['hits'] end |