gold progress: Use Pacific time.

Additionally, this adds a function for fetching revenue by day in batch.
This commit is contained in:
Neil Williams
2013-11-11 11:40:48 -08:00
parent e97767715d
commit 1daefe4ad7
3 changed files with 21 additions and 9 deletions

View File

@@ -36,6 +36,7 @@ from r2.models.gold import (
days_to_pennies,
gold_goal_on,
gold_revenue_on,
TIMEZONE as GOLD_TIMEZONE,
)
from r2.models.promo import (
NO_TRANSACTION,
@@ -4276,13 +4277,13 @@ class InterestBar(Templated):
class Goldvertisement(Templated):
def __init__(self):
now = datetime.datetime.now(g.display_tz)
now = datetime.datetime.now(GOLD_TIMEZONE)
today = now.date()
tomorrow = today + datetime.timedelta(days=1)
end_time = datetime.datetime(tomorrow.year,
tomorrow.month,
tomorrow.day,
tzinfo=g.display_tz)
tzinfo=GOLD_TIMEZONE)
revenue_today = gold_revenue_on(today)
yesterday = today - datetime.timedelta(days=1)
revenue_yesterday = gold_revenue_on(yesterday)

View File

@@ -22,6 +22,8 @@
from r2.lib.db.tdb_sql import make_metadata, index_str, create_table
import pytz
from pylons import g, c
from pylons.i18n import _
from datetime import datetime, timedelta
@@ -54,6 +56,7 @@ ENGINE_NAME = 'authorize'
ENGINE = g.dbm.get_engine(ENGINE_NAME)
METADATA = make_metadata(ENGINE)
TIMEZONE = pytz.timezone("America/Los_Angeles")
Session = scoped_session(sessionmaker(bind=ENGINE))
Base = declarative_base(bind=ENGINE)
@@ -448,14 +451,22 @@ def append_random_bottlecap_phrase(message):
return message
def gold_revenue_multi(dates):
NON_REVENUE_STATUSES = ("declined", "chargeback", "fudge")
date_expr = sa.func.date_trunc('day',
sa.func.timezone(TIMEZONE.zone, gold_table.c.date))
query = (select([date_expr, sa_sum(gold_table.c.pennies)])
.where(~ gold_table.c.status.in_(NON_REVENUE_STATUSES))
.where(date_expr.in_(dates))
.group_by(date_expr)
)
return {truncated_time.date(): pennies
for truncated_time, pennies in ENGINE.execute(query)}
@memoize("gold-revenue", time=600)
def gold_revenue_on(date):
NON_REVENUE_STATUSES = ("declined", "chargeback", "fudge")
query = (select([sa_sum(gold_table.c.pennies)])
.where(~ gold_table.c.status.in_(NON_REVENUE_STATUSES))
.where(sa.func.date_trunc('day', gold_table.c.date) == date))
rows = ENGINE.execute(query)
return rows.fetchone()[0] or 0
return gold_revenue_multi([date]).get(date, 0)
@memoize("gold-goal")

View File

@@ -57,7 +57,7 @@
<p class="aside">This daily goal updates every 10 minutes and is reset at
midnight&#32;<a target="_blank"
href="http://en.wikipedia.org/wiki/Mountain_Standard_Time">MST</a>&#32;
href="http://en.wikipedia.org/wiki/Pacific_Time_Zone">Pacific Time</a>&#32;
(${thing.time_left_today} from now).</p>
<div class="history">