-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.rb
More file actions
170 lines (153 loc) · 5.43 KB
/
event.rb
File metadata and controls
170 lines (153 loc) · 5.43 KB
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
class Event < ApplicationRecord
# Included Modules
include Events::RelationTypeHandler
include Events::EventIndexHandler
include Events::EventFromSqsUpdater
include Elasticsearch::Model
# Extended Modules
extend Events::EventFromSqsCreator
# Attributes
attribute :uuid, :text
attribute :subj_id, :text
attribute :obj_id, :text
attribute :aasm_state, :string, default: "waiting"
attribute :state_event, :string
attribute :callback, :text
attribute :error_messages, :text
attribute :source_token, :text
attribute :indexed_at, :datetime, default: -> { Time.zone.at(0) }
attribute :occurred_at, :datetime
attribute :message_action, :string, default: "create"
attribute :subj, :text
attribute :obj, :text
attribute :total, :integer, default: 1
attribute :license, :string
attribute :source_doi, :text
attribute :target_doi, :text
attribute :source_relation_type_id, :string
attribute :target_relation_type_id, :string
attribute :relation_type_id, :string
attribute :source_id, :string
attribute :indexed_at, :datetime, default: "1970-01-01 00:00:00"
attribute :updated_at, :datetime
attribute :created_at, :datetime
# Validations
validates :uuid, presence: true, uuid_format: true, uniqueness: { case_sensitive: false, length: { maximum: 36 } }
validates :subj_id, presence: true
validates :obj_id, presence: true
validates :source_id, presence: true
validates :source_token, presence: true
validates :message_action, presence: true, length: { maximum: 191 }
validates :indexed_at, presence: true
# Getters
def subj_hash
return {} if subj.blank?
JSON.parse(subj)
rescue => e
Rails.logger.error("JSON parsing failed for event.subj: #{e.message}")
{}
end
def obj_hash
return {} if obj.blank?
JSON.parse(obj)
rescue => e
Rails.logger.error("JSON parsing failed for event.obj: #{e.message}")
{}
end
# Callback Hooks
# We run some special logic in order to set the source and target doi
# and their related relation type ids.
before_validation :set_source_and_target_doi!
# After the event is persisted successfully to the database, we index the event in OpenSearch.
after_commit -> { EventIndexJob.perform_later(self) }
after_commit -> { SqsUtilities.send_events_other_doi_job_message({ subj_id: subj_id, obj_id: obj_id }) }
# OpenSearch Mappings
mapping dynamic: "false" do
indexes :uuid, type: :keyword
indexes :subj_id, type: :keyword
indexes :obj_id, type: :keyword
indexes :doi, type: :keyword
indexes :orcid, type: :keyword
indexes :prefix, type: :keyword
indexes :subtype, type: :keyword
indexes :citation_type, type: :keyword
indexes :issn, type: :keyword
indexes :subj,
type: :object,
properties: {
type: { type: :keyword },
id: { type: :keyword },
uid: { type: :keyword },
proxyIdentifiers: { type: :keyword },
datePublished: {
type: :date,
format: "date_optional_time||yyyy-MM-dd||yyyy-MM||yyyy",
ignore_malformed: true,
},
registrantId: { type: :keyword },
cache_key: { type: :keyword },
}
indexes :obj,
type: :object,
properties: {
type: { type: :keyword },
id: { type: :keyword },
uid: { type: :keyword },
proxyIdentifiers: { type: :keyword },
datePublished: {
type: :date,
format: "date_optional_time||yyyy-MM-dd||yyyy-MM||yyyy",
ignore_malformed: true,
},
registrantId: { type: :keyword },
cache_key: { type: :keyword },
}
indexes :source_doi, type: :keyword
indexes :target_doi, type: :keyword
indexes :source_relation_type_id, type: :keyword
indexes :target_relation_type_id, type: :keyword
indexes :source_id, type: :keyword
indexes :source_token, type: :keyword
indexes :message_action, type: :keyword
indexes :relation_type_id, type: :keyword
indexes :registrant_id, type: :keyword
indexes :access_method, type: :keyword
indexes :metric_type, type: :keyword
indexes :total, type: :integer
indexes :license, type: :text, fields: { keyword: { type: "keyword" } }
indexes :error_messages, type: :object
indexes :callback, type: :text
indexes :aasm_state, type: :keyword
indexes :state_event, type: :keyword
indexes :year_month, type: :keyword
indexes :created_at, type: :date
indexes :updated_at, type: :date
indexes :indexed_at, type: :date
indexes :occurred_at, type: :date
indexes :citation_id, type: :keyword
indexes :citation_year, type: :integer
indexes :cache_key, type: :keyword
end
class << self
def reindex_touched_dois(start_date:, end_date:, threads: 10)
total = 0
start_date.to_date.upto(end_date.to_date) do |date|
dois = Set.new
where(updated_at: date.all_day)
.where(source_relation_type_id: RelationTypes::SOURCE_RELATION_TYPES)
.distinct.pluck(:source_doi)
.each { |doi| dois << doi }
where(updated_at: date.all_day)
.where(target_relation_type_id: RelationTypes::TARGET_RELATION_TYPES)
.distinct.pluck(:target_doi)
.each { |doi| dois << doi }
# Test performance before enabling SQS queues.
# Parallel.each(dois.to_a, in_threads: threads) do |doi|
# SqsUtilities.send_events_doi_index_message(doi)
# end
total += dois.size
end
total
end
end
end