From ef9b1c7d0bff1e881d5e0c302984ae75b879ffb2 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Sun, 16 Feb 2025 12:28:11 -0500 Subject: [PATCH] Add connection to wakatime db --- Gemfile | 2 ++ Gemfile.lock | 36 ++------------------------ app/models/heartbeat.rb | 9 +++++++ app/models/wakatime_record.rb | 4 +++ app/views/layouts/application.html.erb | 3 ++- config/database.yml | 25 +++++++++++++++--- 6 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 app/models/heartbeat.rb create mode 100644 app/models/wakatime_record.rb diff --git a/Gemfile b/Gemfile index a5bcec2..bcd56e6 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ gem "rails", "~> 8.0.1" gem "propshaft" # Use sqlite3 as the database for Active Record gem "sqlite3", ">= 2.1" +# Use PostgreSQL as the database for Wakatime +gem "pg" # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] diff --git a/Gemfile.lock b/Gemfile.lock index 9bd6e0a..a6c71d0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -111,12 +111,6 @@ GEM erubi (1.13.1) et-orbi (1.2.11) tzinfo - faraday (2.12.2) - faraday-net_http (>= 2.0, < 3.5) - json - logger - faraday-net_http (3.4.0) - net-http (>= 0.5.0) ffi (1.17.1-aarch64-linux-gnu) ffi (1.17.1-aarch64-linux-musl) ffi (1.17.1-arm-linux-gnu) @@ -132,7 +126,6 @@ GEM raabro (~> 1.4) globalid (1.2.1) activesupport (>= 6.1) - hashie (5.0.0) http (5.2.0) addressable (~> 2.8) base64 (~> 0.1) @@ -157,8 +150,6 @@ GEM actionview (>= 5.0.0) activesupport (>= 5.0.0) json (2.10.1) - jwt (2.10.1) - base64 kamal (2.5.2) activesupport (>= 7.0) base64 (~> 0.2) @@ -189,11 +180,6 @@ GEM mini_mime (1.1.5) minitest (5.25.4) msgpack (1.8.0) - multi_json (1.15.0) - multi_xml (0.7.1) - bigdecimal (~> 3.1) - net-http (0.6.0) - uri net-imap (0.5.6) date net-protocol @@ -223,28 +209,12 @@ GEM racc (~> 1.4) nokogiri (1.18.2-x86_64-linux-musl) racc (~> 1.4) - oauth2 (1.4.11) - faraday (>= 0.17.3, < 3.0) - jwt (>= 1.0, < 3.0) - multi_json (~> 1.3) - multi_xml (~> 0.5) - rack (>= 1.2, < 4) - omniauth (1.9.2) - hashie (>= 3.4.6) - rack (>= 1.6.2, < 3) - omniauth-oauth2 (1.3.1) - oauth2 (~> 1.0) - omniauth (~> 1.2) - omniauth-rails_csrf_protection (0.1.2) - actionpack (>= 4.2) - omniauth (>= 1.3.1) - omniauth-slack (2.3.0) - omniauth-oauth2 (~> 1.3.1) ostruct (0.6.1) parallel (1.26.3) parser (3.3.7.1) ast (~> 2.4.1) racc + pg (1.5.9) pp (0.6.2) prettyprint prettyprint (0.2.0) @@ -432,9 +402,7 @@ DEPENDENCIES importmap-rails jbuilder kamal - omniauth - omniauth-rails_csrf_protection - omniauth-slack + pg propshaft puma (>= 5.0) rails (~> 8.0.1) diff --git a/app/models/heartbeat.rb b/app/models/heartbeat.rb new file mode 100644 index 0000000..8aab8e0 --- /dev/null +++ b/app/models/heartbeat.rb @@ -0,0 +1,9 @@ +class Heartbeat < WakatimeRecord + def self.cached_recent_count + Rails.cache.fetch("heartbeats_recent_count", expires_in: 5.minutes) do + recent.count + end + end + + scope :recent, -> { where("created_at > ?", 24.hours.ago) } +end diff --git a/app/models/wakatime_record.rb b/app/models/wakatime_record.rb new file mode 100644 index 0000000..608f33b --- /dev/null +++ b/app/models/wakatime_record.rb @@ -0,0 +1,4 @@ +class WakatimeRecord < ApplicationRecord + self.abstract_class = true + connects_to database: { reading: :wakatime, writing: :wakatime } +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c16fae7..bca1b57 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -38,7 +38,8 @@ <%= yield %> diff --git a/config/database.yml b/config/database.yml index 2640cb5..a7506b4 100644 --- a/config/database.yml +++ b/config/database.yml @@ -10,15 +10,27 @@ default: &default timeout: 5000 development: - <<: *default - database: storage/development.sqlite3 + primary: + <<: *default + database: storage/development.sqlite3 + wakatime: + adapter: postgresql + encoding: unicode + url: <%= ENV['WAKATIME_DATABASE_URL'] %> + replica: true # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - <<: *default - database: storage/test.sqlite3 + primary: + <<: *default + database: storage/test.sqlite3 + wakatime: + adapter: postgresql + encoding: unicode + url: <%= ENV['WAKATIME_DATABASE_URL'] %> + replica: true # Store production database in the storage/ directory, which by default @@ -27,6 +39,11 @@ production: primary: <<: *default database: storage/production.sqlite3 + wakatime: + adapter: postgresql + encoding: unicode + url: <%= ENV['WAKATIME_DATABASE_URL'] %> + replica: true cache: <<: *default database: storage/production_cache.sqlite3