Project

General

Profile

Bug #11402 ยป aggregate_master_card_payment.rb

laise (Alexey Chernenkov), 07/31/2015 07:40 AM

 
1
module SummaryStats
2
  class AggregateTxService
3
    class AggregateMasterCardPayment
4
      def input
5
        TxMasterCardPayment.order(:appeared_at)
6
      end
7

    
8
      def aggregate?(a, b)
9
        a.appeared_at.to_date == b.appeared_at.to_date
10
      end
11

    
12
      def aggregate(chunks)
13
        data = DataHash.new {{
14
          "count" => 0,
15
          "amount" => BigDecimal(0)
16
        }}
17

    
18
        chunks.each do |c|
19
          data.set(c.kind, c.currency) { |x|
20
            x["count"] += 1
21
            x["amount"] += c.amount
22
          }
23
        end
24

    
25
        [chunks.first.appeared_at.to_date, data.h]
26
      end
27

    
28
      def output(ary)
29
        Logging.quiet_active_record_log do
30
          AggregatedMasterCardPayment.delete_all
31
          AggregatedMasterCardPayment.import [:for_date, :data], ary, validate: false
32
        end
33
      end
34

    
35
      def perform
36
        Logging.log_execution_time self.class.name.demodulize.underscore do
37
          AggregationService.new(
38
            input:      self.input,
39
            aggregator: self,
40
            output:     -> (ary) { self.output ary }
41
          ).perform
42
        end
43
      end
44
    end
45
  end
46
end