1# frozen_string_literal: true
2
3require "digest"
4
5require_relative "./usage_report"
6
7class CustomerUsage
8 def initialize(customer_id)
9 @customer_id = customer_id
10 end
11
12 def usage_report(range)
13 EMPromise.all([
14 messages_by_day(range),
15 minutes_by_day(range)
16 ]).then do |args|
17 UsageReport.new(range, *args)
18 end
19 end
20
21 def expire_message_usage
22 today = Time.now.utc.to_date
23 REDIS.zremrangebylex(
24 "jmp_customer_outbound_messages-#{@customer_id}",
25 "-",
26 # Store message counts per day for 1 year
27 "[#{(today << 12).strftime('%Y%m%d')}"
28 )
29 end
30
31 def incr_message_usage(amount=1, body=nil)
32 today = Time.now.utc.to_date
33 EMPromise.all([
34 expire_message_usage,
35 REDIS.zincrby(
36 "jmp_customer_outbound_messages-#{@customer_id}",
37 amount,
38 today.strftime("%Y%m%d")
39 ),
40 incr_body(amount, body)
41 ]).then { |result| { today: result[1].to_i, body: result[2].to_i } }
42 end
43
44 def incr_body(amount, body)
45 hash = Digest::SHA2.hexdigest(body[0..100])
46
47 EMPromise.all([
48 REDIS.incrby("jmp_outbound_body-#{hash}", amount),
49 REDIS.expire("jmp_outbound_body-#{hash}", 180)
50 ]).then(&:first)
51 end
52
53 def message_usage(range)
54 messages_by_day(range).then do |by_day|
55 by_day.values.sum
56 end
57 end
58
59 def messages_by_day(range)
60 EMPromise.all(range.first.downto(range.last).map { |day|
61 REDIS.zscore(
62 "jmp_customer_outbound_messages-#{@customer_id}",
63 day.strftime("%Y%m%d")
64 ).then { |c| [day, c.to_i] if c }
65 }).then { |r| r.compact.to_h.tap { |h| h.default = 0 } }
66 end
67
68 QUERY_FOR_MINUTES = <<~SQL
69 SELECT
70 date_trunc('day', start)::date as day,
71 CEIL(SUM(billsec)/60.0)::integer as minutes
72 FROM cdr
73 WHERE customer_id=$1 and start >= $3 and date_trunc('day', start) <= $2
74 GROUP BY date_trunc('day', start);
75 SQL
76
77 def minutes_by_day(range)
78 DB.query_defer(
79 QUERY_FOR_MINUTES,
80 [@customer_id, range.first, range.last]
81 ).then do |result|
82 result.each_with_object(Hash.new(0)) do |row, minutes|
83 minutes[row["day"]] = row["minutes"]
84 end
85 end
86 end
87
88 def calling_charges_this_month
89 DB.query_one(<<~SQL, @customer_id).then { |r| r[:charges] }
90 SELECT COALESCE(SUM(charge), 0) AS charges
91 FROM cdr_with_charge
92 WHERE customer_id=$1 AND start >= DATE_TRUNC('month', LOCALTIMESTAMP)
93 SQL
94 end
95end