claw_druid 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/claw_druid.rb +44 -28
  3. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 461727d00ac8db182328f9173b4bb0c6192ac499
4
- data.tar.gz: aacdcd629b320d2eb048fd63806f6ac7eeb9bc46
3
+ metadata.gz: 0fd57f6da684214c3d8d3971504b56b397f33f85
4
+ data.tar.gz: e691527e157378de94ee9e6d3180d4c849272ff1
5
5
  SHA512:
6
- metadata.gz: 9ec24086c6e0953285cc4016a0d0440c9a2951b2127d4e013d2549dc1524ebfa53d7ac77f1c8373a9bcd3b3b808d774577839d7ee0b3b6ac5f772f6445b2b8a5
7
- data.tar.gz: 56749616e555b261f2aa0e383d15a5748c4b5aeecc1dbc90beaea4fc32d4fec226c54e1ccce0660d10fad889b55e55f073cbfd95c53337247ddd87da824a203e
6
+ metadata.gz: 2b3ca5a109499ae7f6b74953f4fb0b237e26046d87fe21b9d1c0d4a2d0eae53512c05781e8cadea2b562a8c968945cc14374d86a54768a5d3a11327a53100d78
7
+ data.tar.gz: 5617939a490a191389d7683d83a70698b6cc2747fb28ef5901e207e9393eca800462428e43a04756c7ebcf385c1eae23f16284735201db8cc3fadfacff0d7cb3
data/lib/claw_druid.rb CHANGED
@@ -21,6 +21,7 @@ class ClawDruid
21
21
  }
22
22
 
23
23
  TopN = "topN"
24
+ Select = "select"
24
25
  GroupBy = "groupBy"
25
26
  TimeSeries = "timeseries"
26
27
  TimeBoundary = "timeBoundary"
@@ -29,6 +30,7 @@ class ClawDruid
29
30
 
30
31
  Permit_Properties = {
31
32
  TopN => [:queryType, :dataSource, :intervals, :granularity, :filter, :aggregations, :postAggregations, :dimension, :threshold, :metric, :context],
33
+ Select => [:queryType, :dataSource, :intervals, :granularity, :descending, :filter, :dimensions, :metrics, :pagingSpec, :context],
32
34
  GroupBy => [:queryType, :dataSource, :dimensions, :limitSpec, :having, :granularity, :filter, :aggregations, :postAggregations, :intervals, :context],
33
35
  TimeSeries => [:queryType, :dataSource, :descending, :intervals, :granularity, :filter, :aggregations, :postAggregations, :context],
34
36
  TimeBoundary => [:queryType, :dataSource, :bound, :filter, :context],
@@ -38,7 +40,7 @@ class ClawDruid
38
40
 
39
41
  def initialize(params = {})
40
42
  @url = params[:url]
41
- @params = {dataSource: params[:source], granularity: "all", queryType: "select"}
43
+ @params = {dataSource: params[:source], granularity: "all", queryType: Select}
42
44
  @threshold = params[:threshold] || THRESHOLD
43
45
 
44
46
  # The page_identifiers of every query, the key is the params.hash of the query, the value is a identifiers like "publisher_daily_report_2017-02-02T00:00:00.000Z_2017-02-04T00:00:00.000Z_2017-03-30T12:10:27.053Z"
@@ -56,6 +58,7 @@ class ClawDruid
56
58
  if dimensions && dimensions.count > 0
57
59
  @params[:dimensions] ||= []
58
60
  @params[:dimensions] += dimensions.map(&:to_s).map(&:strip)
61
+ @params[:dimensions].uniq!
59
62
  end
60
63
  @params.delete(:metrics)
61
64
  self
@@ -81,6 +84,7 @@ class ClawDruid
81
84
  if columns && columns.count > 0
82
85
  @params[:metrics] ||= []
83
86
  @params[:metrics] += columns.map(&:to_s).map(&:strip)
87
+ @params[:metrics].uniq!
84
88
  end
85
89
  self
86
90
  end
@@ -145,6 +149,7 @@ class ClawDruid
145
149
  end
146
150
  }.compact
147
151
  elsif conditions[0].is_a?(String)
152
+ # Process the ('a = ? and b = ?', 1, 2)
148
153
  conditions[0].gsub!(" \?").each_with_index { |v, i| " #{conditions[i + 1]}" } if conditions[0][" \?"]
149
154
  conditions = [where_chain( conditions[0] )]
150
155
  else
@@ -165,18 +170,18 @@ class ClawDruid
165
170
  @params[:metric] ||= []
166
171
  @params[:metric] += columns.map{|column, direction| column }
167
172
  @params[:descending] = columns.any?{|column, direction| direction.to_s[/desc/]}
