File tree Expand file tree Collapse file tree 3 files changed +29
-3
lines changed
Expand file tree Collapse file tree 3 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 11class HealthController < ApplicationController
22 skip_before_action :authenticate_user!
33 skip_after_action :verify_authorized
4+ before_action :verify_token_for_gc_stats , only : [ :gc ]
45
56 def index
67 respond_to do |format |
@@ -12,6 +13,14 @@ def index
1213 end
1314 end
1415
16+ def gc
17+ render body : JSON . pretty_generate ( [
18+ Time . now . in_time_zone ( "Central Time (US & Canada)" ) . strftime ( "%H" ) ,
19+ GC . stat
20+ ] ) ,
21+ content_type : "application/json"
22+ end
23+
1524 def case_contacts_creation_times_in_last_week
1625 case_contacts_created_in_last_week = CaseContact . where ( "created_at >= ?" , 1 . week . ago )
1726
@@ -67,4 +76,12 @@ def monthly_unique_users_graph_data
6776
6877 render json : monthly_line_graph_combined_data
6978 end
79+
80+ private
81+
82+ def verify_token_for_gc_stats
83+ gc_access_token = ENV [ "GC_ACCESS_TOKEN" ]
84+
85+ head :forbidden unless params [ :token ] == gc_access_token && !gc_access_token . nil?
86+ end
7087end
Original file line number Diff line number Diff line change 4343 resources :health , only : %i[ index ] do
4444 collection do
4545 get :case_contacts_creation_times_in_last_week
46+ get :gc
4647 get :monthly_line_graph_data
4748 get :monthly_unique_users_graph_data
4849 end
Original file line number Diff line number Diff line change 11desc "Post gc stats to discord channel"
22
33task post_gc_stat_to_discord : :environment do
4- stats = GC . stat
4+ require "net/http"
5+
6+ url = URI ( "https://casavolunteertracking.org/health/gc?token=#{ ENV [ "GC_ACCESS_TOKEN" ] } " )
7+ response = Net ::HTTP . get_response ( url )
8+
9+ unless response . is_a? ( Net ::HTTPSuccess )
10+ raise "Failed to fetch GC stats. HTTP status code:#{ response . code } "
11+ end
12+
13+ stats = response . body
514
615 unless ENV [ "DISCORD_WEBHOOK_URL" ] . nil?
7- formatted_stats = JSON . pretty_generate ( stats )
816 discord_message = <<~MULTILINE
917 ```json
10- #{ formatted_stats }
18+ #{ stats }
1119 ```
1220 MULTILINE
1321
You can’t perform that action at this time.
0 commit comments