diff --git a/configuration.nix b/configuration.nix index 5a4cde4..3732426 100644 --- a/configuration.nix +++ b/configuration.nix @@ -43,8 +43,6 @@ ./system-modules/nx2site.nix ./system-modules/postgres.nix ./system-modules/nx2site/proxy.nix - ./system-modules/calendar-publish.nix - ./system-modules/calendar-lec.nix ./system-modules/nx2site/audiobookshelf.nix ./system-modules/nx2site/gitea.nix ./system-modules/nx2site/open-web-calendar.nix @@ -52,6 +50,10 @@ # ./system-modules/nx2site/nextcloud.nix ./system-modules/nx2site/vaultwarden.nix ./system-modules/nx2site/paperless.nix + + ./system-modules/calendar-publish.nix + ./system-modules/calendar-lec.nix + ./system-modules/calendar-dicos.nix ] else []); # Set your time zone. diff --git a/system-modules/calendar-dicos.nix b/system-modules/calendar-dicos.nix new file mode 100644 index 0000000..e34c8da --- /dev/null +++ b/system-modules/calendar-dicos.nix @@ -0,0 +1,132 @@ +{ pkgs, ... }: +{ + systemd.timers."nx_cal_dicos" = { + enable = true; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "40m"; + OnUnitActiveSec = "24h"; + Unit = "nx_cal_dicos.service"; + }; + }; + + systemd.services."nx_cal_dicos" = { + script = let + nx_cal_dicos = (pkgs.writers.writePython3Bin "nx_cal_dicos" { + libraries = with pkgs.python3Packages; [ + ics + ]; + flakeIgnore = [ "E302" "E305" "E226" "E501" ]; + } /*python */ '' +import os +from glob import glob +from ics import Calendar +from ics.event import datetime + +NETTO_STUNDE = 18.46 +WEEKLY = 12 + +week_dict = {} +latest_week = 0 +latest_goal = WEEKLY +deficit = 0 + +def fraction_to_unicode(frac): + div, rem = divmod(frac, 1) + if rem == 0.5: + unicode = "½" + elif rem == 0.25: + unicode = "¼" + elif rem == 0.75: + unicode = "¾" + elif rem == 0: + unicode = "" + else: + unicode = rem + if div == 0: + h = "" + else: + h = int(div) + return f"{h}{unicode}" + +def modify_event(event): + """Modify the event if it contains 'DICOS' in the SUMMARY.""" + global week_dict + global latest_goal + global latest_week + global deficit + + if event.name and "DICOS" in event.name: + length = (event.end - event.begin).seconds / 3600 + money_made = divmod(length * NETTO_STUNDE, 1) + + # Calculate total hours for DICOS events in the same week + year, week, _ = event.begin.isocalendar() + + if week != latest_week: + try: + deficit = latest_goal - week_dict[f"{year}_{latest_week}"] + except KeyError: + deficit = 0 + + week_dict[f"{year}_{week}"] = length + (week_dict[f"{year}_{week}"] if f"{year}_{week}" in week_dict else 0) + + progress = week_dict[f"{year}_{week}"] + goal = WEEKLY + deficit + + if week != latest_week: + latest_goal = goal + latest_week = week + + progress_line = f"({fraction_to_unicode(progress)}/{fraction_to_unicode(goal)})" + + event.description = f"Netto: {money_made[0]:.0f},{int(money_made[1] * 10):02d}€\nThis weeks porgress: {progress_line}\nThis weeks deficit: {fraction_to_unicode(deficit)}" + event.name = f"DICOS {fraction_to_unicode(length)}" + return event + +def process_ics_file(filepath): + """Read, modify, and overwrite an ICS file.""" + with open(filepath, 'r') as f: + calendar = Calendar(f.read()) + + modified = False + + for event in calendar.events: + if 'DICOS' in event.name: + event = modify_event(event) + modified = True + + if modified: + with open(filepath, 'w') as f: + f.writelines(calendar.serialize_iter()) + +def get_event_start_time(filepath): + """Extract the event's start time from an ICS file.""" + with open(filepath, 'r') as f: + calendar = Calendar(f.read()) + + for event in calendar.events: + return event.begin.datetime + else: + return datetime(year=1, month=1, day=1) + + +if __name__ == "__main__": + directory = "/var/lib/radicale/collections/collection-root/nx2/experience" + ics_files = glob(os.path.join(directory, "*.ics")) + if not ics_files: + print("No ICS files found in the directory.") + sorted_files = sorted(ics_files, key=get_event_start_time) + for ics_file in sorted_files: + process_ics_file(ics_file) + print("Processing complete.") +''); + in '' + ${nx_cal_dicos}/bin/nx_cal_dicos + ''; + serviceConfig = { + Type = "oneshot"; + User = "radicale"; + }; + }; +} diff --git a/system-modules/calendar-publish.nix b/system-modules/calendar-publish.nix index c7b706f..0a60d97 100644 --- a/system-modules/calendar-publish.nix +++ b/system-modules/calendar-publish.nix @@ -1,18 +1,30 @@ { config, pkgs, user, ... }: +let + radicale-root = "/var/lib/radicale"; + web-root = "/var/nginx/webroot"; +in { - environment.systemPackages = with pkgs; let - radicale-root = "/var/lib/radicale"; - web-root = "/var/nginx/webroot"; - in [ - (writers.writePython3Bin "nx_cal_pub" { - libraries = with python3Packages; [ - ical - ics - requests - dateutils - ]; - flakeIgnore = [ "E302" "E305" "E226" "E501" ]; - } /*python */ '' + systemd.timers."nx_cal_publish" = { + enable = true; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnBootSec = "2m"; + OnUnitActiveSec = "6h"; + Unit = "nx_cal_publish.service"; + }; + }; + + systemd.services."nx_cal_publish" = { + script = with pkgs; let + nx_cal_publish = (writers.writePython3Bin "nx_cal_publish" { + libraries = with python3Packages; [ + ical + ics + requests + dateutils + ]; + flakeIgnore = [ "E302" "E305" "E226" "E501" ]; + } /*python */ '' import pytz import os from ics import Calendar, Event @@ -114,21 +126,9 @@ if __name__ == "__main__": OUTPUT_FILE = "${web-root}/schedule.ics" combine_ics_from_directories(DIRECTORIES, OUTPUT_FILE) -'') - ]; - systemd.timers."nx_cal_publish" = { - enable = true; - wantedBy = [ "timers.target" ]; - timerConfig = { - OnBootSec = "2m"; - OnUnitActiveSec = "6h"; - Unit = "nx_cal_publish.service"; - }; - }; - - systemd.services."nx_cal_publish" = { - script = '' - nx_cal_publish +''); + in '' + ${nx_cal_publish}/bin/nx_cal_publish ''; serviceConfig = { Type = "oneshot";