diff options
Diffstat (limited to 'app/models/concerns/analytics')
-rw-r--r-- | app/models/concerns/analytics/cycle_analytics/stage.rb | 3 | ||||
-rw-r--r-- | app/models/concerns/analytics/cycle_analytics/stage_event_model.rb | 54 |
2 files changed, 56 insertions, 1 deletions
diff --git a/app/models/concerns/analytics/cycle_analytics/stage.rb b/app/models/concerns/analytics/cycle_analytics/stage.rb index 7bb6004ca83..d9e6756ab86 100644 --- a/app/models/concerns/analytics/cycle_analytics/stage.rb +++ b/app/models/concerns/analytics/cycle_analytics/stage.rb @@ -27,7 +27,8 @@ module Analytics alias_attribute :custom_stage?, :custom scope :default_stages, -> { where(custom: false) } scope :ordered, -> { order(:relative_position, :id) } - scope :for_list, -> { includes(:start_event_label, :end_event_label).ordered } + scope :with_preloaded_labels, -> { includes(:start_event_label, :end_event_label) } + scope :for_list, -> { with_preloaded_labels.ordered } scope :by_value_stream, -> (value_stream) { where(value_stream_id: value_stream.id) } before_save :ensure_stage_event_hash_id diff --git a/app/models/concerns/analytics/cycle_analytics/stage_event_model.rb b/app/models/concerns/analytics/cycle_analytics/stage_event_model.rb new file mode 100644 index 00000000000..7462e1e828b --- /dev/null +++ b/app/models/concerns/analytics/cycle_analytics/stage_event_model.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module Analytics + module CycleAnalytics + module StageEventModel + extend ActiveSupport::Concern + + class_methods do + def upsert_data(data) + upsert_values = data.map do |row| + row.values_at( + :stage_event_hash_id, + :issuable_id, + :group_id, + :project_id, + :author_id, + :milestone_id, + :start_event_timestamp, + :end_event_timestamp + ) + end + + value_list = Arel::Nodes::ValuesList.new(upsert_values).to_sql + + query = <<~SQL + INSERT INTO #{quoted_table_name} + ( + stage_event_hash_id, + #{connection.quote_column_name(issuable_id_column)}, + group_id, + project_id, + milestone_id, + author_id, + start_event_timestamp, + end_event_timestamp + ) + #{value_list} + ON CONFLICT(stage_event_hash_id, #{issuable_id_column}) + DO UPDATE SET + group_id = excluded.group_id, + project_id = excluded.project_id, + start_event_timestamp = excluded.start_event_timestamp, + end_event_timestamp = excluded.end_event_timestamp, + milestone_id = excluded.milestone_id, + author_id = excluded.author_id + SQL + + result = connection.execute(query) + result.cmd_tuples + end + end + end + end +end |