Class: Xmonitor::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/xmonitor/dashboard.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dashboard

Returns a new instance of Dashboard.



12
13
14
15
# File 'lib/xmonitor/dashboard.rb', line 12

def initialize(config)
  @config = config
  @logger = Logger.new(STDERR)
end

Class Method Details

.start(argv) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/xmonitor/dashboard.rb', line 2

def self.start(argv)
  config_file = argv[0]

  config = Xmonitor::Config.from_yaml(config_file)

  server = self.new(config)

  server.run
end

Instance Method Details

#all_metrics_by_hour_queryObject



46
47
48
# File 'lib/xmonitor/dashboard.rb', line 46

def all_metrics_by_hour_query
  'SELECT DATE_FORMAT(timestamp, \'%Y-%m-%d %H:00:00\') AS _timestamp, host AS _host, metric AS _metric, dimension AS _dimension, AVG(value) AS _value FROM "' + @config.athena_database + '"."' + @config.athena_table_name + '" GROUP BY DATE_FORMAT(timestamp, \'%Y-%m-%d %H:00:00\'), host, metric, dimension ORDER BY _host, _metric, _dimension, _timestamp DESC;'
end

#get_s3_object_and_process_body(s3_object_arn) {|@s3.get_object(bucket: bucket, key: key).body| ... } ⇒ Object

Yields:

  • (@s3.get_object(bucket: bucket, key: key).body)


74
75
76
77
78
# File 'lib/xmonitor/dashboard.rb', line 74

def get_s3_object_and_process_body(s3_object_arn, &blk)
  _, bucket, key = s3_object_arn.match(/s3:\/\/(.+?)\/(.+)$/).to_a

  yield @s3.get_object(bucket: bucket, key: key).body
end

#init_aws_config_and_clientsObject



39
40
41
42
43
44
# File 'lib/xmonitor/dashboard.rb', line 39

def init_aws_config_and_clients
  Aws.config[:credentials] = Aws::Credentials.new(@config.access_key_id, @config.secret_access_key)

  @athena = Aws::Athena::Client.new(region: @config.region)
  @s3 = Aws::S3::Client.new(region: @config.region)
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xmonitor/dashboard.rb', line 17

def run
  init_aws_config_and_clients

  query_execution_response = start_query_execution(all_metrics_by_hour_query)

  query_execution_id = query_execution_response.query_execution_id
  @logger.info(query_execution_id: query_execution_id)

  last_get_query_execution_response = wait_for_finish_query_execution(query_execution_id)

  output_location = last_get_query_execution_response.query_execution.result_configuration.output_location
  @logger.info(output_location: output_location)

  get_s3_object_and_process_body(output_location) do |body|
    CSV.new(body, headers: true).each{|row|
      puts [Time.parse(row[0]), row[1], row[2], row[3], row[4].to_f].to_csv
    }
  end
rescue StandardError => e
  @logger.error(error: e, backtrace: e.backtrace)
end

#start_query_execution(query_string) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/xmonitor/dashboard.rb', line 50

def start_query_execution(query_string)
  @athena.start_query_execution(
    query_string: query_string,
    query_execution_context: {database: @config.athena_database},
    result_configuration: {output_location: "s3://#{@config.athena_s3_bucket}/"}
  ).tap{|response|
    @logger.info(response: response)
  }
end

#wait_for_finish_query_execution(query_execution_id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xmonitor/dashboard.rb', line 60

def wait_for_finish_query_execution(query_execution_id)
  loop do
    response = @athena.get_query_execution(query_execution_id: query_execution_id)

    @logger.info(response: response)

    state = response.query_execution.status.state
    
    return response unless ['QUEUED', 'RUNNING'].include?(state)

    sleep 1
  end
end