keen-cli 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -118,34 +118,39 @@ Various examples:
118
118
 
119
119
  ``` shell
120
120
  # add an empty event
121
- $ keen events:add --collection cli-tests
121
+ $ keen events:add --collection signups
122
122
 
123
123
  # use the shorter form of collection
124
- $ keen events:add -c cli-tests
124
+ $ keen events:add -c signups
125
125
 
126
126
  # add a blank event to a collection specified in a .env file:
127
- # KEEN_COLLECTION_NAME=cli-tests
127
+ # KEEN_COLLECTION_NAME=signups
128
128
  $ keen events:add
129
129
 
130
130
  # add an event from JSON using the --data parameter
131
- $ keen events:add -c cli-tests --data "{ \"username\" : \"dzello\", \"zsh\": 1 }"
131
+ $ keen events:add -c signups --data "{ \"username\" : \"dzello\", \"city\": \"San Francisco\" }"
132
132
 
133
133
  # add an event from key value pairs
134
- $ keen events:add -c cli-tests -data "username=dzello&zsh=1" --params
134
+ $ keen events:add -c signups -data "username=dzello&city=SF" --params
135
135
 
136
136
  # pipe in events as JSON
137
- $ echo "{ \"username\" : \"dzello\", \"zsh\": 1 }" | keen events:add -c cli-tests
137
+ $ echo "{ \"username\" : \"dzello\", \"city\": \"San Francisco\" }" | keen events:add -c signups
138
138
 
139
- # add events from a file that contains newline delimited json
140
- # { "apple" : "sauce" }
141
- # { "banana" : "pudding" }
142
- # { "cherry" : "pie" }
139
+ # add events from a file that contains newline delimited json:
140
+ # { "username" : "dzello", "city" : "San Francisco" }
141
+ # { "username" : "KarlTheFog", "city" : "San Francisco" }
142
+ # { "username" : "polarvortex", "city" : "Chicago" }
143
143
  $ keen events:add --file events.json
144
144
 
145
- # add events from a file in CSV format; the first row should be columns
146
- # fruit,dish
147
- # apple,sauce
148
- # banana,pudding
145
+ # add events from a file that contains an array of JSON objects
146
+ # [{ "apple" : "sauce" }, { "banana" : "pudding" }, { "cherry" : "pie" }]
147
+ $ keen events:add --file events.json
148
+
149
+ # add events from a file in CSV format. the first row must be column names:
150
+ # username, city
151
+ # dzello, San Francisco
152
+ # KarlTheFog, San Francisco
153
+ # polarvortex, Chicago
149
154
  $ keen events:add --file events.csv --csv
150
155
  ```
151
156
 
@@ -178,16 +183,16 @@ Some examples:
178
183
 
