counterwise 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf86231a128b10c3db267b181bec88ba90bc1cfc07039629a425254cc7d3c612
4
- data.tar.gz: 27bc3b7c1140653a2588e48c960c551bdb1a455dee92eb5c276c3e0c9eb55891
3
+ metadata.gz: 4c53c61fffe70d6bed84b2d5e5da5349b4a4285dbd7c19a8523721c449dcdbcc
4
+ data.tar.gz: 23e702c8cc478833aa555ad28f4456912420036063bf53d30c35d61b426cbff9
5
5
  SHA512:
6
- metadata.gz: 9f549a07032d2492cc0d4470ebd1e7a5de736f4dceeca90c883b29fb85d60920fc9274c5e0c822f4e07e27768817ccbbe1b9746c2d29e197fa7af1d7f69272f0
7
- data.tar.gz: a777ea9f55d06e37e1ecb35af9a0526f9fa7cd51304f8549bb821b0f527020a78fd843c11d31f5110e08e015c43a8856c1f0456571c13ccfad4f13ed81b91eff
6
+ metadata.gz: 3dc224b1948220028726742754a51b4523147f86de67bac047a3159a0760782d68722ec5afcbd017ad5b5d3434430a6c8db960906360dae858f484f43177b8c6
7
+ data.tar.gz: 8ba0623f2cea3a11c538dbf10f8ac3cb0ebd49400b92a0617c630b62c297e34fefd6d6298a243f8615caeb238d47cffce4d5f1dd79e7701c01957737d958fb17
data/README.md CHANGED
@@ -15,6 +15,7 @@ Counting and aggregation library for Rails.
15
15
  - [Reset a counter](#reset-a-counter)
16
16
  - [Verify a counter](#verify-a-counter)
17
17
  - [Hooks](#hooks)
18
+ - [Testing the counters in production](#testing-the-counters-in-production)
18
19
  - [TODO](#todo)
19
20
  - [Usage](#usage)
20
21
  - [Installation](#installation)
@@ -273,6 +274,29 @@ class OrderRevenueCounter < Counter::Definition
273
274
  end
274
275
  ```
275
276
 
277
+ ## Testing the counters in production
278
+
279
+ It may be useful to verify the accuracy of the counters in production, especially if you are concerned about conditional counters etc causing counter drift over time.
280
+
281
+ This form of script takes a sampling approach suitable for large collections. It will randomly select a record and verify that the counter value is correct; if it's not, it stops giving you a chance to investigate.
282
+
283
+ ```ruby
284
+ site_range = Site.minimum(:id)..Site.maximum(:id)
285
+
286
+ 1000.times do
287
+ random_id = rand(site_range)
288
+ site = Site.where("id >= ?", random_id).limit(1).first
289
+ next if site.nil?
290
+ if site.confirmed_subscribers_counter.correct?
291
+ puts "✅ site #{site.id} has correct counter value"
292
+ else
293
+ puts "❌ site #{site.id} has incorrect counter value. Expected #{site.confirmed_subscribers_counter.value} but got #{site.confirmed_subscribers_counter.count_by_sql}"
294
+ break
295
+ end
296
+ sleep 0.1
297
+ end
298
+ ```
299
+
276
300
  ---
277
301
 
278
302
  ## TODO
@@ -26,8 +26,9 @@ module Counter::Countable
26
26
  def each_counter_to_update
27
27
  # For each definition, find or create the counter on the parent
28
28
  self.class.counted_by.each do |counter_definition|
29
- parent_model = association(counter_definition.inverse_association)
30
- .target
29
+ parent_association = association(counter_definition.inverse_association)
30
+ parent_association.load_target unless parent_association.loaded?
31
+ parent_model = parent_association.target
31
32
  next unless parent_model
32
33
  counter = parent_model.counters.find_or_create_counter!(counter_definition)
33
34
  yield counter if counter
@@ -1,3 +1,3 @@
1
1
  module Counter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counterwise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Lawrence
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-28 00:00:00.000000000 Z
11
+ date: 2023-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails