Class: Toccatore::UsageUpdate

Inherits:
Base
  • Object
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

#format_event(type, data, options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/toccatore/usage_update.rb', line 93

def format_event type, data, options
  { "uuid" => SecureRandom.uuid,
    "message-action" => "add",
    "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



60
61
62
63
64
65
# File 'lib/toccatore/usage_update.rb', line 60

def get_data reponse 
  return OpenStruct.new(body: { "errors" => "Queue is empty" }) if reponse.messages.empty?

  body = JSON.parse(reponse.messages[0].body)
  Maremma.get(body["report_id"])
end

#metrics_urlObject



85
86
87
# File 'lib/toccatore/usage_update.rb', line 85

def metrics_url
  ENV['SASHIMI_URL']
end

#parse_data(result, options = {}) ⇒ Object



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
# File 'lib/toccatore/usage_update.rb', line 112

def parse_data(result, options={})
  return result.body.fetch("errors") if result.body.fetch("errors", nil).present?

  items = result.body.dig("data","report","report-datasets")
  header = result.body.dig("data","report","report-header")
  report_id = metrics_url + "/" + result.body.dig("data","report","id")

  created = header.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. 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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/toccatore/usage_update.rb', line 46

def process_data(options = {})
  message = get_message
  data = get_data(message)
  data = parse_data(data, options)

  return [OpenStruct.new(body: { "data" => [] })] if data.empty?

  errors = push_data(data, options)
  if errors < 1
    delete_message message
  end
  errors
end

#push_data(items, options = {}) ⇒ Object

method returns number of errors



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/toccatore/usage_update.rb', line 69

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|
      error_total += push_item(item, options)
    end
    error_total
  end
end

#push_item(item, options = {}) ⇒ Object



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
174
# File 'lib/toccatore/usage_update.rb', line 141

def push_item(item, options={})
  return OpenStruct.new(body: { "errors" => [{ "title" => "Access token missing." }] }) if options[:access_token].blank?
  return 0 if item == "Queue is empty"

  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: 'json',
                                    host: host)
  # else
  #   response = Maremma.post(push_url, data: item.to_json,
  #                                     bearer: options[:access_token],
  #                                     content_type: 'json',
  #                                     host: host)
  # end

  # return 0 if successful, 1 if error
  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
42
43
44
# 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
  num_messages = total
  while num_messages > 0
    # walk through paginated results
    
    # (0...total).each do |page|
      # options[:offset] = page * 1
      # options[:total] = total
      error_total += process_data(options)
      num_messages = get_total(options)
    # end
  end
  text = "#{num_messages} works processed with #{error_total} errors for Usage Reports Queue"

  puts text
  # send slack notification
  options[:level] = total > 0 ? "good" : "warning"
  options[:title] = "Report for #{source_id}"
  send_notification_to_slack(text, options) if options[:slack_webhook_url].present?

  # return number of works queued
  get_total(options)
end

#source_idObject



89
90
91
# File 'lib/toccatore/usage_update.rb', line 89

def source_id
  "usage_update"
end