Add simple stats endpoint

This commit is contained in:
Max Wofford 2025-03-03 14:26:15 -05:00
parent cd9941bd10
commit 0d26dc587f
2 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,24 @@
class Api::V1::StatsController < ApplicationController
before_action :ensure_authenticated!, unless: -> { Rails.env.development? }
def show
# take either user_id with a start date & end date
start_date = Date.parse(params[:start_date]) if params[:start_date].present?
start_date ||= 10.years.ago
end_date = Date.parse(params[:end_date]) if params[:end_date].present?
end_date ||= Date.today
query = Heartbeat
query = query.where(time: start_date..end_date)
query = query.where(user_id: params[:user_id]) if params[:user_id].present?
render plain: query.duration_seconds
end
private
def ensure_authenticated!
bearer_token = request.headers["Authorization"].split(" ").last
return render json: { error: "Unauthorized" }, status: :unauthorized unless bearer_token == ENV["STATS_API_KEY"]
end
end

View file

@ -48,6 +48,12 @@ Rails.application.routes.draw do
get "my/settings", to: "users#edit", as: :my_settings
patch "my/settings", to: "users#update"
namespace :api do
namespace :v1 do
get "stats", to: "stats#show"
end
end
post "/sailors_log/slack/commands", to: "slack#create"
post "/timedump/slack/commands", to: "slack#create"