Class: Hyrax::Statistic

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
ActionDispatch::Routing::PolymorphicRoutes
Defined in:
app/models/hyrax/statistic.rb

Direct Known Subclasses

FileDownloadStat, FileViewStat, WorkViewStat

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_for(object, attrs) ⇒ Object



16
17
18
# File 'app/models/hyrax/statistic.rb', line 16

def build_for(object, attrs)
  new attrs.merge(filter(object))
end

.convert_date(date_time) ⇒ Object



20
21
22
# File 'app/models/hyrax/statistic.rb', line 20

def convert_date(date_time)
  date_time.to_datetime.to_i * 1000
end

.ga_statistics(start_date, object) ⇒ Object

Hyrax::Download is sent to Hyrax::Analytics.profile as #hyrax__download see Legato::ProfileMethods.method_name_from_klass



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/hyrax/statistic.rb', line 30

def ga_statistics(start_date, object)
  path = polymorphic_path(object)
  profile = Hyrax::Analytics.profile
  unless profile
    Hyrax.logger.error("Google Analytics profile has not been established. Unable to fetch statistics.")
    return []
  end
  profile.hyrax__pageview(sort: 'date',
                          start_date: start_date,
                          end_date: Date.yesterday,
                          limit: 10_000)
         .for_path(path)
end

.query_works(query) ⇒ Object



44
45
46
47
# File 'app/models/hyrax/statistic.rb', line 44

def query_works(query)
  models = Hyrax.config.curation_concerns.map { |m| "\"#{m}\"" }
  Hyrax::SolrService.query("has_model_ssim:(#{models.join(' OR ')})", fl: query, rows: 100_000)
end

.resource_typesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/hyrax/statistic.rb', line 54

def resource_types
  results = query_works("resource_type_tesim")
  resource_types = []
  results.each do |y|
    if y["resource_type_tesim"].nil? || (y["resource_type_tesim"] == [""])
      resource_types.push("Unknown")
    elsif y["resource_type_tesim"].count > 1
      y["resource_type_tesim"].each do |t|
        resource_types.push(t)
      end
    else
      resource_types.push(y["resource_type_tesim"].join(""))
    end
  end
  resource_types.group_by { |rt| rt }.transform_values(&:count)
end

.statistics(object, start_date, user_id = nil) ⇒ Object



24
25
26
# File 'app/models/hyrax/statistic.rb', line 24

def statistics(object, start_date, user_id = nil)
  combined_stats object, start_date, cache_column, event_type, user_id
end

.statistics_for(object) ⇒ Object



12
13
14
# File 'app/models/hyrax/statistic.rb', line 12

def statistics_for(object)
  where(filter(object))
end

.work_typesObject



49
50
51
52
# File 'app/models/hyrax/statistic.rb', line 49

def work_types
  results = query_works("human_readable_type_tesim")
  results.group_by { |result| result['human_readable_type_tesim'].join('') }.transform_values(&:count)
end

Instance Method Details

#to_flotObject



94
95
96
# File 'app/models/hyrax/statistic.rb', line 94

def to_flot
  [self.class.convert_date(date), send(cache_column)]
end