mirror of
https://github.com/Neetpone/foalfetch.git
synced 2025-02-14 12:04:23 +01:00
feat: add visit counts
This commit is contained in:
parent
390b7ac486
commit
b9872ba287
7 changed files with 77 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
before_action :start_timer
|
before_action :start_timer
|
||||||
|
before_action :count_visit
|
||||||
before_action :setup_pagination_and_tags
|
before_action :setup_pagination_and_tags
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -9,6 +10,21 @@ class ApplicationController < ActionController::Base
|
||||||
@start_time = Time.zone.now
|
@start_time = Time.zone.now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def count_visit
|
||||||
|
key = "visit-counter/#{Digest::SHA2.hexdigest(request.remote_ip)}"
|
||||||
|
visited_today = $redis.get(key).present?
|
||||||
|
$redis.setex(key, 1.day.in_seconds, '1') unless visited_today
|
||||||
|
|
||||||
|
VisitCount.transaction do
|
||||||
|
visit_count = VisitCount.for_today
|
||||||
|
visit_count.update!(
|
||||||
|
count: visit_count.count + 1
|
||||||
|
) unless visited_today
|
||||||
|
@today_visit_count = visit_count.count
|
||||||
|
@total_visit_count = Rails.cache.fetch('total_visits', expires_in: 1.hour) { VisitCount.sum(:count) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def setup_pagination_and_tags
|
def setup_pagination_and_tags
|
||||||
@per_page = 15
|
@per_page = 15
|
||||||
per_page = params[:per_page]
|
per_page = params[:per_page]
|
||||||
|
|
13
app/models/visit_count.rb
Normal file
13
app/models/visit_count.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: visit_counts
|
||||||
|
#
|
||||||
|
# id :bigint not null, primary key
|
||||||
|
# count :integer default(0), not null
|
||||||
|
# date :date not null
|
||||||
|
#
|
||||||
|
class VisitCount < ApplicationRecord
|
||||||
|
def self.for_today
|
||||||
|
VisitCount.find_or_create_by(date: Time.zone.today)
|
||||||
|
end
|
||||||
|
end
|
|
@ -17,8 +17,7 @@ html
|
||||||
section
|
section
|
||||||
h4 Stats
|
h4 Stats
|
||||||
span.stat Page generated in #{render_time}
|
span.stat Page generated in #{render_time}
|
||||||
/ span.stat FIXME registered users
|
span.stat #{pluralize(@today_visit_count, 'visitor')} today, #{@total_visit_count} total.
|
||||||
/ span.stat FIXME visitors total
|
|
||||||
section
|
section
|
||||||
h4 FoalFetch
|
h4 FoalFetch
|
||||||
span.stat Frontend designed by DataByte, backend coded by Floorb.
|
span.stat Frontend designed by DataByte, backend coded by Floorb.
|
||||||
|
|
8
db/migrate/20240409044229_create_visit_counts.rb
Normal file
8
db/migrate/20240409044229_create_visit_counts.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class CreateVisitCounts < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
create_table :visit_counts do |t|
|
||||||
|
t.date :date, null: false
|
||||||
|
t.integer :count, null: false, default: 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
7
db/schema.rb
generated
7
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.0].define(version: 2024_04_06_101101) do
|
ActiveRecord::Schema[7.0].define(version: 2024_04_09_044229) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
|
||||||
|
@ -81,4 +81,9 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_06_101101) do
|
||||||
t.text "type", null: false
|
t.text "type", null: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "visit_counts", force: :cascade do |t|
|
||||||
|
t.date "date", null: false
|
||||||
|
t.integer "count", default: 0, null: false
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
18
test/fixtures/visit_counts.yml
vendored
Normal file
18
test/fixtures/visit_counts.yml
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: visit_counts
|
||||||
|
#
|
||||||
|
# id :bigint not null, primary key
|
||||||
|
# count :integer default(0), not null
|
||||||
|
# date :date not null
|
||||||
|
#
|
||||||
|
|
||||||
|
# This model initially had no columns defined. If you add columns to the
|
||||||
|
# model remove the "{}" from the fixture names and add the columns immediately
|
||||||
|
# below each fixture, per the syntax in the comments below
|
||||||
|
#
|
||||||
|
one: {}
|
||||||
|
# column: value
|
||||||
|
#
|
||||||
|
two: {}
|
||||||
|
# column: value
|
15
test/models/visit_count_test.rb
Normal file
15
test/models/visit_count_test.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: visit_counts
|
||||||
|
#
|
||||||
|
# id :bigint not null, primary key
|
||||||
|
# count :integer default(0), not null
|
||||||
|
# date :date not null
|
||||||
|
#
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class VisitCountTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
Loading…
Reference in a new issue