-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy patheditor.rb
1825 lines (1664 loc) · 80.2 KB
/
editor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module ReportController::Reports::Editor
extend ActiveSupport::Concern
included do
helper_method :cashed_reporting_available_fields, :cashed_reporting_available_fields
helper_method :chargeback_allocated_methods, :chargeback_allocated_methods
end
DEFAULT_PDF_PAGE_SIZE = "US-Letter".freeze
CHARGEBACK_ALLOWED_FIELD_SUFFIXES = %w(
_rate
_cost
-owner_name
_metric
-provider_name
-provider_uid
-project_uid
-archived
-chargeback_rates
-vm_guid
-vm_uid
).freeze
METERING_VM_ALLOWED_FIELD_SUFFIXES = %w(
-beginning_of_resource_existence_in_report_interval
-end_of_resource_existence_in_report_interval
).freeze
MAX_REPORT_COLUMNS = 100 # Default maximum number of columns in a report
GRAPH_MAX_COUNT = 10
CHAREGEBACK_ALLOCATED_METHODS = {
:max => N_('Maximum'),
:avg => N_('Average')
}.freeze
def chargeback_allocated_methods
Hash[CHAREGEBACK_ALLOCATED_METHODS.map { |x| _(x) }]
end
def default_chargeback_allocated_method
chargeback_allocated_methods.keys.first
end
def miq_report_new
assert_privileges("miq_report_new")
@_params.delete :id # incase add button was pressed from report show screen.
miq_report_edit
end
def miq_report_copy
assert_privileges("miq_report_copy")
@report = nil # Clear any saved report object
if params[:tab] # Came in to change the tab
check_tabs
else
@sb[:miq_tab] = "edit_1"
@rpt = MiqReport.for_user(current_user).find(params[:id])
@rpt.id = nil # Treat as a new report
set_form_vars
end
build_edit_screen
replace_right_cell
end
def miq_report_edit
assert_privileges("miq_report_edit")
case params[:button]
when "cancel"
@edit[:rpt_id] ?
add_flash(_("Edit of Report \"%{name}\" was cancelled by the user") % {:name => @edit[:rpt_title]}) :
add_flash(_("Add of new Report was cancelled by the user"))
@edit = session[:edit] = nil # clean out the saved info
replace_right_cell
when "add", "save"
id = params[:id] ? params[:id] : "new"
return unless load_edit("report_edit__#{id}", "replace_cell__explorer")
get_form_vars
@changed = (@edit[:new] != @edit[:current])
@rpt = @edit[:rpt_id] ? find_record_with_rbac(MiqReport, params[:id]) :
MiqReport.new
set_record_vars(@rpt)
unless valid_report?(@rpt)
build_edit_screen
replace_right_cell
return
end
if @edit[:new][:graph_type] && (@edit[:new][:sortby1].blank? || @edit[:new][:sortby1] == ReportHelper::NOTHING_STRING)
add_flash(_("Report can not be saved unless sort field has been configured for Charts"), :error)
@sb[:miq_tab] = "edit_4"
build_edit_screen
replace_right_cell
return
end
if @rpt.save
# update report name in menu if name is edited
menu_repname_update(@edit[:current][:name], @edit[:new][:name]) if @edit[:current][:name] != @edit[:new][:name]
AuditEvent.success(build_saved_audit(@rpt, @edit))
@edit[:rpt_id] ?
add_flash(_("Report \"%{name}\" was saved") % {:name => @rpt.name}) :
add_flash(_("Report \"%{name}\" was added") % {:name => @rpt.name})
# only do this for new reports
unless @edit[:rpt_id]
self.x_node = "xx-#{@sb[:rpt_menu].length}_xx-#{@sb[:rpt_menu].length}-0"
setnode_for_customreport
end
@edit = session[:edit] = nil # clean out the saved info
if role_allows?(:feature => "miq_report_widget_editor")
# all widgets for this report
get_all_widgets("report", x_node.split('_').last)
end
replace_right_cell(:replace_trees => [:reports])
else
rpt.errors.each do |field, msg|
add_flash("#{field.to_s.capitalize} #{msg}", :error)
end
@in_a_form = true
session[:changed] = !!@changed
@changed = true
replace_right_cell
end
else
add_flash(_("All changes have been reset"), :warning) if params[:button] == "reset"
@in_a_form = true
@report = nil # Clear any saved report object
if params[:tab] # Came in to change the tab
@rpt = @edit[:rpt_id] ? MiqReport.for_user(current_user).find(@edit[:rpt_id]) :
MiqReport.new
check_tabs
else
@sb[:miq_tab] = "edit_1"
@rpt = params[:id] && params[:id] != "new" ? MiqReport.for_user(current_user).find(params[:id]) :
MiqReport.new
if @rpt.rpt_type == "Default"
flash_to_session(_('Default reports can not be edited'), :error)
redirect_to(:action => "show", :id => @rpt.id)
return
end
set_form_vars
end
build_edit_screen
@changed = (@edit[:new] != @edit[:current])
session[:changed] = @changed
replace_right_cell
end
end
# AJAX driven routine to check for changes in ANY field on the form
def form_field_changed
return unless load_edit("report_edit__#{params[:id]}", "replace_cell__explorer")
get_form_vars
build_edit_screen
@changed = (@edit[:new] != @edit[:current])
render :update do |page|
page << javascript_prologue
page.replace("flash_msg_div", :partial => "layouts/flash_msg") unless @refresh_div && @refresh_div != "column_lists"
page.replace(@refresh_div, :partial => @refresh_partial) if @refresh_div
page.replace("chart_sample_div", :partial => "form_chart_sample") if @refresh_div == "chart_div"
page.replace("tl_sample_div", :partial => "form_tl_sample") if @refresh_div == "tl_settings_div"
page.replace_html("calc_#{@calc_div}_div", :text => @calc_val) if @calc_div
page << "miqSparkle(false);"
page << javascript_for_miq_button_visibility_changed(@changed)
if @tl_changed # Reload the screen if the timeline data was changed
page.replace_html("tl_sample_div", :partial => "form_tl_sample") if @tl_field != ReportHelper::NOTHING_STRING
elsif @formatting_changed # Reload the screen if the formatting pulldowns need to be reset
page.replace_html("formatting_div", :partial => "form_formatting")
elsif @tl_repaint
# page << "tl.paint();"
page << javascript_hide("notification")
end
end
end
def filter_change
return unless load_edit("report_edit__#{params[:id]}", "replace_cell__explorer")
@expkey = $&.to_sym if params[:button].to_s =~ /^(record|display)_filter$/
render :update do |page|
page << javascript_prologue
page.replace("filter_div", :partial => "form_filter")
page << "miqSparkle(false);"
end
end
def display_filter_details_for(target_class, cols)
cols ? cols.select { |_, column| target_class.parse(column).try(:plural?) } : []
end
private
def build_edit_screen
build_tabs
get_time_profiles # Get time profiles list (global and user specific)
cb_entities_by_provider if Chargeback.db_is_chargeback?(@edit[:new][:model]) && [ChargebackContainerImage, ChargebackContainerProject, MeteringContainerImage, MeteringContainerProject].include?(@edit[:new][:model].safe_constantize)
refresh_chargeback_filter_tab if Chargeback.db_is_chargeback?(@edit[:new][:model])
case @sb[:miq_tab].split("_")[1]
when "1" # Select columns
@edit[:models] ||= reportable_models
# Add the blank choice if no table chosen yet
# @edit[:models].insert(0,["<Choose>", "<Choose>"]) if @edit[:new][:model] == nil && @edit[:models][0][0] != "<Choose>"
if @edit[:new][:model].nil?
if @edit[:models][0][0] != "<Choose>"
@edit[:models].insert(0, ["<Choose>", "<Choose>"])
end
else
if @edit[:models][0][0] == "<Choose>"
@edit[:models].delete_at(0)
end
end
when "8" # Consolidate
# Build group chooser arrays
@edit[:new][:pivot].options = @edit[:new][:fields].dup
@pivot = @edit[:new][:pivot]
when "2" # Formatting
# @edit[:calc_xml] = build_calc_combo_xml # Get the combobox XML for any numeric fields
when "3" # Filter
# Build record filter expression
if @edit[:miq_exp] || # Is this stored as an MiqExp object
["new", "copy", "create"].include?(request.parameters["action"]) # or it's a new condition
new_record_filter = @edit[:new][:record_filter]
@edit[:record_filter][:expression] = copy_hash(new_record_filter) unless new_record_filter.blank?
@expkey = :record_filter
# Initialize the exp array
@edit[@expkey].history.reset(@edit[:record_filter][:expression]) if @edit[:record_filter].history.array.nil?
@edit[:record_filter][:exp_table] = exp_build_table(@edit[:record_filter][:expression])
@edit[:record_filter].prefill_val_types
@edit[:record_filter][:exp_model] = @edit[:new][:model] # Set the model for the expression editor
end
new_display_filter = @edit[:new][:display_filter]
@edit[:display_filter][:expression] = copy_hash(new_display_filter) unless new_display_filter.blank?
@expkey = :display_filter
# Initialize the exp array
@edit[@expkey].history.reset(@edit[:display_filter][:expression]) if @edit[:display_filter].history.array.nil?
@edit[:display_filter][:exp_table] = exp_build_table(@edit[:display_filter][:expression])
cols = @edit[:new][:field_order]
@edit[:display_filter][:exp_available_fields] = display_filter_details_for(MiqExpression::Field, cols)
cols = @edit[:new][:fields]
@edit[:display_filter][:exp_available_tags] = display_filter_details_for(MiqExpression::Tag, cols)
@edit[:display_filter][:exp_model] = "_display_filter_" # Set model for display filter
@expkey = :record_filter # Start with Record Filter showing
if @edit[:new][:perf_interval] && !@edit[:new][:time_profile]
set_time_profile_vars(selected_time_profile_for_pull_down, @edit[:new])
end
when "4" # Summarize
# Build sort chooser arrays(@edit[:new][:fields], :field)
@sortby1 = @edit[:new][:sortby1]
@sortby2 = @edit[:new][:sortby2]
@sort1 = @edit[:new][:field_order].dup
@sort2 = @sort1.dup.delete_if { |s| s[1] == @sortby1.split("__").first }
when "5" # Charts
options = chart_fields_options
if options.empty?
@edit[:new][:chart_column] = nil
else
@edit[:new][:chart_column] = options[0][1] unless options.detect { |_, v| v == @edit[:new][:chart_column] }
end
when "6" # Timeline
@tl_fields = []
@edit[:new][:fields].each do |field|
if MiqReport.get_col_type(field[1]) == :datetime
@tl_fields.push(field)
end
end
@tl_field = @edit[:new][:tl_field]
if @edit[:new][:tl_position] == "Last"
@position_time = format_timezone(Time.now, "UTC", nil)
else
@position_time = format_timezone(Time.now - 1.year, "UTC", nil)
end
@timeline = true if @tl_field != ReportHelper::NOTHING_STRING
@tl_json = sample_timeline
when "7" # Preview
# generate preview report when
end
@in_a_form = true
if ["new", "copy", "create"].include?(request.parameters["action"])
# drop_breadcrumb( {:name=>"Add Report", :url=>"/report/new"} )
@gtl_url = "/new"
else
# drop_breadcrumb( {:name=>"Edit Report", :url=>"/report/edit"} )
@gtl_url = "/edit"
end
end
def reportable_models
MiqReport.reportable_models.collect do |m|
[Dictionary.gettext(m, :type => :model, :notfound => :titleize, :plural => true), m]
end
end
def ensure_perf_interval_defaults
case @edit[:new][:perf_interval]
when "hourly"
@edit[:new][:perf_end] ||= "0"
@edit[:new][:perf_start] ||= 1.day.to_s
when "daily"
@edit[:new][:perf_end] ||= "0"
@edit[:new][:perf_start] ||= 2.days.to_s
end
end
# Reset report column fields if model or interval was changed
def reset_report_col_fields
@edit[:new][:fields] = [] # Clear fields array
@edit[:new][:headers] = {} # Clear headers hash
@edit[:new][:pivot] = ReportController::PivotOptions.new
@edit[:new][:sortby1] = ReportHelper::NOTHING_STRING # Clear sort fields
@edit[:new][:sortby2] = ReportHelper::NOTHING_STRING
@edit[:new][:filter_operator] = nil
@edit[:new][:filter_string] = nil
@edit[:new][:categories] = []
@edit[:new][:graph_type] = nil # Clear graph field
@edit[:new][:chart_mode] = nil
@edit[:new][:chart_column] = nil
@edit[:new][:perf_trend_col] = nil
@edit[:new][:perf_trend_db] = nil
@edit[:new][:perf_trend_pct1] = nil
@edit[:new][:perf_trend_pct2] = nil
@edit[:new][:perf_trend_pct3] = nil
@edit[:new][:perf_limit_col] = nil
@edit[:new][:perf_limit_val] = nil
@edit[:new][:record_filter] = nil # Clear record filter
@edit[:new][:display_filter] = nil # Clear display filter
@edit[:miq_exp] = true
end
def build_tabs
req = "edit"
if @edit[:new][:model] == ApplicationController::TREND_MODEL
@tabs = [
["#{req}_1", _("Columns")],
["#{req}_3", _("Filter")],
["#{req}_7", _("Preview")]
]
elsif Chargeback.db_is_chargeback?(@edit[:new][:model].to_s)
@tabs = [
["#{req}_1", _("Columns")],
["#{req}_2", _("Formatting")],
["#{req}_3", _("Filter")],
["#{req}_7", _("Preview")]
]
else
@tabs = [
["#{req}_1", _("Columns")],
["#{req}_8", _("Consolidation")],
["#{req}_2", _("Formatting")],
["#{req}_9", _("Styling")],
["#{req}_3", _("Filter")],
["#{req}_4", _("Summary")],
["#{req}_5", _("Charts")],
["#{req}_6", _("Timeline")],
["#{req}_7", _("Preview")]
]
end
tab = @sb[:miq_tab].split("_")[1] # Get the tab number of the active tab
@active_tab = "#{req}_#{tab}"
end
# Get variables from edit form
def get_form_vars
@assigned_filters = []
gfv_report_fields # Global report fields
gfv_move_cols_buttons # Move cols buttons
gfv_model # Model changes
gfv_trend # Trend fields
gfv_performance # Performance fields
gfv_chargeback # Chargeback fields
gfv_charts # Charting fields
gfv_pivots # Consolidation fields
gfv_sort # Summary fields
gfv_timeline # Timeline fields
# Check for key prefixes (params starting with certain keys)
params.each do |key, value|
# See if any headers were sent in
@edit[:new][:headers][key.split("_")[1..-1].join("_")] = value if key.split("_").first == "hdr"
# See if any formats were sent in
if key.split("_").first == "fmt"
key2 = key.gsub("___", ".") # Put period sub table separator back into the key
@edit[:new][:col_formats][key2.split("_")[1..-1].join("_")] = value.blank? ? nil : value.to_sym
@formatting_changed = value.blank?
end
# See if any group calculation checkboxes were sent in
gfv_key_group_calculations(key, value) if key.split("_").first == "calc"
# See if any pivot calculation checkboxes were sent in
gfv_key_pivot_calculations(key, value) if key.split("_").first == "pivotcalc"
# Check for style fields
prefix = key.split("_").first
gfv_key_style(key, value) if prefix && prefix.starts_with?("style")
end
end
# Handle params starting with "calc"
def gfv_key_group_calculations(key, value)
field = @edit[:new][:field_order][key.split("_").last.to_i].last # Get the field name
@edit[:new][:col_options][field_to_col(field)] = {
:grouping => value.split(",").sort.map(&:to_sym).reject { |a| a == :null }
}
end
# Handle params starting with "pivotcalc"
def gfv_key_pivot_calculations(key, value)
field = @edit[:new][:fields][key.split("_").last.to_i].last # Get the field name
@edit[:pivot_cols][field] = []
value.split(',').sort.map(&:to_sym).each do |agg|
@edit[:pivot_cols][field] << agg
# Create new header from original header + aggregate function
@edit[:new][:headers][field + "__#{agg}"] = @edit[:new][:headers][field] + " (#{agg.to_s.titleize})"
end
build_field_order
end
# Handle params starting with "style"
def gfv_key_style(key, value)
parm, f_idx, s_idx = key.split("_") # Get the parm type, field index, and style index
f_idx = f_idx.to_i
s_idx = s_idx.to_i
f = @edit[:new][:field_order][f_idx] # Get the field element
field_sub_type = MiqExpression.get_col_info(f.last)[:format_sub_type]
field_data_type = MiqExpression.get_col_info(f.last)[:data_type]
field_name = MiqExpression.parse_field_or_tag(f.last).report_column
case parm
when "style" # New CSS class chosen
if value.blank?
@edit[:new][:col_options][field_name][:style].delete_at(s_idx)
@edit[:new][:col_options][field_name].delete(:style) if @edit[:new][:col_options][field_name][:style].empty?
@edit[:new][:col_options].delete(field_name) if @edit[:new][:col_options][field_name].empty?
else
@edit[:new][:col_options][field_name] ||= {}
@edit[:new][:col_options][field_name][:style] ||= []
@edit[:new][:col_options][field_name][:style][s_idx] ||= {}
@edit[:new][:col_options][field_name][:style][s_idx][:class] = value.to_sym
ovs = case field_data_type
when :boolean
["DEFAULT", "true"]
when :integer, :float
["DEFAULT", "", MiqExpression::FORMAT_SUB_TYPES.fetch_path(field_sub_type, :units) ? MiqExpression::FORMAT_SUB_TYPES.fetch_path(field_sub_type, :units).first : nil]
else
["DEFAULT", ""]
end
op ||= ovs[0]
val ||= ovs[1]
suffix ||= ovs[2]
@edit[:new][:col_options][field_name][:style][s_idx][:operator] ||= op
@edit[:new][:col_options][field_name][:style][s_idx][:value] ||= val
@edit[:new][:col_options][field_name][:style][s_idx][:value_suffix] ||= suffix if suffix
end
@refresh_div = "styling_div"
@refresh_partial = "form_styling"
when "styleop" # New operator chosen
@edit[:new][:col_options][field_name][:style][s_idx][:operator] = value
if value == "DEFAULT"
@edit[:new][:col_options][field_name][:style][s_idx].delete(:value) # Remove value key
# Remove all style array elements after this one
((s_idx + 1)...@edit[:new][:col_options][field_name][:style].length).each_with_index do |_i, i_idx|
@edit[:new][:col_options][field_name][:style].delete_at(i_idx)
end
elsif value.include?("NIL") || value.include?("EMPTY")
@edit[:new][:col_options][field_name][:style][s_idx].delete(:value) # Remove value key
elsif [:datetime, :date].include?(field_data_type)
@edit[:new][:col_options][field_name][:style][s_idx][:value] = ApplicationController::Filter::EXP_TODAY # Set default date value
elsif [:boolean].include?(field_data_type)
@edit[:new][:col_options][field_name][:style][s_idx][:value] = true # Set default boolean value
else
@edit[:new][:col_options][field_name][:style][s_idx][:value] = "" # Set default value
end
@refresh_div = "styling_div"
@refresh_partial = "form_styling"
when "styleval" # New value chosen
@edit[:new][:col_options][field_name][:style][s_idx][:value] = value
when "stylesuffix" # New suffix chosen
@edit[:new][:col_options][field_name][:style][s_idx][:value_suffix] = value.to_sym
@refresh_div = "styling_div"
@refresh_partial = "form_styling"
end
end
def gfv_report_fields
@edit[:new][:pdf_page_size] = params[:pdf_page_size] if params[:pdf_page_size]
if params[:chosen_queue_timeout]
@edit[:new][:queue_timeout] = params[:chosen_queue_timeout].blank? ? nil : params[:chosen_queue_timeout].to_i
end
@edit[:new][:row_limit] = params[:row_limit].blank? ? "" : params[:row_limit] if params[:row_limit]
@edit[:new][:name] = params[:name] if params[:name]
@edit[:new][:title] = params[:title] if params[:title]
end
def gfv_move_cols_buttons
case params[:button]
when 'right' then move_cols_right
when 'left' then move_cols_left
when 'up' then move_cols_up
when 'down' then move_cols_down
when 'top' then move_cols_top
when 'bottom' then move_cols_bottom
end && build_field_order
end
def gfv_model
if params[:chosen_model] && # Check for db table changed
params[:chosen_model] != @edit[:new][:model]
@edit[:new][:model] = params[:chosen_model]
@edit[:new][:perf_interval] = nil # Clear performance interval setting
@edit[:new][:tz] = nil
if [:performance, :trend].include?(model_report_type(@edit[:new][:model]))
@edit[:new][:perf_interval] ||= "daily" # Default to Daily
@edit[:new][:perf_avgs] ||= "time_interval"
@edit[:new][:tz] = session[:user_tz]
ensure_perf_interval_defaults
end
if Chargeback.db_is_chargeback?(@edit[:new][:model])
@edit[:new][:cb_model] = Chargeback.report_cb_model(@edit[:new][:model])
@edit[:new][:cb_interval] ||= "daily" # Default to Daily
@edit[:new][:cb_interval_size] ||= 1
@edit[:new][:cb_end_interval_offset] ||= 1
@edit[:new][:cb_groupby] ||= "date" # Default to Date grouping
@edit[:new][:tz] = session[:user_tz]
@edit[:new][:cb_include_metrics] = true if @edit[:new][:model] == 'ChargebackVm'
@edit[:new][:method_for_allocated_metrics] = default_chargeback_allocated_method
end
reset_report_col_fields
build_edit_screen
@refresh_div = "form_div"
@refresh_partial = "form"
end
end
def gfv_trend
if params[:chosen_trend_col]
@edit[:new][:perf_interval] ||= "daily" # Default to Daily
@edit[:new][:perf_target_pct1] ||= 100 # Default to 100%
if params[:chosen_trend_col] == "<Choose>"
@edit[:new][:perf_trend_db] = nil
@edit[:new][:perf_trend_col] = nil
else
@edit[:new][:perf_trend_db], @edit[:new][:perf_trend_col] = params[:chosen_trend_col].split("-")
if MiqExpression.reporting_available_fields(@edit[:new][:model], @edit[:new][:perf_interval]).find { |af| af.last == params[:chosen_trend_col] }.first.include?("(%)")
@edit[:new][:perf_limit_val] = 100
@edit[:new][:perf_limit_col] = nil
@edit[:percent_col] = true
else
@edit[:percent_col] = false
@edit[:new][:perf_limit_val] = nil
end
ensure_perf_interval_defaults
@edit[:limit_cols] = VimPerformanceTrend.trend_limit_cols(@edit[:new][:perf_trend_db], @edit[:new][:perf_trend_col], @edit[:new][:perf_interval])
end
@refresh_div = "columns_div"
@refresh_partial = "form_columns"
# @edit[:limit_cols] = VimPerformanceTrend.trend_limit_cols(@edit[:new][:perf_trend_db], @edit[:new][:perf_trend_col], @edit[:new][:perf_interval])
elsif params[:chosen_limit_col]
if params[:chosen_limit_col] == "<None>"
@edit[:new][:perf_limit_col] = nil
else
@edit[:new][:perf_limit_col] = params[:chosen_limit_col]
@edit[:new][:perf_limit_val] = nil
end
@refresh_div = "columns_div"
@refresh_partial = "form_columns"
elsif params[:chosen_limit_val]
@edit[:new][:perf_limit_val] = params[:chosen_limit_val]
elsif params[:percent1]
@edit[:new][:perf_target_pct1] = params[:percent1].to_i
elsif params[:percent2]
@edit[:new][:perf_target_pct2] = params[:percent2] == "<None>" ? nil : params[:percent2].to_i
elsif params[:percent3]
@edit[:new][:perf_target_pct3] = params[:percent3] == "<None>" ? nil : params[:percent3].to_i
end
end
def gfv_performance
if params[:chosen_interval]
@edit[:new][:perf_interval] = params[:chosen_interval]
@edit[:new][:perf_start] = nil # Clear start/end offsets
@edit[:new][:perf_end] = nil
ensure_perf_interval_defaults
reset_report_col_fields
@refresh_div = "form_div"
@refresh_partial = "form"
elsif params[:perf_avgs]
@edit[:new][:perf_avgs] = params[:perf_avgs]
elsif params[:chosen_start]
@edit[:new][:perf_start] = params[:chosen_start]
elsif params[:chosen_end]
@edit[:new][:perf_end] = params[:chosen_end]
elsif params[:chosen_tz]
@edit[:new][:tz] = params[:chosen_tz]
elsif params.key?(:chosen_time_profile)
@edit[:new][:time_profile] = params[:chosen_time_profile].blank? ? nil : params[:chosen_time_profile].to_i
@refresh_div = "filter_div"
@refresh_partial = "form_filter"
end
end
def gfv_chargeback
# Chargeback options
if params.key?(:cb_show_typ)
@edit[:new][:cb_show_typ] = params[:cb_show_typ].blank? ? nil : params[:cb_show_typ]
@refresh_div = "filter_div"
@refresh_partial = "form_filter"
elsif params.key?(:cb_tag_cat)
@refresh_div = "filter_div"
@refresh_partial = "form_filter"
if params[:cb_tag_cat].blank?
@edit[:new][:cb_tag_cat] = nil
@edit[:new][:cb_tag_value] = nil
else
@edit[:new][:cb_tag_cat] = params[:cb_tag_cat]
@edit[:cb_tags] = entries_hash(params[:cb_tag_cat])
end
elsif params.key?(:cb_include_metrics)
@edit[:new][:cb_include_metrics] = params[:cb_include_metrics] == 'true'
elsif params.key?(:method_for_allocated_metrics)
@edit[:new][:method_for_allocated_metrics] = params[:method_for_allocated_metrics].try(:to_sym) || default_chargeback_allocated_method
elsif params.key?(:cb_owner_id)
@edit[:new][:cb_owner_id] = params[:cb_owner_id].blank? ? nil : params[:cb_owner_id]
elsif params.key?(:cb_tenant_id)
@edit[:new][:cb_tenant_id] = params[:cb_tenant_id].blank? ? nil : params[:cb_tenant_id].to_i
elsif params.key?(:cb_tag_value)
@edit[:new][:cb_tag_value] = params[:cb_tag_value].blank? ? nil : params[:cb_tag_value]
elsif params.key?(:cb_entity_id)
@edit[:new][:cb_entity_id] = params[:cb_entity_id].blank? ? nil : params[:cb_entity_id]
elsif params.key?(:cb_provider_id)
@edit[:new][:cb_provider_id] = params[:cb_provider_id].blank? ? nil : params[:cb_provider_id]
@edit[:new][:cb_entity_id] = "all"
build_edit_screen
@refresh_div = "form_div"
@refresh_partial = "form"
elsif params.key?(:cb_groupby)
@edit[:new][:cb_groupby] = params[:cb_groupby]
@refresh_div = "filter_div"
@refresh_partial = "form_filter"
elsif params.key?(:cb_groupby_tag)
@edit[:new][:cb_groupby_tag] = params[:cb_groupby_tag]
elsif params.key?(:cb_groupby_label)
@edit[:new][:cb_groupby_label] = params[:cb_groupby_label]
elsif params[:cb_interval]
@edit[:new][:cb_interval] = params[:cb_interval]
@edit[:new][:cb_interval_size] = 1
@edit[:new][:cb_end_interval_offset] = 1
@refresh_div = "filter_div"
@refresh_partial = "form_filter"
elsif params[:cb_interval_size]
@edit[:new][:cb_interval_size] = params[:cb_interval_size].to_i
elsif params[:cb_end_interval_offset]
@edit[:new][:cb_end_interval_offset] = params[:cb_end_interval_offset].to_i
end
end
def gfv_charts
if params[:chosen_graph] && params[:chosen_graph] != @edit[:new][:graph_type]
if params[:chosen_graph] == "<No chart>"
@edit[:new][:graph_type] = nil
# Reset other setting to initial settings if choosing <No chart>
@edit[:new][:graph_count] = @edit[:current][:graph_count]
@edit[:new][:graph_other] = @edit[:current][:graph_other]
@edit[:new][:chart_mode] = @edit[:current][:chart_mode]
@edit[:new][:chart_column] = @edit[:current][:chart_column]
else
@edit[:new][:graph_other] = true if @edit[:new][:graph_type].nil? # Reset other setting if choosing first chart
@edit[:new][:graph_type] = params[:chosen_graph] # Save graph type
@edit[:new][:graph_count] ||= GRAPH_MAX_COUNT # Reset graph count, if not set
@edit[:new][:chart_mode] ||= 'counts'
@edit[:new][:chart_column] ||= ''
end
@refresh_div = "chart_div"
@refresh_partial = "form_chart"
end
if params[:chart_mode] && params[:chart_mode] != @edit[:new][:chart_mode]
@edit[:new][:chart_mode] = params[:chart_mode]
@refresh_div = "chart_div"
@refresh_partial = "form_chart"
end
if params[:chart_column] && params[:chart_column] != @edit[:new][:chart_column]
@edit[:new][:chart_column] = params[:chart_column]
@refresh_div = "chart_sample_div"
@refresh_partial = "form_chart_sample"
end
if params[:chosen_count] && params[:chosen_count] != @edit[:new][:graph_count]
@edit[:new][:graph_count] = params[:chosen_count]
@refresh_div = "chart_sample_div"
@refresh_partial = "form_chart_sample"
end
if params[:chosen_other] # If a chart is showing, set the other setting based on check box present
chosen = (params[:chosen_other].to_s == "1")
if @edit[:new][:graph_other] != chosen
@edit[:new][:graph_other] = chosen
@refresh_div = "chart_sample_div"
@refresh_partial = "form_chart_sample"
end
end
end
def gfv_pivots
@edit[:new][:pivot] ||= ReportController::PivotOptions.new
@edit[:new][:pivot].update(params)
if params[:chosen_pivot1] || params[:chosen_pivot2] || params[:chosen_pivot3]
if @edit[:new][:pivot].by1 == ReportHelper::NOTHING_STRING
@edit[:pivot_cols] = {} # Clear pivot_cols if no pivot grouping fields selected
else
@edit[:pivot_cols].delete(@edit[:new][:pivot].by1) # Remove any pivot grouping fields from pivot cols
@edit[:pivot_cols].delete(@edit[:new][:pivot].by2)
@edit[:pivot_cols].delete(@edit[:new][:pivot].by3)
end
build_field_order
@refresh_div = "consolidate_div"
@refresh_partial = "form_consolidate"
end
end
def gfv_sort
@edit[:new][:order] = params[:sort_order] if params[:sort_order]
if params[:sort_group] # If grouping changed,
@edit[:new][:group] = params[:sort_group]
@refresh_div = "sort_div" # Resend the sort tab
@refresh_partial = "form_sort"
if @edit[:new][:chart_mode] = 'values' && !chart_mode_values_allowed?
@edit[:new][:chart_mode] = 'counts'
end
end
@edit[:new][:hide_details] = (params[:hide_details].to_s == "1") if params[:hide_details]
if params[:chosen_sort1] && params[:chosen_sort1] != @edit[:new][:sortby1].split("__").first
# Remove any col options for any existing sort + suffix
@edit[:new][:col_options].delete(@edit[:new][:sortby1].split("-").last) if @edit[:new][:sortby1].split("__")[1]
@edit[:new][:sortby1] = params[:chosen_sort1]
@edit[:new][:sortby2] = ReportHelper::NOTHING_STRING if params[:chosen_sort1] == ReportHelper::NOTHING_STRING || params[:chosen_sort1] == @edit[:new][:sortby2].split("__").first
@refresh_div = "sort_div"
@refresh_partial = "form_sort"
elsif params[:chosen_sort2] && params[:chosen_sort2] != @edit[:new][:sortby2].split("__").first
@edit[:new][:sortby2] = params[:chosen_sort2]
# Look at the 1st sort suffix (ie. month, day_of_week, etc)
elsif params[:sort1_suffix] && params[:sort1_suffix].to_s != @edit[:new][:sortby1].split("__")[1].to_s
# Remove any col options for any existing sort + suffix
@edit[:new][:col_options].delete(@edit[:new][:sortby1].split("-").last) if @edit[:new][:sortby1].split("__")[1]
@edit[:new][:sortby1] = @edit[:new][:sortby1].split("__").first +
(params[:sort1_suffix].blank? ? "" : "__#{params[:sort1_suffix]}")
# Look at the 2nd sort suffix (ie. month, day_of_week, etc)
elsif params[:sort2_suffix] && params[:sort2_suffix].to_s != @edit[:new][:sortby2].split("__")[1].to_s
# Remove any col options for any existing sort + suffix
@edit[:new][:col_options].delete(@edit[:new][:sortby2].split("-").last) if @edit[:new][:sortby2].split("__")[1]
@edit[:new][:sortby2] = @edit[:new][:sortby2].split("__").first + "__" + params[:sort2_suffix]
@edit[:new][:sortby2] = @edit[:new][:sortby2].split("__").first +
(params[:sort2_suffix].blank? ? "" : "__#{params[:sort2_suffix]}")
# Look at the break format
else
co_key1 = @edit[:new][:sortby1].split("-").last
if params[:break_format] &&
params[:break_format].to_s != @edit[:new].fetch_path(:col_options, co_key1)
if params[:break_format].blank? || # Remove format and col key (if empty)
params[:break_format].to_sym == MiqReport.get_col_info(@edit[:new][:sortby1])[:default_format]
if @edit[:new][:col_options][co_key1]
@edit[:new][:col_options][co_key1].delete(:break_format)
@edit[:new][:col_options].delete(co_key1) if @edit[:new][:col_options][co_key1].empty?
end
else # Add col and format to col_options
@edit[:new][:col_options][co_key1] ||= {}
@edit[:new][:col_options][co_key1][:break_format] = params[:break_format].to_sym
end
end
end
# Clear/set up the default break label
sort1 = @edit[:new][:sortby1].split("-").last unless @edit[:new][:sortby1].blank?
if @edit[:new][:group] == "No" # Clear any existing break label
if @edit[:new].fetch_path(:col_options, sort1, :break_label)
@edit[:new][:col_options][sort1].delete(:break_label)
@edit[:new][:col_options].delete(sort1) if @edit[:new][:col_options][sort1].empty?
end
else # Create a break label, if none there already
unless @edit[:new].fetch_path(:col_options, sort1, :break_label)
@edit[:new][:col_options][sort1] ||= {}
sort, suffix = @edit[:new][:sortby1].split("__")
@edit[:new][:col_options][sort1][:break_label] =
@edit[:new][:field_order].collect { |f| f.first if f.last == sort }.compact.join.strip +
(suffix ? " (#{MiqReport.date_time_break_suffixes.collect { |s| s.first if s.last == suffix }.compact.join})" : "") +
": "
end
end
# TODO: Not allowing user to change break label until editor is changed to not use form observe
# if params[:break_label]
# @edit[:new][:col_options][@edit[:new][:sortby1].split("-").last] ||= Hash.new
# @edit[:new][:col_options][@edit[:new][:sortby1].split("-").last][:break_label] == params[:break_label]
# end
end
def gfv_timeline
if params[:chosen_tl] && params[:chosen_tl] != @edit[:new][:tl_field]
if @edit[:new][:tl_field] == ReportHelper::NOTHING_STRING || params[:chosen_tl] == ReportHelper::NOTHING_STRING
@refresh_div = "tl_settings_div"
@refresh_partial = "form_tl_settings"
@tl_changed = true
else
@tl_repaint = true
end
@edit[:new][:tl_field] = params[:chosen_tl]
elsif params[:chosen_position] && params[:chosen_position] != @edit[:new][:tl_position]
@tl_changed = true
@edit[:new][:tl_position] = params[:chosen_position]
end
end
def cashed_reporting_available_fields
@reporting_available_fields ||= {}
@reporting_available_fields[@edit[:new][:model]] ||= MiqExpression.reporting_available_fields(@edit[:new][:model], @edit[:new][:perf_interval])
end
def reporting_available_fields_clear_cash
@reporting_available_fields = nil
end
def move_cols_right
if !params[:available_fields] || params[:available_fields].length == 0 || params[:available_fields][0] == ""
add_flash(_("No fields were selected to move down"), :error)
elsif params[:available_fields].length + @edit[:new][:fields].length > MAX_REPORT_COLUMNS
add_flash(_("Fields not added: Adding the selected %{count} fields will exceed the maximum of %{max} fields") % {:count => params[:available_fields].length + @edit[:new][:fields].length, :max => MAX_REPORT_COLUMNS},
:error)
else
reporting_available_fields_clear_cash
cashed_reporting_available_fields.each do |af| # Go thru all available columns
if params[:available_fields].include?(af[1]) # See if this column was selected to move
unless @edit[:new][:fields].include?(af) # Only move if it's not there already
@edit[:new][:fields].push(af) # Add it to the new fields list
if af[0].include?(":") && !af[1].include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX) # Not a base column
table = af[0].split(" : ")[0].split(".")[-1] # Get the table name
table = table.singularize unless table == "OS" # Singularize, except "OS"
temp = af[0].split(" : ")[1]
temp_header = table == temp.split(" ")[0] ? af[0].split(" : ")[1] : table + " " + af[0].split(" : ")[1]
else
temp_header = af[0].strip # Base column, just use it without leading space
end
@edit[:new][:headers][af[1]] = temp_header # Add the column title to the headers hash
end
end
end
@refresh_div = "column_lists"
@refresh_partial = "column_lists"
end
end
def move_cols_left
if !params[:selected_fields] || params[:selected_fields].length == 0 || params[:selected_fields][0] == ""
add_flash(_("No fields were selected to move up"), :error)
elsif display_filter_contains?(params[:selected_fields])
add_flash(_("No fields were moved up"), :error)
else
@edit[:new][:fields].each do |nf| # Go thru all new fields
if params[:selected_fields].include?(nf.last) # See if this col was selected to move
# Clear out headers and formatting
@edit[:new][:headers].delete(nf.last) # Delete the column name from the headers hash
@edit[:new][:headers].delete_if { |k, _v| k.starts_with?("#{nf.last}__") } # Delete pivot calc keys
@edit[:new][:col_formats].delete(nf.last) # Delete the column name from the col_formats hash
@edit[:new][:col_formats].delete_if { |k, _v| k.starts_with?("#{nf.last}__") } # Delete pivot calc keys
# Clear out pivot field options
@edit[:new][:pivot].drop_from_selection(nf.last)
@edit[:pivot_cols].delete(nf.last) # Delete the column name from the pivot_cols hash
# Clear out sort options
if @edit[:new][:sortby1] && nf.last == @edit[:new][:sortby1].split("__").first # If deleting the first sort field
if MiqReport.is_break_suffix?(@edit[:new][:sortby1].split("__")[1]) # If sort has a break suffix
@edit[:new][:col_options].delete(field_to_col(@edit[:new][:sortby1])) # Remove the <col>__<suffix> from col_options
end
unless @edit[:new][:group] == "No" # If we were grouping, remove all col_options :group keys
@edit[:new][:col_options].each do |co_key, co_val|
co_val.delete(:grouping) # Remove :group key
@edit[:new][:col_options].delete(co_key) if co_val.empty? # Remove the col, if empty
end
end
@edit[:new][:sortby1] = ReportHelper::NOTHING_STRING
@edit[:new][:sortby2] = ReportHelper::NOTHING_STRING
end
if @edit[:new][:sortby1] && nf.last == @edit[:new][:sortby2].split("__").first # If deleting the second sort field
@edit[:new][:sortby2] = ReportHelper::NOTHING_STRING
end
# Clear out selected chart data column
@edit[:new][:chart_column] = nil if @edit[:new][:chart_column] == nf.last
@edit[:new][:col_options].delete(field_to_col(nf.last)) # Remove this column from the col_options hash
end
end
@edit[:new][:fields].delete_if { |nf| params[:selected_fields].include?(nf.last) } # Remove selected fields
@refresh_div = "column_lists"
@refresh_partial = "column_lists"
end
end
# See if any of the fields passed in are present in the display filter expression
def display_filter_contains?(fields)
return false if @edit[:new][:display_filter].nil? # No display filter defined
exp = @edit[:new][:display_filter].inspect
@edit[:new][:fields].each do |f| # Go thru all of the selected fields
if fields.include?(f.last) # Is this field being removed?
add_flash(_("%{name} is currently being used in the Display Filter") %
{:name => f.first}, :error) if exp.include?(f.last)
end
end
!@flash_array.nil?
end
def selected_consecutive?
first_idx = last_idx = 0
@edit[:new][:fields].each_with_index do |nf, idx|
first_idx = idx if nf[1] == params[:selected_fields].first
if nf[1] == params[:selected_fields].last
last_idx = idx
break
end
end
if last_idx - first_idx + 1 > params[:selected_fields].length
return [false, first_idx, last_idx]
else
return [true, first_idx, last_idx]
end
end
# Set record variables to new values
def set_record_vars(rpt)
# Set the simple string/number fields
rpt.template_type = "report"
rpt.name = @edit[:new][:name].to_s.strip
rpt.title = @edit[:new][:title].to_s.strip
rpt.db = @edit[:new][:model]
rpt.rpt_group = @edit[:new][:rpt_group]
rpt.rpt_type = @edit[:new][:rpt_type]
rpt.priority = @edit[:new][:priority]
rpt.categories = @edit[:new][:categories]
rpt.col_options = @edit[:new][:col_options]
rpt.order = @edit[:new][:sortby1].nil? ? nil : @edit[:new][:order]
# Set the graph fields
if @edit[:new][:sortby1] == ReportHelper::NOTHING_STRING || @edit[:new][:graph_type].nil?
rpt.dims = nil
rpt.graph = nil
else
if @edit[:new][:graph_type] =~ /^(Pie|Donut)/ # Pie and Donut charts must be set to 1 dimension
rpt.dims = 1
else
rpt.dims = @edit[:new][:sortby2] == ReportHelper::NOTHING_STRING ? 1 : 2 # Set dims to 1 or 2 based on presence of sortby2
end
if @edit[:new][:chart_mode] == 'values' && @edit[:new][:chart_column].blank?
options = chart_fields_options
@edit[:new][:chart_column] = options[0][1] unless options.empty?
end
rpt.graph = {
:type => @edit[:new][:graph_type],
:mode => @edit[:new][:chart_mode],
:column => @edit[:new][:chart_column],
:count => @edit[:new][:graph_count],
:other => @edit[:new][:graph_other],
}
end
# Set the conditions field (expression)
if !@edit[:new][:record_filter].nil? && @edit[:new][:record_filter]["???"].nil?
rpt.conditions = MiqExpression.new(@edit[:new][:record_filter])
else
rpt.conditions = nil
end
# Set the display_filter field (expression)
if !@edit[:new][:display_filter].nil? && @edit[:new][:display_filter]["???"].nil?
rpt.display_filter = MiqExpression.new(@edit[:new][:display_filter])
else