Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create GET "/api/ca_dashboard/stats" endpoint #852

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Create GET "/api/ca_dashboard/stats" endpoint to fetch Plan, User, and Org-related statistics [#852](https://github.com/portagenetwork/roadmap/pull/852)

### Changed

- Bump rexml from 3.2.8 to 3.3.3 [#839](https://github.com/portagenetwork/roadmap/pull/839)
Expand Down
59 changes: 59 additions & 0 deletions app/controllers/api/ca_dashboard/stats_controller.rb
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
# Allow public access / bypass JWT authentication via "POST /api/v1/authenticate"
skip_before_action :authorize_request, only: [:index]

# GET /api/ca_dashboard/stats
def index
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
6 changes: 6 additions & 0 deletions app/views/api/ca_dashboard/stats/index.json.jbuilder
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick question @aaronskiba , why do we have the value "total_items": 0, in the resulting json output?

Is that something we need ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question; I guess that's just being inherited from api/v1/standard_response. I think it corresponds to the number of plans returned, which would always be zero for this new API endpoint:

I guess it does a similar thing when I check api/v1/heartbeat:

{
  "application": "DMP Assistant",
  "source": "GET /api/v1/heartbeat",
  "time": "2024-08-29T18:21:17+00:00",
  "caller": "142.244.108.150",
  "code": 200,
  "message": "OK",
  "total_items": 0,
  "items": [
  ]
}

Here's an API request for a specific plan:

{
  "application": "DMP Assistant",
  "source": "GET /api/v1/plans/id_redacted",
  "time": "2024-08-29T18:18:34+00:00",
  "caller": "DMP Administrator",
  "code": 200,
  "message": "OK",
  "page": 1,
  "per_page": 100,
  "total_items": 1,
  "items": [
    {
      "dmp": {
        "schema": "https://github.com/RDA-DMP-Common/RDA-DMP-Common-Standard/tree/master/examples/JSON/JSON-schema/1.0",
        "title": "v4.1.3 Test"
...

end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@
resources :plans, only: %i[create show index]
resources :templates, only: [:index]
end

namespace :ca_dashboard do
resources :stats, only: [:index]
end
end

namespace :paginable do
Expand Down
Loading