-
Notifications
You must be signed in to change notification settings - Fork 3
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
42 changed files
with
7,386 additions
and
7 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
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,118 @@ | ||
class Api::V1::ShipmentsController < ApplicationController | ||
respond_to :json | ||
|
||
before_filter :authenticate | ||
|
||
GROUPING_ATTRIBUTES = { | ||
category: ['issue_type'], | ||
commodity: ['term', 'term_id'], | ||
exporting: ['exporter', 'exporter_iso', 'exporter_id'], | ||
importing: ['importer', 'importer_iso', 'importer_id'], | ||
species: ['taxon_name', 'appendix', 'taxon_concept_id'], | ||
taxonomy: [''], | ||
} | ||
|
||
def index | ||
@search = Trade::Filter.new(search_params) | ||
render :json => @search.results, | ||
:each_serializer => Trade::ShipmentApiFromViewSerializer, | ||
:meta => metadata_for_search(@search) | ||
end | ||
|
||
def chart_query | ||
@chart_data = Rails.cache.fetch(['chart_data', params], expires_in: 1.week) do | ||
Trade::ComplianceGrouping.new('year', {attributes: ['issue_type']}) | ||
.countries_reported_range(params[:year]) | ||
end | ||
render :json => @chart_data | ||
end | ||
|
||
def grouped_query | ||
limit = params[:limit].present? ? params[:limit].to_i : '' | ||
years_range = "year >= 2012 AND year <= #{Date.today.year - 1}" | ||
query = Trade::ComplianceGrouping.new('year', {attributes: sanitized_attributes, condition: years_range, limit: limit }) | ||
data = query.run | ||
params_hash = {} | ||
sanitized_attributes.map { |p| params_hash[p] = p } | ||
@grouped_data = Rails.cache.fetch(['grouped_data', params], expires_in: 1.week) do | ||
sanitized_attributes.first.empty? ? query.taxonomic_grouping : | ||
query.json_by_year(data, params, params_hash) | ||
end | ||
render :json => @grouped_data | ||
end | ||
|
||
def search_query | ||
query = Trade::ComplianceGrouping.new('year', {attributes: sanitized_attributes, condition: "year = #{params[:year]}"}) | ||
data = query.run | ||
@search_data = Rails.cache.fetch(['search_data', params], expires_in: 1.week) do | ||
query.build_hash(data, params) | ||
end | ||
@filtered_data = query.filter(@search_data, params) | ||
render :json => Kaminari.paginate_array(@filtered_data).page(params[:page]).per(params[:per_page]), | ||
:meta => metadata(@filtered_data, params) | ||
end | ||
|
||
def download_data | ||
@download_data = Rails.cache.fetch(['download_data', params], expires_in: 1.week) do | ||
Trade::DownloadDataRetriever.dashboard_download(download_params).to_a | ||
end | ||
render :json => @download_data | ||
end | ||
|
||
def search_download_data | ||
@download_data = Rails.cache.fetch(['search_download_data', params], expires_in: 1.week) do | ||
Trade::DownloadDataRetriever.search_download(download_params).to_a | ||
end | ||
render :json => @download_data | ||
end | ||
|
||
def search_download_all_data | ||
query = Trade::ComplianceGrouping.new('year', {attributes: sanitized_attributes, condition: "year = #{params[:year]}"}) | ||
data = query.run | ||
@search_download_all_data = Rails.cache.fetch(['search_download_all_data', params], expires_in: 1.week) do | ||
search_data = query.build_hash(data, params) | ||
filtered_data = query.filter(search_data, params) | ||
data_ids = query.filter_download_data(filtered_data, params) | ||
hash_params = params_hash_builder(data_ids, download_params) | ||
Trade::DownloadDataRetriever.search_download(hash_params).to_a | ||
end | ||
render :json => @search_download_all_data | ||
end | ||
|
||
private | ||
|
||
def params_hash_builder(ids, params) | ||
hash_params = {} | ||
hash_params[:ids] = ids.join(',') | ||
hash_params.merge!(params) | ||
hash_params.symbolize_keys | ||
end | ||
|
||
def metadata(data, params) | ||
{ | ||
:total => data.count, | ||
:page => params[:page] || 1, | ||
:per_page => params[:per_page] || 25 | ||
} | ||
end | ||
|
||
def search_params | ||
params.permit(:compliance_type, :time_range_start, :time_range_end, :page, :per_page) | ||
end | ||
|
||
def download_params | ||
params.permit(:year, :ids, :compliance_type, :type, :group_by, :appendix) | ||
end | ||
|
||
def sanitized_attributes | ||
GROUPING_ATTRIBUTES[params[:group_by].to_sym] | ||
end | ||
|
||
def authenticate | ||
token = request.headers['X-Authentication-Token'] | ||
unless token == Rails.application.secrets["compliance_tool_token"] | ||
head status: :unauthorized | ||
return false | ||
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
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
48 changes: 48 additions & 0 deletions
48
app/serializers/trade/shipment_api_from_view_serializer.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,48 @@ | ||
class Trade::ShipmentApiFromViewSerializer < ActiveModel::Serializer | ||
attributes :id , :year, :appendix, :taxon_name, :class_name, :order_name, :family_name, :genus_name, | ||
:term, :importer_reported_quantity, :exporter_reported_quantity, | ||
:unit, :importer, :importer_iso, :exporter, :exporter_iso, :origin, :purpose, :source, | ||
:import_permit, :export_permit, :origin_permit, :issue_type, :rank_name | ||
|
||
|
||
def importer | ||
object.importer || object.attributes["importer"] | ||
end | ||
|
||
def exporter | ||
object.exporter || object.attributes["exporter"] | ||
end | ||
|
||
def origin | ||
object.origin || object.attributes["origin"] | ||
end | ||
|
||
def term | ||
object.term || object.attributes["term"] | ||
end | ||
|
||
def unit | ||
object.unit || object.attributes["unit"] | ||
end | ||
|
||
def source | ||
object.source || object.attributes["source"] | ||
end | ||
|
||
def purpose | ||
object.purpose || object.attributes["purpose"] | ||
end | ||
|
||
def class_name | ||
object.attributes["class_name"] | ||
end | ||
|
||
def importer_reported_quantity | ||
object.attributes['importer_reported_quantity'] || object.attributes['importer_quantity'] | ||
end | ||
|
||
def exporter_reported_quantity | ||
object.attributes['exporter_reported_quantity'] || object.attributes['exporter_quantity'] | ||
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
14 changes: 14 additions & 0 deletions
14
db/migrate/20180509135814_create_trade_shipments_appendix_i_view.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,14 @@ | ||
class CreateTradeShipmentsAppendixIView < ActiveRecord::Migration | ||
def up | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_appendix_i_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_appendix_i_view" | ||
|
||
execute "CREATE VIEW trade_shipments_appendix_i_view AS #{view_sql('20180509135814', 'trade_shipments_appendix_i_view')}" | ||
execute "CREATE MATERIALIZED VIEW trade_shipments_appendix_i_mview AS SELECT * FROM trade_shipments_appendix_i_view" | ||
end | ||
|
||
def down | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_appendix_i_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_appendix_i_view" | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20180601083440_create_trade_cites_suspension_shipments_view.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,14 @@ | ||
class CreateTradeCitesSuspensionShipmentsView < ActiveRecord::Migration | ||
def up | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_cites_suspensions_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_cites_suspensions_view" | ||
|
||
execute "CREATE VIEW trade_shipments_cites_suspensions_view AS #{view_sql('20180601083440', 'trade_shipments_cites_suspensions_view')}" | ||
execute "CREATE MATERIALIZED VIEW trade_shipments_cites_suspensions_mview AS SELECT * FROM trade_shipments_cites_suspensions_view" | ||
end | ||
|
||
def down | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_cites_suspensions_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_cites_suspensions_view" | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20180627135001_create_trade_shipments_mandatory_quotas_view.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,14 @@ | ||
class CreateTradeShipmentsMandatoryQuotasView < ActiveRecord::Migration | ||
def up | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_mandatory_quotas_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_mandatory_quotas_view" | ||
|
||
execute "CREATE VIEW trade_shipments_mandatory_quotas_view AS #{view_sql('20180627135001', 'trade_shipments_mandatory_quotas_view')}" | ||
execute "CREATE MATERIALIZED VIEW trade_shipments_mandatory_quotas_mview AS SELECT * FROM trade_shipments_mandatory_quotas_view" | ||
end | ||
|
||
def down | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_mandatory_quotas_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_mandatory_quotas_view" | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20180705094119_update_trade_shipments_appendix_i_view.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,14 @@ | ||
class UpdateTradeShipmentsAppendixIView < ActiveRecord::Migration | ||
def up | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_appendix_i_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_appendix_i_view" | ||
|
||
execute "CREATE VIEW trade_shipments_appendix_i_view AS #{view_sql('20180705094119', 'trade_shipments_appendix_i_view')}" | ||
execute "CREATE MATERIALIZED VIEW trade_shipments_appendix_i_mview AS SELECT * FROM trade_shipments_appendix_i_view" | ||
end | ||
|
||
def down | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_appendix_i_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_appendix_i_view" | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20180713123936_update_trade_shipments_cites_suspensions_view.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,14 @@ | ||
class UpdateTradeShipmentsCitesSuspensionsView < ActiveRecord::Migration | ||
def up | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_cites_suspensions_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_cites_suspensions_view" | ||
|
||
execute "CREATE VIEW trade_shipments_cites_suspensions_view AS #{view_sql('20180713123936', 'trade_shipments_cites_suspensions_view')}" | ||
execute "CREATE MATERIALIZED VIEW trade_shipments_cites_suspensions_mview AS SELECT * FROM trade_shipments_cites_suspensions_view" | ||
end | ||
|
||
def down | ||
execute "DROP MATERIALIZED VIEW IF EXISTS trade_shipments_cites_suspensions_mview CASCADE" | ||
execute "DROP VIEW IF EXISTS trade_shipments_cites_suspensions_view" | ||
end | ||
end |
Oops, something went wrong.