Class: KeenCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/keen-cli/cli.rb,
lib/keen-cli/events.rb,
lib/keen-cli/shared.rb,
lib/keen-cli/queries.rb,
lib/keen-cli/projects.rb,
lib/keen-cli/collections.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



14
15
16
# File 'lib/keen-cli/shared.rb', line 14

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

.data_optionsObject



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

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

.delete_optionsObject



5
6
7
8
9
10
11
12
# File 'lib/keen-cli/collections.rb', line 5

def self.delete_options
  self.collection_options
  option :timeframe, :aliases => ['-t']
  option :filters, :aliases => ['-f']
  option :start, :aliases => ['s']
  option :end, :aliases => ['e']
  option :force, :type => :boolean, :default => false
end

.events_optionsObject



7
8
9
10
11
# File 'lib/keen-cli/events.rb', line 7

def self.events_options
  option :csv
  option :params
  option :'batch-size', type: :numeric
end

.file_optionsObject



22
23
24
# File 'lib/keen-cli/shared.rb', line 22

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

.query_optionsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/keen-cli/queries.rb', line 5

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



5
6
7
8
9
10
11
12
# File 'lib/keen-cli/shared.rb', line 5

def self.shared_options
  option :project, :aliases => ['-p']
  option :"master-key", :aliases => ['-k']
  option :"read-key", :aliases => ['-r']
  option :"write-key", :aliases => ['-w']
  option :pretty, :type => :boolean, :default => true
  option :silent, :type => :boolean, :default => false
end

.viz_optionsObject



21
22
23
# File 'lib/keen-cli/queries.rb', line 21

def self.viz_options
  option :"spark", :type => :boolean
end

Instance Method Details

#collections_deleteObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/keen-cli/collections.rb', line 20

def collections_delete

  Utils.process_options!(options)

  collection = Utils.get_collection_name(options)

  unless options[:force]
    puts "WARNING! This is a delete request. Please re-enter the collection name to confirm."
    confirmation = $stdin.gets.chomp!
    unless confirmation == collection
      Utils.out "Confirmation failed!", options
      return false
    end
  end

  q_options = {}
  q_options[:timeframe] = options[:timeframe]

  if start_time = options[:start]
    q_options[:timeframe] = { :start => start_time }
  end

  if filters = options[:filters]
    q_options[:filters] = JSON.parse(filters)
  end

  if end_time = options[:end]
    q_options[:timeframe] = q_options[:timeframe] || {}
    q_options[:timeframe][:end] = end_time
  end

  q_options.delete_if { |k, v| v.nil? }

  Keen.delete(collection, q_options).tap do |result|
    Utils.out(result, options)
  end

end

#docsObject



36
37
38
39
40
41
42
# File 'lib/keen-cli/cli.rb', line 36

def docs
  docs_url = options[:reference] ? 'https://keen.io/docs/api/reference/' :
                                   'https://keen.io/docs'
  docs_url.tap do |url|
    `open #{url}`
  end
end

#events_addObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/keen-cli/events.rb', line 22

def events_add

  Utils.process_options!(options)

  collection = Utils.get_collection_name(options)

  batch_processor = BatchProcessor.new(collection,
    :csv => options[:csv],
    :params => options[:params],
    :pretty => options[:pretty],
    :silent => options[:silent],
    :'batch-size' => options[:'batch-size'])

  if data = options[:data]
    batch_processor.add(data)
  end

  if file = options[:file]
    File.readlines(file).each do |line|
      batch_processor.add(line)
    end
  end

  if !$stdin.tty?
    ARGV.clear
    ARGF.each_line do |line|
      batch_processor.add(line)
    end
  end

  if $stdin.tty? && data.nil? && file.nil?
    batch_processor.add("{}")
  end

  batch_processor.flush

  batch_processor.total_size

end

#projects_collectionsObject



20
21
22
23
24
25
# File 'lib/keen-cli/projects.rb', line 20

def projects_collections
  Utils.process_options!(options)
  Keen.event_collections.tap do |collections|
    Utils.out_json(collections, options)
  end
end

#projects_describeObject



9
10
11
12
13
14
# File 'lib/keen-cli/projects.rb', line 9

def projects_describe
  Utils.process_options!(options)
  Keen.project_info.tap do |info|
    Utils.out_json(info, options)
  end
end

#projects_openObject



31
32
33
34
35
36
# File 'lib/keen-cli/projects.rb', line 31

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

#projects_workbenchObject



42
43
44
45
46
47
# File 'lib/keen-cli/projects.rb', line 42

def projects_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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/keen-cli/queries.rb', line 31

def queries_run(analysis_type=nil)

  Utils.process_options!(options)

  collection = Utils.get_collection_name(options)
  raise "No collection given!" unless collection

  analysis_type = analysis_type || options[:"analysis-type"]
  raise "No analysis type given!" unless analysis_type

  query_options = to_query_options(options)

  result = Keen.query(analysis_type, collection, query_options, :response => :all_keys)

  if (options[:spark])
    raise 'Spark only applies to series queries!' unless options[:interval]
    numbers = result["result"].map do |object|
      object['value']
    end
    return numbers.join(' ').tap do |numbers_str|
      Utils.out(numbers_str, options)
    end
  end

  if result.is_a?(Hash) || result.is_a?(Array)
    Utils.out_json(result, options)
  else
    Utils.out(result, options)
  end

  result
end

#queries_urlObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/keen-cli/queries.rb', line 71

def queries_url

  Utils.process_options!(options)

  collection = Utils.get_collection_name(options)
  raise "No collection given!" unless collection

  analysis_type = options[:"analysis-type"]
  raise "No analysis type given!" unless analysis_type

  query_options = to_query_options(options)

  Keen.query_url(analysis_type, collection, query_options,
                 { :exclude_api_key => options[:'exclude-api-key']}).tap do |url|
    Utils.out(url, options)
  end
end

#versionObject



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

def version
  "keen-cli version #{KeenCli::VERSION}".tap do |s|
    Utils.out(s, options)
  end
end