keen-cli 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,11 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.1.0
4
- env:
5
- global:
6
- - secure: dyzAKPD91LvwtjcX0mo4ENPhpjCgtkrS1Z5mgodYHafMz+GBTrMLdwnA/jE2EnzUhVcTieuAvIsYP7sGsG43qxtYNIl7FMXdz1PywgQVZuPbmNV6Y2iBaU61TlQvR1rtLA+G4N8TOxdVQxThpYlNv1rNHGSqtgOOICMNZLTY224=
7
- - secure: SAfN4kwEsqEBSSr8Fr2h2ySxFz7P+6rgysgEjF24fL6JW5N+YmKmwHh1en81d4R+BYcYIMQRwe1owpyqisvx0kASUVnWdPT0+loHm5rH22IEFsYjTEiuYOAZCj1/4yLcVmdX6EH34cNmoGbxUUziD9k9ctVGoehxaK9te+xphvc=
8
- - secure: k2owfccj86F/b7X/WRsi8HGdJ9v+zuDek/Vu7XzNN+fqqja4rlXTtWSJyOTikklOMRN0AlUh1GjRWp+eT8YXKdiN4gk7WUGiryaTGATKWn1nLaIDtEjugMMmqXq7YwCih1GulPhMlRSwHU4b9q/MsciTFwzxREsxecjrQYL8HQY=
9
- - secure: PXsUN/cmCwMbBjkJ6HLdWPmOEtk6+G1HMP5RhejImbUII7MJ1GRhHdehMCgc74wNfVt/MX/de2AgB17EQc5tDSKoQqV9dCZ4cRDPjlh82MK3gN1L2ZUgD+wJeY/29bZak6NMnVXVN18HGfIMjCVnS3t2rkQ94nwZVtIuhCkAWRk=
10
4
  script:
11
5
  - bundle exec rake spec
data/README.md CHANGED
@@ -18,13 +18,23 @@ Verify the `keen` command is in your path by running it:
18
18
 
19
19
  ``` shell
20
20
  Commands:
21
+ keen average # Alias for queries:run -c average
22
+ keen count # Alias for queries:run -c count
23
+ keen count-unique # Alias for queries:run -c count_unique
21
24
  keen events:add # Add one or more events and print the result
25
+ keen extraction # Alias for queries:run -c extraction
22
26
  keen help [COMMAND] # Describe available commands or one specific command
27
+ keen maximum # Alias for queries:run -c maximum
28
+ keen median # Alias for queries:run -c median
29
+ keen minimum # Alias for queries:run -c minimum
30
+ keen percentile # Alias for queries:run -c percentile
23
31
  keen project:collections # Print information about a project's collections
24
32
  keen project:describe # Print information about a project
25
33
  keen project:open # Open a project's overview page in a browser
26
34
  keen project:workbench # Open a project's workbench page in a browser
27
35
  keen queries:run # Run a query and print the result
36
+ keen select-unique # Alias for queries:run -c select_unique
37
+ keen sum # Alias for queries:run -c sum
28
38
  keen version # Print the keen-cli version
29
39
  ```
30
40
 
@@ -135,6 +145,10 @@ Parameters:
135
145
  + `--interval` (alias -i)
136
146
  + `--filters` (alias -f)
137
147
  + `--percentile`
148
+ + `--property-names` - A comma-separated list of property names. Extractions only.
149
+ + `--latest` - Number of latest events to retrieve. Extractions only.
150
+ + `--email` - Send extraction results via email, asynchronously. Extractions only.
151
+ + `--data` (alias -d) - Specify query parameters as JSON instead of query params. Data can also be piped in via STDIN.
138
152
 
139
153
  Some examples:
140
154
 
@@ -169,10 +183,42 @@ $ keen queries:run --collection cli-tests --analysis-type median --target-proper
169
183
  ...
170
184
  ...
171
185
  ...
