-
Notifications
You must be signed in to change notification settings - Fork 3
Useful console commands (ORCID, MaIS)
-
Production: https://authorize.stanford.edu
Mais.working?
=> true
Orcid.working?
=> true
This lets you fetch a single stanford users that has connected with ORCID, scopes that have been authorized, along with tokens. It returns nil for a user who has not done this.
user = Mais.client.fetch_orcid_user(sunetid:'<SUNETID>');
To see users who have integrated:
RAILS_ENV=production bundle exec rake mais:fetch
To update our local database with ORCID ids for these users (this is a scheduled task):
RAILS_ENV=production bundle exec rake mais:update_authors
Orcid.client.fetch_works('https://sandbox.orcid.org/0000-0002-7262-6251')
All users who have integrated (this is a scheduled task):
RAILS_ENV=production bundle exec rake orcid:add_all_works
A single user:
RAILS_ENV=production bundle exec rake orcid:add_author_works['<SUNETID>']
Delete works at ORCID for a single user and reset their put codes in our database. This should rarely be done, only for debugging, testing, etc:
RAILS_ENV=production bundle exec rake orcid:delete_author_works['<SUNETID>']
All users who have integrated:
RAILS_ENV=production bundle exec rake orcid:harvest_authors
A single user:
RAILS_ENV=production bundle exec rake orcid:harvest_author['petucket']
We store orcids in our local database, but not auth tokens, which are always pulled from the MaIS API as needed. We also store "put codes" in the contribution table for any publications we have already sent to ORCID so we know not to send them again. You can use see this in our database:
sunetid='petucket';
author = Author.find_by(sunetid: sunetid);
author.orcidid
=> "https://sandbox.orcid.org/0000-0002-2230-4756"
Contribution.where(author_id: author.id).where.not(orcid_put_code: nil).count # shows the number of publications that have been sent to ORCID
=> 0
Contribution.where(author_id: author.id).where(orcid_put_code: nil).count # shows the number of publications that have NOT been sent to ORCID
=> 6
This lets you determine how many stanford users have connected with ORCID, scopes that have been authorized, along with tokens.
orcid_users = Mais.client.fetch_orcid_users;
# note that the `fetch_orcid_users` command will returned some duplicated sunets, because it returns a history
# of all changes for that sunet, with the last entry being the current scope (i.e. if they change scope, they will be returned twice)
sunets = orcid_users.map {|user| user.sunetid}.uniq;
# determine how many have authorized write vs read; assumes that a connection implies read scope
# note that the `fetch_orcid_user` for a single user will return the latest scope for that user (no dupes)
scopes = {read: sunets.size, write: 0}
sunets.each { |sunetid| scopes[:write] += 1 if Mais::Client.new.fetch_orcid_user(sunetid:sunetid).update? };
scopes
=> {:read=>915, :write=>203}
# this doesn't give you scopes, but does let you know who has authorized orcid and has a public profile with harvest enabled, it assumes we have been regularly running the rake task in cron to discover new users from MaIS:
users = Author.where.not(orcidid: nil).where(cap_visibility:'public',cap_import_enabled:true).size
You can run a query against ORCID and get ORCIDiDs back as a result.
orcid_client = Orcid::Client.new
response = orcid_client.search('ringgold-org-id:6429)OR(orgname="Stanford%20University")OR("grid.168010.e")OR(email=*.stanford.edu)')
response['result'].each do |result|
puts result['orcid-identifier']['uri']
end
orcid_client = Orcid::Client.new
orcidid = 'https://sandbox.orcid.org/0000-0002-7262-6251'
name = orcid_client.fetch_name(orcidid).join(' ')
puts name
This exports all sunets for users who have authorized write scope and who have public profiles and harvest enabled. Useful for communicating to users who are likely to have publications pushed.
require 'csv'
outputfile = 'tmp/public_sunets.csv'
sunets = Mais.client.fetch_orcid_users.map {|user| user.sunetid if user.update?}.compact.uniq;
CSV.open(outputfile, "w") do |csv|
csv << ["sunet","email","name","cap_profile_id","orcidid","num_orcid_pubs","num_profile_pubs","date"]
sunets.each do |sunetid|
mais_user = Mais::Client.new.fetch_orcid_user(sunetid:sunetid)
orcidid=mais_user.orcidid
number_of_orcid_pubs = Orcid.client.fetch_works(orcidid)[:group].size
author = Author.find_by(sunetid: sunetid)
if author && author.cap_import_enabled && author.cap_visibility='public'
name = "#{author.first_name} #{author.last_name}"
number_of_profile_pubs=author.contributions.where(status:'approved',visibility:'public').size
csv << [sunetid,"#{sunetid}@stanford.edu",name,author.cap_profile_id,orcidid,number_of_orcid_pubs,number_of_profile_pubs,Time.now]
end
end
end;
Look at the log file, though note that the number of ORCID users is a total count of anyone with a linked SUNET, and will include people with read only scope. You can filter to get just user counts with write scope by using the queries shown above.
ssh pub@sul-pub-prod
cd pub/current
grep "Orcid::AddWorks Updated" log/orcid_add_works.log
I, [2021-06-21T11:51:58.540977 #4706] INFO -- : Orcid::AddWorks Updated 8098 contributor records from 989 ORCID users.
I, [2021-06-22T06:01:16.395736 #2481] INFO -- : Orcid::AddWorks Updated 5 contributor records from 991 ORCID users.
Or you can also count the contributions with non-nil put codes between specific dates, though keep in mind that if a contribution is updated in some other way (i.e. it had its status or visibility changed), this will increase the number:
date1=Time.parse('June 22 2021')
date2=Time.parse('June 23 2021')
Contribution.where.not(orcid_put_code:nil).where('updated_at >= ? and updated_at <= ?',date1,date2).count
=> 7
# or all of them
Contribution.where.not(orcid_put_code: nil).size
=> 12769