Commit 5ac38271 authored by Adam Hegyi's avatar Adam Hegyi

Introduce hash_code for VSA stages and events

This change adds the hash_code methods for stages and events.
parent 62c9b458
......@@ -50,6 +50,10 @@ module Analytics
end
end
def events_hash_code
Digest::SHA256.hexdigest("#{start_event.hash_code}-#{end_event.hash_code}")
end
def start_event_label_based?
start_event_identifier && start_event.label_based?
end
......
......@@ -20,6 +20,10 @@ module Gitlab
true
end
def hash_code
Digest::SHA256.hexdigest("#{self.class.identifier}-#{label_id}")
end
def column_list
[Arel.sql("#{join_expression_name}.created_at")]
end
......
......@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe Gitlab::Analytics::CycleAnalytics::StageEvents::IssueLabelAdded do
it_behaves_like 'value stream analytics event' do
let(:params) { { label: GroupLabel.new } }
let(:label_id) { 10 }
let(:params) { { label: GroupLabel.new(id: label_id) } }
let(:expected_hash_code) { Digest::SHA256.hexdigest("#{instance.class.identifier}-#{label_id}") }
end
end
......@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe Gitlab::Analytics::CycleAnalytics::StageEvents::IssueLabelRemoved do
it_behaves_like 'value stream analytics event' do
let(:params) { { label: GroupLabel.new } }
let(:label_id) { 10 }
let(:params) { { label: GroupLabel.new(id: label_id) } }
let(:expected_hash_code) { Digest::SHA256.hexdigest("#{instance.class.identifier}-#{label_id}") }
end
end
......@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe Gitlab::Analytics::CycleAnalytics::StageEvents::MergeRequestLabelAdded do
it_behaves_like 'value stream analytics event' do
let(:params) { { label: GroupLabel.new } }
let(:label_id) { 10 }
let(:params) { { label: GroupLabel.new(id: label_id) } }
let(:expected_hash_code) { Digest::SHA256.hexdigest("#{instance.class.identifier}-#{label_id}") }
end
end
......@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe Gitlab::Analytics::CycleAnalytics::StageEvents::MergeRequestLabelRemoved do
it_behaves_like 'value stream analytics event' do
let(:params) { { label: GroupLabel.new } }
let(:label_id) { 10 }
let(:params) { { label: GroupLabel.new(id: label_id) } }
let(:expected_hash_code) { Digest::SHA256.hexdigest("#{instance.class.identifier}-#{label_id}") }
end
end
......@@ -31,6 +31,10 @@ module Gitlab
raise NotImplementedError
end
def hash_code
Digest::SHA256.hexdigest(self.class.identifier.to_s)
end
# Each StageEvent must expose a timestamp or a timestamp like expression in order to build a range query.
# Example: get me all the Issue records between start event end end event
def timestamp_projection
......
......@@ -3,6 +3,7 @@
RSpec.shared_examples_for 'value stream analytics event' do
let(:params) { {} }
let(:instance) { described_class.new(params) }
let(:expected_hash_code) { Digest::SHA256.hexdigest(instance.class.identifier.to_s) }
it { expect(described_class.name).to be_a_kind_of(String) }
it { expect(described_class.identifier).to be_a_kind_of(Symbol) }
......@@ -19,4 +20,16 @@ RSpec.shared_examples_for 'value stream analytics event' do
expect(output_query).to be_a_kind_of(ActiveRecord::Relation)
end
end
describe '#hash_code' do
it 'returns a hash that uniquely identifies an event' do
expect(instance.hash_code).to eq(expected_hash_code)
end
it 'does not differ when the same object is built with the same params' do
another_instance_with_same_params = described_class.new(params)
expect(another_instance_with_same_params.hash_code).to eq(instance.hash_code)
end
end
end
......@@ -122,6 +122,22 @@ RSpec.shared_examples 'value stream analytics stage' do
expect(stage.parent_id).to eq(parent.id)
end
end
describe '#hash_code' do
it 'does not differ when the same object is built with the same params' do
stage_1 = build(factory)
stage_2 = build(factory)
expect(stage_1.events_hash_code).to eq(stage_2.events_hash_code)
end
it 'differs when the stage events are different' do
stage_1 = build(factory, start_event_identifier: :merge_request_created, end_event_identifier: :merge_request_merged)
stage_2 = build(factory, start_event_identifier: :issue_created, end_event_identifier: :issue_first_mentioned_in_commit)
expect(stage_1.events_hash_code).not_to eq(stage_2.events_hash_code)
end
end
end
RSpec.shared_examples 'value stream analytics label based stage' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment