sufia-models 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77e86bd02adbf9fa79ab2de87508dc843d402376
4
- data.tar.gz: 0c8fab48b8fce691fd8973a3839b4e0c7c556138
3
+ metadata.gz: 1400794c267e0bde112c7a4668b7ee6e06e9b327
4
+ data.tar.gz: 298a15cf5c6e769a2526d81cbb815916d5fe3630
5
5
  SHA512:
6
- metadata.gz: e1bfec2d18baea1222f4913180e4c20dd58bb1ca6db08de6867c8f1cb802a48ceeb71859d16457aa5662e685df5ab7195c7659e3ed7b69442a10c55bb37b3312
7
- data.tar.gz: f9800b7407df366242d621e8316af5db16533dd1e9c2030480ded54514243069827e662de6239c3acfe8a30e04e45548d4d7546c1db4c9ba8abc12aeca3bb5a5
6
+ metadata.gz: f112d21ca35fef090bbe579b096d758eb17e4c592bc16c40f7f4a2a94c646e9859ce733aadeb9580acacee22495934eed3f764e14701068c2c772fe142a299dc
7
+ data.tar.gz: 6b8047750da470d340a0a985a3ffdd722b2a09dc6ecf04da43eed32c18e8b9559abaf3a44152144d83bc12d0dfbe01db16a73eaeb4e69b1c346397bb26ca09c9
@@ -0,0 +1,35 @@
1
+ module Sufia
2
+ module FileStatUtils
3
+
4
+ def to_flots stats
5
+ stats.map {|stat| stat.to_flot}
6
+ end
7
+
8
+ def convert_date date_time
9
+ date_time.to_datetime.to_i * 1000
10
+ end
11
+
12
+ private
13
+
14
+ def cached_stats(file_id, start_date, method)
15
+ stats = self.where(file_id:file_id).order(date: :asc)
16
+ ga_start_date = stats.size > 0 ? stats[stats.size-1].date + 1.day : start_date.to_date
17
+ {ga_start_date: ga_start_date, cached_stats: stats.to_a }
18
+ end
19
+
20
+ def combined_stats file_id, start_date, object_method, ga_key
21
+ stat_cache_info = cached_stats( file_id, start_date, object_method)
22
+ stats = stat_cache_info[:cached_stats]
23
+ if stat_cache_info[:ga_start_date] < Date.today
24
+ ga_stats = ga_statistics(stat_cache_info[:ga_start_date], file_id)
25
+ ga_stats.each do |stat|
26
+ lstat = self.new file_id:file_id, date: stat[:date], object_method => stat[ga_key]
27
+ lstat.save unless Date.parse(stat[:date]) == Date.today
28
+ stats << lstat
29
+ end
30
+ end
31
+ stats
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ class FileDownloadStat < ActiveRecord::Base
2
+ extend Sufia::FileStatUtils
3
+
4
+ def to_flot
5
+ [ self.class.convert_date(date), downloads ]
6
+ end
7
+
8
+ def self.statistics file_id, start_date
9
+ combined_stats file_id, start_date, :downloads, :totalEvents
10
+ end
11
+
12
+ # Sufia::Download is sent to Sufia::Analytics.profile as #sufia__download
13
+ # see Legato::ProfileMethods.method_name_from_klass
14
+ def self.ga_statistics start_date, file_id
15
+ Sufia::Analytics.profile.sufia__download(sort: 'date', start_date: start_date, end_date: Date.yesterday).for_file(file_id)
16
+ end
17
+
18
+ end
@@ -8,52 +8,23 @@ class FileUsage
8
8
  earliest = Sufia.config.analytic_start_date
9
9
  self.created = DateTime.parse(::GenericFile.find(id).create_date)
10
10
  self.created = earliest > created ? earliest : created unless earliest.blank?
11
- self.downloads = download_statistics
12
- self.pageviews = pageview_statistics
11
+ self.downloads = FileDownloadStat.to_flots FileDownloadStat.statistics(id, created)
12
+ self.pageviews = FileViewStat.to_flots FileViewStat.statistics(id, created)
13
13
  end
14
14
 
15
15
  def total_downloads
16
- self.downloads.map(&:marshal_dump).reduce(0) { |total, result| total + result[:totalEvents].to_i }
16
+ self.downloads.reduce(0) { |total, result| total + result[1].to_i }
17
17
  end
18
18
 
19
19
  def total_pageviews
20
- self.pageviews.map(&:marshal_dump).reduce(0) { |total, result| total + result[:pageviews].to_i }
20
+ self.pageviews.reduce(0) { |total, result| total + result[1].to_i }
21
21
  end
22
22
 
23
23
  # Package data for visualization using JQuery Flot
24
24
  def to_flot
25
25
  [
26
- { label: "Pageviews", data: pageviews_to_flot },
27
- { label: "Downloads", data: downloads_to_flot }
26
+ { label: "Pageviews", data: pageviews },
27
+ { label: "Downloads", data: downloads }
28
28
  ]
