20250206.1738834765 diary doctor speed imporoved

This commit is contained in:
fz0x1 2025-02-06 10:39:25 +01:00
parent 0c7242d138
commit 8024771933
3 changed files with 57 additions and 19 deletions

View file

@ -93,6 +93,8 @@
(setq org-id-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
(setq org-log-into-drawer t)
(use-package! org-habit
:after org
:config

View file

@ -1,10 +1,12 @@
#!/usr/bin/env python3
import base64
import cProfile
import hashlib
import json
import logging
import os
import pstats
import re
import shlex
import shutil
@ -15,6 +17,7 @@ import tempfile
import time
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timedelta, timezone
from pathlib import Path
@ -344,11 +347,15 @@ def doctor():
args_len = len(sys.argv)
check_diary = False
fix_diary = False
limit = 50
if args_len < 3:
sys.exit("Usage: script.py doctor <diary_name>")
if args_len >= 4 and sys.argv[3] == "check_diary":
check_diary = True
if args_len >= 5:
limit = int(sys.argv[4])
if args_len >= 4 and sys.argv[3] == "check_and_fix_diary":
fix_diary = True
check_diary = True
@ -364,7 +371,9 @@ def doctor():
initialize_db(conn)
cursor = conn.cursor()
metadata = cursor.execute("SELECT * FROM metadata").fetchall()
metadata = cursor.execute(
f"SELECT * FROM metadata ORDER BY id DESC LIMIT {limit}"
).fetchall()
for m in metadata:
weather = cursor.execute(
"SELECT * FROM weather WHERE metadata_id = ?", (m[0],)
@ -389,8 +398,13 @@ def doctor():
print("An empty metadata was deleted")
remove_metadata(conn, m[0])
if check_diary:
dt = datetime.fromtimestamp(m[1], tz=timezone(timedelta(hours=TZ)))
# Close the connection before starting the multithreading part of the script
conn.close()
if check_diary:
def check_jrnl(metadata: list):
dt = datetime.fromtimestamp(metadata[1], tz=timezone(timedelta(hours=TZ)))
diary_datetime = dt.strftime("%Y/%m/%d at %I:%M:%S %p")
try:
result = subprocess.run(
@ -399,21 +413,38 @@ def doctor():
capture_output=True,
text=True,
)
if not result.stdout:
print(
f"There is some metadata that is not associated with a diary entity: {diary_datetime}."
)
if not fix_diary:
print(
"You can automatically remove it by running the scripts with 'check_and_fix_diary' argument."
)
else:
remove_metadata(conn, m[0])
print("The problem was fixed.")
return diary_datetime, result
except subprocess.CalledProcessError as e:
print(e)
raise
def process_metadata(m):
conn = db_connection(diary_path)
diary_datetime, result = check_jrnl(metadata=m)
if not result.stdout.strip():
print(
f"There is some metadata that is not associated with a diary entity: {diary_datetime}."
)
if not fix_diary:
print(
"You can automatically remove it by running the scripts with 'check_and_fix_diary' argument."
)
else:
remove_metadata(conn, m[0])
print("The problem was fixed.")
conn.close()
with ThreadPoolExecutor(max_workers=10) as executor:
futures = {executor.submit(process_metadata, m): m for m in metadata}
for future in as_completed(futures):
try:
future.result()
except Exception as e:
print(f"Thread error: {e}")
raise
def make_hash(file: Path):
sha256_hash = hashlib.sha256()
@ -634,7 +665,10 @@ if __name__ == "__main__":
elif sys.argv[1] == "insert":
insert()
elif sys.argv[1] == "doctor":
doctor()
cProfile.run("doctor()", "output.prof")
stats = pstats.Stats("output.prof")
stats.strip_dirs().sort_stats("cumulative").print_stats(10)
# doctor()
else:
print("Unknown command")
sys.exit(1)

View file

@ -30,9 +30,6 @@ mullvad-vpn &
# caffeine
caffeine start &
# mega cloud
megasync &
# compositor
picom --config ~/.picom.conf &
@ -76,7 +73,12 @@ workrave &
# watchdog
watchmedo shell-command --pattern='*.org;*.txt;*.md;*.gpg;*.org_archive;*.sh' --recursive --ignore-directories -W --command "~/org/sync.sh" ~/org/ &
watchmedo shell-command --pattern='*.journal;*.sh;*.log;*.prices;*.csv' --recursive --ignore-directories -W --command "~/.hledger/sync.sh" ~/.hledger/ &
watchmedo shell-command --pattern='*.journal;*.sh;*.log;*.prices;*.csv;*.py' --recursive --ignore-directories -W --command "~/.hledger/sync.sh" ~/.hledger/ &
sleep 1
# mega cloud
megasync &
# lockscreen
# xss-lock --transfer-sleep-lock -- ~/scripts/bin/lock.sh