- added beer diagram, generatiors for test consumtions
This commit is contained in:
@@ -4,6 +4,7 @@ from app import app
|
||||
from user import User
|
||||
from product import Product
|
||||
from consumption import Consumption
|
||||
import random as rand
|
||||
import datetime
|
||||
|
||||
DATABASE = 'test/database.db'
|
||||
@@ -177,3 +178,40 @@ def add_consume(username, productid):
|
||||
query_db("INSERT INTO CONSUMED (PRODNR, CONSUMER, PRICE, TIME) VALUES (?, ?, ?, ?)", (str(product.id), str(consumerid), product.price, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
|
||||
get_db().commit()
|
||||
print "consumed"
|
||||
|
||||
return
|
||||
|
||||
##for testing only
|
||||
def generate_test_users():
|
||||
|
||||
for i in range(1, 30):
|
||||
test = User
|
||||
test.name = 'test' + str(i)
|
||||
test.email = 'test' + str(i) + '@test.at'
|
||||
test.password = 'test'
|
||||
test.longname = 'Test User' + str(i)
|
||||
test.rfid_id = '0x00000000'
|
||||
add_user(test)
|
||||
return
|
||||
|
||||
def generate_test_consumptions():
|
||||
|
||||
#rand.seed(5)
|
||||
num_u = len(get_users())
|
||||
num_p = len(get_products())
|
||||
u = 0
|
||||
p = 0
|
||||
for i in range(1, 666):
|
||||
u = 1 + int(rand.random() * (1.0*num_u))
|
||||
p = 1 + int(rand.random() * (1.0*num_p))
|
||||
daysa = int(rand.random() * (30.0))
|
||||
add_test_consume(u,p,daysa)
|
||||
print 'trying to add ' + str(p) + ' to ' + str(u)
|
||||
return
|
||||
|
||||
def add_test_consume(consumerid, productid, daysago):
|
||||
|
||||
product = get_product_by_id(productid)
|
||||
query_db("INSERT INTO CONSUMED (PRODNR, CONSUMER, PRICE, TIME) VALUES (?, ?, ?, ?)", (str(product.id), str(consumerid), product.price, (datetime.datetime.now()-datetime.timedelta(days=daysago)).strftime("%Y-%m-%d %H:%M:%S")))
|
||||
get_db().commit()
|
||||
return
|
||||
91
app/plot.py
91
app/plot.py
@@ -13,22 +13,35 @@ def plot_total(user = None):
|
||||
|
||||
# all_consumptions = list(10)
|
||||
# for consumptions in all_consumptions: #todo fix
|
||||
consumptions = np.zeros(len(dates))
|
||||
# consumptions = np.zeros(len(dates))
|
||||
|
||||
|
||||
allconsumptions = [[0 for x in range(len(dates))] for product in get_products()]
|
||||
print allconsumptions
|
||||
# allconsumptions = ()
|
||||
# allconsumptions = allconsumptions + (np.zeros(len(dates)))
|
||||
#print consumptions
|
||||
|
||||
consumed = get_consumed()
|
||||
for consumption in consumed:
|
||||
if consumption.prodnr == 1:
|
||||
if user == None or consumption.consumer == user.id:
|
||||
if consumption.time.date() > begin:
|
||||
consumptions[(consumption.time.date() - begin).days - 1] += 1
|
||||
if user == None or consumption.consumer == user.id:
|
||||
i = 1
|
||||
for consumptions in allconsumptions:
|
||||
if consumption.prodnr == i:
|
||||
if consumption.time.date() > begin:
|
||||
consumptions[(consumption.time.date() - begin).days - 1] += 1
|
||||
i += 1
|
||||
plt.xkcd()
|
||||
|
||||
|
||||
print allconsumptions
|
||||
fig, ax = plt.subplots()
|
||||
i=1
|
||||
|
||||
ax.plot(dates, consumptions, linestyle='-', marker='')
|
||||
for consumptions in allconsumptions:
|
||||
ax.plot(dates, consumptions, linestyle='-', marker='', label=get_product_by_id(i).name)
|
||||
i += 1
|
||||
|
||||
ax.legend(loc=2,prop={'size':15})
|
||||
|
||||
# ax.axes.margins = 1 # x margin. See `axes.Axes.margins`
|
||||
# ax.axes.ymargin = 1 # y margin See `axes.Axes.margins`
|
||||
@@ -57,7 +70,7 @@ def plot_total(user = None):
|
||||
xy=(30, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
|
||||
|
||||
plt.xlabel('Datum')
|
||||
plt.ylabel('Halbe')
|
||||
plt.ylabel('Konsumate')
|
||||
|
||||
if user == None:
|
||||
tit = "Bierkonsum FET"
|
||||
@@ -75,3 +88,65 @@ def plot_total(user = None):
|
||||
|
||||
fig.set_size_inches(4.8, 3.2)
|
||||
plt.savefig(fill, dpi=400)
|
||||
|
||||
def plot_list(duration):
|
||||
|
||||
today = datetime.date.today()
|
||||
begin = datetime.date.today() - datetime.timedelta(weeks=duration)
|
||||
|
||||
users = get_users()
|
||||
#consumptions = [0 for user in users]
|
||||
allconsumptions = [[0 for user in users] for product in get_products()]
|
||||
|
||||
consumed = get_consumed()
|
||||
for consumption in consumed:
|
||||
allconsumptions[consumption.prodnr-1][consumption.consumer-1] += 1
|
||||
|
||||
#print 'debug ------------------'
|
||||
#print consumptions
|
||||
#print '------------------------'
|
||||
|
||||
plt.xkcd()
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
colors = ['red','green','blue']
|
||||
|
||||
i=0
|
||||
for consumptions in allconsumptions:
|
||||
ax.barh(np.arange(len(consumptions)), consumptions, label=get_product_by_id(i+1).name, align='center', height=0.5, color=colors[i])
|
||||
i+=1
|
||||
|
||||
names = list()
|
||||
|
||||
for user in users:
|
||||
names.append(user.longname)
|
||||
|
||||
plt.yticks(np.arange(len(names)), names)
|
||||
|
||||
ax.legend(loc=2)
|
||||
|
||||
ax.spines['right'].set_visible(False)
|
||||
ax.spines['top'].set_visible(False)
|
||||
|
||||
ax.yaxis.set_ticks_position('none')
|
||||
ax.xaxis.set_ticks_position('none')
|
||||
plt.subplots_adjust(left=0.2)
|
||||
#plt.tick_params(which='minor', length=4)
|
||||
#plt.tick_params(which='major', length=5)
|
||||
|
||||
#plt.annotate(
|
||||
# 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
|
||||
# xy=(30, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
|
||||
|
||||
plt.xlabel('Konsumate')
|
||||
#plt.ylabel('Konsumate')
|
||||
|
||||
plt.title("Bierliste")
|
||||
|
||||
#1024x768
|
||||
fig.set_size_inches(10.24, 7.68)
|
||||
plt.savefig('app/static/bierliste.png', dpi=100)
|
||||
#800x600
|
||||
fig.set_size_inches(12, 8)
|
||||
plt.savefig('app/static/bierliste_small.png', dpi=72)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
<body>
|
||||
<!-- Menu -->
|
||||
<h1> Menu </h1>
|
||||
<ul>
|
||||
<li><a href="/index">Home</a></li>
|
||||
{% if user %}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
</ul>
|
||||
<h1> Bierliste </h1>
|
||||
<img src="{{ url_for('static', filename='bierliste_small.png') }}">
|
||||
<table>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
|
||||
@@ -19,6 +19,9 @@ def static_proxy(path):
|
||||
def index():
|
||||
consumed = get_consumed()
|
||||
plot_total()
|
||||
plot_list(4)
|
||||
#generate_test_users()
|
||||
#generate_test_consumptions()
|
||||
return render_template("index.html", consumed=consumed, user=get_user_by_name(session.get('name')))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user