diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb index 57655f81c2..0213180e08 100644 --- a/app/controllers/health_controller.rb +++ b/app/controllers/health_controller.rb @@ -1,6 +1,7 @@ class HealthController < ApplicationController skip_before_action :authenticate_user! skip_after_action :verify_authorized + before_action :verify_token_for_gc_stats, only: [:gc] def index respond_to do |format| @@ -12,6 +13,14 @@ def index end end + def gc + render body: JSON.pretty_generate([ + Time.now.in_time_zone("Central Time (US & Canada)").strftime("%H"), + GC.stat + ]), + content_type: "application/json" + end + def case_contacts_creation_times_in_last_week case_contacts_created_in_last_week = CaseContact.where("created_at >= ?", 1.week.ago) @@ -67,4 +76,12 @@ def monthly_unique_users_graph_data render json: monthly_line_graph_combined_data end + + private + + def verify_token_for_gc_stats + gc_access_token = ENV["GC_ACCESS_TOKEN"] + + head :forbidden unless params[:token] == gc_access_token && !gc_access_token.nil? + end end diff --git a/config/routes.rb b/config/routes.rb index d3d77105b7..9a353356f1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,7 @@ resources :health, only: %i[index] do collection do get :case_contacts_creation_times_in_last_week + get :gc get :monthly_line_graph_data get :monthly_unique_users_graph_data end diff --git a/lib/tasks/post_gc_stat_to_discord.rake b/lib/tasks/post_gc_stat_to_discord.rake index 373c69898a..77d32ee520 100644 --- a/lib/tasks/post_gc_stat_to_discord.rake +++ b/lib/tasks/post_gc_stat_to_discord.rake @@ -1,13 +1,21 @@ desc "Post gc stats to discord channel" task post_gc_stat_to_discord: :environment do - stats = GC.stat + require "net/http" + + url = URI("https://casavolunteertracking.org/health/gc?token=#{ENV["GC_ACCESS_TOKEN"]}") + response = Net::HTTP.get_response(url) + + unless response.is_a?(Net::HTTPSuccess) + raise "Failed to fetch GC stats. HTTP status code:#{response.code}" + end + + stats = response.body unless ENV["DISCORD_WEBHOOK_URL"].nil? - formatted_stats = JSON.pretty_generate(stats) discord_message = <<~MULTILINE ```json - #{formatted_stats} + #{stats} ``` MULTILINE