Skip to content

Commit

Permalink
organization level stats rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
acoffman committed Jun 8, 2017
1 parent 74afe14 commit 10be0b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class OrganizationsController < ApplicationController
actions_without_auth :index, :show, :events
actions_without_auth :index, :show, :events, :stats

def index
orgs = Organization.order('organizations.id asc')
Expand All @@ -19,6 +19,14 @@ def show
render json: OrganizationDetailPresenter.new(org)
end

def stats
org = Organization.find(params[:organization_id])
stats = Rails.cache.fetch("org_stats_#{org.id}", expires_in: 5.minutes) do
Hash[org.stats_hash]
end
render json: stats
end

def events
user_ids = Organization.find_by!(id: params[:organization_id]).users.pluck(:id)

Expand Down
10 changes: 10 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ class Organization < ActiveRecord::Base
validates_attachment :profile_image,
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },
size: { in: 0..15.megabytes }

def stats_hash
(Hash.new { |h, k| h[k] = 0 }).tap do |accum|
users.each do |u|
u.stats_hash.each do |k, v|
accum[k] += v
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@

resources 'organizations', only: [:index, :show] do
get 'events' => 'organizations#events'
get 'stats' => 'organizations#stats'
end

resources 'subscriptions', except: [:update] do
Expand Down

0 comments on commit 10be0b8

Please sign in to comment.