This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Payday.update_stats to include dues in nusers
- Loading branch information
1 parent
672b29e
commit 918cf40
Showing
1 changed file
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
chadwhitacre
Contributor
|
||
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.") | ||
|
This (querying
events
for application data) is a sign that we should have first class schema fordues
history. Maybe a table nameddue_payments
?