1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

ability to run custom aggregation with query and append result to query

This commit is contained in:
Sylvain 2016-09-06 14:21:52 +02:00
parent ea671d03a4
commit 1fec50accf
6 changed files with 27 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1,5 @@
class RemovePathFromStatisticCustomAggregations < ActiveRecord::Migration
def change
remove_column :statistic_custom_aggregations, :path, :string
end
end

View File

@ -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

View File

@ -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

View File

@ -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"