Class: Gitlab::Usage::EventSelectionRule

Inherits:
Object
  • Object
show all
Extended by:
Gitlab::Utils::StrongMemoize
Includes:
TimeFrame, TimeSeriesStorable
Defined in:
lib/gitlab/usage/event_selection_rule.rb

Constant Summary collapse

TOTAL_COUNTER_KEY_PREFIX =
"event_counters"
SUM_KEY_PREFIX =
"event_sums"

Constants included from TimeFrame

TimeFrame::ALL_TIME_TIME_FRAME_NAME, TimeFrame::DEFAULT_TIMESTAMP_COLUMN, TimeFrame::SEVEN_DAYS_TIME_FRAME_NAME, TimeFrame::TWENTY_EIGHT_DAYS_TIME_FRAME_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeFrame

#monthly_time_range, #monthly_time_range_db_params, #weekly_time_range, #weekly_time_range_db_params

Methods included from TimeSeriesStorable

#apply_time_aggregation, #keys_for_aggregation

Constructor Details

#initialize(name:, time_framed:, filter: {}, unique_identifier_name: nil, operator: nil) ⇒ EventSelectionRule

Returns a new instance of EventSelectionRule.



33
34
35
36
37
38
39
# File 'lib/gitlab/usage/event_selection_rule.rb', line 33

def initialize(name:, time_framed:, filter: {}, unique_identifier_name: nil, operator: nil)
  @name = name
  @time_framed = time_framed
  @filter = filter || {}
  @unique_identifier_name = unique_identifier_name
  @operator = operator
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



31
32
33
# File 'lib/gitlab/usage/event_selection_rule.rb', line 31

def filter
  @filter
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/gitlab/usage/event_selection_rule.rb', line 31

def name
  @name
end

#operatorObject (readonly)

Returns the value of attribute operator.



31
32
33
# File 'lib/gitlab/usage/event_selection_rule.rb', line 31

def operator
  @operator
end

#unique_identifier_nameObject (readonly)

Returns the value of attribute unique_identifier_name.



31
32
33
# File 'lib/gitlab/usage/event_selection_rule.rb', line 31

def unique_identifier_name
  @unique_identifier_name
end

Class Method Details

.key_overridesObject

We want to avoid reading the key_overrides every time a rule is used.



25
26
27
28
29
# File 'lib/gitlab/usage/event_selection_rule.rb', line 25

def self.key_overrides
  strong_memoize(:key_overrides) do
    YAML.safe_load(File.read(Gitlab::UsageDataCounters::HLLRedisCounter::KEY_OVERRIDES_PATH)).freeze
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Implementing ‘==` to make sure that `a == b` is true if and only if a and b have equal properties Checks equality by comparing each attribute.

Parameters:

  • Object (Object)

    to be compared



78
79
80
81
82
83
84
85
# File 'lib/gitlab/usage/event_selection_rule.rb', line 78

def ==(other)
  other.is_a?(self.class) &&
    name == other.name &&
    time_framed? == other.time_framed? &&
    filter == other.filter &&
    unique_identifier_name == other.unique_identifier_name &&
    operator == other.operator
end

#hashInteger

Calculates hash code according to all attributes. Ensures 2 objects with the same attributes can’t be keys in the same hash twice

Returns:

  • (Integer)

    Hash code



93
94
95
# File 'lib/gitlab/usage/event_selection_rule.rb', line 93

def hash
  [name, time_framed?, filter, unique_identifier_name, operator].hash
end

#matches?(additional_properties) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/gitlab/usage/event_selection_rule.rb', line 69

def matches?(additional_properties)
  filter.all? do |property_name, value|
    additional_properties[property_name] == value
  end
end

#redis_key_for_date(date = Date.today) ⇒ Object



41
42
43
# File 'lib/gitlab/usage/event_selection_rule.rb', line 41

def redis_key_for_date(date = Date.today)
  redis_key(nil, date)
end

#redis_keys_for_time_frame(time_frame) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/gitlab/usage/event_selection_rule.rb', line 45

def redis_keys_for_time_frame(time_frame)
  if time_frame == 'all'
    [redis_key]
  else
    keys_for_aggregation(events: [path_part_of_redis_key], **time_constraint(time_frame))
  end
end

#sum?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/gitlab/usage/event_selection_rule.rb', line 61

def sum?
  operator == 'sum(value)'
end

#time_framed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/gitlab/usage/event_selection_rule.rb', line 53

def time_framed?
  @time_framed
end

#total_counter?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/gitlab/usage/event_selection_rule.rb', line 57

def total_counter?
  unique_identifier_name.nil? && !sum?
end

#unique_total?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/gitlab/usage/event_selection_rule.rb', line 65

def unique_total?
  unique_identifier_name && operator == 'total'
end