29
29
  end
30
-
31
- private
32
-
33
- # Sufia::Download is sent to Sufia::Analytics.profile as #sufia__download
34
- # see Legato::ProfileMethods.method_name_from_klass
35
- def download_statistics
36
- Sufia::Analytics.profile.sufia__download(sort: 'date', start_date: created).for_file(self.id)
37
- end
38
-
39
- # Sufia::Pageview is sent to Sufia::Analytics.profile as #sufia__pageview
40
- # see Legato::ProfileMethods.method_name_from_klass
41
- def pageview_statistics
42
- Sufia::Analytics.profile.sufia__pageview(sort: 'date', start_date: created).for_path(self.path)
43
- end
44
-
45
- def pageviews_to_flot values = Array.new
46
- self.pageviews.map(&:marshal_dump).map do |result_hash|
47
- values << [ (Date.parse(result_hash[:date]).to_time.to_i * 1000), result_hash[:pageviews].to_i ]
48
- end
49
- return values
50
- end
51
-
52
- def downloads_to_flot values = Array.new
53
- self.downloads.map(&:marshal_dump).map do |result_hash|
54
- values << [ (Date.parse(result_hash[:date]).to_time.to_i * 1000), result_hash[:totalEvents].to_i ]
55
- end
56
- return values
57
- end
58
-
59
30
  end
@@ -0,0 +1,18 @@
1
+ class FileViewStat < ActiveRecord::Base
2
+ extend Sufia::FileStatUtils
3
+
4
+ def to_flot
5
+ [ self.class.convert_date(date), views ]
6
+ end
7
+
8
+ def self.statistics file_id, start_date
9
+ combined_stats file_id, start_date, :views, :pageviews
10
+ end
11
+
12
+ # Sufia::Download is sent to Sufia::Analytics.profile as #sufia__download
13
+ # see Legato::ProfileMethods.method_name_from_klass
14
+ def self.ga_statistics start_date, file_id
15
+ path = Sufia::Engine.routes.url_helpers.generic_file_path(Sufia::Noid.noidify(file_id))
16
+ Sufia::Analytics.profile.sufia__pageview(sort: 'date', start_date: start_date).for_path(path)
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rails/generators'
3
+ require 'rails/generators/migration'
4
+
5
+ class Sufia::Models::CachedStatsGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ desc """
11
+ This generator adds the ability to cache usage stats to your application:
12
+ 1. Creates several database migrations if they do not exist in /db/migrate
13
+ """
14
+ # Implement the required interface for Rails::Generators::Migration.
15
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
16
+ def self.next_migration_number(path)
17
+ if @prev_migration_nr
18
+ @prev_migration_nr += 1
19
+ else
20
+ if last_migration = Dir[File.join(path, '*.rb')].sort.last
21
+ @prev_migration_nr = last_migration.sub(File.join(path, '/'), '').to_i + 1
22
+ else
23
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
24
+ end
25
+ end
26
+ @prev_migration_nr.to_s
27
+ end
28
+
29
+ def banner
30
+ say_status("warning", "ADDING STATS CACHING-RELATED SUFIA MODELS", :yellow)
31
+ end
32
+
33
+ # Setup the database migrations
34
+ def copy_migrations
35
+ # Can't get this any more DRY, because we need this order.
36
+ [
37
+ 'create_file_view_stats.rb',
38
+ 'create_file_download_stats.rb'
39
+ ].each do |file|
40
+ better_migration_template file
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def better_migration_template(file)
47
+ begin
48
+ migration_template "migrations/#{file}", "db/migrate/#{file}"
49
+ rescue Rails::Generators::Error => e
50
+ say_status("warning", e.message, :yellow)
51
+ end
52
+ end
53
+ end
@@ -18,6 +18,7 @@ This generator makes the following changes to your application:
18
18
  6. Installs Blacklight gallery
19
19
  7. Runs full-text generator
20
20
  8. Runs proxies generator
