Skip to content

Commit

Permalink
Merge pull request #12 from loftwah/dl/analytics-fixes
Browse files Browse the repository at this point in the history
Dl/analytics fixes
  • Loading branch information
loftwah authored Aug 21, 2024
2 parents 1aeac96 + e2e34a3 commit 9ca9048
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.2
3.3.0
1 change: 0 additions & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
web: bin/rails server
css: bin/rails tailwindcss:watch
mailcatcher: mailcatcher --foreground --http-ip=0.0.0.0 --smtp-port 1025
vite: bin/vite dev
3 changes: 1 addition & 2 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
import "chartkick/chart.js"
import "controllers"
1 change: 1 addition & 0 deletions app/javascript/entrypoints/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// <%= vite_client_tag %>
// <%= vite_javascript_tag 'application' %>
import Rails from "@rails/ujs";
import "chartkick/chart.js"
Rails.start();

console.log('Vite ⚡️ Rails')
Expand Down
22 changes: 13 additions & 9 deletions app/middleware/page_view_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ def html_response?(headers)

def track_page_view(request)
user = User.find_by(username: request.path.split('/').last)
PageView.create(
user: user,
path: request.path,
referrer: request.referrer,
browser: request.user_agent,
visited_at: Time.current,
ip_address: request.ip,
session_id: request.session[:session_id]
) if user
if user
PageView.create(
user: user,
path: request.path,
referrer: request.referrer,
browser: request.user_agent,
visited_at: Time.current,
ip_address: request.ip,
session_id: request.session[:session_id]
)
end
rescue ActiveRecord::RecordNotUnique
Rails.logger.info "Duplicate page view detected and ignored"
end
end
263 changes: 121 additions & 142 deletions app/views/analytics/index.html.erb

Large diffs are not rendered by default.

61 changes: 31 additions & 30 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,41 @@
<div class="container mx-auto px-4 py-2 flex justify-between items-center">
<div class="flex items-center">
<%= image_tag "icon.png", class: "h-6 mr-2", alt: "Linkarooie" %>
<h1 class="text-1xl font-bold text-lime-200">Linkarooie</h1>
<h1 class="text-1xl font-bold text-lime-200">
<%= link_to 'Linkarooie', root_path, class: 'text-lime-200 hover:text-lime-300' %>
</h1>
</div>
<nav>
<ul class="flex space-x-2 md:space-x-4 text-sm md:text-base items-center">
<li><%= link_to 'Loftwah', user_links_path('loftwah'), class: 'bg-lime-500 hover:bg-lime-700 text-white font-bold py-1 px-2 rounded' %></li>
<li><%= link_to 'Home', root_path, class: 'text-white hover:text-lime-300' %></li>
<% if user_signed_in? %>
<li><%= link_to 'Public Page', user_links_path(current_user.username), class: 'text-white hover:text-lime-300' %></li>
<li><%= link_to 'Analytics', analytics_path, class: 'text-white hover:text-lime-300' %></li>
<li class="relative">
<button id="userMenuBtn" class="text-white hover:text-lime-300 focus:outline-none flex items-center space-x-1">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
<div id="userMenu" class="absolute right-0 mt-2 w-48 bg-gray-700 rounded-md shadow-xl z-20 hidden">
<%= link_to 'Links', links_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Achievements', achievements_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Profile', edit_user_registration_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Logout', destroy_user_session_path, method: :delete, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
</div>
</li>
<% else %>
<li><%= link_to 'Login', new_user_session_path, class: 'text-white hover:text-lime-300' %></li>
<% end %>
<li>
<a href="https://github.com/loftwah/linkarooie" target="_blank" rel="noopener noreferrer" class="text-white hover:text-lime-300">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 2C6.477 2 2 6.477 2 12c0 4.42 2.865 8.166 6.839 9.489.5.092.682-.217.682-.482 0-.237-.008-.866-.013-1.7-2.782.603-3.369-1.34-3.369-1.34-.454-1.156-1.11-1.463-1.11-1.463-.908-.62.069-.608.069-.608 1.003.07 1.531 1.03 1.531 1.03.892 1.529 2.341 1.087 2.91.831.092-.646.35-1.086.636-1.336-2.22-.253-4.555-1.11-4.555-4.943 0-1.091.39-1.984 1.029-2.683-.103-.253-.446-1.27.098-2.647 0 0 .84-.269 2.75 1.025A9.564 9.564 0 0112 6.844c.85.004 1.705.114 2.504.336 1.909-1.294 2.747-1.025 2.747-1.025.546 1.377.203 2.394.1 2.647.64.699 1.028 1.592 1.028 2.683 0 3.842-2.339 4.687-4.566 4.935.359.309.678.919.678 1.852 0 1.336-.012 2.415-.012 2.743 0 .267.18.578.688.48C19.138 20.161 22 16.418 22 12c0-5.523-4.477-10-10-10z" />
<ul class="flex space-x-2 md:space-x-4 text-sm md:text-base items-center">
<li><%= link_to 'Loftwah', user_links_path('loftwah'), class: 'bg-lime-500 hover:bg-lime-700 text-white font-bold py-1 px-2 rounded' %></li>
<% if user_signed_in? %>
<li class="relative">
<button id="userMenuBtn" class="text-white hover:text-lime-300 focus:outline-none flex items-center space-x-1">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</a>
</button>
<div id="userMenu" class="absolute right-0 mt-2 w-48 bg-gray-700 rounded-md shadow-xl z-20 hidden">
<%= link_to 'Public Page', user_links_path(current_user.username), class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Analytics', analytics_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Links', links_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Achievements', achievements_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Profile', edit_user_registration_path, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
<%= link_to 'Logout', destroy_user_session_path, method: :delete, class: 'block px-4 py-2 text-sm text-white hover:bg-gray-600' %>
</div>
</li>
</ul>
</nav>
<% else %>
<li><%= link_to 'Login', new_user_session_path, class: 'text-white hover:text-lime-300' %></li>
<% end %>
<li>
<a href="https://github.com/loftwah/linkarooie" target="_blank" rel="noopener noreferrer" class="text-white hover:text-lime-300">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 2C6.477 2 2 6.477 2 12c0 4.42 2.865 8.166 6.839 9.489.5.092.682-.217.682-.482 0-.237-.008-.866-.013-1.7-2.782.603-3.369-1.34-3.369-1.34-.454-1.156-1.11-1.463-1.11-1.463-.908-.62.069-.608.069-.608 1.003.07 1.531 1.03 1.531 1.03.892 1.529 2.341 1.087 2.91.831.092-.646.35-1.086.636-1.336-2.22-.253-4.555-1.11-4.555-4.943 0-1.091.39-1.984 1.029-2.683-.103-.253-.446-1.27.098-2.647 0 0 .84-.269 2.75 1.025A9.564 9.564 0 0112 6.844c.85.004 1.705.114 2.504.336 1.909-1.294 2.747-1.025 2.747-1.025.546 1.377.203 2.394.1 2.647.64.699 1.028 1.592 1.028 2.683 0 3.842-2.339 4.687-4.566 4.935.359.309.678.919.678 1.852 0 1.336-.012 2.415-.012 2.743 0 .267.18.578.688.48C19.138 20.161 22 16.418 22 12c0-5.523-4.477-10-10-10z" />
</svg>
</a>
</li>
</ul>
</nav>
</div>
</header>
<main class="container mx-auto px-4 py-6">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# db/seeds.rb

# Delete existing users, links, and achievements to avoid duplication
User.delete_all
# Delete existing records in the correct order to avoid foreign key constraints
PageView.delete_all
LinkClick.delete_all
AchievementView.delete_all
DailyMetric.delete_all
Link.delete_all
Achievement.delete_all
User.delete_all

# Create a new user with the fetched attributes
user = User.create!(
user = User.new(
email: "[email protected]",
encrypted_password: "$2a$12$svOkqgPLTEgvJT9ZTGXYHO20UUIQs9PF/im6bQaBBybmcrTKyDD6i",
avatar: "https://pbs.twimg.com/profile_images/1756873036220059648/zc13kjbX_400x400.jpg",
Expand All @@ -17,9 +21,8 @@
tags: ["AWS", "DevOps", "Docker", "GitHub", "Linux", "Open Source", "Ruby", "Ruby on Rails", "Terraform"].to_json,
created_at: "2024-07-25 02:08:37",
updated_at: "2024-07-26 15:29:25"
) do |user|
user.save!(validate: false)
end
)
user.save!(validate: false)

# Create associated links for the user
links_data = [
Expand Down

0 comments on commit 9ca9048

Please sign in to comment.