Class: KeenCli::CLI
- Inherits:
-
Thor
- Object
- Thor
- KeenCli::CLI
- Defined in:
- lib/keen-cli/cli.rb
Constant Summary collapse
- ANALYSIS_TYPES =
%w(average count count-unique extraction median minimum maximum sum percentile select-unique)
Class Method Summary collapse
- .collection_options ⇒ Object
- .data_options ⇒ Object
- .file_options ⇒ Object
- .query_options ⇒ Object
- .shared_options ⇒ Object
Instance Method Summary collapse
- #events_add ⇒ Object
- #project_collections ⇒ Object
- #project_describe ⇒ Object
- #project_open ⇒ Object
- #project_workbench ⇒ Object
- #queries_run(analysis_type = nil) ⇒ Object
- #version ⇒ Object
Class Method Details
.collection_options ⇒ Object
28 29 30 |
# File 'lib/keen-cli/cli.rb', line 28 def self. option :collection, :aliases => ['-c'] end |
.data_options ⇒ Object
19 20 21 |
# File 'lib/keen-cli/cli.rb', line 19 def self. option :data, :aliases => ['-d'] end |
.file_options ⇒ Object
23 24 25 26 |
# File 'lib/keen-cli/cli.rb', line 23 def self. option :file, :aliases => ['-f'] option :csv end |
.query_options ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/keen-cli/cli.rb', line 32 def self. self. option :"analysis-type", :aliases => ['-a'] option :"group-by", :aliases => ['-g'] option :"target-property", :aliases => ['-y'] option :interval, :aliases => ['-i'] option :timeframe, :aliases => ['-t'] option :filters, :aliases => ['-f'] option :percentile option :"property-names" option :latest option :email option :start, :aliases => ['s'] option :end, :aliases => ['e'] end |
.shared_options ⇒ Object
12 13 14 15 16 17 |
# File 'lib/keen-cli/cli.rb', line 12 def self. option :project, :aliases => ['-p'] option :"master-key", :aliases => ['-k'] option :"read-key", :aliases => ['-r'] option :"write-key", :aliases => ['-w'] end |
Instance Method Details
#events_add ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/keen-cli/cli.rb', line 194 def events_add events = [] collection = Utils.get_collection_name() if [:csv] data = File.read([:file]) csv = CSV.new(data, :headers => true, :header_converters => :symbol, :converters => :all) events = csv.to_a.map {|row| row.to_hash } else if $stdin.tty? if data = [:data] events.push(data) elsif file = [:file] File.readlines(file).each do |line| events.push(line) end else events.push({}) end else ARGV.clear ARGF.each_line do |line| events.push(line) end end events = events.map do |event| begin JSON.parse(event) rescue begin Utils.parse_data_as_querystring(event) rescue event end end end end if events.length > 1 Keen.publish_batch(collection => events).tap do |result| puts JSON.pretty_generate(result) end else Keen.publish(collection, events.first).tap do |result| puts JSON.pretty_generate(result) end end end |
#project_collections ⇒ Object
72 73 74 75 76 77 |
# File 'lib/keen-cli/cli.rb', line 72 def project_collections Utils.() Keen.event_collections.tap do |collections| puts JSON.pretty_generate(collections) end end |
#project_describe ⇒ Object
61 62 63 64 65 66 |
# File 'lib/keen-cli/cli.rb', line 61 def project_describe Utils.() Keen.project_info.tap do |info| puts JSON.pretty_generate(info) end end |
#project_open ⇒ Object
83 84 85 86 87 88 |
# File 'lib/keen-cli/cli.rb', line 83 def project_open Utils.() "https://keen.io/project/#{Keen.project_id}".tap do |project_url| `open #{project_url}` end end |
#project_workbench ⇒ Object
94 95 96 97 98 99 |
# File 'lib/keen-cli/cli.rb', line 94 def project_workbench Utils.() "https://keen.io/project/#{Keen.project_id}/workbench".tap do |project_url| `open #{project_url}` end end |
#queries_run(analysis_type = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/keen-cli/cli.rb', line 106 def queries_run(analysis_type=nil) Utils.() # work with a new set of options = {} data = nil if $stdin.tty? data = [:data] else ARGV.clear ARGF.each_line do |line| data = line end end # if data is provided, parse it and merge it unless data.nil? = JSON.parse(data) .merge!() end # convert dashes to underscores, and merge all into q_options .merge!(.inject({}) do |memo, element| if ['analysis-type', 'group-by', 'target-property', 'property-names'].include?(element.first) memo[element.first.sub('-', '_')] = element.last else memo[element.first] = element.last end memo end) collection = Utils.get_collection_name() analysis_type = analysis_type || ["analysis_type"] # delete fields that shouldn't be passed to keen-gem as options .delete("collection") .delete("event_collection") .delete("data") .delete("analysis_type") if property_names = .delete("property_names") [:property_names] = property_names.split(",") end if start_time = .delete("start") [:timeframe] = { :start => start_time } end if end_time = .delete("end") [:timeframe] = [:timeframe] || {} [:timeframe][:end] = end_time end raise "No analysis type given!" unless analysis_type raise "No collection given!" unless collection Keen.send(analysis_type, collection, ).tap do |result| if result.is_a?(Hash) || result.is_a?(Array) puts JSON.pretty_generate(result) else puts result end end end |