179
184
  ``` shell
180
185
  # run a count
181
- $ keen queries:run --collection cli-tests --analysis-type count
186
+ $ keen queries:run --collection signups --analysis-type count
182
187
  1000
183
188
 
184
189
  # run a count with collection name from .env
185
- # KEEN_COLLECTION_NAME=cli-tests
190
+ # KEEN_COLLECTION_NAME=signups
186
191
  $ keen queries:run --analysis-type count
187
192
  1000
188
193
 
189
194
  # run a count with a group by
190
- $ keen queries:run --collection cli-tests --analysis-type count --group-by username
195
+ $ keen queries:run --collection signups --analysis-type count --group-by username
191
196
  [
192
197
  {
193
198
  "username": "dzello",
@@ -196,7 +201,7 @@ $ keen queries:run --collection cli-tests --analysis-type count --group-by usern
196
201
  ]
197
202
 
198
203
  # run a query with a timeframe, target property, group by, and interval
199
- $ keen queries:run --collection cli-tests --analysis-type median --target-property value --group-by cohort --timeframe last_24_hours --interval hourly
204
+ $ keen queries:run --collection signups --analysis-type count_unique --target-property age --group-by source --timeframe last_24_hours --interval hourly
200
205
 
201
206
  {
202
207
  "timeframe": {
@@ -261,6 +266,7 @@ Parameters that apply to most commands include:
261
266
 
262
267
  ### Changelog
263
268
 
269
+ + 0.1.8 - Inputted lines can also be arrays of JSON objects. `--batch-size` param is now properly recognized.
264
270
  + 0.1.7 - Add docs command
265
271
  + 0.1.6 - Big refactoring to make importing events much cleaner and batching happen automatically. Also adds `queries:url`.
266
272
  + 0.1.5 – Support adding events from files with `--file`. Optionally add from CSV with `--csv`.
@@ -18,16 +18,18 @@ module KeenCli
18
18
  # internal state tracking
19
19
  attr_accessor :size
20
20
  attr_accessor :events
21
+ attr_accessor :total_size
21
22
 
22
23
  def initialize(collection, options={})
23
24
  self.collection = collection
24
- self.batch_size = options[:batch_size] || DEFAULT_BATCH_SIZE
25
+ self.batch_size = options[:'batch-size'] || DEFAULT_BATCH_SIZE
25
26
  self.csv = options[:csv]
26
27
  self.params = options[:params]
27
28
  self.csv_options = DEFAULT_CSV_OPTIONS.merge(options[:csv_options] || {})
28
29
  self.events = []
29
30
  self.pretty = options[:pretty]
30
31
  self.silent = options[:silent]
32
+ self.total_size = 0
31
33
  self.reset
32
34
  end
33
35
 
@@ -46,22 +48,28 @@ module KeenCli
46
48
  csv_row = CSV.parse_line(line, self.csv_options)
47
49
  raise "Could not parse! #{line}" unless csv_row
48
50
 
49
- hash = row_to_hash(csv_row)
51
+ csv_event = row_to_hash(csv_row)
52
+ add_event_and_flush_if_necessary(csv_event)
50
53
 
51
54
  elsif self.params
52
55
 
53
- hash = Utils.parse_data_as_querystring(line)
56
+ querystring_event = Utils.parse_data_as_querystring(line)
57
+ add_event_and_flush_if_necessary(querystring_event)
54
58
 
55
59
  else
56
60
 
57
- hash = JSON.parse(line)
61
+ json_object = JSON.parse(line)
58
62
 
59
- end
60
-
61
- self.events.push(hash)
62
- self.size += 1
63
+ # if it's an array, lets iterate over and flush as necessary
64
+ if json_object.is_a?(Array)
65
+ json_object.each do |json_event|
66
+ add_event_and_flush_if_necessary(json_event)
67
+ end
68
+ else
69
+ add_event_and_flush_if_necessary(json_object)
70
+ end
63
71
 
64
- self.flush if self.size >= self.batch_size
72
+ end
65
73
 
66
74
  end
67
75
 
@@ -77,6 +85,13 @@ module KeenCli
77
85
 
78
86
  private
79
87
 
88
+ def add_event_and_flush_if_necessary(event)
89
+ self.events.push(event)
90
+ self.size += 1
91
+ self.total_size += 1
92
+ self.flush if self.size >= self.batch_size
93
+ end
94
+
80
95
  def set_headers(line)
81
96
  csv_row = CSV.parse_line(line, self.csv_options)
82
97
  self.csv_options[:headers] = csv_row.to_a
@@ -7,7 +7,7 @@ module KeenCli
7
7
  def self.events_options
8
8
  option :csv
9
9
  option :params
10
- option :'batch-size'
10
+ option :'batch-size', type: :numeric
11
11
  end
12
12
 
13
13
  desc 'events:add', 'Add one or more events and print the result'
@@ -30,19 +30,15 @@ module KeenCli
30
30
  :params => options[:params],
31
31
  :pretty => options[:pretty],
32
32
  :silent => options[:silent],
33
- :batch_size => options[:'batch-size'])
34
-
35
- total_events = 0
33
+ :'batch-size' => options[:'batch-size'])
36
34
 
37
35
  if data = options[:data]
38
36
  batch_processor.add(data)
39
- total_events += 1
40
37
  end
41
38
 
42
39
  if file = options[:file]
43
40
  File.readlines(file).each do |line|
44
41
  batch_processor.add(line)
45
- total_events += 1
46
42
  end
47
43
  end
48
44
 
@@ -50,18 +46,16 @@ module KeenCli
50
46
  ARGV.clear
51
47
  ARGF.each_line do |line|
52
48
  batch_processor.add(line)
53
- total_events += 1
54
49
  end
55
50
  end
56
51
 
57
52
  if $stdin.tty? && data.nil? && file.nil?
58
53
  batch_processor.add("{}")
59
- total_events += 1
60
54
  end
61
55
 
62
56
  batch_processor.flush
63
57
 
64
- total_events
58
+ batch_processor.total_size
65
59
 
66
60
  end
67
61
 
@@ -1,3 +1,3 @@
1
1
  module KeenCli
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -0,0 +1 @@
1
+ [{ "foo" : 1 }, { "foo" : 2 }, { "foo" : 3 }]
@@ -12,6 +12,10 @@ module KeenCli
12
12
  expect(batch_processor.batch_size).to eq(1000)
13
13
  end
14
14
 
15
+ it 'sets total size to 0' do
16
+ expect(batch_processor.total_size).to eq(0)
17
+ end
18
+
15
19
  it 'sets the collection' do
16
20
  expect(batch_processor.collection).to eq('signups')
17
21
  end
@@ -47,6 +51,14 @@ module KeenCli
47
51
  })
48
52
  end
49
53
 
54
+ it 'converts a JSON array to a series of hashes' do
55
+ batch_processor.add('[{ "apple": "sauce", "banana": "pancakes" }]')
56
+ expect(batch_processor.events.first).to eq({
57
+ "apple" => "sauce",
58
+ "banana" => "pancakes"
59
+ })
60
+ end
61
+
50
62
  it 'converts a param string to a hash' do
51
63
  batch_processor.params = true
52
64
  batch_processor.add('apple=sauce&banana=pancakes')
@@ -98,6 +110,7 @@ module KeenCli
98
110
  it 'adds events to an array up to batch size and increments count' do
99
111
  batch_processor.add('{ "apple": "sauce", "banana": "pancakes" }')
100
112
  expect(batch_processor.size).to eq(1)
113
+ expect(batch_processor.total_size).to eq(1)
101
114
  expect(batch_processor.events.first).to eq({
102
115
  "apple" => "sauce",
103
116
  "banana" => "pancakes"
@@ -111,8 +124,10 @@ module KeenCli
111
124
  batch_processor.batch_size = 2
112
125
  batch_processor.add('{ "apple": "sauce", "banana": "pancakes" }')
113
126
  expect(batch_processor.size).to eq(1)
127
+ expect(batch_processor.total_size).to eq(1)
114
128
  batch_processor.add('{ "apple": "sauce", "banana": "pancakes" }')
115
129
  expect(batch_processor.size).to eq(0)
130
+ expect(batch_processor.total_size).to eq(2)
116
131
  expect(batch_processor.events).to be_empty
117
132
  end
118
133
 
@@ -28,12 +28,20 @@ describe KeenCli::CLI do
28
28
  expect(_).to eq(3)
29
29
  end
30
30
 
31
+ it 'accepts JSON events from a file param in array format' do
32
+ stub_request(:post, "https://api.keen.io/3.0/projects/#{Keen.project_id}/events").
33
+ with(:body => "{\"minecraft-deaths\":[{\"foo\":1},{\"foo\":2},{\"foo\":3}]}").
34
+ to_return(:body => { :created => true }.to_json)
35
+ _, options = start "events:add --collection minecraft-deaths --file #{File.expand_path('../../fixtures/events-array.json', __FILE__)}"
36
+ expect(_).to eq(3)
37
+ end
38
+
31
39
  it 'accepts JSON events from a file param in CSV format' do
32
40
  stub_request(:post, "https://api.keen.io/3.0/projects/#{Keen.project_id}/events").
33
41
  with(:body => "{\"minecraft-deaths\":[{\"foo\":1},{\"foo\":2},{\"foo\":3}]}").
34
42
  to_return(:body => { :created => true }.to_json)
35
43
  _, options = start "events:add --collection minecraft-deaths --csv --file #{File.expand_path('../../fixtures/events.csv', __FILE__)}"
36
- expect(_).to eq(4)
44
+ expect(_).to eq(3)
37
45
  end
38
46
 
39
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keen-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-10 00:00:00.000000000 Z
12
+ date: 2014-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: keen
@@ -147,6 +147,7 @@ files:
147
147
  - lib/keen-cli/shared.rb
148
148
  - lib/keen-cli/utils.rb
149
149
  - lib/keen-cli/version.rb
150
+ - spec/fixtures/events-array.json
150
151
  - spec/fixtures/events.csv
151
152
  - spec/fixtures/events.json
152
153
  - spec/keen-cli/batch_processor_spec.rb
@@ -181,6 +182,7 @@ signing_key:
181
182
  specification_version: 3
182
183
  summary: Command line interface to Keen IO
183
184
  test_files:
185
+ - spec/fixtures/events-array.json
184
186
  - spec/fixtures/events.csv
185
187
  - spec/fixtures/events.json
186
188
  - spec/keen-cli/batch_processor_spec.rb