168
- end
169
- @params[:limitSpec] = {
170
- type: "default",
171
- limit: 500000,
172
- columns: columns.map{|column, direction|
173
+ else
174
+ @params[:limitSpec] ||= {}
175
+ @params[:limitSpec][:type] ||= "default"
176
+ @params[:limitSpec][:limit] ||= 500000
177
+ @params[:limitSpec][:columns] = columns.map{|column, direction|
173
178
  {
174
179
  dimension: column.to_s,
175
180
  direction: direction.to_s[/desc/] ? "descending" : "ascending",
176
181
  dimensionOrder: "lexicographic"
177
182
  }
178
183
  }
179
- }
184
+ end
180
185
  self
181
186
  end
182
187
 
@@ -198,12 +203,16 @@ class ClawDruid
198
203
  if page_count == 1
199
204
  @params[:pagingSpec] = {pagingIdentifiers: {}, threshold: @threshold}
200
205
  elsif page_count > 1
201
- current = @params.hash
206
+ current = permit_params.reject{|key, value| key == :pagingSpec}.hash
202
207
  @paging_identifiers[current] ||= {0 => {}}
203
208
 
204
209
  (1..page_count-1).each do |current_page|
205
210
  if begin @paging_identifiers[current][current_page].nil? rescue true end
206
- query(@params.merge(pagingSpec: {pagingIdentifiers: @paging_identifiers[current][current_page-1], threshold: @threshold}), current_page)
211
+ result = query(@params.merge(pagingSpec: {pagingIdentifiers: @paging_identifiers[current][current_page-1], threshold: @threshold}))
212
+
213
+ # The pagingIdentifiers is something like { "publisher_daily_report_2017-03-01T00:00:00.000Z_2017-03-11T00:00:00.000Z_2017-04-17T21:04:30.804Z" => -10 }
214
+ @paging_identifiers[current] ||= {}
215
+ @paging_identifiers[current][current_page] = JSON.parse(result)[0]["result"]["pagingIdentifiers"].transform_values{|value| value + 1}
207
216
  end
208
217
  end if begin @paging_identifiers[current][page_count - 1].nil? rescue true end
209
218
 
@@ -213,33 +222,34 @@ class ClawDruid
213
222
  end
214
223
 
215
224
  def having(*conditions)
216
- # Process the ('a = ? and b = ?', 1, 2)
217
- conditions[0].gsub!(" \?").each_with_index { |v, i| " #{conditions[i + 1]}" }
225
+ if conditions[0].is_a?(Hash)
226
+ conditions = conditions[0]
218
227
 
219
- havings = having_chain(conditions[0])
220
- @params[:having] = havings unless havings.blank?
228
+ conditions = conditions.delete_if{|key, value| value.blank?}.map{|column, value|
229
+ { type: OPERATIONS["="], aggregation: column, value: value }
230
+ }.compact
231
+ elsif conditions[0].is_a?(String)
232
+ # Process the ('a = ? and b = ?', 1, 2)
233
+ conditions[0].gsub!(" \?").each_with_index { |v, i| " #{conditions[i + 1]}" }
234
+ conditions = [having_chain( conditions[0] )]
235
+ else
236
+ conditions = nil
237
+ end
238
+
239
+ unless conditions.blank?
240
+ @params[:having] ||= { type: "and", havingSpecs: [] }
241
+ @params[:having][:havingSpecs] += conditions
242
+ end
221
243
 
222
244
  self
223
245
  end
224
246
 
225
- def query(params = @params, page_count = nil)
226
- params = params.slice(*Permit_Properties[params[:queryType]])
247
+ def query(params = @params)
248
+ params = permit_params(params)
227
249
  ap params if ENV['DEBUG']
228
250
  puts params.to_json if ENV['DEBUG']
229
251
  result = HTTParty.post(@url, body: params.to_json, headers: { 'Content-Type' => 'application/json' })
230
252
  puts result.code if ENV['DEBUG']
231
-
232
- # The result is a String, try to find the existence of substring 'pagingIdentifiers'.
233
- if page_count && result["pagingIdentifiers"]
234
- params.delete(:pagingSpec)
235
- current = params.hash
236
-
237
- # The pagingIdentifiers is something like { "publisher_daily_report_2017-03-01T00:00:00.000Z_2017-03-11T00:00:00.000Z_2017-04-17T21:04:30.804Z" => -10 }
238
- @paging_identifiers[current] ||= {}
239
- @paging_identifiers[current][page_count] = JSON.parse(result.body)[0]["result"]["pagingIdentifiers"].transform_values{|value| value + 1}
240
- end
241
- # ap JSON.parse(result) if ENV['DEBUG']
242
-
243
253
  result.body
244
254
  end
245
255
 
@@ -275,7 +285,8 @@ class ClawDruid
275
285
  end
276
286
 
277
287
  def to_a
278
- @params[:queryType] == SegmentMetaData ? JSON.parse(query)[0]["columns"] : JSON.parse(query)[0]["result"]["events"]
288
+ result = JSON.parse(query)
289
+ @params[:queryType] == SegmentMetaData ? result[0]["columns"] : begin result[0]["result"]["events"] rescue result end
279
290
  end
280
291
 
281
292
  def each(&block)
@@ -394,6 +405,7 @@ class ClawDruid
394
405
  retainMissingValue: true,
395
406
  }
396
407
  }
408
+ @params[:dimensions].uniq!
397
409
  end
398
410
  end
399
411
 
@@ -410,4 +422,8 @@ class ClawDruid
410
422
  sentences.all?{|sentence| sentence.scan("\(").count == sentence.scan("\)").count }
411
423
  end
412
424
 
425
+ def permit_params(params = @params)
426
+ params.slice(*Permit_Properties[params[:queryType]])
427
+ end
428
+
413
429
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: claw_druid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fan Jieqi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-24 00:00:00.000000000 Z
11
+ date: 2017-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -80,15 +80,15 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 4.2.3
83
- description: The ruby client of Druid.
84
- email: fanjieqi@nibirutech.com
83
+ description: A ruby client of Druid.
84
+ email: fjqqiqi@163.com
85
85
  executables: []
86
86
  extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
89
  - lib/array.rb
90
90
  - lib/claw_druid.rb
91
- homepage: http://galileo.tap4fun.com/fanjieqi/claw_druid
91
+ homepage: https://github.com/fanjieqi/claw_druid
92
92
  licenses:
93
93
  - MIT
94
94
  metadata: {}
@@ -111,5 +111,5 @@ rubyforge_project:
111
111
  rubygems_version: 2.6.11
112
112
  signing_key:
113
113
  specification_version: 4
114
- summary: The ruby client of Druid.
114
+ summary: A ruby client of Druid.
115
115
  test_files: []