Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
aldwin7894 committed Aug 10, 2022
1 parent 9b76e75 commit 4084b2c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
15 changes: 5 additions & 10 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class HomeController < ApplicationController
def index; end

def anilist_user_statistics
user_id = Rails.cache.fetch("ANILIST_USER_ID_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 1.week, skip_nil: true) do
query(AniList::UserIdQuery, username: ENV.fetch("ANILIST_USERNAME")).user.id
end
@user_statistics = Rails.cache.fetch("ANILIST_USER_STATS_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 1.day, skip_nil: true) do
user_id = ENV.fetch("ANILIST_USER_ID")
@user_statistics = Rails.cache.fetch("ANILIST_USER_STATS_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 30.minutes, skip_nil: true) do
user_statistics = query(AniList::UserStatisticsQuery, user_id:)
user_statistics.user.statistics.anime.to_h
end
Expand Down Expand Up @@ -84,16 +82,13 @@ def anilist_user_statistics
end

def anilist_user_activities
user_id = Rails.cache.fetch("ANILIST_USER_ID_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 1.week, skip_nil: true) do
query(AniList::UserIdQuery, username: ENV.fetch("ANILIST_USERNAME")).user.id
end

user_id = ENV.fetch("ANILIST_USER_ID")
@user_activity = []
now = Time.zone.now.beginning_of_day
last_week = (now.beginning_of_day - 1.week).to_i
page = 1
loop do
res = Rails.cache.fetch("#{now.to_i}_#{page}/ANILIST_USER_ACTIVITIES_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 4.hours, skip_nil: true) do
res = Rails.cache.fetch("#{now.to_i}_#{page}/ANILIST_USER_ACTIVITIES_#{ENV.fetch('ANILIST_USERNAME')}", expires_in: 30.minutes, skip_nil: true) do
data = query(AniList::UserAnimeActivitiesQuery, date: last_week, user_id:, page:, per_page: 50)
{ data: data.page.activities.to_a.map(&:to_h), has_next_page: data.page.page_info.has_next_page? }
end
Expand Down Expand Up @@ -147,7 +142,7 @@ def lastfm_stats
end

def lastfm_top_artists
@lastfm_top_artists = Rails.cache.fetch("LASTFM_TOP_ARTISTS", expires_in: 1.day, skip_nil: true) do
@lastfm_top_artists = Rails.cache.fetch("LASTFM_TOP_ARTISTS", expires_in: 1.week, skip_nil: true) do
LASTFM_CLIENT.user.get_top_artists(user: ENV.fetch("LASTFM_USERNAME"), period: "overall", limit: 12)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace :assets do

puts "Clear cache"
Rake::Task["tmp:clear"].invoke
Rake::Task["tmp:cache:clear"].invoke
Rake::Task["tmp:cache:clear"].invoke if ENV.fetch("CLEAR_CACHE_STORE", false)
end

desc "Remove 'node_modules' folder"
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
},
"version": "0.1.0",
"browserslist": [
"defaults"
"defaults",
"chrome >= 87",
"firefox >= 78",
"safari >= 13",
"edge >= 88"
],
"devDependencies": {
"@types/node": "^18.6.5",
Expand Down
2 changes: 2 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ export default defineConfig({
emptyOutDir: true,
assetsInlineLimit: 24000,
cssCodeSplit: true,
target: "esnext",
sourcemap: false,
},
});

0 comments on commit 4084b2c

Please sign in to comment.