186
+
187
+ # run an extraction with specific property names
188
+ $ keen queries:run --collection minecraft-deaths --analysis-type extraction --property-names player,enemy
189
+ [
190
+ {
191
+ "player": "dzello",
192
+ "enemy": "creeper"
193
+ },
194
+ {
195
+ "player": "dkador",
196
+ "enemy": "creeper"
197
+ }
198
+ ]
199
+
200
+ # run a query using JSON to specify parameters
201
+ $ echo "{ \"event_collection\" : \"minecraft-deaths\", \"target_property\": \"level\" }" | keen queries:run -a average
172
202
  ```
173
203
 
204
+ **Query Aliases**
205
+
206
+ For each type of analysis (e.g. count, average, extraction, etc.) there is an alias that can be used
207
+ instead of `queries:run`. The command name is simply the type of analysis, using a dash to delimit words.
208
+ Here are a few examples:
209
+
210
+ ``` shell
211
+ $ keen count -c logins
212
+ 1000
213
+ $ keen minimum -c cpu-checks -y iowait
214
+ 0.17
215
+ ```
216
+
217
+ Run `keen` with no arguments to see the full list of aliases.
218
+
174
219
  ### Changelog
175
220
 
221
+ + 0.1.3 – Add querying via JSON. Add query aliases. Add support for extraction fields.
176
222
  + 0.1.2 – Change `project:show` to `project:describe`
177
223
  + 0.1.1 – Add `project:collections`
178
224
  + 0.1.0 - Initial version
data/keen-cli.gemspec CHANGED
@@ -12,14 +12,14 @@ Gem::Specification.new do |s|
12
12
  s.description = "Record events and run queries from the comfort of your command line"
13
13
  s.license = "MIT"
14
14
 
15
- s.add_dependency "keen"
15
+ s.add_dependency "keen", ">= 0.8.4"
16
16
  s.add_dependency "thor"
17
17
  s.add_dependency "dotenv"
18
- s.add_dependency "http"
19
18
 
20
19
  s.add_development_dependency 'rake'
21
20
  s.add_development_dependency 'rspec'
22
21
  s.add_development_dependency 'debugger'
22
+ s.add_development_dependency 'webmock'
23
23
 
24
24
  s.files = `git ls-files`.split("\n")
25
25
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/lib/keen-cli/cli.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'http'
2
1
  require 'thor'
3
2
  require 'keen'
4
3
  require 'json'
@@ -33,6 +32,9 @@ module KeenCli
33
32
  option :timeframe, :aliases => ['-t']
34
33
  option :filters, :aliases => ['-f']
35
34
  option :percentile
35
+ option :"property-names"
36
+ option :latest
37
+ option :email
36
38
  end
37
39
 
38
40
  desc 'version', 'Print the keen-cli version'
@@ -50,9 +52,9 @@ module KeenCli
50
52
 
51
53
  def project_describe
52
54
  Utils.process_options!(options)
53
- response = HTTP.get(
54
- "https://api.keen.io/3.0/projects/#{Keen.project_id}?api_key=#{Keen.master_key}")
55
- JSON.pretty_generate(JSON.parse(response.to_s)).tap do |s| puts s end
55
+ Keen.project_info.tap do |info|
56
+ puts JSON.pretty_generate(info)
57
+ end
56
58
  end
57
59
 
58
60
  desc 'project:collections', 'Print information about a project\'s collections'
@@ -92,23 +94,58 @@ module KeenCli
92
94
  map 'queries:run' => :queries_run
93
95
  shared_options
94
96
  query_options
95
- def queries_run
97
+ data_options
98
+ def queries_run(analysis_type=nil)
96
99
 
97
100
  Utils.process_options!(options)
98
101
 
99
- # convert dashes
100
- q_options = options.inject({}) do |memo, element|
101
- if ['analysis-type', 'group-by', 'target-property'].include?(element.first)
102
+ # work with a new set of options
103
+ q_options = {}
104
+
105
+ data = nil
106
+
107
+ if $stdin.tty?
108
+ data = options[:data]
109
+ else
110
+ ARGV.clear
111
+ ARGF.each_line do |line|
112
+ data = line
113
+ end
114
+ end
115
+
116
+ # if data is provided, parse it and merge it
117
+ unless data.nil?
118
+ data_options = JSON.parse(data)
119
+ q_options.merge!(data_options)
120
+ end
121
+
122
+ # convert dashes to underscores, and merge all into q_options
123
+ q_options.merge!(options.inject({}) do |memo, element|
124
+ if ['analysis-type', 'group-by', 'target-property', 'property-names'].include?(element.first)
102
125
  memo[element.first.sub('-', '_')] = element.last
103
126
  else
104
127
  memo[element.first] = element.last
105
128
  end
106
129
  memo
130
+ end)
131
+
132
+ collection = Utils.get_collection_name(q_options)
133
+ analysis_type = analysis_type || q_options["analysis_type"]
134
+
135
+ # delete fields that shouldn't be passed to keen-gem as options
136
+ q_options.delete("collection")
137
+ q_options.delete("event_collection")
138
+ q_options.delete("data")
139
+ q_options.delete("analysis_type")
140
+
141
+ if property_names = q_options.delete("property_names")
142
+ q_options[:property_names] = property_names.split(",")
107
143
  end
108
144
 
109
- collection = Utils.get_collection_name(options)
145
+ raise "No analysis type given!" unless analysis_type
146
+ raise "No collection given!" unless collection
110
147
 
111
- Keen.send(q_options["analysis_type"], collection, q_options).tap do |result|
148
+ Keen.send(analysis_type, collection, q_options).tap do |result|
112
149
  if result.is_a?(Hash) || result.is_a?(Array)
113
150
  puts JSON.pretty_generate(result)
114
151
  else
@@ -117,6 +154,18 @@ module KeenCli
117
154
  end
118
155
  end
119
156
 
157
+ ANALYSIS_TYPES = %w(average count count-unique extraction median minimum maximum sum percentile select-unique)
158
+
159
+ ANALYSIS_TYPES.each do |analysis_type|
160
+ underscored_analysis_type = analysis_type.sub('-', '_')
161
+ desc analysis_type, "Alias for queries:run -c #{underscored_analysis_type}"
162
+ map analysis_type => method_name = "queries_run_#{underscored_analysis_type}"
163
+ shared_options
164
+ query_options
165
+ data_options
166
+ self.send(:define_method, method_name) { queries_run(underscored_analysis_type) }
167
+ end
168
+
120
169
  desc 'events:add', 'Add one or more events and print the result'
121
170
  map 'events:add' => :events_add
122
171
 
@@ -25,7 +25,7 @@ module KeenCli
25
25
  end
26
26
 
27
27
  def get_collection_name(options)
28
- options["collection"] || ENV['KEEN_COLLECTION_NAME']
28
+ options["collection"] || options["event_collection"] || ENV['KEEN_COLLECTION_NAME']
29
29
  end
30
30
 
31
31
  def parse_data_as_querystring(query)
@@ -1,3 +1,3 @@
1
1
  module KeenCli
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,27 +1,121 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe KeenCli::CLI do
4
+
5
+ let(:project_id) { 'AAAAAAA' }
6
+ let(:master_key) { 'DDDDDD' }
7
+ let(:read_key) { 'BBBBBB' }
8
+ let(:write_key) { 'CCCCCC' }
9
+
10
+ def start(str=nil)
11
+ KeenCli::CLI.start(str ? str.split(" ") : [])
12
+ end
13
+
14
+ before do
15
+ Keen.project_id = project_id
16
+ Keen.read_key = read_key
17
+ Keen.write_key = write_key
18
+ Keen.master_key = master_key
19
+ end
20
+
4
21
  it 'prints help by default' do
5
- _, options = KeenCli::CLI.start
22
+ _, options = start
6
23
  expect(_).to be_empty
7
24
  end
8
25
 
9
26
  it 'prints version info if -v is used' do
10
- _, options = KeenCli::CLI.start(%w[-v])
27
+ _, options = start "-v"
11
28
  expect(_).to match /version/
12
29
  end
13
30
 
14
31
  describe 'project:describe' do
15
32
  it 'gets the project' do
16
- _, options = KeenCli::CLI.start(%w[project:describe])
17
- expect(_).to match /\/3.0\/projects\/#{Keen.project_id}\/events/
33
+ url = "https://api.keen.io/3.0/projects/#{project_id}"
34
+ stub_request(:get, url).to_return(:body => { :fake => "response" }.to_json)
35
+ _, options = start 'project:describe'
36
+ expect(_).to eq("fake" => "response")
37
+ end
38
+
39
+ it 'uses the project id param if present' do
40
+ url = "https://api.keen.io/3.0/projects/GGGG"
41
+ stub_request(:get, url).to_return(:body => { :fake => "response" }.to_json)
42
+ _, options = start 'project:describe --project GGGG'
43
+ expect(_).to eq("fake" => "response")
18
44
  end
19
45
  end
20
46
 
21
47
  describe 'project:collections' do
22
48
  it 'prints the project\'s collections' do
23
- _, options = KeenCli::CLI.start(%w[project:collections])
24
- expect(_["properties"]["keen.timestamp"]).to eq 'datetime'
49
+ url = "https://api.keen.io/3.0/projects/#{project_id}/events"
50
+ stub_request(:get, url).to_return(:body => { :fake => "response" }.to_json)
51
+ _, options = start 'project:collections'
52
+ expect(_).to eq("fake" => "response")
53
+ end
54
+
55
+ it 'uses the project id param if present' do
56
+ url = "https://api.keen.io/3.0/projects/GGGG/events"
57
+ stub_request(:get, url).to_return(:body => { :fake => "response" }.to_json)
58
+ _, options = start 'project:collections --project GGGG'
59
+ expect(_).to eq("fake" => "response")
60
+ end
61
+ end
62
+
63
+ describe 'queries:run' do
64
+ it 'runs the query using certain params' do
65
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/count?event_collection=minecraft-deaths"
66
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
67
+ _, options = start 'queries:run --analysis-type count --collection minecraft-deaths'
68
+ expect(_).to eq(10)
69
+ end
70
+
71
+ it 'runs the query using aliased params' do
72
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/count?event_collection=minecraft-deaths"
73
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
74
+ _, options = start 'queries:run -a count -c minecraft-deaths'
75
+ expect(_).to eq(10)
76
+ end
77
+
78
+ it 'converts dashes to underscores for certain properties' do
79
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/count?event_collection=minecraft-deaths&group_by=foo&target_property=bar"
80
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
81
+ _, options = start 'queries:run --analysis-type count --collection minecraft-deaths --group-by foo --target-property bar'
82
+ expect(_).to eq(10)
83
+ end
84
+
85
+ it 'accepts extraction-specific properties' do
86
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/extraction?event_collection=minecraft-deaths&property_names=%5B%22foo%22,%22bar%22%5D&latest=1&email=bob@bob.io"
87
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
88
+ _, options = start 'queries:run --analysis-type extraction --collection minecraft-deaths --property-names foo,bar --latest 1 --email bob@bob.io'
89
+ expect(_).to eq(10)
90
+ end
91
+
92
+ it 'converts comma-delimited property names to an array' do
93
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/extraction?event_collection=minecraft-deaths&property_names=%5B%22foo%22,%22bar%22%5D"
94
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
95
+ _, options = start 'queries:run --analysis-type extraction --collection minecraft-deaths --property-names foo,bar'
96
+ expect(_).to eq(10)
97
+ end
98
+
99
+
100
+ it 'uses a data option to take in query JSON' do
101
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/count?event_collection=minecraft-deaths"
102
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
103
+ _, options = start 'queries:run --analysis-type count --data {"event_collection":"minecraft-deaths"}'
104
+ expect(_).to eq(10)
105
+ end
106
+ end
107
+
108
+ describe "queries:run aliases" do
109
+ KeenCli::CLI::ANALYSIS_TYPES.each do |analysis_type|
110
+ describe analysis_type do
111
+ it "aliases to queries run, passing along the #{analysis_type} analysis type" do
112
+ underscored_analysis_type = analysis_type.sub('-', '_')
113
+ url = "https://api.keen.io/3.0/projects/#{project_id}/queries/#{underscored_analysis_type}?event_collection=minecraft-deaths"
114
+ stub_request(:get, url).to_return(:body => { :result => 10 }.to_json)
115
+ _, options = start "#{analysis_type} --collection minecraft-deaths"
116
+ expect(_).to eq(10)
117
+ end
118
+ end
25
119
  end
26
120
  end
27
121
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rspec'
2
+ require 'webmock/rspec'
2
3
 
3
4
  require File.expand_path("../../lib/keen-cli", __FILE__)
4
5
 
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.2
4
+ version: 0.1.3
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-07-02 00:00:00.000000000 Z
12
+ date: 2014-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: keen
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.8.4
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 0.8.4
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: thor
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -60,14 +60,14 @@ dependencies:
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
- name: http
63
+ name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- type: :runtime
70
+ type: :development
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
73
73
  none: false
@@ -76,7 +76,7 @@ dependencies:
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  - !ruby/object:Gem::Dependency
79
- name: rake
79
+ name: rspec
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
@@ -92,7 +92,7 @@ dependencies:
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
94
  - !ruby/object:Gem::Dependency
95
- name: rspec
95
+ name: debugger
96
96
  requirement: !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
@@ -108,7 +108,7 @@ dependencies:
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  - !ruby/object:Gem::Dependency
111
- name: debugger
111
+ name: webmock
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements: