Class: Jobs::Analysis::PlotDates

Inherits:
Base show all
Defined in:
lib/jobs/analysis/plot_dates.rb

Overview

Plot a dataset's members by year

Class Method Summary (collapse)

Methods inherited from Base

add_concern, job_list, view_paths

Class Method Details

+ (Boolean) download?

We don't want users to download the YAML file



87
88
89
# File 'lib/jobs/analysis/plot_dates.rb', line 87

def self.download?
  false
end

+ (undefined) perform(args = { })

Export the date format data

Like all view/multiexport jobs, this job saves its data out as a YAML file and then sends it to the user in various formats depending on user selectons.

Examples:

Start a job for plotting a dataset by year

Resque.enqueue(Jobs::Analysis::PlotDates,
               user_id: current_user.to_param,
               dataset_id: dataset.to_param,
               task_id: task.to_param)

Options Hash (args):

  • user_id (String)

    the user whose dataset we are to work on

  • dataset_id (String)

    the dataset to operate on

  • task_id (String)

    the analysis task we're working from



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jobs/analysis/plot_dates.rb', line 28

def self.perform(args = { })
  args.symbolize_keys!

  # Fetch the user based on ID
  user = User.find(args[:user_id])
  fail ArgumentError, 'User ID is not valid' unless user

  # Fetch the dataset based on ID
  dataset = user.datasets.find(args[:dataset_id])
  fail ArgumentError, 'Dataset ID is not valid' unless dataset

  # Make a new analysis task
  task = dataset.analysis_tasks.find(args[:task_id])
  fail ArgumentError, 'Task ID is not valid' unless task

  task.name = 'Plot dataset by date'
  task.save

  # Get the counts and normalize if requested
  dates = Solr::DataHelpers::count_by_field(dataset, :year)
  dates = normalize_document_counts(user, :year, dates, args)

  dates = dates.to_a
  dates.each { |d| d[0] = Integer(d[0]) }

  # Sort by date
  dates = dates.sort_by { |y| y[0] }

  # Save out the data, including getting the name of the normalization
  # set for pretty display
  normalization_set_name = ''
  if args[:normalize_doc_counts] == 'on'
    if args[:normalize_doc_dataset].blank?
      normalization_set_name = 'Entire Corpus'
    else
      normalization_set_name = user.datasets.find(args[:normalize_doc_dataset]).name
    end
  end

  output = { data: dates,
             percent: (args[:normalize_doc_counts] == 'on'),
             normalization_set: normalization_set_name
           }

  # Serialize out to YAML
  ios = StringIO.new
  ios.write(output.to_yaml)
  ios.original_filename = 'plot_dates.yml'
  ios.content_type = 'text/yaml'
  ios.rewind

  task.result = ios
  ios.close

  # We're done here
  task.finish!
end