Skip to content

Commit d2c8630

Browse files
committed
feat: enhance user sleep tracking with new sleep types and methods; add indexes for performance
1 parent 8a25edc commit d2c8630

12 files changed

Lines changed: 400 additions & 95 deletions

File tree

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Metrics/AbcSize:
2929
Metrics/BlockLength:
3030
Max: 25
3131
Exclude:
32+
- '**/initializers/rails_pulse.rb'
3233
- 'spec/**/*'
3334
- 'app/admin/**/*'
3435

@@ -88,3 +89,7 @@ Style/HashEachMethods:
8889
Rails/LexicallyScopedActionFilter:
8990
Exclude:
9091
- 'app/controllers/**/**/*'
92+
93+
RSpec/InstanceVariable:
94+
Exclude:
95+
- 'spec/models/user_spec.rb'

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ end
7272

7373
group :test do
7474
gem 'database_cleaner-active_record'
75+
gem 'db-query-matchers'
7576
gem 'factory_bot_rails'
7677
gem 'shoulda-matchers'
7778
end

Gemfile.lock

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ GEM
117117
database_cleaner-core (~> 2.0)
118118
database_cleaner-core (2.0.1)
119119
date (3.5.0)
120+
db-query-matchers (0.8.0)
120121
descendants_tracker (0.0.4)
121122
thread_safe (~> 0.3, >= 0.3.1)
122123
devise (4.9.4)
@@ -166,13 +167,19 @@ GEM
166167
zeitwerk (~> 2.6)
167168
erb (5.1.3)
168169
erubi (1.13.1)
169-
factory_bot (6.5.5)
170+
factory_bot (6.5.6)
170171
activesupport (>= 6.1.0)
171172
factory_bot_rails (6.5.1)
172173
factory_bot (~> 6.5)
173174
railties (>= 6.1.0)
175+
ffi (1.17.2-aarch64-linux-gnu)
176+
ffi (1.17.2-aarch64-linux-musl)
177+
ffi (1.17.2-arm-linux-gnu)
178+
ffi (1.17.2-arm-linux-musl)
174179
ffi (1.17.2-arm64-darwin)
180+
ffi (1.17.2-x86_64-darwin)
175181
ffi (1.17.2-x86_64-linux-gnu)
182+
ffi (1.17.2-x86_64-linux-musl)
176183
flay (2.13.3)
177184
erubi (~> 1.10)
178185
path_expander (~> 1.0)
@@ -236,12 +243,24 @@ GEM
236243
timeout
237244
net-smtp (0.5.1)
238245
net-protocol
239-
nio4r (2.7.4)
246+
nio4r (2.7.5)
247+
nokogiri (1.18.10-aarch64-linux-gnu)
248+
racc (~> 1.4)
249+
nokogiri (1.18.10-aarch64-linux-musl)
250+
racc (~> 1.4)
251+
nokogiri (1.18.10-arm-linux-gnu)
252+
racc (~> 1.4)
253+
nokogiri (1.18.10-arm-linux-musl)
254+
racc (~> 1.4)
240255
nokogiri (1.18.10-arm64-darwin)
241256
racc (~> 1.4)
257+
nokogiri (1.18.10-x86_64-darwin)
258+
racc (~> 1.4)
242259
nokogiri (1.18.10-x86_64-linux-gnu)
243260
racc (~> 1.4)
244-
oj (3.16.11)
261+
nokogiri (1.18.10-x86_64-linux-musl)
262+
racc (~> 1.4)
263+
oj (3.16.12)
245264
bigdecimal (>= 3.0)
246265
ostruct (>= 0.2)
247266
orm_adapter (0.5.0)
@@ -252,8 +271,13 @@ GEM
252271
ast (~> 2.4.1)
253272
racc
254273
path_expander (1.1.3)
274+
pg (1.6.2)
275+
pg (1.6.2-aarch64-linux)
276+
pg (1.6.2-aarch64-linux-musl)
255277
pg (1.6.2-arm64-darwin)
278+
pg (1.6.2-x86_64-darwin)
256279
pg (1.6.2-x86_64-linux)
280+
pg (1.6.2-x86_64-linux-musl)
257281
pp (0.6.3)
258282
prettyprint
259283
prettyprint (0.2.0)
@@ -328,13 +352,13 @@ GEM
328352
actionpack (>= 7.0)
329353
railties (>= 7.0)
330354
rexml (3.4.4)
331-
rspec-core (3.13.5)
355+
rspec-core (3.13.6)
332356
rspec-support (~> 3.13.0)
333357
rspec-expectations (3.13.5)
334358
diff-lcs (>= 1.2.0, < 2.0)
335359
rspec-support (~> 3.13.0)
336360
rspec-json_expectations (2.2.0)
337-
rspec-mocks (3.13.5)
361+
rspec-mocks (3.13.6)
338362
diff-lcs (>= 1.2.0, < 2.0)
339363
rspec-support (~> 3.13.0)
340364
rspec-rails (8.0.2)
@@ -346,7 +370,7 @@ GEM
346370
rspec-mocks (~> 3.13)
347371
rspec-support (~> 3.13)
348372
rspec-support (3.13.6)
349-
rubocop (1.81.6)
373+
rubocop (1.81.7)
350374
json (~> 2.3)
351375
language_server-protocol (~> 3.17.0.2)
352376
lint_roller (~> 1.1.0)
@@ -423,7 +447,7 @@ GEM
423447
unicode-emoji (~> 4.1)
424448
unicode-emoji (4.1.0)
425449
uniform_notifier (1.18.0)
426-
uri (1.0.4)
450+
uri (1.1.0)
427451
useragent (0.16.11)
428452
virtus (2.0.0)
429453
axiom-types (~> 0.1)
@@ -443,8 +467,16 @@ GEM
443467
zeitwerk (2.7.3)
444468

