From 8024771933bf225df2ee01d92281b06ee0cbc7df Mon Sep 17 00:00:00 2001 From: fz0x1 Date: Thu, 6 Feb 2025 10:39:25 +0100 Subject: [PATCH] 20250206.1738834765 diary doctor speed imporoved --- global/.config/doom/config.el | 2 ++ global/scripts/bin/diary.py | 64 +++++++++++++++++++++++++++-------- linux/.config/i3/autostart.sh | 10 +++--- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/global/.config/doom/config.el b/global/.config/doom/config.el index 4f46758..9bd84f2 100644 --- a/global/.config/doom/config.el +++ b/global/.config/doom/config.el @@ -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 diff --git a/global/scripts/bin/diary.py b/global/scripts/bin/diary.py index 04863e9..884467f 100755 --- a/global/scripts/bin/diary.py +++ b/global/scripts/bin/diary.py @@ -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 ") 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) diff --git a/linux/.config/i3/autostart.sh b/linux/.config/i3/autostart.sh index 36d984c..d5d1bc3 100755 --- a/linux/.config/i3/autostart.sh +++ b/linux/.config/i3/autostart.sh @@ -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