From b75ce9ef4c8df9a9c33e57211331fd93abb7dd1a Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 15 Dec 2024 16:05:21 +0100 Subject: [PATCH] Added plot. --- .gitignore | 2 ++ Makefile | 9 ++++++ plot.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 plot.py diff --git a/.gitignore b/.gitignore index 0f2d683..7c4d409 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ *.db* +.venv + # ---> C++ # Prerequisites *.d diff --git a/Makefile b/Makefile index 6b06542..4c25a92 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,12 @@ tikker: tikker.c sormc.h run: ./tikker + +PYTHON="./.venv/bin/python" + +ensure_env: + -@python3 -m venv .venv + +plot: ensure_env + $(PYTHON) plot.py + diff --git a/plot.py b/plot.py new file mode 100644 index 0000000..65112ef --- /dev/null +++ b/plot.py @@ -0,0 +1,92 @@ +import sqlite3 + +connection = sqlite3.connect('tikker.db') + +def render_per_hour(): + cursor = connection.cursor() + + cursor.execute("SELECT count(0) as total, strftime('%H',timestamp) as hour FROM kevent WHERE event = 'PRESSED' GROUP BY hour ORDEr BY hour") + rows_presses = cursor.fetchall() + + cursor.execute("SELECT count(0) as total, strftime('%H',timestamp) as hour FROM kevent WHERE event = 'REPEATED' GROUP BY hour ORDEr BY hour") + rows_repeated = cursor.fetchall() + + + cursor.execute("SELECT count(0) as total, strftime('%H',timestamp) as hour FROM kevent WHERE event = 'RELEASED' GROUP BY hour ORDEr BY hour") + rows_released = cursor.fetchall() + + import matplotlib.pyplot as plt + + 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_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, totals, marker='o', label='Presses per hour', color='red') + plt.xlabel('Hour') + plt.ylabel('Event count') + plt.title('Keyboard events') + plt.legend() + + plt.grid(True) + plt.show() + +def render_per_weekday(): + cursor = connection.cursor() + + weekday_sql = """ CASE + WHEN strftime("%w",timestamp) = 0 THEN 'Sunday' + WHEN strftime("%w",timestamp) = 1 THEN 'Monday' + WHEN strftime("%w",timestamp) = 2 THEN 'Tuesday' + WHEN strftime("%w",timestamp) = 3 THEN 'Wednesday' + WHEN strftime("%w",timestamp) = 4 THEN 'Thursday' + WHEN strftime("%w",timestamp) = 5 THEN 'Friday' + WHEN strftime("%w",timestamp) = 6 THEN 'Saturday' + END """ + + cursor.execute(f"SELECT count(0) as total, {weekday_sql} as weekday, strftime('%w',timestamp) as hour FROM kevent WHERE event = 'PRESSED' GROUP BY hour ORDEr BY hour") + rows_presses = cursor.fetchall() + + cursor.execute(f"SELECT count(0) as total, {weekday_sql} as weekday, strftime('%w',timestamp) as hour FROM kevent WHERE event = 'REPEATED' GROUP BY hour ORDEr BY hour") + rows_repeated = cursor.fetchall() + + + cursor.execute(f"SELECT count(0) as total, {weekday_sql} as weekday, strftime('%w',timestamp) as hour FROM kevent WHERE event = 'RELEASED' GROUP BY hour ORDEr BY hour") + rows_released = cursor.fetchall() + + import matplotlib.pyplot as plt + + totals = [row[0] for row in rows_presses] + hours = [row[2] for row in rows_presses] + + totals_repeated = [row[0] for row in rows_repeated] + hours_repeated = [row[2] for row in rows_repeated] + + totals_released = [row[0] for row in rows_released] + hours_released = [row[2] for row in rows_released] + + plt.figure(figsize=(8, 6)) + + plt.plot(hours_repeated, totals_repeated, marker='o', label='Repeats per weekday', color='green') + plt.plot(hours_released, totals_released, marker='o', label='Releases per weekday', color='orange') + + plt.plot(hours, totals, marker='o', label='Presses per weekday', color='red') + plt.xlabel('Day of week (0 = Sunday, 6 = Saturday)') + plt.ylabel('Event count') + plt.title('Keyboard events') + plt.legend() + + plt.grid(True) + plt.show() + +render_per_hour() +render_per_weekday()