mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-20 00:35:22 +00:00
Add simple stats endpoint
This commit is contained in:
parent
cd9941bd10
commit
0d26dc587f
2 changed files with 30 additions and 0 deletions
24
app/controllers/api/v1/stats_controller.rb
Normal file
24
app/controllers/api/v1/stats_controller.rb
Normal 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
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue