Class: ClarkKent::Report
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ClarkKent::Report
show all
- Includes:
- Cloneable, ForeignOffice::Broadcaster
- Defined in:
- app/models/clark_kent/report.rb
Constant Summary
collapse
- SortDirections =
{'ascending' => 'asc', 'descending' => 'desc'}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Cloneable
#cloneable_attributes, #reset_timestamps
Instance Attribute Details
#browser_tab_id ⇒ Object
Returns the value of attribute browser_tab_id.
14
15
16
|
# File 'app/models/clark_kent/report.rb', line 14
def browser_tab_id
@browser_tab_id
end
|
#report_result_url ⇒ Object
Returns the value of attribute report_result_url.
14
15
16
|
# File 'app/models/clark_kent/report.rb', line 14
def report_result_url
@report_result_url
end
|
#summary_row_storage ⇒ Object
Returns the value of attribute summary_row_storage.
14
15
16
|
# File 'app/models/clark_kent/report.rb', line 14
def summary_row_storage
@summary_row_storage
end
|
Class Method Details
.send_report_to_s3(report_id, params) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'app/models/clark_kent/report.rb', line 28
def self.send_report_to_s3(report_id, params)
report_class = params[:report_class].constantize if params[:report_class]
report_class ||= ::ClarkKent::Report
reportable = report_class.find(report_id)
is_email = 'ClarkKent::ReportEmail' == report_class.name
report = is_email ? reportable.report : reportable
query = reportable.get_query(params)
row_count = reportable.get_query(params, true)
bucket = AWS::S3::Bucket.new(ClarkKent.bucket_name)
report_destination = bucket.objects[params[:report_result_name]]
byte_count = 0
temp_buffer = report..to_csv
offset = 0
summary_rows = []
some_left = true
report_destination.write(estimated_content_length: row_count * 1000) do |buffer, bytes|
while temp_buffer.length < bytes and some_left
this_batch = query.offset(offset).limit(100)
some_left = this_batch.each do |row|
temp_buffer << report.report_columns.map{ |column| row[column.column_name] }.to_csv
end.any?
summary_rows.push report.summary_row(this_batch) if report.summary_row? and some_left
offset += 100
end
if report.summary_row_storage.blank? and (!some_left)
if report.summary_row? and summary_rows.any?
summary_row_map = report.summary_row(summary_rows)
report.summary_row_storage = report.report_columns.map do |col|
summary_row_map.send col.column_name
end
temp_buffer << report.summary_row_storage.to_csv
end
end
if temp_buffer.present?
chunk = temp_buffer.slice!(0...bytes)
buffer << chunk
end
end
report.report_result_url = report_destination.url_for(
:read,
secure: true,
response_content_type: 'text/csv',
response_content_encoding: 'binary',
response_content_disposition: 'attachment',
expires: 60*60*24*30 )
if 'ClarkKent::Report' == report_class.name
report.browser_tab_id = params[:browser_tab_id]
report.broadcast_change
end
if is_email && 'login_required' == ClarkKent.email_security
params[:report_result_name]
else
report.report_result_url.to_s
end
end
|
Instance Method Details
#arel_includes ⇒ Object
135
136
137
138
139
140
|
# File 'app/models/clark_kent/report.rb', line 135
def arel_includes
viable_report_columns.map{|column|
column_info = self.column_options_for(column.column_name)
column_info.includes
}.compact
end
|
#arel_joins ⇒ Object
142
143
144
145
146
147
|
# File 'app/models/clark_kent/report.rb', line 142
def arel_joins
viable_report_columns.map{|column|
column_info = self.column_options_for(column.column_name)
column_info.joins
}.compact
end
|
#available_columns ⇒ Object
This is the set of columns not chosed to use in the report. These are the ones available to add when updating a report.
246
247
248
|
# File 'app/models/clark_kent/report.rb', line 246
def available_columns
column_options.reject{|column| viable_report_columns.map(&:column_name).include? column.name.to_s}
end
|
#available_email_filters ⇒ Object
This is the full set of filter options for defining a report, including the date filters for an automatic, timed, emailed report.
230
231
232
|
# File 'app/models/clark_kent/report.rb', line 230
def available_email_filters
self.resource_class.report_definition_options.reject{|name| (viable_report_filters.map(&:filter_name)).include? name}
end
|
#available_filter_options ⇒ Object
224
225
226
|
# File 'app/models/clark_kent/report.rb', line 224
def available_filter_options
self.available_filters.map{|filter_name| [self.filter_options_for(filter_name).label,filter_name]}
end
|
#available_filters ⇒ Object
These are the filters available for defining a report for this resource. They do not include date filters as those only make sense at runtime, or in an auto-generated, timed emailed report.
220
221
222
|
# File 'app/models/clark_kent/report.rb', line 220
def available_filters
self.available_email_filters.reject{|name| self.date_filter_names.include? name}
end
|
#collection_for(filter_name) ⇒ Object
234
235
236
|
# File 'app/models/clark_kent/report.rb', line 234
def collection_for(filter_name)
self.filter_options_for(filter_name).collection
end
|
#column_options ⇒ Object
195
196
197
|
# File 'app/models/clark_kent/report.rb', line 195
def column_options
@column_options ||= self.resource_class.report_column_options
end
|
#column_options_for(column_name) ⇒ Object
199
200
201
202
203
204
205
206
207
208
|
# File 'app/models/clark_kent/report.rb', line 199
def column_options_for(column_name)
if column_options.any?{|co| co.name == column_name.to_sym}
column_options.detect{|co| co.name == column_name.to_sym}
else
column_name = column_name.to_s.split('_')[0..-2].join('_')
if column_options.any?{|co| co.name == column_name.to_sym}
column_options.detect{|co| co.name == column_name.to_sym}
end
end
end
|
#custom_filters ⇒ Object
These are the filters available at runtime, ie. not including the ones set to define this report. If updating the report, this is the set available to add as new report definition filters.
240
241
242
|
# File 'app/models/clark_kent/report.rb', line 240
def custom_filters
self.resource_class.report_filter_options.select{|filter| viable_report_filters.map(&:filter_name).exclude? filter.param}
end
|
#date_filter_names ⇒ Object
214
215
216
|
# File 'app/models/clark_kent/report.rb', line 214
def date_filter_names
self.resource_class.report_filter_options.select{|filter| 'date_filter' == filter.kind}.map{|filter| filter.param}
end
|
#deep_clone ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'app/models/clark_kent/report.rb', line 299
def deep_clone
Report.transaction do
new_report = dup.reset_timestamps
new_report.name << " CLONED: #{Date.today.to_s(:db)}"
new_report.save!
report_filters.each do |report_filter|
new_report.report_filters << report_filter.dup.reset_timestamps
end
report_columns.each do |report_column|
new_report.report_columns << report_column.dup.reset_timestamps
end
report_emails.each do |report_email|
new_report_email = report_email.dup.reset_timestamps
new_report.report_emails << new_report_email
report_email.report_filters.each do |report_filter|
new_report_email.report_filters << report_filter.dup.reset_timestamps
end
report_email.user_report_emails.each do |user_report_email|
new_report_email.user_report_emails << user_report_email.dup.reset_timestamps
end
end
new_report
end
end
|
156
157
158
159
160
161
|
# File 'app/models/clark_kent/report.rb', line 156
def
viable_report_columns.map{|column|
column_info = self.column_options_for(column.column_name)
column_info.where
}.flatten.compact
end
|
149
150
151
152
153
154
|
# File 'app/models/clark_kent/report.rb', line 149
def
viable_report_columns.map{|column|
column_info = self.column_options_for(column.column_name)
column_info.
}.flatten.compact
end
|
#filter_kind(filter_name) ⇒ Object
210
211
212
|
# File 'app/models/clark_kent/report.rb', line 210
def filter_kind(filter_name)
self.filter_options_for(filter_name).kind
end
|
#filter_options_for(filter_name) ⇒ Object
191
192
193
|
# File 'app/models/clark_kent/report.rb', line 191
def filter_options_for(filter_name)
self.resource_class.report_filter_options.detect{|filter| filter.param == filter_name}
end
|
#get_filter_class(params) ⇒ Object
267
268
269
270
|
# File 'app/models/clark_kent/report.rb', line 267
def get_filter_class(params)
filter_option = self.resource_class.report_filter_options.detect{|filter| filter.param == params[:filter_name]}
"ClarkKent::Report#{filter_option.kind.camelcase}".constantize
end
|
#get_query(params, count = false) ⇒ Object
94
95
96
|
# File 'app/models/clark_kent/report.rb', line 94
def get_query(params, count = false)
self.resource_class.report(params,self, count)
end
|
#groups ⇒ Object
163
164
165
166
167
168
|
# File 'app/models/clark_kent/report.rb', line 163
def groups
viable_report_columns.map{|column|
column_info = self.column_options_for(column.column_name)
column_info.group
}.flatten.compact
end
|
289
290
291
292
293
294
295
296
297
|
# File 'app/models/clark_kent/report.rb', line 289
def
= viable_report_columns.sort_by{|c| c.column_order || 100}.map(&:column_name)
unless name =~ /net\s?promoter/i
.map(&:humanize)
else
end
end
|
#report_filter_params ⇒ Object
These are the built-in filter params that define this report. They are merged at a later step with the runtime params entered by the user for a specific report run. nb. the sorter column here may be overridden by a runtime sort if requested by the user.
173
174
175
176
|
# File 'app/models/clark_kent/report.rb', line 173
def report_filter_params
Hash[*viable_report_filters.map{|filter| filter.filter_match_params}.flatten].
merge(order: self.sorter)
end
|
#resource_class ⇒ Object
98
99
100
|
# File 'app/models/clark_kent/report.rb', line 98
def resource_class
@resource_class ||= self.resource_type.constantize
end
|
#resource_type_pretty ⇒ Object
259
260
261
262
263
264
265
|
# File 'app/models/clark_kent/report.rb', line 259
def resource_type_pretty
if self.resource_class.respond_to? :prettify_name
self.resource_class.prettify_name.pluralize
else
self.resource_class.name.humanize
end
end
|
#row_class ⇒ Object
This ephemeral class allows us to create a row object that has the same attributes as the AR response to the query, including all the custom columns defined in the resource class report config. currently only used for the summary row, since we can’t get that in the same AR query and have to add it to the collection after the query returns.
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'app/models/clark_kent/report.rb', line 106
def row_class
report_columns = viable_report_columns
@row_class ||= Class.new do
report_columns.each do |report_column|
attr_accessor report_column.column_name.to_sym
end
def initialize params = {}
params.each { |key, value| send "#{key}=", value }
end
def [](key)
self.send key
end
end
end
|
#select_clauses ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'app/models/clark_kent/report.rb', line 178
def select_clauses
@selects = []
viable_report_columns.each do |report_column|
column_option = self.column_options_for(report_column.column_name)
@selects.push column_option.custom_select if column_option.present? && column_option.custom_select.present?
end
viable_report_filters.each do |report_filter|
column_option = self.column_options_for(report_filter.filter_name.to_sym)
@selects.push column_option.custom_select if column_option.present? && column_option.custom_select.present?
end
@selects
end
|
#serialize ⇒ Object
330
331
332
333
334
|
# File 'app/models/clark_kent/report.rb', line 330
def serialize
{ report_result_url: self.report_result_url,
browser_tab_id: self.browser_tab_id
}
end
|
#sharing_scope_pretty ⇒ Object
255
256
257
|
# File 'app/models/clark_kent/report.rb', line 255
def sharing_scope_pretty
(self.sharing_scope.try :name ) || 'Everyone'
end
|
#sort_column ⇒ Object
123
124
125
|
# File 'app/models/clark_kent/report.rb', line 123
def sort_column
@sort_column ||= viable_report_columns.detect{|rc| rc.report_sort.present?}
end
|
#sortable?(column) ⇒ Boolean
250
251
252
253
|
# File 'app/models/clark_kent/report.rb', line 250
def sortable?(column)
column_info = self.column_options_for(column.column_name)
column_info.present? && column_info.order_sql.present?
end
|
#sorter ⇒ Object
127
128
129
130
131
132
133
|
# File 'app/models/clark_kent/report.rb', line 127
def sorter
if self.sort_column
sort_column_name = self.sort_column.column_name
sort_direction = Report::SortDirections[sort_column.report_sort]
ReportSort.new(order_column: sort_column_name, order_direction: sort_direction)
end
end
|
#summary_row(rows) ⇒ Object
278
279
280
281
282
283
|
# File 'app/models/clark_kent/report.rb', line 278
def summary_row(rows)
row_array = viable_report_columns.map do |report_column|
[report_column.column_name,report_column.calculate_summary(rows.map{|row| row.send(report_column.column_name)})]
end
row_class.new(row_array.to_h)
end
|
#summary_row? ⇒ Boolean
285
286
287
|
# File 'app/models/clark_kent/report.rb', line 285
def summary_row?
@summary_row_presence ||= viable_report_columns.any?{|c| c.summary_method.present? }
end
|
#summary_row_values(rows) ⇒ Object
272
273
274
275
276
|
# File 'app/models/clark_kent/report.rb', line 272
def summary_row_values(rows)
viable_report_columns.each_with_index.map do |report_column,index|
report_column.calculate_summary(rows.map{|row| row[index]})
end
end
|
#viable_report_columns ⇒ Object
86
87
88
|
# File 'app/models/clark_kent/report.rb', line 86
def viable_report_columns
@viable_report_columns ||= report_columns.to_a.select{|rc| column_options_for(rc.column_name).present? }
end
|
#viable_report_filters ⇒ Object
90
91
92
|
# File 'app/models/clark_kent/report.rb', line 90
def viable_report_filters
@viable_report_filters ||= report_filters.to_a.select{|rf| filter_options_for(rf.filter_name).present? }
end
|