Module: ScoutApmMcp::Helpers::TimeRange

Included in:
ScoutApmMcp::Helpers
Defined in:
lib/scout_apm_mcp/helpers/time_range.rb

Instance Method Summary collapse

Instance Method Details

#calculate_range(range:, to: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 48

def calculate_range(range:, to: nil)
  return {from: nil, to: to} if range.nil? || range.to_s.strip.empty?

  range_str = range.to_s.strip
  range_str = "#{range_str}days" if range_str.match?(/\A\d+\z/)

  end_time = to ? parse_time(to) : Time.now.utc
  duration_seconds = parse_range(range_str)
  start_time = end_time - duration_seconds

  {
    from: format_time(start_time),
    to: format_time(end_time)
  }
end

#format_time(time) ⇒ Object



4
5
6
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 4

def format_time(time)
  time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
end

#make_duration(from_str, to_str) ⇒ Object



13
14
15
16
17
18
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 13

def make_duration(from_str, to_str)
  {
    start: parse_time(from_str),
    end: parse_time(to_str)
  }
end

#parse_range(range_str) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 20

def parse_range(range_str)
  return nil if range_str.nil? || range_str.empty?

  normalized = range_str.downcase.strip.gsub(/\s+/, "")
  match = normalized.match(/\A(\d+)(min|mins?|hr|hrs?|hour|hours|day|days)\z/)
  raise_invalid_range!(range_str) unless match

  range_seconds(match[1].to_i, match[2])
end

#parse_time(time_str) ⇒ Object



8
9
10
11
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 8

def parse_time(time_str)
  normalized = time_str.sub(/Z\z/i, "+00:00")
  Time.parse(normalized).utc
end

#raise_invalid_range!(range_str) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 43

def raise_invalid_range!(range_str)
  valid_ranges = %w[30min 60min 3hrs 6hrs 12hrs 1day 3days 7days]
  raise ArgumentError, "Invalid range format: #{range_str}. Valid formats: #{valid_ranges.join(", ")}"
end

#range_seconds(value, unit) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scout_apm_mcp/helpers/time_range.rb', line 30

def range_seconds(value, unit)
  case unit
  when /^min/
    value * 60
  when /^hr/, /^hour/
    value * 60 * 60
  when /^day/
    value * 24 * 60 * 60
  else
    raise ArgumentError, "Unknown time unit: #{unit}"
  end
end