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()