From 17340079ce08284a772696ced6e973144d652077 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 26 Jul 2025 12:14:08 +0200 Subject: [PATCH 01/12] Expose prometheus metrics at /metrics --- .app_version | 2 +- .gitignore | 1 + CHANGELOG.md | 8 +++++++ app/controllers/metrics_controller.rb | 17 ++++++++++++++ app/services/prometheus_metrics.rb | 34 +++++++++++++++++++++++++++ config/routes.rb | 2 ++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/controllers/metrics_controller.rb create mode 100644 app/services/prometheus_metrics.rb diff --git a/.app_version b/.app_version index e8262eb5..db287d4a 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.30.3 +0.30.4 diff --git a/.gitignore b/.gitignore index 1510b45b..ce9010e1 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ .dotnet/ .cursorrules .cursormemory.md +.serena/project.yml /config/credentials/production.key /config/credentials/production.yml.enc diff --git a/CHANGELOG.md b/CHANGELOG.md index 2046bacf..410a8730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# [0.30.4] - 2025-07-25 + +## Added + +- Prometheus metrics are now available at `/metrics`. Configure `METRICS_USERNAME` and `METRICS_PASSWORD` environment variables for basic authentication. All other prometheus-related environment variables are also necessary. + + + # [0.30.3] - 2025-07-23 ## Changed diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb new file mode 100644 index 00000000..4ff77603 --- /dev/null +++ b/app/controllers/metrics_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class MetricsController < ApplicationController + http_basic_authenticate_with name: ENV['METRICS_USERNAME'], password: ENV['METRICS_PASSWORD'], only: :index + + def index + result = PrometheusMetrics.fetch_data + + if result[:success] + render plain: result[:data], content_type: 'text/plain' + elsif result[:error] == 'Prometheus exporter not enabled' + head :not_found + else + head :service_unavailable + end + end +end diff --git a/app/services/prometheus_metrics.rb b/app/services/prometheus_metrics.rb new file mode 100644 index 00000000..a4077b97 --- /dev/null +++ b/app/services/prometheus_metrics.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'net/http' +require 'uri' + +class PrometheusMetrics + class << self + def fetch_data + return { success: false, error: 'Prometheus exporter not enabled' } unless prometheus_enabled? + + host = ENV.fetch('PROMETHEUS_EXPORTER_HOST', 'localhost') + port = ENV.fetch('PROMETHEUS_EXPORTER_PORT', 9394) + + begin + response = Net::HTTP.get_response(URI("http://#{host}:#{port}/metrics")) + + if response.code == '200' + { success: true, data: response.body } + else + { success: false, error: "Prometheus server returned #{response.code}" } + end + rescue => e + Rails.logger.error "Failed to fetch Prometheus metrics: #{e.message}" + { success: false, error: e.message } + end + end + + private + + def prometheus_enabled? + DawarichSettings.prometheus_exporter_enabled? + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 93ceb12d..0c8026fb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,6 +87,8 @@ Rails.application.routes.draw do devise_for :users end + resources :metrics, only: [:index] + get 'map', to: 'map#index' namespace :api do From 9c7084a10bb3275f6a8260727e9825c2953d9e02 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 26 Jul 2025 12:37:46 +0200 Subject: [PATCH 02/12] Fix request to a user in partial --- app/models/concerns/distance_convertible.rb | 5 ----- app/views/trips/_countries.html.erb | 2 +- app/views/trips/_distance.html.erb | 2 +- app/views/trips/_trip.html.erb | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/models/concerns/distance_convertible.rb b/app/models/concerns/distance_convertible.rb index 2a757303..30c7693b 100644 --- a/app/models/concerns/distance_convertible.rb +++ b/app/models/concerns/distance_convertible.rb @@ -37,11 +37,6 @@ module DistanceConvertible distance.to_f / conversion_factor end - def distance_for_user(user) - user_unit = user.safe_settings.distance_unit - distance_in_unit(user_unit) - end - module ClassMethods def convert_distance(distance_meters, unit) return 0.0 unless distance_meters.present? diff --git a/app/views/trips/_countries.html.erb b/app/views/trips/_countries.html.erb index 69a7fe08..0ae8f7e5 100644 --- a/app/views/trips/_countries.html.erb +++ b/app/views/trips/_countries.html.erb @@ -2,7 +2,7 @@
Distance
-
<%= trip.distance_for_user(current_user).round %> <%= distance_unit %>
+
<%= trip.distance_in_unit(distance_unit).round %> <%= distance_unit %>
diff --git a/app/views/trips/_distance.html.erb b/app/views/trips/_distance.html.erb index 6bb835e6..e4d2cc65 100644 --- a/app/views/trips/_distance.html.erb +++ b/app/views/trips/_distance.html.erb @@ -1,5 +1,5 @@ <% if trip.distance.present? %> - <%= trip.distance_for_user(current_user).round %> <%= distance_unit %> + <%= trip.distance_in_unit(distance_unit).round %> <%= distance_unit %> <% else %> Calculating... diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb index c65373a1..d2fdd57d 100644 --- a/app/views/trips/_trip.html.erb +++ b/app/views/trips/_trip.html.erb @@ -5,7 +5,7 @@ <%= trip.name %>

