2025-02-11 14:45:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2025-02-11 15:04:12 -05:00
|
|
|
class Maps::TileUsage::Track
|
2025-02-13 15:04:29 -05:00
|
|
|
def initialize(user_id, count = 1)
|
|
|
|
|
@user_id = user_id
|
2025-02-11 14:45:36 -05:00
|
|
|
@count = count
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
2025-02-13 15:04:29 -05:00
|
|
|
report_to_prometheus
|
|
|
|
|
report_to_cache
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
Rails.logger.error("Failed to send tile usage metric: #{e.message}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def report_to_prometheus
|
|
|
|
|
return unless DawarichSettings.prometheus_exporter_enabled?
|
|
|
|
|
|
2025-02-11 14:45:36 -05:00
|
|
|
metric_data = {
|
|
|
|
|
type: 'counter',
|
2025-02-11 15:17:33 -05:00
|
|
|
name: 'dawarich_map_tiles_usage',
|
2025-02-11 14:45:36 -05:00
|
|
|
value: @count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrometheusExporter::Client.default.send_json(metric_data)
|
2025-02-13 15:04:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def report_to_cache
|
|
|
|
|
today_key = "dawarich_map_tiles_usage:#{@user_id}:#{Time.zone.today}"
|
|
|
|
|
|
|
|
|
|
current_value = (Rails.cache.read(today_key) || 0).to_i
|
|
|
|
|
Rails.cache.write(today_key, current_value + @count, expires_in: 7.days)
|
2025-02-11 14:45:36 -05:00
|
|
|
end
|
|
|
|
|
end
|