forked from DMPRoadmap/roadmap
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
GET "/api/ca_dashboard/stats"
endpoint
- Loading branch information
1 parent
a96bc6e
commit 1ace30b
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module CaDashboard | ||
# Handles CRUD operations for "/api/ca_dashboard/stats" | ||
class StatsController < Api::V1::BaseApiController | ||
|
||
# GET /api/ca_dashboard/stats | ||
def index | ||
# To access this endpoint, the user must provide a valid JWT | ||
# JWT is acquired by authenticating via POST /api/v1/authenticate | ||
base_hash = { | ||
'plans' => Plan.all, | ||
'orgs' => Org.where(managed: true).all, | ||
'users' => User.all | ||
} | ||
@totals = { | ||
'all_time' => all_time_counts(base_hash), | ||
'last_30_days' => last_30_days_counts(base_hash) | ||
} | ||
begin | ||
@totals['custom_range'] = custom_range_counts(base_hash) if date_params_present? | ||
render 'api/ca_dashboard/stats/index', status: :ok | ||
rescue ArgumentError | ||
error_msg = _('Invalid date format. please use YYYY-MM-DD when supplying `start` or `end` params.') | ||
render_error(errors: [error_msg], status: :bad_request) | ||
end | ||
end | ||
|
||
private | ||
|
||
def all_time_counts(base_hash) | ||
base_hash.transform_values(&:count) | ||
end | ||
|
||
def last_30_days_counts(base_hash) | ||
base_hash.transform_values do |scope| | ||
scope.where('created_at >= ?', 30.days.ago).count | ||
end | ||
end | ||
|
||
def custom_range_counts(base_hash) | ||
start_date = parse_date(params[:start]) | ||
end_date = parse_date(params[:end]) | ||
base_hash.transform_values do |scope| | ||
scope.where(created_at: start_date..end_date).count | ||
end | ||
end | ||
|
||
def date_params_present? | ||
params[:start].present? || params[:end].present? | ||
end | ||
|
||
def parse_date(date_string) | ||
Date.strptime(date_string, '%Y-%m-%d') if date_string.present? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'api/v1/standard_response' | ||
json.stats do | ||
json.totals @totals | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters