Class: Echo_cli::Echo

Inherits:
Thor
  • Object
show all
Defined in:
lib/echo_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#convert(timestamp) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/echo_cli/cli.rb', line 189

def convert(timestamp)
  if timestamp.include? ":"
    time = Time.iso8601(timestamp).to_i.to_s
  else
    time = Time.at(timestamp.to_i).to_datetime.to_s
  end
  puts "\nConverted timstamp: ".green + time.yellow
  return time
end

#nowObject



213
214
215
216
# File 'lib/echo_cli/cli.rb', line 213

def now
  timestamp = Time.new.to_i.to_s
  puts "\nCurrent Epoch time: ".green + timestamp.to_s.yellow + "\nCurrent time: ".green + Time.at(timestamp.to_i).to_datetime.to_s.yellow
end

#populate(metric, timespan = 600, frequency = 600) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/echo_cli/cli.rb', line 148

def populate(metric, timespan = 600, frequency = 600)
  helper = Helper.new
  helper.set_envs
  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  now = Time.new.to_i
  prng = Random.new

  puts "\nWARNING: You are about to back-populate a metric.\nAre you sure you want to back-populate ".red + metric.yellow + " for the last ".red + timespan.to_s.yellow + " seconds?".red
  input = ask "[y/n]".red

  if input == "y"
    puts "\nPopulating..."
    while timespan >= 0
      to_post = helper.get_metric_string_populate(metric) + " " + prng.rand(0...100).to_s + " " + (now - timespan).to_s
      helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "2003", to_post)
      timespan = timespan - frequency
    end
    puts "Complete."
  else
    exit
  end
end

#post(metric, num = 1) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/echo_cli/cli.rb', line 37

def post(metric, num = 1)
  helper = Helper.new
  helper.set_envs()
  helper.check_for_IP()

  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  if options[:z]
    time_start = Time.new.to_i.to_s

    # sort of a mini-smoketest to ensure the posts will work before creating a background process
    helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "8125", metric)

    pid = Process.fork do
      prng = Random.new
      while true do
        helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "8125", metric)
        sleep prng.rand(0.1...1.0)
      end
    end
    puts "\nProcess ID: ".green + pid.to_s.yellow + "\nUse ".green + "$ kill ".yellow + pid.to_s.yellow + " to kill the zombie process.".green
    puts "Process began posting at ".green + time_start.yellow
  else
    num = num.to_i
    if num <= 0
      puts "\nThe number of posts specified is an incorrect expression, or is less than or equal to zero!".red
      exit
    end

    while num > 0
      helper.do_post_tcp(ENV["ECHO_CLI_HOST"], "8125", metric)
      puts "\nPosted ".green + metric.yellow + " to ".green + "http://".yellow + ENV["ECHO_CLI_HOST"].yellow + ":".yellow + "8125".yellow + " at ".green + Time.new.to_i.to_s.yellow
      num -= 1
    end
  end

  helper.unset_envs()
end

#query(metric, time_start = '-15min', time_end = 'now') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/echo_cli/cli.rb', line 103

def query(metric, time_start = '-15min', time_end = 'now')
  helper = Helper.new
  helper.set_envs()
  helper.check_for_IP()
  metric = metric.gsub(/'/,"").gsub(/"/,"")
  exit if !helper.check_metric_format(metric)

  query_url = 'http://' + ENV["ECHO_CLI_HOST"] + ':8080/render?target=' + helper.get_metric_string(metric) + '&from=' + time_start + '&until=' + time_end + '&format=json'

  response = helper.query_graphite(query_url)
  if(response.code != '200')
    puts "\nThe query returned a status code of ".red + response.code.yellow + ". If the query has start or end times, ensure they are formatted correctly.".red
    puts "Otherwise, check your metric query formatting and the 'ECHO_CLI_HOST' environment variable value.".red
    puts "\nResponse Body:\n".red
    puts response.body
  else
    res = JSON.parse(response.body)
    if(!options[:n])
      res.each_with_index{|target, index| res[index]["datapoints"].delete_if{|value| value[0] == nil}}
    end
    puts "\nSuccessful Query:\n\n".green + JSON.pretty_generate(res).to_s.green
  end
  helper.unset_envs()
end