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

Commit

Permalink
Merge pull request #774 from jofusa/master
Browse files Browse the repository at this point in the history
Cleanup for #545 moved complex sql into view
  • Loading branch information
chadwhitacre committed Mar 20, 2013
2 parents 85e4aa6 + 357952f commit 170e386
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 54 deletions.
18 changes: 18 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,21 @@ ALTER TABLE exchanges ADD COLUMN recorder text DEFAULT NULL
ON UPDATE CASCADE ON DELETE RESTRICT;

ALTER TABLE exchanges ADD COLUMN note text DEFAULT NULL;



-------------------------------------------------------------------------------
--- https://github.com/gittip/www.gittip.com/issues/545
create view goal_summary as SELECT tippee as id, goal, (amount/goal) * 100 as percentage, statement, sum(amount) as amount
FROM ( SELECT DISTINCT ON (tipper, tippee) tippee, amount
FROM tips
JOIN participants p ON p.id = tipper
JOIN participants p2 ON p2.id = tippee
WHERE p.last_bill_result = ''
AND p2.claimed_time IS NOT NULL
ORDER BY tipper, tippee, mtime DESC
) AS tips_agg
join participants p3 on p3.id = tips_agg.tippee
GROUP BY tippee, goal, percentage, statement
;

65 changes: 11 additions & 54 deletions www/about/goals.html
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
import locale
from gittip import db

def rows(goals, backed):
for rec in goals:
so_far = backed.get(rec['id'], 0)
row = [ rec['id']
, rec['goal']
, so_far
, (so_far / rec['goal']) * 100
, rec['statement']
]
yield row

def sortfunc(a, b):
percentage = cmp(a[3], b[3])
if percentage != 0: # percentage descending
return percentage * -1
else: # goal, id ascending
return cmp((a[1], a[0]), (b[1], a[1]))
from gittip import db

# ==== ^L

ngoals = db.fetchone("SELECT count(id) FROM participants WHERE goal > 0")
ngoals = ngoals['count'] if ngoals is not None else 0
goals = db.fetchall(""" \

SELECT id, statement, goal
FROM participants
WHERE goal > 0
ORDER BY goal DESC

ngoals = db.fetchone("SELECT count(id) FROM participants WHERE goal > 0")['count']
goals = db.fetchall("""
select * from goal_summary
order by percentage desc, goal asc, id asc
""")
goals = goals if goals is not None else []

backed = db.fetchall("""
SELECT tippee as id, sum(amount) as amount
FROM ( SELECT DISTINCT ON (tipper, tippee) tippee, amount
FROM tips
JOIN participants p ON p.id = tipper
JOIN participants p2 ON p2.id = tippee
WHERE p.last_bill_result = ''
AND p2.claimed_time IS NOT NULL
ORDER BY tipper, tippee, mtime DESC
) AS foo
GROUP BY tippee
""")
backed = backed if backed is not None else []
backed = {rec['id']:rec['amount'] for rec in backed}


rows = list(rows(goals, backed))
rows.sort(cmp=sortfunc)


# ==== ^L
{% extends templates/base.html %}
Expand Down Expand Up @@ -81,15 +38,15 @@ <h2>{{ ngoals }} people have set a weekly funding goal.</h2>
<th style="text-align: right;">Goal ($)</a></th>
<th colspan="2" style="text-align: right;">Backed ($, %)</a></th>
</tr>
{% for participant_id, goal, backed, percentage, statement, in rows %}
{% for row in goals %}
<tr>
<th><a href="/{{ participant_id }}/">{{ participant_id }}</a></th>
<td>{{ locale.format("%.2f", goal, grouping=True) }}</td>
<td>{{ locale.format("%.2f", backed, grouping=True) }}</td>
<td>{{ "%5.1f" % percentage }}</td>
<th><a href="/{{ row['id'] }}/">{{ row['id'] }}</a></th>
<td>{{ locale.format("%.2f", row['goal'], grouping=True) }}</td>
<td>{{ locale.format("%.2f", row['amount'], grouping=True) }}</td>
<td>{{ "%5.1f" % row['percentage'] }}</td>
</tr>
<tr>
<td class="statement" colspan="4"><div>{{ escape(statement) }}</div></td>
<td class="statement" colspan="4"><div>{{ escape(row['statement']) }}</div></td>
</tr>
{% end %}
</table>
Expand Down

0 comments on commit 170e386

Please sign in to comment.