Store customer outbound messages/day for 1 year in redis

Stephen Paul Weber created

Storage is a sorted set, with dates as the values and message counts as the
scores.  Use zincrby to increment the message count by 1 on each new message.
Use zremrangebylex to remove all items older than 1 year so the set does not
grow unboundedly.

Dates with known message counts can be found using zrangebylex. Scores can be
had one at a time with zscore.  In redis 6.2+ zrange bylex withscores or zmscore
may also be used, but that is only in Debian experimental at time of writing.

Change summary

.rubocop.yml |  3 +++
sgx_jmp.rb   | 17 ++++++++++++++++-
2 files changed, 19 insertions(+), 1 deletion(-)

Detailed changes

.rubocop.yml 🔗

@@ -71,3 +71,6 @@ Layout/IndentArray:
 
 Style/FormatString:
   EnforcedStyle: percent
+
+Style/FormatStringToken:
+  EnforcedStyle: unannotated

sgx_jmp.rb 🔗

@@ -5,6 +5,7 @@ require "bigdecimal"
 require "blather/client/dsl" # Require this first to not auto-include
 require "blather/client"
 require "braintree"
+require "date"
 require "dhall"
 require "em-hiredis"
 require "em_promise"
@@ -127,7 +128,21 @@ end
 
 message do |m|
 	Customer.for_jid(m.from.stripped).then { |customer|
-		customer.stanza_from(m)
+		today = Time.now.utc.to_date
+		EMPromise.all([
+			REDIS.zremrangebylex(
+				"jmp_customer_outbound_messages-#{customer.customer_id}",
+				"-",
+				# Store message counts per day for 1 year
+				"[#{(today << 12).strftime('%Y%m%d')}"
+			),
+			REDIS.zincrby(
+				"jmp_customer_outbound_messages-#{customer.customer_id}",
+				1,
+				today.strftime("%Y%m%d")
+			),
+			customer.stanza_from(m)
+		])
 	}.catch(&method(:panic))
 end