- <%= "#{human_date(trip.started_at)} – #{human_date(trip.ended_at)}, #{trip.distance_for_user(current_user).round} #{current_user.safe_settings.distance_unit}" %> + <%= "#{human_date(trip.started_at)} – #{human_date(trip.ended_at)}, #{trip.distance_in_unit(current_user.safe_settings.distance_unit).round} #{distance_unit}" %>

Date: Sat, 26 Jul 2025 12:39:30 +0200 Subject: [PATCH 03/12] Update app version and changelog --- .app_version | 2 +- CHANGELOG.md | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.app_version b/.app_version index e8262eb5..db287d4a 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.30.3 +0.30.4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2046bacf..daf30f53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + + +# [0.30.4] - 2025-07-25 + +## Fixed + +- The Warden error in jobs is now fixed. #1556 + + + # [0.30.3] - 2025-07-23 ## Changed From e127511262aa4f6ba7f0064ff12190dae4e4439a Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 26 Jul 2025 13:06:06 +0200 Subject: [PATCH 04/12] Fix live map checkbox --- CHANGELOG.md | 1 + app/javascript/controllers/maps_controller.js | 6 ++++++ app/models/point.rb | 2 ++ 3 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 309ec73c..6f9d8e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Fixed - The Warden error in jobs is now fixed. #1556 +- The Live Map setting is now respected. diff --git a/app/javascript/controllers/maps_controller.js b/app/javascript/controllers/maps_controller.js index 12092891..1efff1e7 100644 --- a/app/javascript/controllers/maps_controller.js +++ b/app/javascript/controllers/maps_controller.js @@ -969,6 +969,12 @@ export default class extends BaseController { this.routeOpacity = parseFloat(newSettings.route_opacity) || 0.6; this.clearFogRadius = parseInt(newSettings.fog_of_war_meters) || 50; + // Update the DOM data attribute to keep it in sync + const mapElement = document.getElementById('map'); + if (mapElement) { + mapElement.setAttribute('data-user_settings', JSON.stringify(this.userSettings)); + } + // Store current layer states const layerStates = { Points: this.map.hasLayer(this.markersLayer), diff --git a/app/models/point.rb b/app/models/point.rb index 7be8524a..838e59ff 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -70,6 +70,8 @@ class Point < ApplicationRecord # rubocop:disable Metrics/MethodLength Metrics/AbcSize def broadcast_coordinates + return unless user.safe_settings.live_map_enabled + PointsChannel.broadcast_to( user, [ From cb9525cb776f81f72b1c1d033e305ce7716db24f Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 26 Jul 2025 13:10:24 +0200 Subject: [PATCH 05/12] Add missing live map info modal. --- CHANGELOG.md | 1 + app/views/map/_settings_modals.html.erb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f9d8e87..549c693d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The Warden error in jobs is now fixed. #1556 - The Live Map setting is now respected. +- The Live Map info modal is now displayed. #665 diff --git a/app/views/map/_settings_modals.html.erb b/app/views/map/_settings_modals.html.erb index 24d965a5..31bb3d12 100644 --- a/app/views/map/_settings_modals.html.erb +++ b/app/views/map/_settings_modals.html.erb @@ -156,6 +156,23 @@
+ + +