-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathworkflow.rb
173 lines (139 loc) · 5.17 KB
/
workflow.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
class Workflow < ActiveRecord::Base
include Activatable
include ExtendedCacheKey
include RankedModel
include ModelCacheKey
include Translatable
include Versioning
versioned association: :workflow_versions, attributes: %w(tasks first_task strings major_version minor_version)
belongs_to :project
has_many :subject_workflow_statuses, dependent: :destroy
has_many :subject_sets_workflows, dependent: :destroy
has_many :subject_sets, through: :subject_sets_workflows
has_many :non_training_subject_sets,
->(workflow) { where.not(subject_sets_workflows: { subject_set_id: workflow.training_set_ids }) },
through: :subject_sets_workflows,
source: :subject_set
has_many :set_member_subjects, through: :subject_sets
has_many :subjects, through: :set_member_subjects
has_many :classifications, dependent: :restrict_with_exception
has_many :user_seen_subjects, dependent: :destroy
has_many :workflow_tutorials, dependent: :destroy
has_many :tutorials, through: :workflow_tutorials
has_many :aggregations, dependent: :destroy
has_many :attached_images, -> { where(type: "workflow_attached_image") }, class_name: "Medium",
as: :linked
has_one :classifications_export, -> { where(type: "workflow_classifications_export").order(created_at: :desc) },
class_name: "Medium", as: :linked
has_and_belongs_to_many :expert_subject_sets, -> { expert_sets }, class_name: "SubjectSet"
belongs_to :tutorial_subject, class_name: "Subject"
# TODO: remove this association from the cache key
cache_by_resource_method :subjects_count, :finished?
enum subject_selection_strategy: %i{default cellect designator builtin}
max_paginates_per 25
DEFAULT_RETIREMENT_OPTIONS = {
'criteria' => 'classification_count',
'options' => {'count' => 15}
}.freeze
JSON_ATTRIBUTES = %w(tasks retirement aggregation strings steps).freeze
SELECTOR_PAGE_SIZE_KEY = 'subject_queue_page_size'.freeze
# Used by HttpCacheable
scope :private_scope, -> { where(project_id: Project.private_scope) }
validates_presence_of :project, :display_name
validate :retirement_config
has_many :workflow_versions, dependent: :destroy
belongs_to :published_version, class_name: "WorkflowVersion"
def publish!
update!(published_version: workflow_versions.order(:id).last)
end
def latest_version
self
end
ranks :display_order, with_same: :project_id
delegate :owner, to: :project
delegate :communication_emails, to: :project
def self.translatable_attributes
%i(display_name strings)
end
before_save :update_version
def update_version
if (changes.keys & %w(tasks grouped pairwise prioritized first_task)).present?
self.major_version += 1
end
if changes.include? :strings
self.minor_version += 1
end
if new_record?
self.major_version = 1 if major_version < 1
self.minor_version = 1 if minor_version < 1
end
end
# select a workflow without any json attributes (some can be very large)
# this can be used generally in most workers
# access to non-loaded attributes will raise an undefined_error
# workflow.reload can be used to retrieve the missing attributes if needed
#
# Longer term the large json attributes could be sliced out to their own table
# and linked to the workflow as an assocaition
def self.find_without_json_attrs(id)
non_json_attrs = Workflow.attribute_names - JSON_ATTRIBUTES
select(*non_json_attrs).find(id)
end
def self.same_project?(subject_set)
where(project: subject_set.project)
end
def retired_subjects
subject_workflow_statuses.retired.includes(:subject).map(&:subject)
end
def retire_subject(subject_id, reason=nil)
count = subject_workflow_statuses.where(subject_id: subject_id).first_or_create!
count.retire!(reason)
end
def retirement_scheme
criteria = retirement_with_defaults.fetch('criteria')
options = retirement_with_defaults.fetch('options')
scheme_class = RetirementSchemes.for(criteria).new(options)
end
def retirement_with_defaults
self.retirement.presence || DEFAULT_RETIREMENT_OPTIONS
end
def subject_selector
@subject_selector ||=
case subject_selection_strategy
when "builtin"
Subjects::BuiltInSelector.new(self)
when "cellect"
Subjects::CellectSelector.new(self)
else
Subjects::DesignatorSelector.new(self)
end
end
def subjects_count
real_set_member_subjects_count
end
def retired_subjects_count
retired_set_member_subjects_count
end
def finished?
@finished ||= case
when subject_sets.empty? || subjects_count == 0
false
when finished_at.present?
true
else
retired_subjects_count >= subjects_count
end
end
def retirement_config
RetirementValidator.new(self).validate
end
def training_set_ids
config_training_set_ids = Array.wrap(configuration.dig('training_set_ids'))
config_training_set_ids = config_training_set_ids.map(&:to_i)
config_training_set_ids = config_training_set_ids.reject(&:zero?)
config_training_set_ids & subject_set_ids
end
def selector_page_size(default_page_size=10)
configuration.fetch(SELECTOR_PAGE_SIZE_KEY, default_page_size)
end
end