mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
working custom aggs w/ result tidy in stats query
This commit is contained in:
parent
1fec50accf
commit
53dd54b175
@ -374,6 +374,8 @@ Application.Controllers.controller "StatisticsController", ["$scope", "$state",
|
||||
"size": RESULTS_PER_PAGE
|
||||
"scroll": ES_SCROLL_TIME+'m'
|
||||
"stat-type": type
|
||||
"start-date": moment($scope.datePickerStart.selected).format()
|
||||
"end-date": moment($scope.datePickerEnd.selected).format()
|
||||
"body": buildElasticDataQuery(type, custom, $scope.agePicker.start, $scope.agePicker.end, moment($scope.datePickerStart.selected), moment($scope.datePickerEnd.selected), $scope.sorting)
|
||||
, (error, response) ->
|
||||
if (error)
|
||||
|
@ -10,18 +10,27 @@ class API::StatisticsController < API::ApiController
|
||||
class_eval %{
|
||||
def #{path}
|
||||
authorize :statistic, :#{path}?
|
||||
query = MultiJson.load(request.body.read)
|
||||
|
||||
# remove additional parameters
|
||||
statistic_type = request.query_parameters.delete('stat-type')
|
||||
start_date = request.query_parameters.delete('start-date')
|
||||
end_date = request.query_parameters.delete('end-date')
|
||||
puts start_date, end_date
|
||||
|
||||
# run main query in elasticSearch
|
||||
query = MultiJson.load(request.body.read)
|
||||
results = Stats::#{path.classify}.search(query, request.query_parameters.symbolize_keys).response
|
||||
|
||||
# run additional custom aggregations, if any
|
||||
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
|
||||
c_res = client.search index: custom.es_index, type:custom.es_type, body:sprintf(custom.query, {aggs_name: custom.field, start_date: start_date, end_date: end_date})
|
||||
results['aggregations'][custom.field] = c_res['aggregations'][custom.field]
|
||||
end
|
||||
|
||||
# return result
|
||||
render json: results
|
||||
end
|
||||
|
||||
|
@ -93,6 +93,13 @@ class Availability < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def as_indexed_json
|
||||
json = JSON.parse(to_json)
|
||||
json['hours_duration'] = (end_at - start_at) / (60*60)
|
||||
json.to_json
|
||||
end
|
||||
|
||||
private
|
||||
def length_must_be_1h_minimum
|
||||
if end_at < (start_at + 1.hour)
|
||||
|
@ -11,7 +11,7 @@ class AvailabilityIndexerWorker
|
||||
case operation.to_s
|
||||
when /index/
|
||||
record = Availability.find(record_id)
|
||||
Client.index index: Availability.index_name, type: Availability.document_type, id: record.id, body: record.to_json
|
||||
Client.index index: Availability.index_name, type: Availability.document_type, id: record.id, body: record.as_indexed_json
|
||||
#puts record.as_indexed_json
|
||||
when /delete/
|
||||
Client.delete index: Availability.index_name, type: Availability.document_type, id: record_id
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
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"}}]}}}'
|
||||
query: '{"size":0, "aggregations":{"%{aggs_name}":{"sum":{"field":"hours_duration"}}}, "query":{"bool":{"must":[{"range":{"start_at":{"gte":"%{start_date}", "lte":"%{end_date}"}}}, {"match":{"available_type":"machines"}}]}}}'
|
||||
statistic_type_id: 2
|
||||
field: "available_hours"
|
||||
es_index: "fablab"
|
||||
|
Loading…
Reference in New Issue
Block a user