Skip to content

Commit

Permalink
Merge pull request #488 from photonia-io/impressionist-fixes
Browse files Browse the repository at this point in the history
Impressionist fixes
  • Loading branch information
janosrusiczki authored Apr 10, 2023
2 parents 4d8e1af + 34d8b4e commit 1c7178d
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 139 deletions.
2 changes: 2 additions & 0 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class GraphqlController < ApplicationController
# protect_from_forgery with: :null_session

def execute
session[:dummy] = true

query = params[:query]
variables = prepare_variables(params[:variables])
operation_name = params[:operationName]
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/graphql_query_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class GraphqlQueryCollection
extralargeImageUrl: imageUrl(type: "extralarge")
dateTaken
importedAt
impressions
impressionsCount
previousPhoto {
id
name
Expand Down
7 changes: 4 additions & 3 deletions app/graphql/types/photo_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PhotoType < Types::BaseObject
field :description, String, 'Description', null: false
field :id, String, 'ID of the photo', null: false
field :imported_at, GraphQL::Types::ISO8601DateTime, 'Datetime the photo was imported', null: true
field :impressions, Integer, 'Number of impressions', null: true
field :impressions_count, Integer, 'Number of impressions', null: true
field :intelligent_thumbnail, IntelligentThumbnailType, 'Intelligent thumbnail', null: true
field :labels, [LabelType], 'Labels', null: true
field :license, String, 'License type of the photo', null: true
Expand Down Expand Up @@ -68,8 +68,9 @@ def image_url(type:)
end
end

def impressions
@object.impressions_count.zero? ? 1 : @object.impressions_count
def impressions_count
total_impressions_count = @object.impressions_count + @object.flickr_impressions_count
total_impressions_count.zero? ? 1 : total_impressions_count
end

def rekognition_label_model_version
Expand Down
44 changes: 22 additions & 22 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ class QueryType < GraphQL::Schema::Object
# Photos

def photos(page: nil, query: nil)
pagy, @photos = context[:pagy].call(
pagy, photos = context[:pagy].call(
query.present? ? Photo.search(query) : Photo.all.order(imported_at: :desc),
page:
)
@photos.define_singleton_method(:total_pages) { pagy.pages }
@photos.define_singleton_method(:current_page) { pagy.page }
@photos.define_singleton_method(:limit_value) { pagy.items }
@photos.define_singleton_method(:total_count) { pagy.count }
@photos
photos.define_singleton_method(:total_pages) { pagy.pages }
photos.define_singleton_method(:current_page) { pagy.page }
photos.define_singleton_method(:limit_value) { pagy.items }
photos.define_singleton_method(:total_count) { pagy.count }
photos
end

def photo(id:)
@photo = Photo.includes(:albums).includes(:albums_photos).friendly.find(id)
# context[:impressionist].call(@photo, 'graphql', unique: [:session_hash])
@photo
photo = Photo.includes(:albums).includes(:albums_photos).friendly.find(id)
context[:impressionist].call(photo, 'graphql', unique: [:session_hash])
photo
end

# Tags

def tag(id:)
@tag = ActsAsTaggableOn::Tag.friendly.find(id)
# context[:impressionist].call(@tag, 'graphql', unique: [:session_hash])
@tag
tag = ActsAsTaggableOn::Tag.friendly.find(id)
context[:impressionist].call(tag, 'graphql', unique: [:session_hash])
tag
end

def most_used_user_tags
Expand All @@ -108,25 +108,25 @@ def least_used_machine_tags
# Albums

def albums(page: nil)
pagy, @albums = context[:pagy].call(Album.includes(:albums_photos, :photos).order(created_at: :desc), page:)
@albums.define_singleton_method(:total_pages) { pagy.pages }
@albums.define_singleton_method(:current_page) { pagy.page }
@albums.define_singleton_method(:limit_value) { pagy.items }
@albums.define_singleton_method(:total_count) { pagy.count }
@albums
pagy, albums = context[:pagy].call(Album.includes(:albums_photos, :photos).order(created_at: :desc), page:)
albums.define_singleton_method(:total_pages) { pagy.pages }
albums.define_singleton_method(:current_page) { pagy.page }
albums.define_singleton_method(:limit_value) { pagy.items }
albums.define_singleton_method(:total_count) { pagy.count }
albums
end

def album(id:)
@album = Album.includes(:albums_photos).friendly.find(id)
# context[:impressionist].call(@album, 'graphql', unique: [:session_hash])
@album
album = Album.includes(:albums_photos).friendly.find(id)
context[:impressionist].call(album, 'graphql', unique: [:session_hash])
album
end

# Homepage

def latest_photo
latest_photo = object ? object[:latest_photo] : Photo.order(imported_at: :desc).first
# context[:impressionist].call(latest_photo, 'graphql', unique: [:session_hash])
context[:impressionist].call(latest_photo, 'graphql', unique: [:session_hash])
latest_photo
end

Expand Down
4 changes: 2 additions & 2 deletions app/javascript/photos/photo-info.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- display views -->
<span class="icon"><i class="fas fa-eye"></i></span>
<span class="has-text-weight-semibold">Views:</span>
<span v-if="!loading" class="ml-1">{{ photo.impressions }}</span>
<span v-if="!loading" class="ml-1">{{ photo.impressionsCount }}</span>
</span>
<span class="icon-text is-size-7">
<span class="icon"><i class="fas fa-camera"></i></span>
Expand Down Expand Up @@ -50,4 +50,4 @@
function momentFormat(date) {
return moment(date).format(format)
}
</script>
</script>
18 changes: 9 additions & 9 deletions app/models/album.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#
# Table name: albums
#
# id :bigint not null, primary key
# description :text
# flickr_views :integer
# impressions_count :integer default(0), not null
# serial_number :bigint
# slug :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# description :text
# flickr_impressions_count :integer default(0), not null
# impressions_count :integer default(0), not null
# serial_number :bigint
# slug :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Album < ApplicationRecord
is_impressionable counter_cache: true, unique: :session_hash
Expand Down
44 changes: 22 additions & 22 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
#
# Table name: photos
#
# id :bigint not null, primary key
# date_taken :datetime
# description :text
# exif :jsonb
# flickr_faves :integer
# flickr_json :jsonb
# flickr_original :string
# flickr_photopage :string
# flickr_views :integer
# image_data :jsonb
# imported_at :datetime
# impressions_count :integer default(0), not null
# license :string
# name :string
# privacy :enum default("public")
# rekognition_response :jsonb
# serial_number :bigint not null
# slug :string
# tsv :tsvector
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
# id :bigint not null, primary key
# date_taken :datetime
# description :text
# exif :jsonb
# flickr_faves :integer
# flickr_impressions_count :integer default(0), not null
# flickr_json :jsonb
# flickr_original :string
# flickr_photopage :string
# image_data :jsonb
# imported_at :datetime
# impressions_count :integer default(0), not null
# license :string
# name :string
# privacy :enum default("public")
# rekognition_response :jsonb
# serial_number :bigint not null
# slug :string
# tsv :tsvector
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint
#
# Indexes
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameFlickrViewsToFlickrImpressionsCount < ActiveRecord::Migration[7.0]
def change
rename_column :photos, :flickr_views, :flickr_impressions_count
rename_column :albums, :flickr_views, :flickr_impressions_count
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ChangeFlickrImpressionsCountToNotNullDefault0 < ActiveRecord::Migration[7.0]
def change
change_column_null :photos, :flickr_impressions_count, false, 0
change_column_null :albums, :flickr_impressions_count, false, 0
change_column_default :photos, :flickr_impressions_count, 0
change_column_default :albums, :flickr_impressions_count, 0
end
end
8 changes: 5 additions & 3 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CREATE TABLE public.albums (
title character varying,
description text,
serial_number bigint,
flickr_views integer,
flickr_impressions_count integer DEFAULT 0 NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
impressions_count integer DEFAULT 0 NOT NULL
Expand Down Expand Up @@ -258,7 +258,7 @@ CREATE TABLE public.photos (
license character varying,
exif jsonb,
serial_number bigint NOT NULL,
flickr_views integer,
flickr_impressions_count integer DEFAULT 0 NOT NULL,
flickr_faves integer,
imported_at timestamp without time zone,
flickr_photopage character varying,
Expand Down Expand Up @@ -922,6 +922,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230409165520'),
('20230409171752'),
('20230409171819'),
('20230409184758');
('20230409184758'),
('20230410080718'),
('20230410080845');


10 changes: 0 additions & 10 deletions lib/capistrano/tasks/albums.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,4 @@ namespace :albums do
end
end
end

task :copy_flickr_views_to_impressions_count do
on roles(:app) do
within current_path.to_s do
with rails_env: fetch(:stage).to_s do
execute :rake, 'albums:copy_flickr_views_to_impressions_count'
end
end
end
end
end
10 changes: 0 additions & 10 deletions lib/capistrano/tasks/photos.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,4 @@ namespace :photos do
end
end
end

task :copy_flickr_views_to_impressions_count do
on roles(:app) do
within current_path.to_s do
with rails_env: fetch(:stage).to_s do
execute :rake, 'photos:copy_flickr_views_to_impressions_count'
end
end
end
end
end
7 changes: 0 additions & 7 deletions lib/tasks/albums.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ namespace :albums do
SQL
)
end

desc 'Copy flickr_views to impressions_count'
task copy_flickr_views_to_impressions_count: :environment do
Album.all.each do |album|
album.update(impressions_count: album.flickr_views)
end
end
end
4 changes: 2 additions & 2 deletions lib/tasks/flickr.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace :flickr do
license: photo_hash['license'],
exif: photo_hash['exif'],
serial_number: photo_hash['id'],
flickr_views: photo_hash['count_views'],
flickr_impressions_count: photo_hash['count_views'],
flickr_faves: photo_hash['count_faves'],
imported_at: photo_hash['date_imported'],
flickr_photopage: photo_hash['photopage'],
Expand Down Expand Up @@ -83,7 +83,7 @@ namespace :flickr do
album = Album.find_or_create_by(serial_number: flickr_album['id']) do |a|
a.title = flickr_album['title']
a.description = flickr_album['description']
a.flickr_views = flickr_album['view_count']
a.flickr_impressions_count = flickr_album['view_count']
a.created_at = DateTime.strptime(flickr_album['created'], '%s')
a.updated_at = DateTime.strptime(flickr_album['last_updated'], '%s')
end
Expand Down
7 changes: 0 additions & 7 deletions lib/tasks/photos.rake
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,4 @@ namespace :photos do
end
end
end

desc 'Copy flickr_views to impressions_count'
task copy_flickr_views_to_impressions_count: :environment do
Photo.unscoped.all.each do |photo|
photo.update(impressions_count: photo.flickr_views)
end
end
end
20 changes: 10 additions & 10 deletions spec/factories/albums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#
# Table name: albums
#
# id :bigint not null, primary key
# description :text
# flickr_views :integer
# impressions_count :integer default(0), not null
# serial_number :bigint
# slug :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# description :text
# flickr_impressions_count :integer default(0), not null
# impressions_count :integer default(0), not null
# serial_number :bigint
# slug :string
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#
FactoryBot.define do
factory :album do
title { Faker::Lorem.sentence }
description { Faker::Lorem.paragraph }
flickr_views { Faker::Number.number(digits: 5) }
flickr_impressions_count { Faker::Number.number(digits: 5) }
end
end
Loading

0 comments on commit 1c7178d

Please sign in to comment.