21
+ 9. Runs cached stats generator
21
22
  """
22
23
 
23
24
  # Implement the required interface for Rails::Generators::Migration.
@@ -56,9 +57,7 @@ This generator makes the following changes to your application:
56
57
  'add_linkedin_to_users.rb',
57
58
  'create_tinymce_assets.rb',
58
59
  'create_content_blocks.rb',
59
- 'create_featured_works.rb',
60
- 'create_proxy_deposit_requests.rb',
61
- 'create_proxy_deposit_rights.rb'
60
+ 'create_featured_works.rb'
62
61
  ].each do |file|
63
62
  better_migration_template file
64
63
  end
@@ -106,10 +105,15 @@ This generator makes the following changes to your application:
106
105
  end
107
106
 
108
107
  # Sets up proxies and transfers
109
- def full_text_indexing
108
+ def proxies
110
109
  generate "sufia:models:proxies"
111
110
  end
112
111
 
112
+ # Sets up cached usage stats
113
+ def cached_stats
114
+ generate 'sufia:models:cached_stats'
115
+ end
116
+
113
117
  private
114
118
 
115
119
  def better_migration_template(file)
@@ -0,0 +1,12 @@
1
+ class CreateFileDownloadStats < ActiveRecord::Migration
2
+ def change
3
+ create_table :file_download_stats do |t|
4
+ t.datetime :date
5
+ t.integer :downloads
6
+ t.string :file_id
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :file_download_stats, :file_id
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class CreateFileViewStats < ActiveRecord::Migration
2
+ def change
3
+ create_table :file_view_stats do |t|
4
+ t.datetime :date
5
+ t.integer :views
6
+ t.string :file_id
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :file_view_stats, :file_id
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module Sufia
2
2
  module Models
3
- VERSION = "4.1.0"
3
+ VERSION = "4.2.0"
4
4
  end
5
5
  end
data/sufia-models.gemspec CHANGED
@@ -42,4 +42,9 @@ Gem::Specification.new do |spec|
42
42
  spec.add_dependency 'google-api-client', '~> 0.7'
43
43
  spec.add_dependency 'legato', '~> 0.3'
44
44
  spec.add_dependency 'activerecord-import', '~> 0.5'
45
+ if RUBY_VERSION < '2.1.0'
46
+ spec.add_dependency 'mini_magick', '< 4'
47
+ else
48
+ spec.add_dependency 'mini_magick'
49
+ end
45
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sufia-models
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-31 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -260,6 +260,20 @@ dependencies:
260
260
  - - "~>"
261
261
  - !ruby/object:Gem::Version
262
262
  version: '0.5'
263
+ - !ruby/object:Gem::Dependency
264
+ name: mini_magick
265
+ requirement: !ruby/object:Gem::Requirement
266
+ requirements:
267
+ - - ">="
268
+ - !ruby/object:Gem::Version
269
+ version: '0'
270
+ type: :runtime
271
+ prerelease: false
272
+ version_requirements: !ruby/object:Gem::Requirement
273
+ requirements:
274
+ - - ">="
275
+ - !ruby/object:Gem::Version
276
+ version: '0'
263
277
  description: Models and services for sufia
264
278
  email:
265
279
  - jeremy.n.friesen@gmail.com
@@ -285,6 +299,7 @@ files:
285
299
  - app/models/collection.rb
286
300
  - app/models/concerns/sufia/ability.rb
287
301
  - app/models/concerns/sufia/collection.rb
302
+ - app/models/concerns/sufia/file_stat_utils.rb
288
303
  - app/models/concerns/sufia/generic_file.rb
289
304
  - app/models/concerns/sufia/generic_file/accessible_attributes.rb
290
305
  - app/models/concerns/sufia/generic_file/audit.rb
@@ -313,7 +328,9 @@ files:
313
328
  - app/models/datastreams/properties_datastream.rb
314
329
  - app/models/domain_term.rb
315
330
  - app/models/featured_work.rb
331
+ - app/models/file_download_stat.rb
316
332
  - app/models/file_usage.rb
333
+ - app/models/file_view_stat.rb
317
334
  - app/models/follow.rb
318
335
  - app/models/generic_file.rb
319
336
  - app/models/geo_names_resource.rb
@@ -334,6 +351,7 @@ files:
334
351
  - app/services/sufia/id_service.rb
335
352
  - app/services/sufia/noid.rb
336
353
  - config/locales/sufia.en.yml
354
+ - lib/generators/sufia/models/cached_stats_generator.rb
337
355
  - lib/generators/sufia/models/fulltext_generator.rb
338
356
  - lib/generators/sufia/models/install_generator.rb
339
357
  - lib/generators/sufia/models/proxies_generator.rb
@@ -358,6 +376,8 @@ files:
358
376
  - lib/generators/sufia/models/templates/migrations/create_checksum_audit_logs.rb
359
377
  - lib/generators/sufia/models/templates/migrations/create_content_blocks.rb
360
378
  - lib/generators/sufia/models/templates/migrations/create_featured_works.rb
379
+ - lib/generators/sufia/models/templates/migrations/create_file_download_stats.rb
380
+ - lib/generators/sufia/models/templates/migrations/create_file_view_stats.rb
361
381
  - lib/generators/sufia/models/templates/migrations/create_local_authorities.rb
362
382
  - lib/generators/sufia/models/templates/migrations/create_proxy_deposit_requests.rb
363
383
  - lib/generators/sufia/models/templates/migrations/create_proxy_deposit_rights.rb
@@ -405,7 +425,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
405
425
  version: '0'
406
426
  requirements: []
407
427
  rubyforge_project:
408
- rubygems_version: 2.4.2
428
+ rubygems_version: 2.4.3
409
429
  signing_key:
410
430
  specification_version: 4
411
431
  summary: Models and services for sufia