debian-mirror-gitlab/app/services/metrics/sample_metrics_service.rb

37 lines
868 B
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module Metrics
class SampleMetricsService
DIRECTORY = "sample_metrics"
2020-03-13 15:44:24 +05:30
attr_reader :identifier, :range_minutes
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
def initialize(identifier, range_start:, range_end:)
2020-01-01 13:55:28 +05:30
@identifier = identifier
2020-03-13 15:44:24 +05:30
@range_minutes = convert_range_minutes(range_start, range_end)
2020-01-01 13:55:28 +05:30
end
def query
return unless identifier && File.exist?(file_location)
2020-03-13 15:44:24 +05:30
query_interval
2020-01-01 13:55:28 +05:30
end
private
def file_location
sanitized_string = identifier.gsub(/[^0-9A-Za-z_]/, '')
File.join(Rails.root, DIRECTORY, "#{sanitized_string}.yml")
end
2020-03-13 15:44:24 +05:30
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
2020-01-01 13:55:28 +05:30
end
end