Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Fix Payday.update_stats to include dues in nusers
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Oct 24, 2015
1 parent 672b29e commit 918cf40
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,57 @@ def take_over_balances(self):

def update_stats(self):
log("Updating stats.")
self.db.run("""\
WITH our_payments AS (SELECT * FROM payments WHERE payday=%(payday)s)
UPDATE paydays p
SET nusers = (SELECT count(*) FROM (
SELECT DISTINCT ON (participant) participant FROM our_payments GROUP BY participant
) AS foo)
, nteams = (SELECT count(*) FROM (
SELECT DISTINCT ON (team) team FROM our_payments GROUP BY team
) AS foo)
, volume = (
SELECT COALESCE(sum(amount), 0)
FROM our_payments
WHERE payday=p.id AND direction='to-team'
)
WHERE id=%(payday)s
self.db.run("""
WITH payments_and_dues AS (
-- Participants who have either received/given money
SELECT participant, team, amount, direction FROM payments WHERE payday = %(payday)s
UNION
-- Participants who weren't charged due to amount + due < MINIMUM_CHARGE
SELECT payload->>'participant' AS participant
, payload->>'team' AS team
, '0' AS amount
, 'to-team' AS direction
FROM events

This comment has been minimized.

Copy link
@rohitpaulk

rohitpaulk Oct 24, 2015

Author Contributor

This (querying events for application data) is a sign that we should have first class schema for dues history. Maybe a table named due_payments?

This comment has been minimized.

Copy link
@chadwhitacre

chadwhitacre Oct 27, 2015

Contributor

Agreed. We already have payment_instructions and payments, so how about payments_due?

This comment has been minimized.

Copy link
@rohitpaulk

rohitpaulk Oct 27, 2015

Author Contributor

👍 Reticketed as #3851

WHERE (
(SELECT ts_end FROM paydays WHERE id = %(payday)s) = '1970-01-01T00:00:00+00'::timestamptz
OR
ts < (SELECT ts_end FROM paydays WHERE id = %(payday)s)
)
AND ts > (SELECT ts_start FROM paydays WHERE id = %(payday)s)
AND type='payday'
AND payload->>'action' IN ('due')
-- Filter out participants with bad CCs
AND (
SELECT COUNT(*)
FROM current_exchange_routes r
JOIN participants p ON p.id = r.participant
WHERE p.username = payload->>'participant'
AND network = 'braintree-cc'
AND error = ''
) > 0
)
UPDATE paydays p
SET nusers = (
SELECT COUNT(DISTINCT(participant)) FROM payments_and_dues
)
, nteams = (
SELECT COUNT(DISTINCT(team)) FROM payments_and_dues
)
, volume = (
SELECT COALESCE(sum(amount), 0) FROM payments_and_dues WHERE direction='to-team'
)
WHERE id=%(payday)s
""", {'payday': self.id})
log("Updated payday stats.")
Expand Down

0 comments on commit 918cf40

Please sign in to comment.