Updated merge and plot.

This commit is contained in:
retoor 2025-01-07 23:45:49 +01:00
parent be1e5c9b59
commit ff53059a74
3 changed files with 97 additions and 44 deletions

View File

@ -11,6 +11,9 @@ PYTHON="./.venv/bin/python"
ensure_env:
-@python3 -m venv .venv
merge:
$(PYTHON) merge.py
plot: ensure_env
time $(PYTHON) plot.py
time $(PYTHON) merge.py

View File

@ -15,6 +15,7 @@ html_content = ['<html>',
<style>
body {
width:100%;
background-color: #000;
}
img {
width:40%;

137
plot.py
View File

@ -35,106 +35,153 @@ def query(sql):
return result
def render_per_hour(week):
week = f"'{week}'"
week = f"{week}"
sql_presses = (
"SELECT count(0) as total, strftime('%H', timestamp) as hour, "
"strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'PRESSED' AND week = {week} "
"FROM kevent WHERE event = 'PRESSED' AND week = '{week}' "
"GROUP BY week, hour ORDER BY hour"
)
sql_repeated = (
"SELECT count(0) as total, strftime('%H', timestamp) as hour, "
"strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'REPEATED' AND week = {week} "
"GROUP BY week, hour ORDER BY hour"
)
#sql_repeated = (
# "SELECT count(0) as total, strftime('%H', timestamp) as hour, "
# "strftime('%U', timestamp) as week "
# "FROM kevent WHERE event = 'REPEATED' AND week = {week} "
# "GROUP BY week, hour ORDER BY hour"
#)
sql_released = (
"SELECT count(0) as total, strftime('%H', timestamp) as hour, "
"strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'RELEASED' AND week = {week} "
"GROUP BY week, hour ORDER BY hour"
)
#sql_released = (
# "SELECT count(0) as total, strftime('%H', timestamp) as hour, "
# "strftime('%U', timestamp) as week "
# "FROM kevent WHERE event = 'RELEASED' AND week = {week} "
# "GROUP BY week, hour ORDER BY hour"
#)
rows_presses = query(sql_presses.format(week=week))
rows_repeated = query(sql_repeated.format(week=week))
rows_released = query(sql_released.format(week=week))
#rows_repeated = query(sql_repeated.format(week=week))
#rows_released = query(sql_released.format(week=week))
totals = [row[0] for row in rows_presses]
hours = [row[1] for row in rows_presses]
totals_repeated = [row[0] for row in rows_repeated]
hours_repeated = [row[1] for row in rows_repeated]
#totals_repeated = [row[0] for row in rows_repeated]
#hours_repeated = [row[1] for row in rows_repeated]
totals_released = [row[0] for row in rows_released]
hours_released = [row[1] for row in rows_released]
#totals_released = [row[0] for row in rows_released]
#hours_released = [row[1] for row in rows_released]
plt.figure(figsize=(8, 6))
plt.plot(hours_repeated, totals_repeated, marker='o', label='Repeats per hour', color='green')
plt.plot(hours_released, totals_released, marker='o', label='Releases per hour', color='orange')
#plt.plot(hours_repeated, totals_repeated, marker='o', label='Repeats per hour', color='green')
#plt.plot(hours_released, totals_released, marker='o', label='Releases per hour', color='orange')
plt.plot(hours, totals, marker='o', label=f'Presses per hour week {week}', color='red')
plt.xlabel('Hour')
plt.ylabel('Event count')
plt.title('Keyboard events')
plt.title(f'Key presses per hour. Week {week}')
plt.style.use('dark_background')
plt.legend()
plt.grid(True)
plt.savefig(f"graph_week_{week.strip("'")}_per_hour.png")
plt.savefig(f"graph_week_{week.strip('\'')}_per_hour.png")
def render_per_day():
sql_pressed_per_day = (
"SELECT strftime('%Y-%m-%d', timestamp) as month_day,count(0) as total FROM kevent WHERE event = 'PRESSED' GROUP BY month_day ORDER BY month_day"
)
plt.figure(figsize=(8,6))
rows_pressed_per_day = query(sql_pressed_per_day)
totals = [row[0] for row in rows_pressed_per_day]
dates = [row[1] for row in rows_pressed_per_day]
plt.plot(totals, dates, marker='o', label='Presses per day', color='red')
plt.xlabel('Date')
plt.ylabel('Event count')
plt.xticks(rotation=45)
plt.title('Keyboard events')
plt.tight_layout()
plt.style.use('dark_background')
plt.legend()
plt.savefig(f"graph_per_day.png")
def render_per_week():
sql_pressed_per_day = (
f"SELECT strftime('%Y-%U', timestamp) as week,count(0) as total FROM kevent WHERE event = 'PRESSED' GROUP BY week ORDER BY week"
)
plt.figure(figsize=(8,6))
rows_pressed_per_day = query(sql_pressed_per_day)
totals = [row[0] for row in rows_pressed_per_day]
dates = [row[1] for row in rows_pressed_per_day]
plt.plot(totals, dates, marker='o', label='Presses per day', color='red')
plt.xlabel('Week')
plt.ylabel('Presses count')
plt.xticks(rotation=45)
plt.title(f'Presses per week')
plt.tight_layout()
plt.style.use('dark_background')
plt.legend()
plt.savefig(f"graph_per_week.png")
def render_per_weekday(week):
week = f"'{week}'"
sql_presses = (
f"SELECT count(0) as total, {weekday_sql} as weekday, "
"strftime('%w', timestamp) as day, strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'PRESSED' AND week = {week} "
"FROM kevent WHERE event = 'PRESSED' AND week = '{week}' "
"GROUP BY week, day ORDER BY day"
)
sql_repeated = (
f"SELECT count(0) as total, {weekday_sql} as weekday, "
"strftime('%w', timestamp) as day, strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'REPEATED' AND week = {week} "
"FROM kevent WHERE event = 'REPEATED' AND week = '{week}' "
"GROUP BY week, day ORDER BY day"
)
sql_released = (
f"SELECT count(0) as total, {weekday_sql} as weekday, "
"strftime('%w', timestamp) as day, strftime('%U', timestamp) as week "
"FROM kevent WHERE event = 'RELEASED' AND week = {week} "
"FROM kevent WHERE event = 'RELEASED' AND week = '{week}' "
"GROUP BY week, day ORDER BY day"
)
rows_presses = query(sql_presses.format(week=week))
rows_repeated = query(sql_repeated.format(week=week))
rows_released = query(sql_released.format(week=week))
#rows_repeated = query(sql_repeated.format(week=week))
#rows_released = query(sql_released.format(week=week))
totals = [row[0] for row in rows_presses]
days = [row[2] for row in rows_presses]
totals_repeated = [row[0] for row in rows_repeated]
days_repeated = [row[2] for row in rows_repeated]
#totals_repeated = [row[0] for row in rows_repeated]
#days_repeated = [row[2] for row in rows_repeated]
totals_released = [row[0] for row in rows_released]
days_released = [row[2] for row in rows_released]
#totals_released = [row[0] for row in rows_released]
#days_released = [row[2] for row in rows_released]
plt.figure(figsize=(8, 6))
plt.plot(days_repeated, totals_repeated, marker='o', label='Repeats per weekday', color='green')
plt.plot(days_released, totals_released, marker='o', label='Releases per weekday', color='orange')
plt.plot(days, totals, marker='o', label=f'Presses per weekday week {week}', color='red')
#plt.plot(days_repeated, totals_repeated, marker='o', label='Repeats per weekday', color='green')
#plt.plot(days_released, totals_released, marker='o', label='Releases per weekday', color='orange')
plt.plot(days, totals, marker='o', label=f'Press count', color='red')
plt.xlabel('Day of week (0 = Sunday, 6 = Saturday)')
plt.xlabel('Weekday (0 = Sunday, 6 = Saturday)')
plt.ylabel('Event count')
plt.title('Keyboard events')
plt.title(f'Presses per weekday. Week {week}')
plt.style.use('dark_background')
plt.legend()
plt.grid(True)
plt.savefig(f"graph_week_{week.strip("'")}_per_weekday.png")
plt.savefig(f"graph_week_{week.strip('\"')}_per_weekday.png")
def get_weeks():
sql = "SELECT strftime('%U', timestamp) as week FROM kevent GROUP BY week"
@ -166,6 +213,8 @@ def get_totals():
if __name__ == "__main__":
time_start = time.time()
render_per_day()
render_per_week()
for week in get_weeks():
render_per_hour(week)
render_per_weekday(week)