Guest User

Untitled

a guest
Jan 2nd, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. require 'sinatra'
  2. require 'sequel'
  3. require 'yaml'
  4. require 'rufus-scheduler'
  5. require 'json'
  6.  
  7. # fetch data from the database on a schedule
  8. class DataFetcher
  9. def initialize(db)
  10. @db = db
  11. @current_status = fetch_current_status
  12. @historical_status_last_30_min = fetch_historical_status_last_30_min
  13. setup_scheduler
  14. end
  15.  
  16. attr_reader :current_status, :historical_status_last_30_min
  17.  
  18. private
  19.  
  20. def setup_scheduler
  21. scheduler = Rufus::Scheduler.new
  22.  
  23. scheduler.every '15s' do
  24. @current_status = fetch_current_status
  25. end
  26.  
  27. scheduler.every '60s' do
  28. @historical_status_last_30_min = fetch_historical_status_last_30_min
  29. end
  30. end
  31.  
  32. def fetch_current_status
  33. puts 'Fetching current status...'
  34. result = @db[:current_status].all
  35. puts "Current status: #{result.inspect}"
  36. result
  37. end
  38.  
  39. def fetch_historical_status_last_30_min
  40. puts 'Fetching historical status for the last 30 minutes...'
  41. result = @db[:historical_status]
  42. .where { recorded_at > Time.now - (30 * 60) }
  43. .order(Sequel.desc(:recorded_at))
  44. .all
  45. puts "Historical status: #{result.inspect}"
  46. result
  47. end
  48. end
  49.  
  50. # Load database configuration and initialize DataFetcher
  51. config = YAML.load_file('config.yml')['api']
  52. DB = Sequel.connect(adapter: config['adapter'], host: config['host'], database: config['database'], user: config['user'], password: config['password'], port: config['port'])
  53. data_fetcher = DataFetcher.new(DB)
  54.  
  55. # Sinatra routes
  56. get '/' do
  57. current_status = data_fetcher.current_status
  58. historical_status = data_fetcher.historical_status_last_30_min
  59.  
  60. puts "Current status: #{current_status.inspect}"
  61. puts "Historical status: #{historical_status.inspect}"
  62.  
  63. erb :_overall_network_status, locals: {
  64. active_partial: 'overall_network_status',
  65. current_status: current_status,
  66. historical_status: historical_status
  67. }
  68. end
  69.  
  70. get '/by-server-status' do
  71. erb :_by_server_status, locals: {
  72. active_partial: 'by_server_status',
  73. current_status: data_fetcher.current_status
  74. }
  75. end
  76.  
  77. get '/status-charts' do
  78. erb :_status_charts, locals: {
  79. active_partial: 'status_charts',
  80. historical_status: data_fetcher.historical_status_last_30_min
  81. }
  82. end
  83.  
  84. get '/current-status' do
  85. content_type :json
  86. data = data_fetcher.current_status.to_json
  87. puts "Current status JSON: #{data}"
  88. data
  89. end
  90.  
  91. get '/network-data-last-30-minutes' do
  92. content_type :json
  93. data = data_fetcher.historical_status_last_30_min.to_json
  94. puts "Network data JSON: #{data}"
  95. data
  96. end
  97.  
Advertisement
Add Comment
Please, Sign In to add comment