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

Commit

Permalink
Add an "Active Teams" chart
Browse files Browse the repository at this point in the history
I discovered in the process that we were computing nusers wrong.
  • Loading branch information
chadwhitacre committed Aug 31, 2015
1 parent a7666e1 commit e9158f1
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
13 changes: 10 additions & 3 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,17 @@ def update_stats(self):
WITH our_payments AS (SELECT * FROM payments WHERE payday=%(payday)s)
UPDATE paydays p
SET nactive = (SELECT DISTINCT count(*) FROM (
SELECT participant FROM our_payments WHERE payday=p.id
SET nusers = (SELECT count(*) FROM (
SELECT DISTINCT ON (participant) participant FROM our_payments GROUP BY participant
) AS foo)
, volume = (SELECT COALESCE(sum(amount), 0) FROM our_payments WHERE payday=p.id)
, 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
""", {'payday': self.id})
Expand Down
6 changes: 3 additions & 3 deletions gratipay/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ def get_team(state):


def update_cta(website):
nactive = website.db.one("""
SELECT nactive FROM paydays
nusers = website.db.one("""
SELECT nusers FROM paydays
ORDER BY ts_end DESC LIMIT 1
""", default=(0.0, 0))
nreceiving_from = website.db.one("""
SELECT nreceiving_from
FROM teams
WHERE slug = 'Gratipay'
""", default=0)
website.support_current = cur = int(round(nreceiving_from / nactive * 100)) if nactive else 0
website.support_current = cur = int(round(nreceiving_from / nusers * 100)) if nusers else 0
if cur < 10: goal = 20
elif cur < 15: goal = 30
elif cur < 25: goal = 40
Expand Down
2 changes: 1 addition & 1 deletion gratipay/utils/fake_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def populate_db(db, num_participants=100, ntips=200, num_teams=5, num_transfers=
payday = {
'ts_start': date,
'ts_end': end_date,
'nactive': len(actives),
'nusers': len(actives),
'volume': sum(x['amount'] for x in week_transfers)
}
_fake_thing(db, "paydays", **payday)
Expand Down
6 changes: 6 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN;

ALTER TABLE paydays RENAME COLUMN nactive TO nusers;
ALTER TABLE paydays ADD COLUMN nteams integer NOT NULL DEFAULT 0;

END;
12 changes: 6 additions & 6 deletions sql/update-payday-numbers.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- Reset post-Gratipocalypse values
-- Fix nusers and backfill nteams for post-Gratipocalypse paydays
UPDATE paydays p
SET nactive = (SELECT DISTINCT count(*) FROM (
SELECT participant FROM payments WHERE payday=p.id
SET nteams = (SELECT count(*) FROM (
SELECT DISTINCT ON (team) team FROM payments WHERE payday=p.id GROUP BY team
) AS foo)
, nusers = (SELECT count(*) FROM (
SELECT DISTINCT ON (participant) participant FROM payments WHERE payday=p.id GROUP BY participant
) AS foo)
, volume = (SELECT COALESCE(sum(amount), 0)
FROM payments
WHERE payday=p.id AND direction='to-team')
WHERE ts_start > '2015-05-07'::timestamptz;
2 changes: 1 addition & 1 deletion tests/py/test_paydays_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def test_paydays_json_gives_paydays(self):

response = self.client.GET("/about/paydays.json")
paydays = json.loads(response.body)
assert paydays[0]['nactive'] == 0
assert paydays[0]['nusers'] == 0
7 changes: 4 additions & 3 deletions www/about/charts.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ charts = website.db.all("""\

SELECT ts_start::date AS date
, ts_start::date AS xTitle
, volume::text AS weekly_volume
, nactive::text AS active_users
FROM paydays p
, volume::text
, nusers::text
, nteams::text
FROM paydays
ORDER BY ts_start DESC

""", back_as=dict)
Expand Down
3 changes: 2 additions & 1 deletion www/about/paydays.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ paydays = website.db.all("""\
SELECT ts_start
, ts_end
, volume
, nactive
, nusers
, nteams
FROM paydays
ORDER BY ts_start DESC

Expand Down
19 changes: 13 additions & 6 deletions www/about/stats.spt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ banner = _("About")
title = _("Stats")
one = website.db.one

volume, nusers = one("""
SELECT volume, nactive
volume, nusers, nteams = one("""
SELECT volume, nusers, nteams
FROM paydays
ORDER BY ts_end DESC
LIMIT 1
""", default=(0.0, 0))
nteams = one("SELECT count(*) FROM teams WHERE is_approved IS true AND receiving > 0")
""", default=(0.0, 0, 0))
total = one("SELECT sum(amount) FROM exchanges WHERE amount > 0", default=0)
age_in_years = (date.today() - birthday).days // 365
escrow = one("SELECT sum(balance) FROM participants", default=0)
Expand Down Expand Up @@ -82,10 +81,18 @@ average_number_of_payments = average_number_of_payments or 0
</div>

<div class="chart-wrapper">
<a name="active"></a>
<a name="users"></a>
<h2>Active ~users</h2>
<p class="note">~users that gave and/or took money on Gratipay (per week)</p>
<div class="chart" data-chart="active_users"></div>
<div class="chart" data-chart="nusers"></div>
<div class="x-axis">weeks</div>
</div>

<div class="chart-wrapper">
<a name="teams"></a>
<h2>Active Teams</h2>
<p class="note">Teams that received and shared money on Gratipay (per week)</p>
<div class="chart" data-chart="nteams"></div>
<div class="x-axis">weeks</div>
</div>

Expand Down

0 comments on commit e9158f1

Please sign in to comment.