class KeenCli::CLI

Constants

ANALYSIS_TYPES

Public Class Methods

collection_options() click to toggle source
# File lib/keen-cli/shared.rb, line 14
def self.collection_options
  option :collection, :aliases => ['-c']
end
data_options() click to toggle source
# File lib/keen-cli/shared.rb, line 18
def self.data_options
  option :data, :aliases => ['-d']
end
delete_options() click to toggle source
# 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_options() click to toggle source
# File lib/keen-cli/events.rb, line 7
def self.events_options
  option :csv
  option :params
  option :'batch-size', type: :numeric
end
file_options() click to toggle source
# File lib/keen-cli/shared.rb, line 22
def self.file_options
  option :file, :aliases => ['-f']
end
query_options() click to toggle source
# 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_options() click to toggle source
# 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_options() click to toggle source
# File lib/keen-cli/queries.rb, line 21
def self.viz_options
  option :"spark", :type => :boolean
end

Public Instance Methods

collections_delete() click to toggle source
# 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
docs() click to toggle source
# 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_add() click to toggle source
# 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_collections() click to toggle source
# 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_describe() click to toggle source
# 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_open() click to toggle source
# 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_workbench() click to toggle source
# 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) click to toggle source
# 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_url() click to toggle source
# 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
version() click to toggle source
# File lib/keen-cli/cli.rb, line 24
def version
  "keen-cli version #{KeenCli::VERSION}".tap do |s|
    Utils.out(s, options)
  end
end

Private Instance Methods

to_query_options(options) click to toggle source
# File lib/keen-cli/queries.rb, line 104
def to_query_options(options)

  data = nil

  if $stdin.tty?
    data = options[:data]
  else
    ARGV.clear
    ARGF.each_line do |line|
      data += line
    end
  end

  # setup a holder for query options
  q_options = {}

  # if data is provided, parse it and merge it
  unless data.nil?
    data_options = JSON.parse(data)
    q_options.merge!(data_options)
  end

  # copy query options in intelligently
  q_options[:target_property] = options[:"target-property"]
  q_options[:interval] = options[:interval]
  q_options[:timeframe] = options[:timeframe]
  q_options[:percentile] = options[:percentile]
  q_options[:latest] = options[:latest]
  q_options[:email] = options[:email]

  if group_by = options[:"group-by"]
    q_options[:group_by] = group_by.split(",")
  end

  if property_names = options[:"property-names"]
    q_options[:property_names] = property_names.split(",")
  end

  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? }

  q_options
end