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 ) :
2025-01-07 22:45:49 +00:00
week = f " { week } "
2024-12-22 07:06:49 +00:00
sql_presses = (
" SELECT count(0) as total, strftime( ' % H ' , timestamp) as hour, "
" strftime( ' % U ' , timestamp) as week "
2025-01-07 22:45:49 +00:00
" FROM kevent WHERE event = ' PRESSED ' AND week = ' {week} ' "
2024-12-22 07:06:49 +00:00
" GROUP BY week, hour ORDER BY hour "
)
2025-01-07 22:45:49 +00:00
#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"
#)
2024-12-22 07:06:49 +00:00
rows_presses = query ( sql_presses . format ( week = week ) )
2025-01-07 22:45:49 +00:00
#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 ]
2025-01-07 22:45:49 +00:00
#totals_repeated = [row[0] for row in rows_repeated]
#hours_repeated = [row[1] for row in rows_repeated]
2024-12-15 15:05:21 +00:00
2025-01-07 22:45:49 +00:00
#totals_released = [row[0] for row in rows_released]
#hours_released = [row[1] for row in rows_released]
2024-12-15 15:05:21 +00:00
plt . figure ( figsize = ( 8 , 6 ) )
2025-01-07 22:45:49 +00:00
#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 ' )
2025-01-07 22:45:49 +00:00
plt . title ( f ' Key presses per hour. Week { week } ' )
plt . style . use ( ' dark_background ' )
plt . legend ( )
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 )
2025-01-12 16:54:05 +00:00
plt . style . use ( ' dark_background ' )
2024-12-15 15:05:21 +00:00
plt . title ( ' Keyboard events ' )
2025-01-07 22:45:49 +00:00
plt . tight_layout ( )
2024-12-15 15:05:21 +00:00
plt . legend ( )
2025-01-07 22:45:49 +00:00
plt . savefig ( f " graph_per_day.png " )
2024-12-15 15:05:21 +00:00
2025-01-07 22:45:49 +00:00
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 ) )
2024-12-15 15:05:21 +00:00
2025-01-07 22:45:49 +00:00
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 ) :
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 "
2025-01-07 22:45:49 +00:00
" FROM kevent WHERE event = ' PRESSED ' AND week = ' {week} ' "
2024-12-22 07:06:49 +00:00
" 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 "
2025-01-07 22:45:49 +00:00
" FROM kevent WHERE event = ' REPEATED ' AND week = ' {week} ' "
2024-12-22 07:06:49 +00:00
" 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 "
2025-01-07 22:45:49 +00:00
" FROM kevent WHERE event = ' RELEASED ' AND week = ' {week} ' "
2024-12-22 07:06:49 +00:00
" 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 ) )
2025-01-07 22:45:49 +00:00
#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
2025-01-07 22:45:49 +00:00
#totals_repeated = [row[0] for row in rows_repeated]
#days_repeated = [row[2] for row in rows_repeated]
2024-12-15 15:05:21 +00:00
2025-01-07 22:45:49 +00:00
#totals_released = [row[0] for row in rows_released]
#days_released = [row[2] for row in rows_released]
2024-12-15 15:05:21 +00:00
plt . figure ( figsize = ( 8 , 6 ) )
2025-01-07 22:45: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 ' Press count ' , color = ' red ' )
2024-12-15 15:05:21 +00:00
2025-01-07 22:45:49 +00:00
plt . xlabel ( ' Weekday (0 = Sunday, 6 = Saturday) ' )
2024-12-15 15:05:21 +00:00
plt . ylabel ( ' Event count ' )
2025-01-07 22:45:49 +00:00
plt . title ( f ' Presses per weekday. Week { week } ' )
plt . style . use ( ' dark_background ' )
2024-12-15 15:05:21 +00:00
plt . legend ( )
2025-01-07 22:45:49 +00:00
plt . savefig ( f " graph_week_ { week . strip ( ' \" ' ) } _per_weekday.png " )
2024-12-22 07:06:49 +00:00
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 ( ) :
2025-01-06 23:53:41 +00:00
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 ' GROUP BY week, day ORDER BY day "
2024-12-22 07:06:49 +00:00
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 ( )
2025-01-07 22:45:49 +00:00
render_per_day ( )
render_per_week ( )
2024-12-22 07:06:49 +00:00
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 )
2025-01-06 23:53:41 +00:00
for file in pathlib . Path ( " . " ) . glob ( " logs_summaries/*.txt " ) :
dest_file = file . parent . parent . joinpath ( " logs_lines " ) . joinpath ( file . name )
if dest_file . exists ( ) :
print ( " One liner already exists for " + file . name )
continue
with dest_file . open ( " w+ " ) as f :
source = file . read_text ( ) . replace ( " @ " , " " ) . replace ( " ` " , " " )
response = api . gpt4o_mini ( " The following data is a hour of work summarized from the user. Describe what user was doing in a onliner.: " + source )
f . write ( response )
print ( " Made one liner for " + file . name )
2024-12-22 07:06:49 +00:00
print ( " Duration: {} " . format ( time . time ( ) - time_start ) )