445469
PLATFORMS
446-
arm64-darwin-24
470+
aarch64-linux
471+
aarch64-linux-gnu
472+
aarch64-linux-musl
473+
arm-linux-gnu
474+
arm-linux-musl
475+
arm64-darwin
476+
x86_64-darwin
447477
x86_64-linux
478+
x86_64-linux-gnu
479+
x86_64-linux-musl
448480

449481
DEPENDENCIES
450482
blueprinter
@@ -453,6 +485,7 @@ DEPENDENCIES
453485
bullet
454486
bundler-audit
455487
database_cleaner-active_record
488+
db-query-matchers
456489
devise
457490
devise-jwt
458491
dotenv

app/controllers/api/v1/stats/dashboard_stats_controller.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@ def no_record?
2323
def json_response
2424
{
2525
totals: {
26-
dreams: current_user.dreams.size,
27-
lucids: current_user.lucids.size,
28-
nightmares: current_user.nightmares.size
29-
},
30-
by_month: {
31-
dreams: build_months_records(current_user.dreams),
32-
lucids: build_months_records(current_user.lucids),
33-
nightmares: build_months_records(current_user.nightmares)
34-
},
35-
by_year: {
36-
dreams: build_years(current_user.dreams),
37-
lucids: build_years(current_user.lucids),
38-
nightmares: build_years(current_user.nightmares)
26+
dream: current_user.dreams.size,
27+
lucid: current_user.lucids.size,
28+
nightmare: current_user.nightmares.size,
29+
sleep_paralysis: current_user.paralyses.size,
30+
sleep_walking: current_user.walkings.size,
31+
sleep_talking: current_user.talkings.size,
32+
sleep_apnea: current_user.apneas.size,
33+
erotic: current_user.erotics.size
3934
}
35+
# by_month: {
36+
# dreams: build_months_records(current_user.dreams),
37+
# lucids: build_months_records(current_user.lucids),
38+
# nightmares: build_months_records(current_user.nightmares)
39+
# },
40+
# by_year: {
41+
# dreams: build_years(current_user.dreams),
42+
# lucids: build_years(current_user.lucids),
43+
# nightmares: build_years(current_user.nightmares)
44+
# }
4045
}
4146
end
4247

app/models/user.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
class User < ApplicationRecord
55
ITEMS_PER_PAGE = 15
66

7+
SLEEP_TYPE_METHODS = {
8+
dreams: :dream,
9+
nightmares: :nightmare,
10+
lucids: :lucid,
11+
paralyses: :sleep_paralysis,
12+
walkings: :sleep_walking,
13+
talkings: :sleep_talking,
14+
apneas: :sleep_apnea,
15+
erotics: :erotic
16+
}.freeze
17+
718
# Include default devise modules. Others available are:
819
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
920
devise :confirmable, :database_authenticatable, :registerable,
@@ -29,4 +40,10 @@ def to_json(_arg)
2940
# Tell Devise to use our custom user serializer
3041
UserSerializer.render(self)
3142
end
43+
44+
SLEEP_TYPE_METHODS.each do |method_name, sleep_type_key|
45+
define_method(method_name) do
46+
sleeps.where(sleep_type: Sleep::SLEEP_TYPE[sleep_type_key])
47+
end
48+
end
3249
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class AddIndexesToSleeps < ActiveRecord::Migration[8.1]
4+
def change
5+
# Index simple sur sleep_type pour les requêtes de filtrage par type
6+
add_index :sleeps, :sleep_type, if_not_exists: true
7+
8+
# Index composite sur user_id et sleep_type
9+
# Optimise les requêtes user.sleeps.where(sleep_type: ...)
10+
add_index :sleeps, [:user_id, :sleep_type], if_not_exists: true
11+
end
12+
end

0 commit comments

Comments
 (0)