Skip to content

Useful console commands (ORCID, MaIS)

Peter Mangiafico edited this page Jun 16, 2021 · 41 revisions

Authorization endpoints for users to connect their Sunet with ORCID

See if services are running

Mais.working?
=> true
Orcid.working?
=> true

Fetch all users from MaIS who have integrated with SUNET

This lets you determine how many stanford users have connected with ORCID, scopes that have been authorized, along with tokens.

users = Mais.client.fetch_orcid_users;
users.size
=> 11
sunets = users.map {|user| user.sunetid}
# an array of sunets

# determine how many have authorized write vs read; assumes that a connection implies read scope
scopes = {read: users.size, write: 0}
users.each {|user| scopes[:write] += 1 if user.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

Fetch a single user from MaIS

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>');

Rake task for users who have integrated with ORCID

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

Fetch all the works from an ORCID profile for a given ORCID ID

Orcid.client.fetch_works('https://sandbox.orcid.org/0000-0002-7262-6251')

Rake task for pushing works to ORCID from Profiles

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>']

Rake tasks for pulling works from ORCID and adding to Profiles

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']

Examining our local database

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

Export sunets for users who have authorized write scope

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'
users = Mais.client.fetch_orcid_users.map {|user| {sunetid: user.sunetid, orcidid: user.orcidid}}.compact.uniq;

CSV.open(outputfile, "w") do |csv|
  csv << ["sunet","email","name","cap_profile_id","orcidid","num_orcid_pubs","num_profile_pubs","date"]  
  users.each do |user|
    orcidid=user[:orcidid]
    sunetid=user[:sunetid]
    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;