From 1fec50accfe9b202eb071ba01f2bc7c4c737dc75 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 6 Sep 2016 14:21:52 +0200 Subject: [PATCH] ability to run custom aggregation with query and append result to query --- .../controllers/admin/statistics.coffee.erb | 1 + app/controllers/api/statistics_controller.rb | 11 ++++++++++- ..._remove_path_from_statistic_custom_aggregations.rb | 5 +++++ ..._index_es_type_to_statistic_custom_aggregations.rb | 6 ++++++ db/schema.rb | 5 +++-- test/fixtures/statistic_custom_aggregations.yml | 3 ++- 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20160906094739_remove_path_from_statistic_custom_aggregations.rb create mode 100644 db/migrate/20160906094847_add_es_index_es_type_to_statistic_custom_aggregations.rb diff --git a/app/assets/javascripts/controllers/admin/statistics.coffee.erb b/app/assets/javascripts/controllers/admin/statistics.coffee.erb index e86707d69..fb2cd40fb 100644 --- a/app/assets/javascripts/controllers/admin/statistics.coffee.erb +++ b/app/assets/javascripts/controllers/admin/statistics.coffee.erb @@ -373,6 +373,7 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state", "type": index "size": RESULTS_PER_PAGE "scroll": ES_SCROLL_TIME+'m' + "stat-type": type "body": buildElasticDataQuery(type, custom, $scope.agePicker.start, $scope.agePicker.end, moment($scope.datePickerStart.selected), moment($scope.datePickerEnd.selected), $scope.sorting) , (error, response) -> if (error) diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index 3368308b7..7a4ef5dc3 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -11,8 +11,17 @@ class API::StatisticsController < API::ApiController def #{path} authorize :statistic, :#{path}? query = MultiJson.load(request.body.read) + statistic_type = request.query_parameters.delete('stat-type') results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response - # TODO + + stat_index = StatisticIndex.find_by_es_type_key("#{path}") + stat_type = StatisticType.where(statistic_index_id: stat_index.id, key: statistic_type).first + client = Elasticsearch::Model.client + stat_type.statistic_custom_aggregations.each do |custom| + c_res = client.search index: custom.es_index, type:custom.es_type, q:custom.query + results['aggregations'][custom.field] = c_res + end + render json: results end diff --git a/db/migrate/20160906094739_remove_path_from_statistic_custom_aggregations.rb b/db/migrate/20160906094739_remove_path_from_statistic_custom_aggregations.rb new file mode 100644 index 000000000..f58d20a1c --- /dev/null +++ b/db/migrate/20160906094739_remove_path_from_statistic_custom_aggregations.rb @@ -0,0 +1,5 @@ +class RemovePathFromStatisticCustomAggregations < ActiveRecord::Migration + def change + remove_column :statistic_custom_aggregations, :path, :string + end +end diff --git a/db/migrate/20160906094847_add_es_index_es_type_to_statistic_custom_aggregations.rb b/db/migrate/20160906094847_add_es_index_es_type_to_statistic_custom_aggregations.rb new file mode 100644 index 000000000..894325854 --- /dev/null +++ b/db/migrate/20160906094847_add_es_index_es_type_to_statistic_custom_aggregations.rb @@ -0,0 +1,6 @@ +class AddEsIndexEsTypeToStatisticCustomAggregations < ActiveRecord::Migration + def change + add_column :statistic_custom_aggregations, :es_index, :string + add_column :statistic_custom_aggregations, :es_type, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index fcc573fa0..04fed69b0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160905142700) do +ActiveRecord::Schema.define(version: 20160906094847) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -543,11 +543,12 @@ ActiveRecord::Schema.define(version: 20160905142700) do create_table "statistic_custom_aggregations", force: :cascade do |t| t.text "query" - t.string "path" t.integer "statistic_type_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "field" + t.string "es_index" + t.string "es_type" end add_index "statistic_custom_aggregations", ["statistic_type_id"], name: "index_statistic_custom_aggregations_on_statistic_type_id", using: :btree diff --git a/test/fixtures/statistic_custom_aggregations.yml b/test/fixtures/statistic_custom_aggregations.yml index 40debb04d..7e4362f0d 100644 --- a/test/fixtures/statistic_custom_aggregations.yml +++ b/test/fixtures/statistic_custom_aggregations.yml @@ -2,6 +2,7 @@ one: query: '{query:{bool:{must:[range:{start_at:{gte:"2016-08-01T02:00:00+02:00", lte:"2016-09-01T02:00:00+02:00"}, match:{available_type: "machines"}}]}}}' - path: "/fablab/availabilities" statistic_type_id: 2 field: "available_hours" + es_index: "fablab" + es_type: "availabilities"