Class: KeenCli::CLI

Inherits:
Thor
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.collection_optionsObject



28
29
30
# File 'lib/keen-cli/cli.rb', line 28

def self.collection_options
  option :collection, :aliases => ['-c']
end

.data_optionsObject



19
20
21
# File 'lib/keen-cli/cli.rb', line 19

def self.data_options
  option :data, :aliases => ['-d']
end

.file_optionsObject



23
24
25
26
# File 'lib/keen-cli/cli.rb', line 23

def self.file_options
  option :file, :aliases => ['-f']
  option :csv
end

.query_optionsObject



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.query_options
  self.collection_options
  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_optionsObject



12
13
14
15
16
17
# File 'lib/keen-cli/cli.rb', line 12

def self.shared_options
  option :project, :aliases => ['-p']
  option :"master-key", :aliases => ['-k']
  option :"read-key", :aliases => ['-r']
  option :"write-key", :aliases => ['-w']
end

Instance Method Details

#events_addObject



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(options)

  if options[:csv]

    data = File.read(options[: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 = options[:data]
        events.push(data)
      elsif file = options[: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_collectionsObject



72
73
74
75
76
77
# File 'lib/keen-cli/cli.rb', line 72

def project_collections
  Utils.process_options!(options)
  Keen.event_collections.tap do |collections|
    puts JSON.pretty_generate(collections)
  end
end

#project_describeObject



61
62
63
64
65
66
# File 'lib/keen-cli/cli.rb', line 61

def project_describe
  Utils.process_options!(options)
  Keen.project_info.tap do |info|
    puts JSON.pretty_generate(info)
  end
end

#project_openObject



83
84
85
86
87
88
# File 'lib/keen-cli/cli.rb', line 83

def project_open
  Utils.process_options!(options)
  "https://keen.io/project/#{Keen.project_id}".tap do |project_url|
    `open #{project_url}`
  end
end

#project_workbenchObject



94
95
96
97
98
99
# File 'lib/keen-cli/cli.rb', line 94

def project_workbench
  Utils.process_options!(options)
  "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.process_options!(options)

  # work with a new set of options
  q_options = {}

  data = nil

  if $stdin.tty?
    data = options[: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?
    data_options = JSON.parse(data)
    q_options.merge!(data_options)
  end

  # convert dashes to underscores, and merge all into q_options
  q_options.merge!(options.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(q_options)
  analysis_type = analysis_type || q_options["analysis_type"]

  # delete fields that shouldn't be passed to keen-gem as options
  q_options.delete("collection")
  q_options.delete("event_collection")
  q_options.delete("data")
  q_options.delete("analysis_type")

  if property_names = q_options.delete("property_names")
    q_options[:property_names] = property_names.split(",")
  end

  if start_time = q_options.delete("start")
    q_options[:timeframe] = { :start => start_time }
  end

  if end_time = q_options.delete("end")
    q_options[:timeframe] = q_options[:timeframe] || {}
    q_options[:timeframe][:end] = end_time
  end

  raise "No analysis type given!" unless analysis_type
  raise "No collection given!" unless collection

  Keen.send(analysis_type, collection, q_options).tap do |result|
    if result.is_a?(Hash) || result.is_a?(Array)
      puts JSON.pretty_generate(result)
    else
      puts result
    end
  end
end

#versionObject



51
52
53
54
55
# File 'lib/keen-cli/cli.rb', line 51

def version
  "keen-cli version #{KeenCli::VERSION}".tap do |s|
    puts s
  end
end