Skip to content

Instantly share code, notes, and snippets.

@diamondap
Last active April 23, 2021 20:28
Show Gist options
  • Save diamondap/a17e7825c5cd751e5385adcce83d1e6a to your computer and use it in GitHub Desktop.
Save diamondap/a17e7825c5cd751e5385adcce83d1e6a to your computer and use it in GitHub Desktop.

Pharos Code vs. Registry Code

Defining routes for a few basic controllers

Pharos Routes

Be sure to scroll sideways, so you can take it all in.

  # INSTITUTION ROUTES
  institution_ptrn = /(?:[a-zA-Z0-9\-_]+)(?:\.[a-zA-Z]+)+/
  resources :institutions, format: [:json, :html], param: :institution_identifier, institution_identifier: institution_ptrn
  resources :institutions, only: [:index], format: :json, param: :institution_identifier, institution_identifier: institution_ptrn, path: 'api/v2/institutions'
  get ':institution_identifier/single_snapshot', to: 'institutions#single_snapshot', format: [:html, :json], institution_identifier: institution_ptrn, as: :institution_snapshot
  get 'api/v2/:institution_identifier/single_snapshot', to: 'institutions#single_snapshot', format: [:html, :json], institution_identifier: institution_ptrn, as: :api_institution_snapshot
  get '/group_snapshot', to: 'institutions#group_snapshot', format: [:html, :json], as: :group_snapshot
  get 'api/v2/group_snapshot', to: 'institutions#group_snapshot', format: [:html, :json], as: :api_group_snapshot
  get '/:institution_identifier/deactivate', to: 'institutions#deactivate', as: :deactivate_institution, institution_identifier: institution_ptrn
  get '/:institution_identifier/reactivate', to: 'institutions#reactivate', as: :reactivate_institution, institution_identifier: institution_ptrn
  get '/:institution_identifier/enable_otp', to: 'institutions#enable_otp', as: :enable_otp_institution, format: [:html, :json], institution_identifier: institution_ptrn
  get '/:institution_identifier/disable_otp', to: 'institutions#disable_otp', as: :disable_otp_institution, format: [:html, :json], institution_identifier: institution_ptrn
  post 'api/v2/:institution_identifier/trigger_bulk_delete', to: 'institutions#trigger_bulk_delete', as: :api_bulk_deletion, format: :json, institution_identifier: institution_ptrn
  get '/:institution_identifier/confirm_bulk_delete_institution', to: 'institutions#partial_confirmation_bulk_delete', as: :bulk_deletion_institutional_confirmation, format: [:html, :json], institution_identifier: institution_ptrn
  post 'api/v2/:institution_identifier/confirm_bulk_delete_institution', to: 'institutions#partial_confirmation_bulk_delete', as: :api_bulk_deletion_institutional_confirmation, format: :json, institution_identifier: institution_ptrn
  get '/:institution_identifier/confirm_bulk_delete_admin', to: 'institutions#final_confirmation_bulk_delete', as: :bulk_deletion_admin_confirmation, format: [:html, :json], institution_identifier: institution_ptrn
  post 'api/v2/:institution_identifier/confirm_bulk_delete_admin', to: 'institutions#final_confirmation_bulk_delete', as: :api_bulk_deletion_admin_confirmation, format: :json, institution_identifier: institution_ptrn
  get '/:institution_identifier/finished_bulk_delete', to: 'institutions#finished_bulk_delete', as: :bulk_deletion_finished, format: [:html, :json], institution_identifier: institution_ptrn
  post 'api/v2/:institution_identifier/finished_bulk_delete', to: 'institutions#finished_bulk_delete', as: :api_bulk_deletion_finished, format: :json, institution_identifier: institution_ptrn
  get '/notifications/deletion', to: 'institutions#deletion_notifications', as: :institution_deletion_notifications, format: [:html, :json]
  get 'api/v2/notifications/deletion', to: 'institutions#deletion_notifications', as: :api_institution_deletion_notifications, format: [:html, :json]
  get '/:institution_identifier/mass_forced_password_update', to: 'institutions#mass_forced_password_update', as: :mass_forced_password_update, format: [:html, :json], institution_identifier: institution_ptrn
  get 'api/v2/:institution_identifier/mass_forced_password_update', to: 'institutions#mass_forced_password_update', as: :api_mass_forced_password_update, format: [:html, :json], institution_identifier: institution_ptrn	
  
