Sha256: 33f964645ceda24fdd08dcebbab414d497500e26417dddb2d1d22d1b6f3947fc

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'url'
require 'cloudwatch_logs_insights_url_builder/criteria'

class CloudWatchLogsInsightsUrlBuilder
  attr_accessor :time_type, :timezone, :start_time, :end_time, :log_groups

  STRING_TIME_FORMAT = '%Y-%m-%dT%T.000Z'.freeze

  def initialize(region = 'us-east-1')
    @region = region
    @time_type = 'RELATIVE'
    @timezone = 'Local'
    @unit = 'minutes'
    @start_time = -86_400
    @end_time = 0
    @log_groups = []
  end

  def log_insights_url(query)
    url = +"https://#{@region}.console.aws.amazon.com/cloudwatch/home?region=#{@region}#logsV2:logs-insights"
    url << URI.encode_www_form_component('?queryDetail=').gsub('%', '$')

    start_time = @start_time.is_a?(Time) ? @start_time.strftime(STRING_TIME_FORMAT) : @start_time
    end_time = @end_time.is_a?(Time) ? @end_time.strftime(STRING_TIME_FORMAT) : @end_time

    builder = CloudWatchLogsInsightsUrlBuilder::Criteria.new
    builder.add('end', end_time)
    builder.add('start', start_time)
    builder.add('timeType', @time_type)
    builder.add('unit', 'seconds') if @time_type == 'RELATIVE'
    builder.add('tz', @timezone)
    builder.add('editorString', query)
    builder.add('isLiveTail', false)
    builder.add('source', @log_groups) if @log_groups.size.positive?

    url << builder.build
    url
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudwatch_logs_insights_url_builder-0.0.7 lib/cloudwatch_logs_insights_url_builder.rb