Class: Toccatore::UsageUpdate
- Inherits:
-
Base
- Object
- Base
- Toccatore::UsageUpdate
show all
- Includes:
- Queue
- Defined in:
- lib/toccatore/usage_update.rb
Constant Summary
collapse
- LICENSE =
"https://creativecommons.org/publicdomain/zero/1.0/"
Constants inherited
from Base
Base::ICON_URL
Instance Method Summary
collapse
Methods included from Queue
#delete_message, #get_message, #get_total, #queue, #queue_url
Methods inherited from Base
#cleanup_author, #get_authors, #get_doi_ra, #get_hashed_authors, #get_name_identifier, #get_one_author, #get_one_hashed_author, #get_query_url, #get_total, #is_personal_name?, #job_batch_size, #name_detector, #normalize_doi, #orcid_as_url, #orcid_from_url, #send_notification_to_slack, #timeout, #unfreeze, #url, #validate_doi, #validate_orcid, #validate_prefix
Constructor Details
#initialize(options = {}) ⇒ UsageUpdate
Returns a new instance of UsageUpdate.
10
11
12
|
# File 'lib/toccatore/usage_update.rb', line 10
def initialize options={}
@sqs = queue options
end
|
Instance Method Details
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/toccatore/usage_update.rb', line 92
def format_event type, data, options
fail "Not type given. Report #{data[:report_id]} not proccessed" if type.blank?
fail "Access token missing." if options[:source_token].blank?
fail "Report_id is missing" if data[:report_id].blank?
{ "uuid" => SecureRandom.uuid,
"message-action" => "create",
"subj-id" => data[:report_id],
"subj"=> {
"pid"=> data[:report_id],
"issued"=> data[:created]
},
"total"=> data[:count],
"obj-id" => data[:pid],
"relation-type-id" => type,
"source-id" => "datacite-usage",
"source-token" => options[:source_token],
"occurred-at" => data[:created_at],
"license" => LICENSE
}
end
|
#get_data(reponse) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/toccatore/usage_update.rb', line 57
def get_data reponse
body = JSON.parse(reponse.messages[0].body)
url = body["report_id"]
host = URI.parse(body["report_id"]).host.downcase
puts url
puts host
puts body
puts "%%%%%%%%%%"
Maremma.get(url, timeout: 120, host: host)
end
|
#parse_data(result, options = {}) ⇒ Object
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
|
# File 'lib/toccatore/usage_update.rb', line 114
def parse_data(result, options={})
puts result.status
puts "*************"
return result.body.fetch("errors") if result.body.fetch("errors", nil).present?
return [{ "errors" => { "title" => "The report is blank" }}] if result.body.blank?
items = result.body.dig("data","report","report-datasets")
= result.body.dig("data","report","report-header")
report_id = result.url
created = .fetch("created")
Array.wrap(items).reduce([]) do |x, item|
data = {}
data[:doi] = item.dig("dataset-id").first.dig("value")
data[:pid] = normalize_doi(data[:doi])
data[:created] = created
data[:report_id] = report_id
data[:created_at] = created
instances = item.dig("performance", 0, "instance")
return x += [OpenStruct.new(body: { "errors" => "There are too many instances in #{data[:doi]} for report #{report_id}. There can only be 4" })] if instances.size > 8
x += Array.wrap(instances).reduce([]) do |ssum, instance|
data[:count] = instance.dig("count")
event_type = "#{instance.dig("metric-type")}-#{instance.dig("access-method")}"
ssum << format_event(event_type, data, options)
ssum
end
end
end
|
#process_data(options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/toccatore/usage_update.rb', line 43
def process_data(options = {})
errors = 0
message = get_message
unless message.messages.empty?
data = get_data(message)
events = parse_data(data, options)
errors = push_data(events, options)
if errors < 1
delete_message message
end
end
errors
end
|
#push_data(items, options = {}) ⇒ Object
method returns number of errors
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/toccatore/usage_update.rb', line 70
def push_data(items, options={})
if items.empty?
puts "No works found in the Queue."
0
elsif options[:access_token].blank?
puts "An error occured: Access token missing."
options[:total]
else
error_total = 0
Array(items).each do |item|
puts item
puts "*************"
error_total += push_item(item, options)
end
error_total
end
end
|
#push_item(item, options = {}) ⇒ Object
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
174
175
176
177
178
179
180
|
# File 'lib/toccatore/usage_update.rb', line 146
def push_item(item, options={})
if item["subj-id"].blank?
puts OpenStruct.new(body: { "errors" => [{ "title" => "There is no Subject" }] })
return 1
elsif options[:access_token].blank?
puts OpenStruct.new(body: { "errors" => [{ "title" => "Access token missing." }] })
return 1
elsif item["errors"].present?
puts OpenStruct.new(body: { "errors" => [{ "title" => "#{item["errors"]["title"]}" }] })
return 1
end
host = options[:push_url].presence || "https://api.test.datacite.org"
push_url = host + "/events/" + item["uuid"].to_s
data = { "data" => {
"id" => item["uuid"],
"type" => "events",
"attributes" => item.except("id") }}
response = Maremma.put(push_url, data: data.to_json,
bearer: options[:access_token],
content_type: 'application/json',
host: host)
if response.status == 201
puts "#{item['subj-id']} #{item['relation-type-id']} #{item['obj-id']} pushed to Event Data service."
0
elsif response.status == 200
puts "#{item['subj-id']} #{item['relation-type-id']} #{item['obj-id']} pushed to Event Data service for update."
0
elsif response.body["errors"].present?
puts "#{item['subj-id']} #{item['relation-type-id']} #{item['obj-id']} had an error:"
puts "#{response.body['errors'].first['title']}"
1
end
end
|
#queue_jobs(options = {}) ⇒ Object
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
|
# File 'lib/toccatore/usage_update.rb', line 14
def queue_jobs(options={})
total = get_total(options)
if total < 1
text = "No works found for in the Usage Reports Queue."
end
error_total = 0
proccessed_messages = 0
num_messages = total
while num_messages != 0
processed = process_data(options)
error_total += processed
proccessed_messages += 1 if processed == 0
num_messages -= proccessed_messages
end
text = "#{proccessed_messages} works processed with #{error_total} errors for Usage Reports Queue #{queue_url}"
puts text
options[:level] = total > 0 ? "good" : "warning"
options[:title] = "Report for #{source_id}"
send_notification_to_slack(text, options) if options[:slack_webhook_url].present?
proccessed_messages
end
|
#source_id ⇒ Object
88
89
90
|
# File 'lib/toccatore/usage_update.rb', line 88
def source_id
"usage_update"
end
|