Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trending 105 #161

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions ui/bartendro/view/trending.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import time
import time, datetime
from bartendro import app, db
from sqlalchemy import desc
from flask import Flask, request, render_template
Expand All @@ -10,6 +10,7 @@
from bartendro.model.booze_group import BoozeGroup
from bartendro.form.booze import BoozeForm

BARTENDRO_DAY_START_TIME = 10 * 60 * 60
DEFAULT_TIME = 12
display_info = {
12 : 'Drinks poured in the last 12 hours.',
Expand All @@ -20,7 +21,48 @@

@app.route('/trending')
def trending_drinks():
return trending_drinks_detail(DEFAULT_TIME)
return trending_drinks_detail(DEFAULT_TIME,'')

# figure out begindate and enddate
# begindate exists, but no enddate = assume it is one day
# enddate exists but no beginndate = assume begindate=first day
# need some text for this.
# begin and enddate need to be in timestamp format.

@app.route('/trending/date/')
def trending_drinks_date():
""" this assumes ?begindate=yyyy-mm-dd&enddate=yyyy-mm-dd
or ?begindate=yyyy-mm-dd (with no enddate)
"""

title = "Drinks by date"

begindate = request.args.get("begindate", "")
if (len(begindate) > 0) :
begin_ts = time.mktime(datetime.datetime.strptime(begindate, "%Y-%m-%d").timetuple())
begin_ts = begin_ts + BARTENDRO_DAY_START_TIME
begindate = datetime.datetime.fromtimestamp(begin_ts).strftime('%c')
else:
begin_ts = 0
begindate = 'The beginning of time'

enddate = request.args.get("enddate", "")
if (len(enddate) == 0):
end_ts = begin_ts + (24 * 60 * 60) - 1
#end_ts = end_ts + BARTENDRO_DAY_START_TIME - 1
enddate = datetime.datetime.fromtimestamp(end_ts).strftime('%c')
else:
end_ts = time.mktime(datetime.datetime.strptime(enddate, "%Y-%m-%d").timetuple())
end_ts = end_ts + 24*60*60+BARTENDRO_DAY_START_TIME - 1
enddate = datetime.datetime.fromtimestamp(end_ts).strftime('%c')

try:
txt = "Drinks poured from %s to %s " % (begindate, enddate)
except IndexError:
txt = "Drinks poured by date"

hours = 0
return trending_drinks_detail(begin_ts, end_ts, txt, hours)

@app.route('/trending/<int:hours>')
def trending_drinks_detail(hours):
Expand All @@ -44,10 +86,19 @@ def trending_drinks_detail(hours):
else:
begindate = 0
else:
begindate = 0
begindate = 0
enddate = 0
txt = ""

return trending_drinks_detail(begindate, enddate, txt, hours)


def trending_drinks_detail(begindate, enddate, txt='', hours=''):

title = "Trending drinks"

#import pdb
#pdb.set_trace()
total_number = db.session.query("number")\
.from_statement("""SELECT count(*) as number
FROM drink_log
Expand Down Expand Up @@ -75,9 +126,24 @@ def trending_drinks_detail(hours):
ORDER BY count(drink_log.drink_id) desc;""")\
.params(begin=begindate, end=enddate).all()

return render_template("trending", top_drinks = top_drinks, options=app.options,
drinks_by_date = db.session.query("date", "number", "volume")\
.from_statement("""SELECT date(time- :BARTENDRO_DAY_START_TIME,'unixepoch') as date,
count(drink_log.drink_id) AS number,
sum(drink_log.size) AS volume
FROM drink_log, drink_name, drink
WHERE drink_log.drink_id = drink_name.id
AND drink_name.id = drink.id
GROUP BY date
ORDER BY date desc;""")\
.params(BARTENDRO_DAY_START_TIME=BARTENDRO_DAY_START_TIME).all()

return render_template("trending", top_drinks = top_drinks,
drinks_by_date = drinks_by_date,
options=app.options,
title="Trending drinks",
txt=txt,
total_number=total_number[0],
total_volume=total_volume[0],
hours=hours)


3 changes: 3 additions & 0 deletions ui/content/templates/drink/index
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<td id="booze_unit{{ ing.id }}"></td>
</tr>
{% endfor %}
<tr>
<td class="main-td"><div class="adjust-middle" id="size-text">{{ options.drink_size }}</div></td>
</tr>
</table>
</div>
</div>
Expand Down
20 changes: 19 additions & 1 deletion ui/content/templates/trending
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{% set active = "trending" %}
{% block body %}
<div id="scroll-pane">
{% if top_drinks %}
<!-- { % if top_drinks %} -->
{% if 1%}
<div class="row-fluid">
<div class="span6">
<h1>{{ title }}</h1>
Expand Down Expand Up @@ -38,6 +39,7 @@
{% endif %}
</div>
</div>

<table class="table table-striped table-bordered">
<tr><th>drink</th><th>number</th><th>volume</th></tr>
{% for id, name, number, volume in top_drinks %}
Expand All @@ -50,12 +52,28 @@
{% endif %}
{% endfor %}
</table>
<h2>Drinks by Date</h2>
(A day starts at BARTENDRO_DAY_START_TIME, and lasts until BARTENDRO_DAY_START_TIME the next day)
<table class="table table-striped table-bordered">
<tr><th>date</th><th>number</th><th>volume</th></tr>
{% for date, number, volume in drinks_by_date %}
<tr><td><a href="/trending/date?begindate={{ date }}">{{ date }}</a></td><td>{{ number }}</td>
<td>

{% if options.metric %}
{{ "%.2f" | format((volume / 1000)|float) }} liters
{% else %}
{{ "%.2f" | format((volume / 1000) / 3.81|float) }} gallons
{% endif %}
{% endfor %}
</table>
{% else %}
<div>
<h4>No drinks have been poured in the last {{ hours }} hours! Go and be a trend setter!</h4>
<a href="/" class="btn btn-large btn-warning button-border">Main menu</a>
</div>
{% endif %}

</div>
</script>
{% endblock %}