# INTELLECTUAL OBJECT ROUTES
  object_ptrn = /(\w+\.)*\w+(\.edu|\.com|\.org|\.museum)(\%|\/)[\w\-\.\%\?\=\(\)\:\#\[\]\!\$\&\'\*\+\,\;\_\~\ \p{L}]+/
  resources :intellectual_objects, only: [:show, :edit, :update, :destroy], format: [:json, :html], param: :intellectual_object_identifier, intellectual_object_identifier: object_ptrn, path: 'objects'
  get 'objects/:intellectual_object_identifier/restore', to: 'intellectual_objects#restore', format: [:json, :html], intellectual_object_identifier: object_ptrn, as: :intellectual_object_restore
  get 'objects', to: 'intellectual_objects#index', format: [:json, :html]
  get 'objects/:institution_identifier', to: 'intellectual_objects#index', format: [:json, :html], institution_identifier: institution_ptrn, as: :intellectual_objects
  resources :intellectual_objects, only: [:show, :update, :destroy], format: :json, param: :intellectual_object_identifier, intellectual_object_identifier: object_ptrn, path: 'api/v2/objects'
  get 'api/v2/objects/:institution_identifier', to: 'intellectual_objects#index', format: [:json, :html], institution_identifier: institution_ptrn
  get 'api/v2/objects', to: 'intellectual_objects#index', format: [:json, :html]
  post 'api/v2/objects/:institution_identifier', to: 'intellectual_objects#create', format: :json, institution_identifier: institution_ptrn
  get 'member-api/v2/objects/:institution_identifier', to: 'intellectual_objects#index', format: [:json, :html], institution_identifier: institution_ptrn
  get 'member-api/v2/objects/', to: 'intellectual_objects#index', format: [:json, :html]
  get 'member-api/v2/objects/:intellectual_object_identifier/restore', to: 'intellectual_objects#restore', format: :json, intellectual_object_identifier: object_ptrn
  put 'api/v2/objects/:intellectual_object_identifier/restore', to: 'intellectual_objects#restore', format: :json, intellectual_object_identifier: object_ptrn
  delete 'api/v2/objects/:intellectual_object_identifier/delete', to: 'intellectual_objects#destroy', format: :json, intellectual_object_identifier: object_ptrn
  delete 'objects/:intellectual_object_identifier/confirm_delete', to: 'intellectual_objects#confirm_destroy', format: [:html, :json], intellectual_object_identifier: object_ptrn, as: :object_confirm_destroy
  delete 'api/v2/objects/:intellectual_object_identifier/confirm_delete', to: 'intellectual_objects#confirm_destroy', format: :json, intellectual_object_identifier: object_ptrn, as: :api_object_confirm_destroy
  get 'objects/:intellectual_object_identifier/confirm_delete', to: 'intellectual_objects#confirm_destroy', format: [:html, :json], intellectual_object_identifier: object_ptrn, as: :get_object_confirm_destroy
  get 'api/v2/objects/:intellectual_object_identifier/confirm_delete', to: 'intellectual_objects#confirm_destroy', format: :json, intellectual_object_identifier: object_ptrn, as: :get_api_object_confirm_destroy
  get 'objects/:intellectual_object_identifier/finish_delete', to: 'intellectual_objects#finished_destroy', format: [:html, :json], intellectual_object_identifier: object_ptrn, as: :get_object_finish_destroy
  get 'api/v2/objects/:intellectual_object_identifier/finish_delete', to: 'intellectual_objects#finished_destroy', format: :json, intellectual_object_identifier: object_ptrn, as: :get_api_object_finish_destroy
  
  # WORK ITEM ROUTES
  resources :work_items, only: [:index, :create, :show, :update, :edit], format: [:html, :json], path: 'items'
  put 'items/', to: 'work_items#update', format: :json
  resources :work_items, path: '/api/v2/items'
  resources :work_items, only: [:index], path: 'member-api/v2/items', format: [:json, :html]
  get '/api/v2/items/:etag/:name/:bag_date', to: 'work_items#show', as: :work_item_by_etag, name: /[^\/]*/, bag_date: /[^\/]*/
  put '/api/v2/items/:etag/:name/:bag_date', to: 'work_items#update', format: 'json', as: :work_item_api_update_by_etag, name: /[^\/]*/, bag_date: /[^\/]*/
  get 'items/items_for_restore', to: 'work_items#items_for_restore', format: :json
  get 'items/items_for_delete', to: 'work_items#items_for_delete', format: :json
  get 'items/ingested_since', to: 'work_items#ingested_since', format: :json
  get 'items/set_restoration_status', to: 'work_items#set_restoration_status', format: :json
  get 'api/v2/items/search', to: 'work_items#api_search', format: :json
  get 'items/:id/requeue', to: 'work_items#requeue', format: [:json, :html], as: :requeue_work_item
  get 'notifications/successful_restoration', to: 'work_items#notify_of_successful_restoration', format: :json
  get '/api/v2/notifications/successful_restoration', to: 'work_items#notify_of_successful_restoration', format: :json
  get 'notifications/spot_test_restoration/:id', to: 'work_items#spot_test_restoration', format: :json
  get '/api/v2/notifications/spot_test_restoration/:id', to: 'work_items#spot_test_restoration', format: :json

  # GENERIC FILE ROUTES
  file_ptrn = /(\w+\.)*\w+(\.edu|\.com|\.org|\.museum)(\%2[Ff]|\/)+[\w\-\/\.\%\?\=\(\)\:\#\[\]\!\$\&\'\*\+\,\;\_\~\ \p{L}]+(\%2[fF]|\/)+[\w\-\/\.\%\@\?\=\(\)\:\#\[\]\!\$\&\'\*\+\,\;\_\~\ \p{L}]+/
  resources :generic_files, only: [:show, :update, :destroy], format: [:json, :html], defaults: { format: :html }, param: :generic_file_identifier, generic_file_identifier: file_ptrn, path: 'files'
  resources :generic_files, only: [:show, :update, :destroy], format: [:json, :html], defaults: { format: :json }, param: :generic_file_identifier, generic_file_identifier: file_ptrn, path: 'api/v2/files'
  get '/api/v2/files/:institution_identifier', to: 'generic_files#index', format: [:json, :html], institution_identifier: institution_ptrn, as: :institution_files_api
  get '/api/v2/files/:intellectual_object_identifier', to: 'generic_files#index', format: [:json, :html], intellectual_object_identifier: object_ptrn, as: :intellectual_object_files_api
  get '/api/v2/files/', to: 'generic_files#index', format: [:json], as: :generic_files_admin_list
  get 'files/:institution_identifier', to: 'generic_files#index', format: [:json, :html], institution_identifier: institution_ptrn, as: :institution_files
  get 'files/:intellectual_object_identifier', to: 'generic_files#index', format: [:json, :html], intellectual_object_identifier: object_ptrn, as: :intellectual_object_files
  post '/api/v2/files/:intellectual_object_id/create_batch', to: 'generic_files#create_batch', format: :json
  post '/api/v2/files/:intellectual_object_identifier', to: 'generic_files#create', format: :json, intellectual_object_identifier: object_ptrn
  put '/api/v2/files/:intellectual_object_identifier', to: 'generic_files#update', format: :json, intellectual_object_identifier: object_ptrn
  resources :generic_files, only: [:index, :create], format: [:json, :html], param: :intellectual_object_identifier, intellectual_object_identifier: object_ptrn, path: 'api/v2/files'
  get 'member-api/v2/files/:intellectual_object_identifier', to: 'generic_files#index', format: :json, intellectual_object_identifier: object_ptrn
  get 'member-api/v2/files/:institution_identifier', to: 'generic_files#index', format: :json, institution_identifier: institution_ptrn
  delete 'files/confirm_delete/:generic_file_identifier', to: 'generic_files#confirm_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :file_confirm_destroy
  delete 'api/v2/files/confirm_delete/:generic_file_identifier', to: 'generic_files#confirm_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :api_file_confirm_destroy
  get 'files/confirm_delete/:generic_file_identifier', to: 'generic_files#confirm_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :get_file_confirm_destroy
  get 'api/v2/files/confirm_delete/:generic_file_identifier', to: 'generic_files#confirm_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :get_api_file_confirm_destroy
  get 'files/finish_delete/:generic_file_identifier', to: 'generic_files#finished_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :get_file_finish_destroy
  get 'api/v2/files/finish_delete/:generic_file_identifier', to: 'generic_files#finished_destroy', format: [:html, :json], generic_file_identifier: file_ptrn, as: :get_api_file_finish_destroy
  get 'files/restore/:generic_file_identifier', to: 'generic_files#restore', format: [:json, :html], generic_file_identifier: file_ptrn, as: :generic_file_restore
  get 'member-api/v2/files/restore/:generic_file_identifier', to: 'generic_files#restore', format: :json, generic_file_identifier: file_ptrn
  put 'api/v2/files/restore/:generic_file_identifier', to: 'generic_files#restore', format: :json, generic_file_identifier: file_ptrn

Registry Version of the Code Above

// Institutions
webRoutes.POST("/institutions/new", web.InstitutionCreate)
webRoutes.DELETE("/institutions/delete/:id", web.InstitutionDelete)
webRoutes.GET("/institutions/delete/:id", web.InstitutionDelete)
webRoutes.GET("/institutions/undelete/:id", web.InstitutionUndelete)
webRoutes.GET("/institutions", web.InstitutionIndex)
webRoutes.GET("/institutions/new", web.InstitutionNew)
webRoutes.GET("/institutions/show/:id", web.InstitutionShow)
webRoutes.GET("/institutions/edit/:id", web.InstitutionEdit)
webRoutes.PUT("/institutions/edit/:id", web.InstitutionUpdate)
webRoutes.POST("/institutions/edit/:id", web.InstitutionUpdate)	

// IntellectualObjects
webRoutes.GET("/objects", web.IntellectualObjectIndex)
webRoutes.GET("/objects/show/:id", web.IntellectualObjectShow)
webRoutes.GET("/objects/delete/:id", web.IntellectualObjectDelete)
webRoutes.GET("/objects/restore/:id", web.IntellectualObjectRestore)

// WorkItems 
webRoutes.GET("/work_items", web.WorkItemIndex)
webRoutes.GET("/work_items/show/:id", web.WorkItemShow)
webRoutes.GET("/work_items/edit/:id", web.WorkItemEdit)
webRoutes.PUT("/work_items/edit/:id", web.WorkItemUpdate)
webRoutes.POST("/work_items/edit/:id", web.WorkItemUpdate)
webRoutes.PUT("/work_items/requeue/:id", web.WorkItemRequeue)
webRoutes.POST("/work_items/requeue/:id", web.WorkItemRequeue)

Requeuing a WorkItem

Pharos Requeue (Controller)

  def requeue
    if @work_item
      authorize @work_item
      if @work_item.status == Pharos::Application::PHAROS_STATUSES['success']
        respond_to do |format|
          format.json { render :json => { status: 'error', message: 'Work Items that have succeeded cannot be requeued.' }, :status => :conflict }
          format.html { }
        end
      else
        options = {}
        options[:stage] = params[:item_stage] if params[:item_stage]
        options[:work_item_state_delete] = 'true' if params[:delete_state_item] && params[:delete_state_item] == 'true'
        @work_item.requeue_item(options)
        if Rails.env.development?
          flash[:notice] = 'The response from NSQ to the requeue request is as follows: Status: 200, Body: ok'
          flash.keep(:notice)
          respond_to do |format|
            format.json { render json: { status: 200, body: 'ok' } }
            format.html {
              redirect_to work_item_path(@work_item.id)
              flash[:notice] = 'The response from NSQ to the requeue request is as follows: Status: 200, Body: ok'
            }
          end
        else
          options[:stage] ? response = issue_requeue_http_post(options[:stage]) : response = issue_requeue_http_post('')
          respond_to do |format|
            format.json { render json: { status: response.code, body: response.body } }
            format.html {
              redirect_to work_item_path(@work_item.id)
              flash[:notice] = "The response from NSQ to the requeue request is as follows: Status: #{response.code}, Body: #{response.body}"
            }
          end
        end
      end
    else
      authorize current_user, :nil_index?
      respond_to do |format|
        format.json { render nothing: true, status: :not_found }
        format.html { redirect_to root_url, alert: 'That Work Item could not be found.' }
      end
    end
  end

Registry Version of the Code Above

func WorkItemRequeue(c *gin.Context) {
	aptContext := common.Context()
	req := NewRequest(c)
	item, err := pgmodels.WorkItemByID(req.Auth.ResourceID)
	if AbortIfError(c, err) {
		return
	}
	stage := c.Request.PostFormValue("Stage")
	aptContext.Log.Info().Msgf("Requeueing WorkItem %d to %s", item.ID, stage)
	err = item.SetForRequeue(stage)
	if AbortIfError(c, err) {
		return
	}
	topic := constants.TopicFor(item.Action, item.Stage)
	err = aptContext.NSQClient.Enqueue(topic, item.ID)
	redirectTo := fmt.Sprintf("/work_items/show/%d", item.ID)
	c.Redirect(http.StatusSeeOther, redirectTo)
}	

Pharos Reque (Model)

  def requeue_item(options={})
    self.status = Pharos::Application::PHAROS_STATUSES['pend']
    self.retry = true
    self.needs_admin_review = false
    self.node = nil
    self.pid = 0
    self.queued_at = nil
    if self.action == Pharos::Application::PHAROS_ACTIONS['delete']
      self.stage = Pharos::Application::PHAROS_STAGES['requested']
      self.note = 'Delete requested'
      self.outcome = 'Not started'
    elsif self.action == Pharos::Application::PHAROS_ACTIONS['restore']
      self.stage = Pharos::Application::PHAROS_STAGES['requested']
      self.note = 'Restore requested'
      self.work_item_state.delete if self.work_item_state && options[:work_item_state_delete]
    elsif self.action == Pharos::Application::PHAROS_ACTIONS['glacier_restore']
      self.stage = Pharos::Application::PHAROS_STAGES['requested']
      self.note = 'Restore requested'
      self.work_item_state.delete if self.work_item_state && options[:work_item_state_delete]
    elsif self.action == Pharos::Application::PHAROS_ACTIONS['ingest']
      if options[:stage]
        if options[:stage] == Pharos::Application::PHAROS_STAGES['fetch']
          self.stage = Pharos::Application::PHAROS_STAGES['receive']
          self.note = 'Item is pending ingest'
          self.work_item_state.delete if self.work_item_state
        elsif options[:stage] == Pharos::Application::PHAROS_STAGES['store']
          self.stage = Pharos::Application::PHAROS_STAGES['store']
          self.note = 'Item is pending storage'
        elsif options[:stage] == Pharos::Application::PHAROS_STAGES['record']
          self.stage = Pharos::Application::PHAROS_STAGES['record']
          self.note = 'Item is pending record'
        end
      end
    end
    self.save!
  end 	

Registry Version of the Code Above

func (item *WorkItem) SetForRequeue(stage string) error {
	topic := constants.TopicFor(item.Action, stage)
	if topic == "" {
		return common.ErrInvalidRequeue
	}
	item.Stage = stage
	item.Status = constants.StatusPending
	item.Retry = true
	item.NeedsAdminReview = false
	item.Node = ""
	item.PID = 0
	item.Note = fmt.Sprintf("Requeued for %s", item.Stage)
	return item.Save()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment