blocktrain 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00f8d71661cf1f2ce7b8113fbd9d8596c53b4b01
4
- data.tar.gz: 9ebae0e584ec5ae46f827ed4d7ee2f642ca3a9c1
3
+ metadata.gz: 0188b5e441869b10f247af390e35a481eb2a9d3a
4
+ data.tar.gz: f4ee99a885391d634d7653683c025ae76c08930e
5
5
  SHA512:
6
- metadata.gz: 05428c8c6cd98ac5589e86a826d1ddbd89b8b3c2c2aa60f3bbd4e0b47324a1c7ae70e1bd8401f313478dac6dac29007fac8c3ba0c50e89ba0b2a7d9033db35fb
7
- data.tar.gz: 8e1269d337f1d472930ef888c7885f5676db449b7434821c681102c751001cc1ded49e7c2fa87d0623c2882fcc999f820502f900ae78c82c532be8f611937e8a
6
+ metadata.gz: 50b481a7f794f4e65abc5b33930d9468bba70c43102251469f01ede84ab3d2133f25a69fa76ea587126a079136050e66839f877a1b3c0e587b0c72ee26f22a8e
7
+ data.tar.gz: 6743e14e31e753b04f0d5d3709c50a9f0e3b0b32bc68bf332dc565f474f60df5da468ed1eb2a4fdb2fcef2923041f201ebb7f11685a0f38a1756371a795fdd6e
data/blocktrain.gemspec CHANGED
@@ -37,4 +37,5 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency 'vcr', '~> 2.9'
38
38
  spec.add_development_dependency 'webmock', '~> 1.21'
39
39
  spec.add_development_dependency 'coveralls', '~> 0.8'
40
+ spec.add_development_dependency 'terminal-notifier-guard', '~> 1.6'
40
41
  end
data/lib/blocktrain.rb CHANGED
@@ -5,5 +5,6 @@ require 'date'
5
5
 
6
6
  require 'blocktrain/version'
7
7
  require 'blocktrain/client'
8
+ require 'blocktrain/aggregation'
8
9
 
9
10
  Dotenv.load
@@ -0,0 +1,98 @@
1
+ module Blocktrain
2
+ class Aggregation
3
+
4
+ CAR_CODES = {
5
+ 'A' => '2E64930W',
6
+ 'B' => '2E64932W',
7
+ 'C' => '2E64934W',
8
+ 'D' => '2E64936W'
9
+ }
10
+
11
+ def initialize(options = {})
12
+ @car = options[:car]
13
+
14
+ @from = parse_datetime(options.fetch(:from, '2015-09-01T00:00:00'))
15
+ @to = parse_datetime(options.fetch(:to, '2015-09-02T00:00:00'))
16
+
17
+ @interval = options.fetch(:interval, '10m')
18
+ end
19
+
20
+ def results
21
+ Client.results(body)['aggregations']
22
+ end
23
+
24
+ def parse_datetime(datetime)
25
+ utc = Time.parse(datetime).utc
26
+ return utc.to_i * 1000
27
+ end
28
+
29
+ def address_query
30
+ if @car.nil?
31
+ CAR_CODES.map { |code| "memoryAddress:#{code}" }.join(' OR ')
32
+ else
33
+ "memoryAddress:#{CAR_CODES[@car]}"
34
+ end
35
+ end
36
+
37
+ def query
38
+ {
39
+ 'filtered' => {
40
+ 'query' => {
41
+ 'query_string' => {
42
+ 'analyze_wildcard' =>true,
43
+ 'query' =>address_query
44
+ }
45
+ },
46
+ 'filter' => {
47
+ 'bool' => {
48
+ 'must' => [
49
+ {
50
+ 'range' => {
51
+ 'timeStamp' => {
52
+ 'gte' =>@from,
53
+ 'lte' =>@to
54
+ }
55
+ }
56
+ }
57
+ ],
58
+ 'must_not' => []
59
+ }
60
+ }
61
+ }
62
+ }
63
+ end
64
+
65
+ def aggs
66
+ {
67
+ 'weight_chart' => {
68
+ 'date_histogram' => {
69
+ 'field' => 'timeStamp',
70
+ 'interval' => @interval,
71
+ 'pre_zone' => '+01:00',
72
+ 'pre_zone_adjust_large_interval' =>true,
73
+ 'min_doc_count' => 1,
74
+ 'extended_bounds' => {
75
+ 'min' =>@from,
76
+ 'max' =>@to
77
+ }
78
+ },
79
+ 'aggregations' => {
80
+ 'weight' => {
81
+ 'avg' => {
82
+ 'field' => 'value'
83
+ }
84
+ }
85
+ }
86
+ },
87
+ }
88
+ end
89
+
90
+ def body
91
+ {
92
+ 'query' => query,
93
+ 'size' =>0,
94
+ 'aggregations' => aggs,
95
+ }
96
+ end
97
+ end
98
+ end
@@ -1,106 +1,12 @@
1
1
  module Blocktrain
2
2
  class Client
3
3
 
4
- CAR_CODES = {
5
- 'A' => '2E64930W',
6
- 'B' => '2E64932W',
7
- 'C' => '2E64934W',
8
- 'D' => '2E64936W'
9
- }
10
-
11
- def initialize(options = {})
12
- @url = ENV['ES_URL']
13
- @car = options[:car]
14
-
15
- @from = parse_datetime(options.fetch(:from, '2015-09-01T00:00:00'))
16
- @to = parse_datetime(options.fetch(:to, '2015-09-02T00:00:00'))
17
-
18
- @interval = options.fetch(:interval, '10m')
19
- end
20
-
21
- def parse_datetime(datetime)
22
- utc = Time.parse(datetime).utc
23
- return utc.to_i * 1000
24
- end
25
-
26
- def search
27
- @client = Curl::Easy.http_post("#{@url}/train_data/_search", body.to_json) do |c|
4
+ def self.results query
5
+ r = Curl::Easy.http_post("#{ENV['ES_URL']}/train_data/_search", query.to_json) do |c|
28
6
  c.ssl_verify_peer = false
29
7
  end
30
- end
31
-
32
- def results
33
- search
34
- JSON.parse(@client.body_str)
35
- end
36
-
37
- def address_query
38
- if @car.nil?
39
- CAR_CODES.map { |code| "memoryAddress:#{code}" }.join(' OR ')
40
- else
41
- "memoryAddress:#{CAR_CODES[@car]}"
42
- end
43
- end
44
-
45
- def query
46
- {
47
- 'filtered' => {
48
- 'query' => {
49
- 'query_string' => {
50
- 'analyze_wildcard' =>true,
51
- 'query' =>address_query
52
- }
53
- },
54
- 'filter' => {
55
- 'bool' => {
56
- 'must' => [
57
- {
58
- 'range' => {
59
- 'timeStamp' => {
60
- 'gte' =>@from,
61
- 'lte' =>@to
62
- }
63
- }
64
- }
65
- ],
66
- 'must_not' => []
67
- }
68
- }
69
- }
70
- }
71
- end
72
-
73
- def aggs
74
- {
75
- 'weight_chart' => {
76
- 'date_histogram' => {
77
- 'field' => 'timeStamp',
78
- 'interval' => @interval,
79
- 'pre_zone' => '+01:00',
80
- 'pre_zone_adjust_large_interval' =>true,
81
- 'min_doc_count' => 1,
82
- 'extended_bounds' => {
83
- 'min' =>@from,
84
- 'max' =>@to
85
- }
86
- },
87
- 'aggregations' => {
88
- 'weight' => {
89
- 'avg' => {
90
- 'field' => 'value'
91
- }
92
- }
93
- }
94
- },
95
- }
96
- end
97
8
 
98
- def body
99
- {
100
- 'query' => query,
101
- 'size' =>0,
102
- 'aggregations' => aggs,
103
- }
9
+ JSON.parse r.body_str
104
10
  end
105
11
  end
106
12
  end
@@ -1,3 +1,3 @@
1
1
  module Blocktrain
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blocktrain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley
@@ -137,6 +137,20 @@ dependencies:
137
137
  - - "~>"
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0.8'
140
+ - !ruby/object:Gem::Dependency
141
+ name: terminal-notifier-guard
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '1.6'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '1.6'
140
154
  description: Train if fools
141
155
  email:
142
156
  - ops@theodi.org
@@ -156,6 +170,7 @@ files:
156
170
  - bin/setup
157
171
  - blocktrain.gemspec
158
172
  - lib/blocktrain.rb
173
+ - lib/blocktrain/aggregation.rb
159
174
  - lib/blocktrain/client.rb
160
175
  - lib/blocktrain/version.rb
161
176
  homepage: http://github.org/theodi/blocktrain