-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
2 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,33 @@ | ||
module API | ||
module V1 | ||
module Beneficiaries | ||
class StatsController < BaseController | ||
def index | ||
validate_request_schema( | ||
with: ::V1::BeneficiaryStatsRequestSchema, | ||
serializer_class: StatSerializer, | ||
**serializer_options | ||
) do |permitted_params| | ||
AggregateDataQuery.new(permitted_params).apply(beneficiaries_scope) | ||
end | ||
end | ||
|
||
private | ||
|
||
def beneficiaries_scope | ||
current_account.beneficiaries | ||
end | ||
|
||
def serializer_options | ||
{ | ||
input_params: request.query_parameters, | ||
decorator_class: nil, | ||
pagination_options: { | ||
sort_direction: :asc | ||
} | ||
} | ||
end | ||
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,15 @@ | ||
class AggregateDataQuery | ||
attr_reader :filters, :groups | ||
|
||
def initialize(options) | ||
@filters = options.fetch(:filters, {}) | ||
@groups = Array(options.fetch(:groups)) | ||
end | ||
|
||
def apply(scope) | ||
result = scope.where(filters).group(groups).count | ||
result.map.with_index do |(key, value), index| | ||
StatResult.new(groups:, key:, value:, sequence_number: index + 1) | ||
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,11 @@ | ||
StatResult = Data.define(:key, :groups, :value, :sequence_number) do | ||
def id | ||
StatResult::IDGenerator.generate_id(key) | ||
end | ||
end | ||
|
||
class StatResult::IDGenerator | ||
def self.generate_id(key) | ||
Digest::SHA256.hexdigest(Array(key).reject(&:blank?).map(&:downcase).join(":")) | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
app/request_schemas/v1/beneficiary_stats_request_schema.rb
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,49 @@ | ||
module V1 | ||
class BeneficiaryStatsRequestSchema < ApplicationRequestSchema | ||
# Group = Data.define(:name, :column) | ||
|
||
# COUNTRY_GROUP = Group.new(name: "country", column: :iso_country_code) | ||
# REGION_GROUP = Group.new(name: "region", column: :iso_region_code) | ||
# LOCALITY_GROUP = Group.new(name: "locality", column: :locality) | ||
# | ||
# GROUPS = [ COUNTRY_GROUP, REGION_GROUP, LOCALITY_GROUP ].freeze | ||
# | ||
# VALID_GROUP_BY_OPTIONS = [ | ||
# [ COUNTRY_GROUP, REGION_GROUP, LOCALITY_GROUP ] | ||
# ] | ||
|
||
params do | ||
optional(:filter).value(:hash).hash do | ||
optional(:gender).filled(Types::UpcaseString, included_in?: Contact.gender.values) | ||
end | ||
required(:group_by).value(array[:string]) | ||
end | ||
|
||
# rule(:group_by) do |context:| | ||
# context[:groups] = find_groups(value) | ||
# key.failure("is invalid") if context[:groups].blank? | ||
# end | ||
|
||
def output | ||
result = super | ||
|
||
# filter = params.fetch(:filter) | ||
# conditions = filter.slice(:type, :locality) | ||
# conditions[:iso_country_code] = filter.fetch(:country) if filter.key?(:country) | ||
# conditions[:iso_region_code] = filter.fetch(:region) if filter.key?(:region) | ||
# | ||
# result = {} | ||
# | ||
# result[:named_scopes] = :available | ||
# result[:conditions] = conditions | ||
result[:groups] = result[:group_by] | ||
result | ||
end | ||
|
||
private | ||
|
||
def find_groups(group_names) | ||
VALID_GROUP_BY_OPTIONS.find { |group_list| group_list.map(&:name).sort == group_names.sort } | ||
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,17 @@ | ||
class StatSerializer < JSONAPISerializer | ||
attribute :result do |object| | ||
result = {} | ||
|
||
object.groups.each_with_index do |group, index| | ||
group_value = Array(object.key)[index] | ||
|
||
# NOTE: Handle field value object. Eg. enumerize field. | ||
group_value = group_value.value if group_value.respond_to?(:value) | ||
|
||
result[group] = group_value | ||
end | ||
|
||
result[:value] = object.value | ||
result | ||
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
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
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
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,10 @@ | ||
module APIResponseSchema | ||
StatSchema = Dry::Schema.JSON do | ||
required(:id).filled(:str?) | ||
required(:type).filled(eql?: "stat") | ||
|
||
required(:attributes).schema do | ||
required(:result).filled(:hash?) | ||
end | ||
end | ||
end |