diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fb1084b..c121fd47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ To set a custom tile URL, go to the user settings and set the `Maps` section to - Safe settings for user with default values. - In the user settings, you can now set a custom tile URL for the map. #429 #715 +- In the user map settings, you can now see a chart of map tiles usage. - If you have Prometheus exporter enabled, you can now see a `ruby_dawarich_map_tiles` metric in Prometheus, which shows the total number of map tiles loaded. Example: ``` diff --git a/app/controllers/api/v1/maps/tile_usage_controller.rb b/app/controllers/api/v1/maps/tile_usage_controller.rb index 43f8f070..c22778e7 100644 --- a/app/controllers/api/v1/maps/tile_usage_controller.rb +++ b/app/controllers/api/v1/maps/tile_usage_controller.rb @@ -2,7 +2,7 @@ class Api::V1::Maps::TileUsageController < ApiController def create - Maps::TileUsage::Track.new(tile_usage_params[:count].to_i).call + Maps::TileUsage::Track.new(current_api_user.id, tile_usage_params[:count].to_i).call head :ok end diff --git a/app/controllers/settings/maps_controller.rb b/app/controllers/settings/maps_controller.rb index 59beb04d..58e2fef6 100644 --- a/app/controllers/settings/maps_controller.rb +++ b/app/controllers/settings/maps_controller.rb @@ -5,6 +5,13 @@ class Settings::MapsController < ApplicationController def index @maps = current_user.safe_settings.maps + + @tile_usage = 7.days.ago.to_date.upto(Time.zone.today).map do |date| + [ + date.to_s, + Rails.cache.read("dawarich_map_tiles_usage:#{current_user.id}:#{date}") || 0 + ] + end end def update diff --git a/app/javascript/controllers/maps_controller.js b/app/javascript/controllers/maps_controller.js index 53b39c20..d2f59dbb 100644 --- a/app/javascript/controllers/maps_controller.js +++ b/app/javascript/controllers/maps_controller.js @@ -34,9 +34,6 @@ import { TileMonitor } from "../maps/tile_monitor"; export default class extends Controller { static targets = ["container"]; - static values = { - monitoringEnabled: Boolean - } settingsButtonAdded = false; layerControl = null; @@ -249,7 +246,7 @@ export default class extends Controller { } // Initialize tile monitor - this.tileMonitor = new TileMonitor(this.monitoringEnabledValue, this.apiKey); + this.tileMonitor = new TileMonitor(this.apiKey); // Add tile load event handlers to each base layer Object.entries(this.baseMaps()).forEach(([name, layer]) => { diff --git a/app/javascript/maps/tile_monitor.js b/app/javascript/maps/tile_monitor.js index 3a4ff36e..0a1edc60 100644 --- a/app/javascript/maps/tile_monitor.js +++ b/app/javascript/maps/tile_monitor.js @@ -1,15 +1,11 @@ export class TileMonitor { - constructor(monitoringEnabled, apiKey) { - this.monitoringEnabled = monitoringEnabled; + constructor(apiKey) { this.apiKey = apiKey; this.tileQueue = 0; this.tileUpdateInterval = null; } startMonitoring() { - // Only start the interval if monitoring is enabled - if (!this.monitoringEnabled) return; - // Clear any existing interval if (this.tileUpdateInterval) { clearInterval(this.tileUpdateInterval); @@ -29,13 +25,11 @@ export class TileMonitor { } recordTileLoad() { - if (!this.monitoringEnabled) return; this.tileQueue += 1; } sendTileUsage() { - // Don't send if monitoring is disabled or queue is empty - if (!this.monitoringEnabled || this.tileQueue === 0) return; + if (this.tileQueue === 0) return; const currentCount = this.tileQueue; console.log('Sending tile usage batch:', currentCount); diff --git a/app/services/maps/tile_usage/track.rb b/app/services/maps/tile_usage/track.rb index 0affd754..a2ec819d 100644 --- a/app/services/maps/tile_usage/track.rb +++ b/app/services/maps/tile_usage/track.rb @@ -1,11 +1,23 @@ # frozen_string_literal: true class Maps::TileUsage::Track - def initialize(count = 1) + def initialize(user_id, count = 1) + @user_id = user_id @count = count end def call + 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? + metric_data = { type: 'counter', name: 'dawarich_map_tiles_usage', @@ -13,7 +25,12 @@ class Maps::TileUsage::Track } PrometheusExporter::Client.default.send_json(metric_data) - rescue StandardError => e - Rails.logger.error("Failed to send tile usage metric: #{e.message}") + 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) end end diff --git a/app/services/reverse_geocoding/places/fetch_data.rb b/app/services/reverse_geocoding/places/fetch_data.rb index 12186c9f..9b691d36 100644 --- a/app/services/reverse_geocoding/places/fetch_data.rb +++ b/app/services/reverse_geocoding/places/fetch_data.rb @@ -96,7 +96,13 @@ class ReverseGeocoding::Places::FetchData end def reverse_geocoded_places - data = Geocoder.search([place.latitude, place.longitude], limit: 10, distance_sort: true, radius: 10) + data = Geocoder.search( + [place.latitude, place.longitude], + limit: 10, + distance_sort: true, + radius: 1, + units: DISTANCE_UNITS + ) data.reject do |place| place.data['properties']['osm_value'].in?(IGNORED_OSM_VALUES) || diff --git a/app/views/map/index.html.erb b/app/views/map/index.html.erb index 511f12a7..9fa4a0fe 100644 --- a/app/views/map/index.html.erb +++ b/app/views/map/index.html.erb @@ -53,7 +53,6 @@ data-coordinates="<%= @coordinates %>" data-distance="<%= @distance %>" data-points_number="<%= @points_number %>" - data-maps-monitoring-enabled-value="<%= DawarichSettings.prometheus_exporter_enabled? %>" data-timezone="<%= Rails.configuration.time_zone %>">