Class: Blocktrain::Aggregation
- Inherits:
-
Object
- Object
- Blocktrain::Aggregation
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_query ⇒ Object
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
|
#aggs ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/blocktrain/aggregation.rb', line 57
def aggs
{
weight_chart: {
date_histogram: {
field: 'timeStamp',
interval: @interval,
time_zone: '+01:00',
min_doc_count: 1,
extended_bounds: {
min: @from,
max: @to
}
},
aggregations: local_aggregations
}
}
end
|
#body ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/blocktrain/aggregation.rb', line 75
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
|
#query ⇒ Object
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
|
#results ⇒ Object
13
14
15
|
# File 'lib/blocktrain/aggregation.rb', line 13
def results
Client.results(body)['aggregations']
end
|