forked from openSUSE/open-build-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decision.rb
89 lines (73 loc) · 2.41 KB
/
decision.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
class Decision < ApplicationRecord
TYPES = %w[DecisionFavored DecisionCleared].freeze
validates :reason, presence: true, length: { maximum: 65_535 }
validates :type, presence: true, length: { maximum: 255 }
belongs_to :moderator, class_name: 'User', optional: false
has_many :reports, dependent: :nullify
# TODO: Remove this after type is deployed
enum kind: {
cleared: 0,
favor: 1
}
after_create :create_event
after_create :track_decision
def description
'The moderator decided on the report'
end
# List of all viable types for a reportable, used in the decision creation form
def self.types(reportable)
TYPES.filter_map do |decision_type_name|
decision_type = decision_type_name.constantize
[decision_type.display_name, decision_type.name] if decision_type.display?(reportable)
end.compact.to_h
end
# We use this to determine if the decision type should be displayed for reportable
def self.display?(_reportable)
true
end
# We display this in the decision creation form
def self.display_name
'unknown'
end
private
# TODO: Replace this with `AbstractMethodCalled` after type is deployed
def create_event
case kind
when 'cleared'
Event::ClearedDecision.create(event_parameters)
else
Event::FavoredDecision.create(event_parameters)
end
end
def event_parameters
{ id: id, moderator_id: moderator.id, reason: reason, report_last_id: reports.last.id, reportable_type: reports.first.reportable.class.name }
end
# TODO: Remove kind after type is deployed
def track_decision
RabbitmqBus.send_to_bus('metrics', "decision,kind=#{kind},type=#{type} hours_before_decision=#{hours_before_decision},count=1")
end
def hours_before_decision
first_report_created_at = reports.order(created_at: :asc).first.created_at
((created_at - first_report_created_at) / 1.hour).floor
end
end
# == Schema Information
#
# Table name: decisions
#
# id :bigint not null, primary key
# kind :integer default("cleared")
# reason :text(65535) not null
# type :string(255) not null, default("DecisionCleared")
# created_at :datetime not null
# updated_at :datetime not null
# moderator_id :integer not null, indexed
#
# Indexes
#
# index_decisions_on_moderator_id (moderator_id)
#
# Foreign Keys
#
# fk_rails_... (moderator_id => users.id)
#