2024-12-15 15:05:21 +00:00
import sqlite3
2024-12-22 07:06:49 +00:00
import time
import matplotlib . pyplot as plt
import pathlib
from xmlrpc . client import ServerProxy
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
api = ServerProxy ( " https://api.molodetz.nl/rpc " )
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
connection = sqlite3 . connect ( ' tikker.db ' )
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
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 "
)
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
def query ( sql ) :
start = time . time ( )
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
cursor = connection . cursor ( )
print ( sql )
result = cursor . execute ( sql ) . fetchall ( )
cursor . close ( )
duration = time . time ( ) - start
print ( " Duration: {} \n " . format ( duration ) )
return result
def render_per_hour ( 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} "
" 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 "
)
rows_presses = query ( sql_presses . format ( week = week ) )
rows_repeated = query ( sql_repeated . format ( week = week ) )
rows_released = query ( sql_released . format ( week = week ) )
2024-12-15 15:05:21 +00:00
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 ' )
2024-12-22 07:06:49 +00:00
plt . plot ( hours , totals , marker = ' o ' , label = f ' Presses per hour week { week } ' , color = ' red ' )
2024-12-15 15:05:21 +00:00
plt . xlabel ( ' Hour ' )
plt . ylabel ( ' Event count ' )
plt . title ( ' Keyboard events ' )
plt . legend ( )
plt . grid ( True )
2024-12-22 07:06:49 +00:00
plt . savefig ( f " graph_week_ { week . strip ( " ' " ) } _per_hour.png " )
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
def render_per_weekday ( week ) :
week = f " ' { week } ' "
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
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} "
" GROUP BY week, day ORDER BY day "
)
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
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} "
" GROUP BY week, day ORDER BY day "
)
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
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} "
" GROUP BY week, day ORDER BY day "
)
2024-12-15 15:05:21 +00:00
2024-12-22 07:06:49 +00:00
rows_presses = query ( sql_presses . format ( week = week ) )
rows_repeated = query ( sql_repeated . format ( week = week ) )
rows_released = query ( sql_released . format ( week = week ) )
2024-12-15 15:05:21 +00:00
totals = [ row [ 0 ] for row in rows_presses ]
2024-12-22 07:06:49 +00:00
days = [ row [ 2 ] for row in rows_presses ]
2024-12-15 15:05:21 +00:00
totals_repeated = [ row [ 0 ] for row in rows_repeated ]
2024-12-22 07:06:49 +00:00
days_repeated = [ row [ 2 ] for row in rows_repeated ]
2024-12-15 15:05:21 +00:00
totals_released = [ row [ 0 ] for row in rows_released ]
2024-12-22 07:06:49 +00:00
days_released = [ row [ 2 ] for row in rows_released ]
2024-12-15 15:05:21 +00:00
plt . figure ( figsize = ( 8 , 6 ) )
2024-12-22 07:06:49 +00:00
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 ' )
2024-12-15 15:05:21 +00:00
plt . xlabel ( ' Day of week (0 = Sunday, 6 = Saturday) ' )
plt . ylabel ( ' Event count ' )
plt . title ( ' Keyboard events ' )
plt . legend ( )
plt . grid ( True )
2024-12-22 07:06:49 +00:00
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 "
weeks = query ( sql )
return [ record [ 0 ] for record in weeks ]
def get_score_per_week ( ) :
sql = (
" SELECT strftime( ' % U ' , timestamp) as week, event, COUNT(0) as total "
" FROM kevent GROUP BY event, week "
)
return query ( sql )
def get_score_per_day ( ) :
sql = " SELECT count(0) as total, 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 as weekday, strftime( ' % w ' , timestamp) as day, strftime( ' % U ' , timestamp) as week FROM kevent WHERE event = ' REPEATED ' AND week = ' 50 ' GROUP BY week, day ORDER BY day "
sql = (
f " SELECT strftime( ' %U ' ,timestamp) as week, { weekday_sql } as wday, event, COUNT(0) as total "
f " FROM kevent WHERE event in ( ' PRESSED ' ) GROUP BY week, event, wday ORDER BY week, event, wday "
)
return query ( sql )
def get_totals ( ) :
sql = " SELECT count(0) as total, event from kevent group by event "
return query ( sql )
# Main execution
if __name__ == " __main__ " :
time_start = time . time ( )
for week in get_weeks ( ) :
render_per_hour ( week )
render_per_weekday ( week )
print ( " Score per week: " )
for record in get_score_per_week ( ) :
print ( f " { record [ 0 ] } \t { record [ 1 ] } \t { record [ 2 ] } " )
print ( " Score per day: " )
for record in get_score_per_day ( ) :
print ( f " { record [ 0 ] } \t { record [ 1 ] } \t { record [ 2 ] } " )
print ( " Total events: " )
totals = 0
for record in get_totals ( ) :
print ( f " { record [ 1 ] } : { record [ 0 ] } " )
totals + = record [ 0 ]
print ( totals )
result = { }
2024-12-25 22:31:46 +00:00
rows = query ( " SElECT strftime( ' % Y- % m- %d . % H ' , timestamp) as date_hour, GROUP_CONCAT(char, ' ' ) FROM kevent WHERE event = ' PRESSED ' group by date_hour " )
2024-12-22 07:06:49 +00:00
for row in rows :
result [ row [ 0 ] ] = row [ 1 ]
with open ( " keylog.txt " , " w " ) as f :
for day in result . keys ( ) :
2024-12-25 22:31:46 +00:00
date , hour = day . split ( " . " )
label = f " { date } { hour } :00 "
if not pathlib . Path ( " logs_plain/ " + day + " .txt " ) . exists ( ) :
with open ( " logs_plain/ " + day + " .txt " , " w " ) as g :
g . write ( f " ** { label } **: ``` { result [ day ] } ``` \n \n " )
f . write ( f " ** { label } **: ``` { result [ day ] } ``` \n \n " )
import json
2024-12-22 07:06:49 +00:00
for file in pathlib . Path ( " . " ) . glob ( " logs_plain/*.txt " ) :
print ( " Working on: {} " . format ( file ) )
dest_file = file . parent . parent . joinpath ( " logs_summaries " ) . joinpath ( file . name )
print ( " Dest file: " , dest_file )
2024-12-25 22:31:46 +00:00
if dest_file . exists ( ) :
continue
2024-12-22 07:06:49 +00:00
with dest_file . open ( " w+ " ) as f :
print ( " Requesting... " )
2024-12-25 22:31:46 +00:00
param = file . read_text ( ) . replace ( " @ " , " " ) . replace ( " ` " , " " )
response = api . gpt4o_mini ( " The following data is key presses made by user. Describe what user could be working on using bulletpoints: " + param )
2024-12-22 07:06:49 +00:00
print ( " Done " )
f . write ( response )
print ( response )
print ( " Duration: {} " . format ( time . time ( ) - time_start ) )