2019-12-16 16:08:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Metrics
|
|
|
|
class SampleMetricsService
|
|
|
|
DIRECTORY = "sample_metrics"
|
|
|
|
|
2019-12-19 13:07:48 -05:00
|
|
|
attr_reader :identifier, :range_minutes
|
2019-12-16 16:08:00 -05:00
|
|
|
|
2019-12-19 13:07:48 -05:00
|
|
|
def initialize(identifier, range_start:, range_end:)
|
2019-12-16 16:08:00 -05:00
|
|
|
@identifier = identifier
|
2019-12-19 13:07:48 -05:00
|
|
|
@range_minutes = convert_range_minutes(range_start, range_end)
|
2019-12-16 16:08:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def query
|
|
|
|
return unless identifier && File.exist?(file_location)
|
|
|
|
|
2019-12-19 13:07:48 -05:00
|
|
|
query_interval
|
2019-12-16 16:08:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def file_location
|
|
|
|
sanitized_string = identifier.gsub(/[^0-9A-Za-z_]/, '')
|
|
|
|
File.join(Rails.root, DIRECTORY, "#{sanitized_string}.yml")
|
|
|
|
end
|
2019-12-19 13:07:48 -05:00
|
|
|
|
|
|
|
def query_interval
|
|
|
|
result = YAML.load_file(File.expand_path(file_location, __dir__))
|
|
|
|
result[range_minutes]
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_range_minutes(range_start, range_end)
|
|
|
|
((range_end.to_time - range_start.to_time) / 1.minute).to_i
|
|
|
|
end
|
2019-12-16 16:08:00 -05:00
|
|
|
end
|
|
|
|
end
|