2019-07-29 17:51:53 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-27 11:28:54 +02:00
|
|
|
require 'abstract_controller'
|
|
|
|
require 'action_controller'
|
|
|
|
require 'action_view'
|
|
|
|
require 'active_record'
|
|
|
|
|
|
|
|
# require any helpers
|
2022-12-01 13:03:19 +01:00
|
|
|
require './app/helpers/excel_helper'
|
2016-07-27 11:28:54 +02:00
|
|
|
|
2022-12-01 13:03:19 +01:00
|
|
|
# Export statistics (from elasticsearch) to an excel file
|
2016-07-27 11:28:54 +02:00
|
|
|
class StatisticsExportService
|
|
|
|
def export_global(export)
|
|
|
|
# query all stats with range arguments
|
|
|
|
query = MultiJson.load(export.query)
|
|
|
|
|
2019-07-29 17:51:53 +02:00
|
|
|
@results = Elasticsearch::Model.client.search(index: 'stats', scroll: '30s', body: query)
|
2016-07-27 11:28:54 +02:00
|
|
|
scroll_id = @results['_scroll_id']
|
|
|
|
while @results['hits']['hits'].size != @results['hits']['total']
|
|
|
|
scroll_res = Elasticsearch::Model.client.scroll(scroll: '30s', scroll_id: scroll_id)
|
|
|
|
@results['hits']['hits'].concat(scroll_res['hits']['hits'])
|
|
|
|
scroll_id = scroll_res['_scroll_id']
|
|
|
|
end
|
|
|
|
|
|
|
|
ids = @results['hits']['hits'].map { |u| u['_source']['userId'] }
|
2019-07-29 17:51:53 +02:00
|
|
|
@users = User.includes(:profile).where(id: ids)
|
2016-07-27 11:28:54 +02:00
|
|
|
|
2019-07-29 17:51:53 +02:00
|
|
|
@indices = StatisticIndex.all.includes(:statistic_fields, statistic_types: [:statistic_sub_types])
|
2016-07-27 11:28:54 +02:00
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
content = ApplicationController.render(
|
|
|
|
template: 'exports/statistics_global',
|
|
|
|
locals: { results: @results, users: @users, indices: @indices },
|
|
|
|
handlers: [:axlsx],
|
|
|
|
formats: [:xlsx]
|
|
|
|
)
|
2016-07-27 11:28:54 +02:00
|
|
|
# write content to file
|
2022-12-01 13:03:19 +01:00
|
|
|
File.binwrite(export.file, content)
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
|
|
|
|
2022-12-01 13:03:19 +01:00
|
|
|
# rubocop:disable Style/DocumentDynamicEvalDefinition
|
2023-02-20 15:37:51 +01:00
|
|
|
%w[account event machine project subscription training space order].each do |path|
|
2016-07-27 11:28:54 +02:00
|
|
|
class_eval %{
|
|
|
|
def export_#{path}(export)
|
|
|
|
|
|
|
|
query = MultiJson.load(export.query)
|
|
|
|
type_key = export.key
|
|
|
|
|
|
|
|
@results = Elasticsearch::Model.client.search({index: 'stats', type: '#{path}', scroll: '30s', body: query})
|
|
|
|
scroll_id = @results['_scroll_id']
|
|
|
|
while @results['hits']['hits'].size != @results['hits']['total']
|
|
|
|
scroll_res = Elasticsearch::Model.client.scroll(scroll: '30s', scroll_id: scroll_id)
|
|
|
|
@results['hits']['hits'].concat(scroll_res['hits']['hits'])
|
|
|
|
scroll_id = scroll_res['_scroll_id']
|
|
|
|
end
|
|
|
|
|
|
|
|
ids = @results['hits']['hits'].map { |u| u['_source']['userId'] }
|
|
|
|
@users = User.includes(:profile).where(:id => ids)
|
|
|
|
|
|
|
|
@index = StatisticIndex.find_by(es_type_key: '#{path}')
|
|
|
|
@type = StatisticType.find_by(key: type_key, statistic_index_id: @index.id)
|
|
|
|
@subtypes = @type.statistic_sub_types
|
|
|
|
@fields = @index.statistic_fields
|
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
content = ApplicationController.render(
|
|
|
|
template: 'exports/statistics_current',
|
|
|
|
locals: { results: @results, users: @users, index: @index, type: @type, subtypes: @subtypes, fields: @fields },
|
|
|
|
handlers: [:axlsx],
|
|
|
|
formats: [:xlsx]
|
|
|
|
)
|
2016-07-27 11:28:54 +02:00
|
|
|
# write content to file
|
2022-12-01 13:03:19 +01:00
|
|
|
File.binwrite(export.file, content)
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
2023-02-24 17:26:55 +01:00
|
|
|
}, __FILE__, __LINE__ - 31
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
2022-12-01 13:03:19 +01:00
|
|
|
# rubocop:enable Style/DocumentDynamicEvalDefinition
|
2019-07-29 17:51:53 +02:00
|
|
|
end
|