feat: add visit counts

This commit is contained in:
Neetpone 2024-04-13 18:19:59 -04:00
parent 390b7ac486
commit b9872ba287
7 changed files with 77 additions and 3 deletions

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
class ApplicationController < ActionController::Base
before_action :start_timer
before_action :count_visit
before_action :setup_pagination_and_tags
private
@ -9,6 +10,21 @@ class ApplicationController < ActionController::Base
@start_time = Time.zone.now
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
@per_page = 15
per_page = params[:per_page]

13
app/models/visit_count.rb Normal file
View 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

View file

@ -17,8 +17,7 @@ html
section
h4 Stats
span.stat Page generated in #{render_time}
/ span.stat FIXME registered users
/ span.stat FIXME visitors total
span.stat #{pluralize(@today_visit_count, 'visitor')} today, #{@total_visit_count} total.
section
h4 FoalFetch
span.stat Frontend designed by DataByte, backend coded by Floorb.

View 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
View file

@ -10,7 +10,7 @@
#
# 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
enable_extension "plpgsql"
@ -81,4 +81,9 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_06_101101) do
t.text "type", null: false
end
create_table "visit_counts", force: :cascade do |t|
t.date "date", null: false
t.integer "count", default: 0, null: false
end
end

18
test/fixtures/visit_counts.yml vendored Normal file
View 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

View 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