dibbler/statistikk.py

198 lines
5.4 KiB
Python
Raw Normal View History

2011-06-22 20:33:57 +02:00
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
from statistikkHelpers import *
import matplotlib.dates as mdates
def getInputType():
inp = 0
while not (inp == '1' or inp == '2' or inp == '3' or inp == '4'):
2018-08-19 16:38:45 +02:00
print('type 1 for user-statistics')
print('type 2 for product-statistics')
print('type 3 for global-statistics')
print('type 4 to enter loop-mode')
inp = input('')
2011-06-22 20:33:57 +02:00
return int(inp)
def getDateFile(date, n):
try:
if n==0:
2018-08-19 16:38:45 +02:00
inp = input('start date? (yyyy-mm-dd) ')
2011-06-22 20:33:57 +02:00
elif n==-1:
2018-08-19 16:38:45 +02:00
inp = input('end date? (yyyy-mm-dd) ')
2011-06-22 20:33:57 +02:00
year = inp.partition('-')
month = year[2].partition('-')
return datetime.date(int(year[0]), int(month[0]), int(month[2]))
except:
2018-08-19 16:38:45 +02:00
print('invalid date, setting start start date')
2011-06-22 20:33:57 +02:00
if n==0:
2018-08-19 16:38:45 +02:00
print('to date found on first line')
2011-06-22 20:33:57 +02:00
elif n==-1:
2018-08-19 16:38:45 +02:00
print('to date found on last line')
print(date)
2011-06-22 20:33:57 +02:00
return datetime.date(int(date.partition('-')[0]), int(date.partition('-')[2].partition('-')[0]), int(date.partition('-')[2].partition('-')[2]))
def dateToDateNumFile(date, startDate):
year = date.partition('-')
month = year[2].partition('-')
day = datetime.date(int(year[0]), int(month[0]), int(month[2]))
deltaDays = day-startDate
return int(deltaDays.days), day.weekday()
def getProducts(products):
product = []
products = products.partition('¤')
product.append(products[0])
while (products[1]=='¤'):
products = products[2].partition('¤')
product.append(products[0])
return product
def piePlot(dictionary, n):
keys = []
values = []
i=0
for key in sorted(dictionary, key=dictionary.get, reverse=True):
values.append(dictionary[key])
if i<n:
keys.append(key)
i += 1
else:
keys.append('')
plt.pie(values, labels=keys)
def datePlot(array, dateLine):
if (not array == []):
plt.bar(dateLine, array)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b'))
def dayPlot(array, days):
if (not array == []):
for i in range(7):
array[i]=array[i]*7.0/days
2018-08-19 16:38:45 +02:00
plt.bar(list(range(7)), array)
plt.xticks(list(range(7)),[' mon',' tue',' wed',' thu',' fri',' sat',' sun'])
2011-06-22 20:33:57 +02:00
def graphPlot(array, dateLine):
if (not array == []):
plt.plot(dateLine, array)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b'))
def plotUser(database, dateLine, user, n):
printUser(database, dateLine, user, n)
plt.subplot(221)
piePlot(database.personVareAntall[user], n)
plt.xlabel('antall varer kjøpt gjengitt i antall')
plt.subplot(222)
datePlot(database.personDatoVerdi[user], dateLine)
plt.xlabel('penger brukt over dato')
plt.subplot(223)
piePlot(database.personVareVerdi[user], n)
plt.xlabel('antall varer kjøpt gjengitt i verdi')
plt.subplot(224)
dayPlot(database.personUkedagVerdi[user], len(dateLine))
plt.xlabel('forbruk over ukedager')
plt.show()
def plotProduct(database, dateLine, product, n):
printProduct(database, dateLine, product, n)
plt.subplot(221)
piePlot(database.varePersonAntall[product], n)
plt.xlabel('personer som har handler produktet')
plt.subplot(222)
datePlot(database.vareDatoAntall[product], dateLine)
plt.xlabel('antall produkter handlet per dag')
#plt.subplot(223)
plt.subplot(224)
dayPlot(database.vareUkedagAntall[product], len(dateLine))
plt.xlabel('antall over ukedager')
plt.show()
def plotGlobal(database, dateLine, n):
printGlobal(database, dateLine, n)
plt.subplot(231)
piePlot(database.globalVareVerdi, n)
plt.xlabel('varer kjøpt gjengitt som verdi')
plt.subplot(232)
datePlot(database.globalDatoForbruk, dateLine)
plt.xlabel('forbruk over dato')
plt.subplot(233)
graphPlot(database.pengebeholdning, dateLine)
plt.xlabel('pengebeholdning over tid (negativ verdi utgjør samlet kreditt)')
plt.subplot(234)
piePlot(database.globalPersonForbruk, n)
plt.xlabel('penger brukt av personer')
plt.subplot(235)
dayPlot(database.globalUkedagForbruk, len(dateLine))
plt.xlabel('forbruk over ukedager')
plt.show()
def alt4menu(database, dateLine, useDatabase):
n=10
while 1:
2018-08-19 16:38:45 +02:00
print('\n1: user-statistics, 2: product-statistics, 3:global-statistics, n: adjust amount of data shown q:quit')
2011-06-22 20:33:57 +02:00
try:
2018-08-19 16:38:45 +02:00
inp = input('')
2011-06-22 20:33:57 +02:00
except:
continue
if inp == 'q':
break
elif inp == '1':
if i=='0':
2018-08-19 16:38:45 +02:00
user = input('input full username: ')
2011-06-22 20:33:57 +02:00
else:
user = getUser()
plotUser(database, dateLine, user, n)
elif inp == '2':
if i=='0':
2018-08-19 16:38:45 +02:00
product = input('input full product name: ')
2011-06-22 20:33:57 +02:00
else:
product = getProduct()
plotProduct(database, dateLine, product, n)
elif inp == '3':
plotGlobal(database, dateLine, n)
elif inp == 'n':
try:
2018-08-19 16:38:45 +02:00
n=int(input('set number to show '));
2011-06-22 20:33:57 +02:00
except:
pass
#---------------------------------------MAIN-------------------------------------
inputType=getInputType()
2018-08-19 16:38:45 +02:00
i = input('0:fil, 1:database \n? ')
2011-06-22 20:33:57 +02:00
if (inputType == 1):
if i=='0':
2018-08-19 16:38:45 +02:00
user = input('input full username: ')
2011-06-22 20:33:57 +02:00
else:
user = getUser()
product = ''
elif (inputType == 2):
if i=='0':
2018-08-19 16:38:45 +02:00
product = input('input full product name: ')
2011-06-22 20:33:57 +02:00
else:
product = getProduct()
user = ''
else :
product = ''
user = ''
if i=='0':
2018-08-19 16:38:45 +02:00
inputFile=input('logfil? ')
2011-06-22 20:33:57 +02:00
if inputFile=='':
inputFile='default.dibblerlog'
database, dateLine = buildDatabaseFromFile(inputFile, inputType, product, user)
else:
database, dateLine = buildDatabaseFromDb(inputType, product, user)
if (inputType==1):
plotUser(database, dateLine, user, 10)
if (inputType==2):
plotProduct(database, dateLine, product, 10)
if (inputType==3):
plotGlobal(database, dateLine, 10)
if (inputType==4):
alt4menu(database, dateLine, i)