You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
coffeestatistics/main.py

56 lines
2.0 KiB

from sqlalchemy import create_engine, text
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
# Get the relevant data
engine = create_engine('sqlite:///history/data.sqlite')
query = 'SELECT * FROM messages'
data = pd.read_sql_query(sql=text(query), con=engine.connect())
data = data[data['content']=='Coffee']
grand_total = len(data)
data['datetime'] = pd.to_datetime(data['date'])
# Calculate weekdays and hours
data['weekday'] = data['datetime'].dt.dayofweek + 1
data['hour'] = data['datetime'].dt.hour
data['day'] = data['datetime'].dt.day
lastmonth = (dt.datetime.now().month - 1) % 12
monthly = data[data['datetime'].dt.month == lastmonth].copy()
# Make plots, first for monthly, then for all time
monthly['count'] = 1
monthly.groupby(['weekday']).count()['count'].plot.bar(x='weekday', align='center')
plt.xticks(rotation=0)
plt.title('Weekday frequency of last month')
plt.savefig('files/monthly_weekdays.png')
plt.close()
monthly.groupby(['hour']).count()['count'].plot.bar(x='hour', align='center')
plt.xticks(rotation=0)
plt.title('Hourly frequency of last month')
plt.savefig('files/monthly_hour.png')
plt.close()
data['count'] = 1
data.groupby(['weekday']).count()['count'].plot.bar(x='weekday', align='center')
plt.xticks(rotation=0)
plt.title('Weekday frequency of all time')
plt.savefig('files/all_weekdays.png')
plt.close()
data.groupby(['hour']).count()['count'].plot.bar(x='hour', align='center')
plt.title('Hourly frequency of all time')
plt.savefig('files/all_hour.png')
plt.close()
# Calculate on which day the most coffee was made
aggregated = monthly.groupby(['day']).count()['count']
message1 = 'Last month, coffee was made {amount} times.'.format(amount=len(monthly)) + ' Most coffee was made on day {day} of the month, with a total amount of {amount} times.'.format(day=aggregated.idxmax(),amount=aggregated.max()) + ' From the start of this chat, a grand total amount of {amount} times coffee was made.'.format(amount=grand_total)
f = open('message.txt', 'w')
f.write(message1)
f.close()